@@ -479,6 +479,41 @@ test_that("check_outliers numeric, z-score", {
479479})
480480
481481
482+ test_that(" influential observations plot data uses standardized residuals for lmer models" , {
483+ skip_if_not_installed(" lme4" )
484+
485+ # the plot draws Cook's distance contours from leverage, standardized
486+ # residuals and the number of parameters, so these must reproduce the
487+ # Cook's distance that check_outliers() tests (#857)
488+ m <- lme4 :: lmer(Reaction ~ Days + (Days | Subject ), data = lme4 :: sleepstudy )
489+ out <- check_outliers(m )
490+ d <- attributes(out )$ influential_obs
491+ p <- attributes(d )$ n_params
492+ implied_cook <- d $ Std_Residuals ^ 2 * d $ Hat / (p * (1 - d $ Hat ))
493+ expect_equal(implied_cook , d $ Cooks_Distance , tolerance = 1e-6 , ignore_attr = TRUE )
494+ expect_identical(
495+ which(implied_cook > attributes(out )$ threshold $ cook ),
496+ which(as.numeric(out ) == 1 )
497+ )
498+ })
499+
500+
501+ test_that(" influential observations plot data uses the Cook's distance threshold for lm models" , {
502+ # the contour lines must follow the threshold that check_outliers() uses,
503+ # including a user-supplied threshold (#916)
504+ m <- lm(disp ~ mpg + hp , data = mtcars )
505+ for (th in list (NULL , list (cook = 2 ))) {
506+ out <- check_outliers(m , method = " cook" , threshold = th )
507+ d <- attributes(out )$ influential_obs
508+ expect_equal(attributes(d )$ cook_levels , attributes(out )$ threshold $ cook )
509+ expect_identical(
510+ which(d $ Influential == " Influential" ),
511+ which(as.numeric(out ) == 1 )
512+ )
513+ }
514+ })
515+
516+
482517test_that(" check_outliers with psych" , {
483518 skip_if_not_installed(" psych" )
484519 data(Harman.5 , package = " psych" )
0 commit comments