Computes residuals for a given linear mixed model.

compute_redres(model, type = "raw_cond")

Arguments

model

Model fit using lmer from lme4.

type

String identifying type of residual. Default is "raw_cond". See details for the options available.

Value

Returns a vector of residuals according to type specified. Residuals appear in the same order as the observations used to fit the model.

Details

Residual types available:

  • "pearson_cond": Pearson conditional residuals

  • "pearson_mar": Pearson marginal residuals

  • "raw_cond": raw conditional residuals (default)

  • "raw_mar": raw marginal residuals

  • "std_cond": studentized conditional residuals

  • "std_mar": studentized marginal residuals

See the vignette for details on how residual types are computed.

Examples

# fits a linear mixed effects model library(lme4)
#> Loading required package: Matrix
fm1 <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy) # computes the default residuals (raw conditional) compute_redres(fm1)
#> [1] -4.1026378 -14.6244974 -42.1951571 8.7774832 24.5230236 #> [6] 62.6946639 10.5418043 -101.1799554 19.5901849 35.6918253 #> [11] 11.7230671 -7.5918028 -11.7265727 -11.8441426 -10.6818125 #> [16] -4.2828824 -8.4611523 -6.2110221 -1.4892920 9.6824381 #> [21] -13.3948506 -23.1342569 11.8358367 5.3397304 -3.2121759 #> [26] -17.0793822 -7.1341885 8.1784052 8.4220989 -10.0928075 #> [31] 46.4486248 19.6530836 -2.5437575 -6.9203986 -11.9092398 #> [36] -5.7741809 -28.7732221 3.5953368 -14.9696043 28.0764545 #> [41] 13.9442543 3.9387449 13.3617356 24.2588263 13.0232169 #> [46] -17.3329924 -27.9743018 9.3707889 -39.0976204 31.3389702 #> [51] -25.5832674 -27.8272999 -7.8730323 18.7392352 16.2381027 #> [56] -1.4224298 132.5466378 15.0206053 -11.7054272 -98.3365597 #> [61] 15.5983119 11.0670237 -11.9625644 0.8339475 -12.0486407 #> [66] 18.7029712 2.3190830 8.8885949 -16.8351933 1.6037186 #> [71] 21.3000414 20.4863479 -23.8919456 -24.1261390 -11.3158325 #> [76] -17.6908260 -7.8990195 6.5572870 -0.7605064 29.2498001 #> [81] -9.4641147 23.1599792 3.9886731 20.5852671 1.5202610 #> [86] 4.9897550 -3.9089511 -13.7650571 -13.0367632 -11.2590692 #> [91] 26.0749699 8.4178631 -32.8730437 2.5416495 3.0555428 #> [96] 10.0653360 3.3906292 -3.2734776 16.8022156 0.7583088 #> [101] 9.9061142 -7.5207714 -10.5522570 -6.1962426 -22.0483282 #> [106] -14.6246138 -14.4745995 0.4222149 16.9604293 20.6845437 #> [111] 17.9611132 -11.9628913 -16.2940958 -34.0531003 -37.7452047 #> [116] 5.9814908 38.6197863 5.0121819 19.4994774 -3.0165271 #> [121] -5.4563148 36.6227817 -0.9929218 2.2502747 -13.9634288 #> [126] 11.3907677 -12.9482358 -41.5513393 5.9429572 24.5146537 #> [131] -50.5894572 11.9236506 26.6045585 32.5778663 20.4588742 #> [136] 10.5436820 -1.8621102 -9.8598023 -8.6554945 -9.7580867 #> [141] 17.2433069 2.4171334 -20.1169402 -11.0423138 14.7837127 #> [146] 5.8354391 -24.5823345 14.0656920 -5.1167816 9.7767448 #> [151] -0.5300304 -6.5600681 -17.4723058 -31.1896435 -19.4148812 #> [156] 41.9519811 -36.3831567 14.7647056 17.0526679 8.8289302 #> [161] 17.6682360 10.7515230 6.7285100 1.1399971 -10.9581159 #> [166] -15.0957289 -49.8210419 -13.9353549 22.7356321 31.9450191 #> [171] 5.6932460 -1.9959780 10.3752981 11.6585741 -23.5519499 #> [176] 7.1315261 0.2542021 -2.7592219 11.4110541 -5.3585698
# changes the residual type to studentized marginal compute_redres(fm1, type = "std_mar")
#> [1] -0.052196884 -0.088251555 -0.576516852 0.974414478 1.491671897 #> [6] 2.404013211 1.356669412 -0.634358690 1.617487253 1.892716994 #> [11] -1.135411156 -1.598970790 -1.247549019 -0.995784369 -0.837607308 #> [16] -0.695297918 -0.668072120 -0.611240078 -0.555726283 -0.483447913 #> [21] -2.018393354 -2.596782865 -1.459385029 -1.916936254 -2.455603753 #> [26] -3.202705774 -3.038547689 -2.668911686 -2.885515301 -3.843004299 #> [31] 1.984146693 1.073380977 0.308260319 0.058668339 -0.175425704 #> [36] -0.133387832 -0.677770667 -0.117856419 -0.504927475 0.132271107 #> [41] 1.433670392 0.653287383 0.530246564 0.475686003 0.225197999 #> [46] -0.082558637 -0.160303936 0.057961774 -0.207538095 0.115934943 #> [51] -0.637871062 -0.732840921 0.023861003 1.034399042 0.928570723 #> [56] 0.240579517 5.397446244 0.857884878 -0.188515437 -3.594270679 #> [61] 0.917631173 0.771234771 0.118563691 0.428850546 0.091426379 #> [66] 0.745913850 0.355507140 0.443950748 -0.030223774 0.257583476 #> [71] 0.557108037 0.404746992 -0.521146565 -0.358720496 -0.139504858 #> [76] -0.154857007 -0.057681819 0.039131158 0.003025638 0.141460305 #> [81] -0.377714436 0.464251836 -0.685132849 -0.460570190 -1.605505328 #> [86] -1.888365457 -2.651671728 -3.460392521 -3.868688405 -4.245292957 #> [91] 1.724563285 1.446859675 0.515823312 1.596964868 1.700028010 #> [96] 1.908868924 1.796747604 1.690573047 2.045899271 1.776140642 #> [101] -0.605972214 -0.891356062 -0.600988621 -0.355536790 -0.416697591 #> [106] -0.269051599 -0.216831187 -0.094725215 0.005701301 0.026938350 #> [111] 0.188598620 -0.708138103 -0.619344810 -1.046600185 -0.935062150 #> [116] 0.999193107 2.515886569 1.480816745 2.309933938 1.701893382 #> [121] -0.024855192 1.063836175 -0.065463879 -0.055939913 -0.503190363 #> [126] 0.019329109 -0.527987295 -1.067089436 -0.230510927 0.030643013 #> [131] -1.177261595 1.025976506 0.980940231 0.816626923 0.543007146 #> [136] 0.388816439 0.267138321 0.204357413 0.203029599 0.191649009 #> [141] 0.791083845 0.252392017 -0.579508864 -0.197595224 0.827197700 #> [146] 0.518069863 -0.619893209 0.907905913 0.199940389 0.818877474 #> [151] -0.739515813 -0.761937410 -0.895027962 -1.067765898 -0.603845570 #> [156] 0.876510116 -0.659643826 0.420904208 0.508752745 0.417255207 #> [161] 0.731641950 0.298583174 0.099988694 -0.012972778 -0.138074551 #> [166] -0.152315970 -0.364947705 -0.114565145 0.078396425 0.106507179 #> [171] 0.694241748 0.446058260 0.969494500 1.067500625 -0.234234322 #> [176] 0.994689980 0.781850010 0.718052119 1.323370502 0.725265941