`check_outliers()`: Cook's distances land on the wrong rows when the model skips or reorders rows
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 22/100
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- R
- Estrellas
- 1.2k
- Forks
- 113
- Merge medio
- 9 h 8 min
- PR fusionados (30 d)
- 27
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de easystats/performance
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
easystats/performance#1038 ·
Los mantenedores suelen responder en 1 día
-
Waiting for response :love_letter:
Dificultad 3/5 1-2 días Aptitud para principiantes 62/100
easystats/performance#1005 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
misleading subtitle with check_heteroscedasticity with binomial data?Posiblemente ocupada @jmgirard la tomó hace 26 días. AbiertoConsistency :green_apple: :apple: Docs :books:
easystats/performance#1003 · 2 comentarios · 1 asignado ·
Los mantenedores suelen responder en 1 día
-
[Proposal] Support for glmtoolbox glmgee objects (glmgee or gnmgee models) / interim warningsAbiertoDocs :books: Enhancement :boom:
Dificultad 5/5 Más de una semana Aptitud para principiantes 45/100
easystats/performance#901 · 2 comentarios · 1 reacción ·
Los mantenedores suelen responder en 1 día
-
Enhancement :boom:
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
easystats/performance#891 ·
Los mantenedores suelen responder en 1 día
Todos los issues de easystats/performance
Issues similares
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
ThinkR-open/mordew#89 ·
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
epiforecasts/EpiNow2#1694 · 2 reacciones ·
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
easystats/correlation#403 ·
-
add temporal concepts vignetteAbierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100
mitchelloharawild/mixtime#111 ·
-
Unpin CCCL for 3.5.0+ supportAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
conda-forge/xgboost-feedstock#313 ·