ranef: Extract the modes of the random effects

Description Usage Arguments Details Value Note Examples

Description

A generic function to extract the conditional modes of the random effects from a fitted model object. For linear mixed models the conditional modes of the random effects are also the conditional means.

Usage

1
2
3
4
5
6
7
8
9
## S3 method for class 'merMod'
 ranef(object, condVar = TRUE,
      drop = FALSE, whichel = names(ans), postVar = FALSE, ...)
## S3 method for class 'ranef.mer'
 dotplot(x, data, main = TRUE, transf = I, ...)
## S3 method for class 'ranef.mer'
 qqmath(x, data, main = TRUE, ...)
## S3 method for class 'ranef.mer'
 as.data.frame(x, ..., stringsAsFactors = default.stringsAsFactors())

Arguments

object

an object of a class of fitted models with random effects, typically a merMod object.

condVar

a logical argument indicating if the conditional variance-covariance matrices of the random effects should be added as an attribute.

drop

should components of the return value that would be data frames with a single column, usually a column called ‘(Intercept)’, be returned as named vectors instead?

whichel

character vector of names of grouping factors for which the random effects should be returned.

postVar

a (deprecated) synonym for condVar

x

a random-effects object (of class ranef.mer) produced by ranef

main

include a main title, indicating the grouping factor, on each sub-plot?

transf

transformation for random effects: for example, exp for plotting parameters from a (generalized) logistic regression on the odds rather than log-odds scale

data

This argument is required by the dotplot and qqmath generic methods, but is not actually used.

stringsAsFactors

see data.frame

...

some methods for these generic functions require additional arguments.

Details

If grouping factor i has k levels and j random effects per level the ith component of the list returned by ranef is a data frame with k rows and j columns. If condVar is TRUE the "postVar" attribute is an array of dimension j by j by k (or a list of such arrays). The kth face of this array is a positive definite symmetric j by j matrix. If there is only one grouping factor in the model the variance-covariance matrix for the entire random effects vector, conditional on the estimates of the model parameters and on the data, will be block diagonal; this j by j matrix is the kth diagonal block. With multiple grouping factors the faces of the "postVar" attributes are still the diagonal blocks of this conditional variance-covariance matrix but the matrix itself is no longer block diagonal.

Value

Note

To produce a (list of) “caterpillar plots” of the random effects apply dotplot to the result of a call to ranef with condVar = TRUE; qqmath will generate a list of Q-Q plots.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
library(lattice) ## for dotplot, qqmath
fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
fm2 <- lmer(Reaction ~ Days + (1|Subject) + (0+Days|Subject), sleepstudy)
fm3 <- lmer(diameter ~ (1|plate) + (1|sample), Penicillin)
ranef(fm1)
str(rr1 <- ranef(fm1))
dotplot(rr1)  ## default
qqmath(rr1)
## specify free scales in order to make Day effects more visible
dotplot(rr1,scales = list(x = list(relation = 'free')))[["Subject"]]
ranef(fm2)
op <- options(digits = 4)
ranef(fm3, drop = TRUE)
options(op)
## as.data.frame() provides RE's and conditional standard deviations:
str(dd <- as.data.frame(rr1))
if (require(ggplot2)) {
    ggplot(dd, aes(y=grp,x=condval)) +
        geom_point() + facet_wrap(~term,scales="free_x") +
        geom_errorbarh(aes(xmin=condval -2*condsd,
                           xmax=condval +2*condsd), height=0)
}

Example output

Loading required package: Matrix
$Subject
    (Intercept)        Days
308   2.2575329   9.1992737
309 -40.3942719  -8.6205161
310 -38.9563542  -5.4495796
330  23.6888704  -4.8141448
331  22.2585409  -3.0696766
332   9.0387625  -0.2720535
333  16.8389833  -0.2233978
334  -7.2320462   1.0745075
335  -0.3326901 -10.7524799
337  34.8865253   8.6290208
349 -25.2080191   1.1730997
350 -13.0694180   6.6142185
351   4.5777099  -3.0152825
352  20.8614523   3.5364062
369   3.2750882   0.8722876
370 -25.6110745   4.8222518
371   0.8070591  -0.9881730
372  12.3133491   1.2842380

with conditional variances for "Subject" 
List of 1
 $ Subject:'data.frame':	18 obs. of  2 variables:
  ..$ (Intercept): num [1:18] 2.26 -40.39 -38.96 23.69 22.26 ...
  ..$ Days       : num [1:18] 9.2 -8.62 -5.45 -4.81 -3.07 ...
  ..- attr(*, "postVar")= num [1:2, 1:2, 1:18] 145.69 -21.44 -21.44 5.31 145.69 ...
 - attr(*, "class")= chr "ranef.mer"
$Subject

$Subject

$Subject
    (Intercept)        Days
308   1.5116973   9.3237308
309 -40.3720066  -8.5995358
310 -39.1795117  -5.3880715
330  24.5188072  -4.9686809
331  22.9141930  -3.1939310
332   9.2217762  -0.3084673
333  17.1557243  -0.2871512
334  -7.4516633   1.1159907
335   0.5798353 -10.9062401
337  34.7661741   8.6279628
349 -25.7538155   1.2806250
350 -13.8653871   6.7565202
351   4.9161750  -3.0751926
352  20.9281601   3.5123758
369   3.2584763   0.8730848
370 -26.4756822   4.9838147
371   0.9057286  -1.0053150
372  12.4213189   1.2584805

with conditional variances for "Subject" 
$plate
       a        b        c        d        e        f        g        h 
 0.80454  0.80454  0.18167  0.33739  0.02595 -0.44120 -1.37551  0.80454 
       i        j        k        l        m        n        o        p 
-0.75264 -0.75264  0.96026  0.49311  1.42742  0.49311  0.96026  0.02595 
       q        r        s        t        u        v        w        x 
-0.28548 -0.28548 -1.37551  0.96026 -0.90836 -0.28548 -0.59692 -1.21979 
attr(,"postVar")
 [1] 0.07363 0.07363 0.07363 0.07363 0.07363 0.07363 0.07363 0.07363 0.07363
[10] 0.07363 0.07363 0.07363 0.07363 0.07363 0.07363 0.07363 0.07363 0.07363
[19] 0.07363 0.07363 0.07363 0.07363 0.07363 0.07363

$sample
       A        B        C        D        E        F 
 2.18706 -1.01048  1.93790 -0.09689 -0.01384 -3.00374 
attr(,"postVar")
[1] 0.04087 0.04087 0.04087 0.04087 0.04087 0.04087

with conditional variances for "plate" "sample" 
'data.frame':	36 obs. of  5 variables:
 $ grpvar : chr  "Subject" "Subject" "Subject" "Subject" ...
 $ term   : Factor w/ 2 levels "(Intercept)",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ grp    : Factor w/ 18 levels "309","310","370",..: 9 1 2 17 16 12 14 6 7 18 ...
 $ condval: num  2.26 -40.39 -38.96 23.69 22.26 ...
 $ condsd : num  12.1 12.1 12.1 12.1 12.1 ...
Loading required package: ggplot2

lme4 documentation built on June 22, 2021, 9:07 a.m.