HLMresid
is a function that extracts residuals
from a hierarchical linear model fit
using lmer
. That is, it is a unified framework that
extracts/calculates residuals from mer
or lmerMod
objects.
# S3 method for default HLMresid(object, ...) # S3 method for mer HLMresid(object, level, type = "EB", sim = NULL, standardize = FALSE, ...) # S3 method for lmerMod HLMresid(object, level, type = "EB", sim = NULL, standardize = FALSE, ...)
object | an object of class |
---|---|
... | do not use |
level | which residuals should be extracted: 1 for within-group (case-level)
residuals, the name of a grouping factor (as defined in |
type | how are the residuals predicted: either |
sim | optional argument giving the data frame used for LS residuals. This is used mainly for dealing with simulations. |
standardize | if |
This function extracts residuals from the model, and can extract residuals estimated using least squares (LS) or Empirical Bayes (EB). This unified framework enables the analyst to more easily conduct an upward residual analysis during model exploration/checking.
The HLMresid
function provides a wrapper that will extract
residuals from a fitted mer
or lmerMod
object.
The function provides access to
residual quantities already made available by the functions resid
and
ranef
, but adds additional functionality. Below is a list of types of
residuals that can be extracted.
These are equivalent to the residuals extracted
by resid
if level = 1
, type = "EB"
, and
standardize = FALSE
is specified.
You can also specify type = "LS"
for LS residuals
that are not equivalent to those from resid
.
Specify level = 1
, and
standardize = TRUE
. This works with both type = "EB"
or "LS"
.
Specify level = 1
, type = "LS"
and
standardize = "semi"
.
These are equivalent to extracting the
predicted random effects for a given group using ranef
. Set
level
to a grouping factor name and type = "EB"
. type = "LS"
can also be specified, though this is less common.
Set
level
to a grouping factor name, type = "EB"
, and
standardized = TRUE
. This will not produce standardized residuals for
type = "LS"
.
The marginal residuals can be obtained by setting
level = "marginal"
. Only type = "EB"
is implemented.
These are essentially standardized marginal residuals.
To obtain cholesky residuals set level = "marginal"
, type = "EB"
,
and standardize = TRUE
.
Note that standardize = "semi"
is only implemented for level-1 LS residuals.
Hilden-Minton, J. (1995) Multilevel diagnostics for mixed and hierarchical linear models. University of California Los Angeles.
Houseman, E. A., Ryan, L. M., & Coull, B. A. (2004) Cholesky Residuals for Assessing Normal Errors in a Linear Model With Correlated Outcomes. Journal of the American Statistical Association, 99(466), 383--394.
Adam Loy loyad01@gmail.com
if (FALSE) { data(sleepstudy, package = "lme4") fm1 <- lme4::lmer(Reaction ~ Days + (Days|Subject), sleepstudy) # level-1 residuals all.equal(HLMresid(object = fm1, level = 1, type = "EB"), resid(fm1)) ## EB r1LS <- HLMresid(object = fm1, level = 1, type = "LS") ## raw LS resids head(r1LS) r1LS.std <- HLMresid(object = fm1, level = 1, type = "LS", standardize = TRUE) ## std. LS resids head(r1LS.std) # level-2 residuals all.equal(r2EB <- HLMresid(object = fm1, level = "Subject", type = "EB"), lme4::ranef(fm1)[["Subject"]]) r2EB.std <- HLMresid(object = fm1, level = "Subject", type = "EB", standardize = TRUE) head(r2EB) head(r2EB.std) # marginal residuals mr <- HLMresid(object = fm1, level = "marginal") cholr <- HLMresid(object = fm1, level = "marginal", standardize = TRUE) # Cholesky residuals }