Extract random effects from a fitted glmmTMB model, both
for the conditional model and zero inflation.
# S3 method for glmmTMB
ranef(object, condVar = TRUE, ...)
# S3 method for ranef.glmmTMB
as.data.frame(x, ...)
# S3 method for glmmTMB
coef(object, condVar = FALSE, ...)a glmmTMB model.
whether to include conditional variances in result.
some methods for this generic function require additional arguments (they are unused here and will trigger an error)
a ranef.glmmTMB object (i.e., the result of running ranef on a fitted glmmTMB model)
For ranef, an object of class ranef.glmmTMB with two components:
a list of data frames, containing random effects for the conditional model.
a list of data frames, containing random effects for the zero inflation.
If condVar=TRUE, the individual list elements within the
cond and zi components (corresponding to individual
random effects terms) will have associated condVar attributes
giving the conditional variances of the random effects values.
These are in the form of three-dimensional arrays: see
ranef.merMod for details. The only difference between
the packages is that the attributes are called ‘postVar’
in lme4, vs. ‘condVar’ in glmmTMB.
For coef.glmmTMB: a similar list, but containing
the overall coefficient value for each level, i.e., the sum of
the fixed effect estimate and the random effect value for that
level. Conditional variances are not yet available as
an option for coef.glmmTMB.
For as.data.frame: a data frame with components
part of the model to which the random effects apply (conditional or zero-inflation)
grouping variable
random-effects term (e.g., intercept or slope)
group, or level of the grouping variable
value of the conditional mode
conditional standard deviation
When a model has no zero inflation, the
ranef and coef print methods simplify the
structure shown, by default. To show the full list structure, use
print(ranef(model),simplify=FALSE) or the analogous
code for coef.
In all cases, the full list structure is used to access
the data frames, see example.
if (requireNamespace("lme4")) {
   data(sleepstudy, package="lme4")
   model <- glmmTMB(Reaction ~ Days + (1|Subject), sleepstudy)
   rr <- ranef(model)
   print(rr, simplify=FALSE)
   ## extract Subject conditional modes for conditional model
   rr$cond$Subject
   as.data.frame(rr)
}
#> $cond
#> $cond$Subject
#>     (Intercept)
#> 308   40.635065
#> 309  -77.565893
#> 310  -62.878623
#> 330    4.390358
#> 331   10.178934
#> 332    8.191252
#> 333   16.440339
#> 334   -2.986087
#> 335  -45.117144
#> 337   71.919622
#> 349  -21.119036
#> 350   14.059914
#> 351   -7.833598
#> 352   36.245834
#> 369    7.010713
#> 370   -6.339544
#> 371   -3.282295
#> 372   18.049705
#> 
#> 
#> $zi
#> list()
#> 
#>         component  grpvar        term grp    condval   condsd
#> cond.1       cond Subject (Intercept) 308  40.635065 12.53483
#> cond.2       cond Subject (Intercept) 309 -77.565893 12.65075
#> cond.3       cond Subject (Intercept) 310 -62.878623 12.59611
#> cond.4       cond Subject (Intercept) 330   4.390358 12.49122
#> cond.5       cond Subject (Intercept) 331  10.178934 12.49347
#> cond.6       cond Subject (Intercept) 332   8.191252 12.49250
#> cond.7       cond Subject (Intercept) 333  16.440339 12.49793
#> cond.8       cond Subject (Intercept) 334  -2.986087 12.49094
#> cond.9       cond Subject (Intercept) 335 -45.117144 12.54508
#> cond.10      cond Subject (Intercept) 337  71.919622 12.62842
#> cond.11      cond Subject (Intercept) 349 -21.119036 12.50263
#> cond.12      cond Subject (Intercept) 350  14.059914 12.49599
#> cond.13      cond Subject (Intercept) 351  -7.833598 12.49234
#> cond.14      cond Subject (Intercept) 352  36.245834 12.52582
#> cond.15      cond Subject (Intercept) 369   7.010713 12.49202
#> cond.16      cond Subject (Intercept) 370  -6.339544 12.49178
#> cond.17      cond Subject (Intercept) 371  -3.282295 12.49099
#> cond.18      cond Subject (Intercept) 372  18.049705 12.49942