Extract what would be the random effects from a mixed model from
a gam object. Assumes an mgcv model of the form gam(... + s(g, bs='re'))
.
extract_ranef(model, re = NULL, ci_level = 0.95, digits = 3)
model | A gam or bam model |
---|---|
re | Filter results to a specific random effect/grouping variable. |
ci_level | Level for the confidence interval. Must be between 0 and 1. |
digits | Rounding for the output. |
A tibble
or data frame with the random effect,
its standard error, and its lower and upper bounds. The bounds are based
on a simple normal approximation using the standard error.
Returns a data frame of the the component
type, the estimated
random effect re
, the estimated se
, and approximate lower
and upper
bounds. Note that the standard errors are Bayesian
estimates (see gamObject
, specifically type Vp
). The re
will only reflect smooth terms whose basis function is 're', i.e. of class
random.effect
. Others will be ignored.
mgcv
strips the level names for 're' smooth terms, so this attempts
to get them back. This may not work under every circumstance, but the
attempt is made to extract the names of random effect groups based on how
they are ordered in the data (which is how the model matrix would be
constructed), and in the case of random slopes, detect that second variable
in the 're' specification would be the grouping variable. This will not
work for continuous x continuous smooths of type 're', but I can't think of
a reason why you'd use that given your other options with mgcv
.
library(mgcv) library(lme4) lmer_model <- lmer(Reaction ~ Days + (Days || Subject), data = sleepstudy) ga_model <- gam(Reaction ~ Days + s(Subject, bs = "re") + s(Days, Subject, bs = "re"), data = sleepstudy, method = "REML" ) ranef(lmer_model)#> $Subject #> (Intercept) Days #> 308 1.5126648 9.3234970 #> 309 -40.3738728 -8.5991757 #> 310 -39.1810279 -5.3877944 #> 330 24.5189244 -4.9686503 #> 331 22.9144470 -3.1939378 #> 332 9.2219759 -0.3084939 #> 333 17.1561243 -0.2872078 #> 334 -7.4517382 1.1159911 #> 335 0.5787623 -10.9059754 #> 337 34.7679030 8.6276228 #> 349 -25.7543312 1.2806892 #> 350 -13.8650598 6.7564064 #> 351 4.9159912 -3.0751356 #> 352 20.9290332 3.5122123 #> 369 3.2586448 0.8730514 #> 370 -26.4758468 4.9837910 #> 371 0.9056510 -1.0052938 #> 372 12.4217547 1.2584037 #> #> with conditional variances for “Subject”extract_ranef(ga_model)#> # A tibble: 36 x 7 #> group_var effect group value se lower_2.5 upper_97.5 #> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 Subject Intercept 308 1.51 13.3 -24.5 27.5 #> 2 Subject Intercept 309 -40.4 13.3 -66.4 -14.3 #> 3 Subject Intercept 310 -39.2 13.3 -65.2 -13.2 #> 4 Subject Intercept 330 24.5 13.3 -1.51 50.5 #> 5 Subject Intercept 331 22.9 13.3 -3.11 48.9 #> 6 Subject Intercept 332 9.22 13.3 -16.8 35.2 #> 7 Subject Intercept 333 17.2 13.3 -8.87 43.2 #> 8 Subject Intercept 334 -7.45 13.3 -33.5 18.6 #> 9 Subject Intercept 335 0.579 13.3 -25.4 26.6 #> 10 Subject Intercept 337 34.8 13.3 8.74 60.8 #> # … with 26 more rows