residuals: Extract The Residuals From a Linear Mixed Model

Description Usage Arguments Details Value Examples

Description

Extract or compute the residuals of a linear mixed model.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
## S3 method for class 'lmm'
residuals(
  object,
  type = "response",
  format = "long",
  data = NULL,
  p = NULL,
  keep.data = FALSE,
  var = NULL,
  plot = "none",
  engine.qqplot = "ggplot2",
  add.smooth = TRUE,
  digit.cor = 2,
  size.text = 16,
  scales = "free",
  ...
)

Arguments

object

a lmm object.

type

[character] Should the raw residuals be output ("response"), the Pearson residuals ("pearson"), normalized residuals ("normalized" , "scaled"), or partial residuals ("partial" or "partial-ref"). See detail section.

format

[character] Should the residuals be output relative as a vector ("long"), or as a matrix with in row the clusters and in columns the outcomes ("wide").

data

[data.frame] dataset relative to which the residuals should be computed. Only relevant if differs from the dataset used to fit the model.

p

[numeric vector] value of the model coefficients at which to evaluate the residuals. Only relevant if differs from the fitted values.

keep.data

[logical] Should the argument data be output along side the residuals? Only possible in the long format.

var

[character vector] name of the variable relative to which the partial residuals should be computed.

plot

[character] Should a qqplot ("qqplot"), or a heatmap of the correlation between residuals ("correlation", require wide format), or a plot of residuals along the fitted values ("scatterplot", require long format) be displayed?

engine.qqplot

[character] Should ggplot2 or qqtest be used to display quantile-quantile plots? Only used when argument plot is "qqplot".

add.smooth

[logical] should a local smoother be used to display the mean of the residual values across the fitted values. Only relevant for plot="scatterplot".

digit.cor

[integer, >0] Number of digit used to display the correlation coefficients? No correlation coefficient is displayed when set to 0. Only used when argument plot is "correlation".

size.text

[numeric, >0] Size of the font used to displayed text when using ggplot2.

scales

[character] Passed to ggplot2::facet_wrap.

...

Not used. For compatibility with the generic method.

Details

The argument type defines how the residuals are computed:

where

Value

When argument format is "long" and type.oobject is "lmm", a vector containing the value of the residual realtive to each observation. It is a matrix if the argument type contains several values. When argument format is "wide" and type.oobject is "lmm", a data.frame with the value of the residual relative to each cluster (in rows) at each timepoint (in columns).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
## simulate data in the long format
set.seed(10)
dL <- sampleRem(100, n.times = 3, format = "long")

## fit Linear Model
e.lm <- lmm(Y ~ visit + X1 + X2 + X5, data = dL)
residuals(e.lm, type = "partial", var = "X1")
residuals(e.lm, type = "partial", var = "X1", keep.data = TRUE)

## fit Linear Mixed Model
eUN.lmm <- lmm(Y ~ visit + X1 + X2 + X5,
               repetition = ~visit|id, structure = "UN", data = dL)

## residuals
residuals(eUN.lmm, format = "long", type = c("normalized","pearson"))
residuals(eUN.lmm, format = "long", type = "all", keep.data = TRUE)
residuals(eUN.lmm, format = "wide", plot = "correlation")
residuals(eUN.lmm, format = "wide", type = "normalized")
residuals(eUN.lmm, format = "wide", type = "scaled")

## residuals and predicted values
residuals(eUN.lmm, data = fitted(eUN.lmm,keep.newdata=TRUE),keep.data=TRUE)

LMMstar documentation built on Nov. 5, 2021, 1:07 a.m.