`check_outliers()`: Cook's distances land on the wrong rows when the model skips or reorders rows

Đang mở
#981 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
22/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
r
Lĩnh vực
data

Hướng nghiên cứu

Start in check_outliers() in the performance package, where Cook's distances are numbered by fitted-row position and joined to insight::get_data() rows. Compare that with model.frame() and cooks.distance() for the subset, zero-weight and na.exclude cases in the reproduction. There is no agreed fix yet, so first settle how to map each distance to its data row, then check the four cases return NA for unused rows.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

3 investigators :grey_question::question: Bug :bug:

check_outliers() numbers the Cook's distances 1, 2, 3, ... in the order of the rows the model was fitted on. It then joins them to the rows of the data (from insight::get_data()) by that number. If the model did not use every data row, or used the rows in another order, each distance lands on the wrong row. No warning is given.

This happens with subset, with prior weights of 0, and with na.action = na.exclude. It affects method = "cook" alone and combined with other methods.

library(performance)

# 1. subset: rows 1 to 4 are not in the model, but they get distances
m <- lm(mpg ~ disp + drat, data = mtcars, subset = 5:32)
out <- as.data.frame(check_outliers(m, method = c("cook", "zscore_robust")))
out[1:5, c("Row", "Distance_Cook")]
#>   Row Distance_Cook
#> 1   1  0.0408896798
#> 2   2  0.0028593118
#> 3   3  0.0003970294
#> 4   4  0.0019153130
#> 5   5  0.0036171642
round(cooks.distance(m)[1:2], 4)
#> Hornet Sportabout           Valiant
#>            0.0409            0.0029
# row 1 (Mazda RX4) shows the distance of row 5 (Hornet Sportabout)

# 2. a subset in another order: the distances come out reversed
m2 <- lm(mpg ~ disp + drat, data = mtcars, subset = 32:1)
out2 <- as.data.frame(check_outliers(m2, method = "cook"))
round(out2$Distance_Cook[1:3], 4)
#> [1] 0.0289 0.0102 0.0263
round(cooks.distance(m2)[c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710")], 4)
#>     Mazda RX4 Mazda RX4 Wag    Datsun 710
#>        0.0071        0.0071        0.0110

# 3. weights of 0: the distances shift up by two rows
w <- rep(1, 32)
w[1:2] <- 0
m3 <- lm(mpg ~ disp + drat, data = mtcars, weights = w)
out3 <- as.data.frame(check_outliers(m3, method = c("cook", "zscore_robust")))
out3[c(1:3, 31:32), c("Row", "Distance_Cook")]
#>    Row Distance_Cook
#> 1    1    0.01401184
#> 2    2    0.02684947
#> 3    3    0.03754641
#> 31  31            NA
#> 32  32            NA
round(cooks.distance(m3)["Datsun 710"], 4)
#> Datsun 710
#>      0.014
# row 1 shows the distance of row 3 (Datsun 710)

The expected result: rows that the model did not use have no Cook's distance (NA), and every other row has its own distance.

A fix by row name is not enough, because the row names of insight::get_data() do not always name the model's rows. For tibble data with missing values, it numbers the complete rows 1 to n. For data.table data with a logical subset, it numbers the rows by their position after the subset:

d <- data.table::as.data.table(mtcars)
d$mpg[5] <- NA
m4 <- lm(mpg ~ disp + drat, data = d, subset = drat > 3)
head(rownames(insight::get_data(m4)))
#> [1] "1" "2" "3" "4" "6" "7"
head(rownames(model.frame(m4)))
#> [1] "1" "2" "3" "4" "7" "8"

Do you have ideas for a reliable way to tell which data row each Cook's distance belongs to?

Versions: performance 0.18.2.20 (main), insight 1.5.4.32, data.table 1.18.6.1.

Ngôn ngữ chính
R
Star
1.2k
Fork
113
Merge trung bình
9 giờ 8 phút
Pull request đã merge (30 ngày)
27

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của easystats/performance

Tất cả issue của easystats/performance

Issue tương tự

Thêm issue về R

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.