This function extracts the variance components from a gam or bam object, where one is using the associated functions from the mgcv package for mixed models.
extract_vc(model, ci_level = 0.95, ci_scale = "sd", digits = 3)
| model | A gam or bam model |
|---|---|
| ci_level | Level for the confidence interval. Must be between 0 and 1. |
| ci_scale | Whether the confidence interval should be on the variance or standard deviation scale. |
| digits | Rounding for the output. |
A tibble or data frame with the standard deviation, its lower and upper bounds, the variance, and the relative proportion of total variance for each variance component.
This is essentially a pretty way to print gam.vcomp.
Note that if you do something like a random slope, the gam approach does
not estimate the intercept-slope correlation, so none will be printed.
Should work fine on standard smooth terms also, but the model must be
estimated with method = REML or method = ML. This is the default for
non-exponential families, but otherwise you'll have to change the value or
will receive an error. Have not tested much with interactions, using by,
etc., but it has worked on some default mgcv examples.
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" ) VarCorr(lmer_model)#> Groups Name Std.Dev. #> Subject (Intercept) 25.0513 #> Subject.1 Days 5.9882 #> Residual 25.5653extract_vc(ga_model)#> group effect variance sd sd_2.5 sd_97.5 var_prop #> 1 Subject Intercept 627.571 25.051 16.085 39.015 0.477 #> 2 Subject Days 35.858 5.988 4.025 8.908 0.027 #> 3 Residual 653.582 25.565 22.792 28.676 0.496