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

Abierto
#981 1 comentario 0 reacciones 0 asignados Ver en GitHub

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
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
r
Área
data

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

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.

Lenguaje dominante
R
Estrellas
1.2k
Forks
113
Merge medio
9 h 8 min
PR fusionados (30 d)
27

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de easystats/performance

Todos los issues de easystats/performance

Issues similares

Más issues de R

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.