This function extracts the estimated variance components of a linear (mixed) model. It is compatible with models from lme4, glmmTMB, and lm functions. The resulting output is a tibble that contains the variance components, their proportion of the total variance, and standard deviations. This function is heavily based on mixedup::extract_vc(). Use docx_tab() to format output for reports.
get_varcomp(model, digits = 3)
# S3 method for class 'lm'
get_varcomp(model, digits = 3)
# S3 method for class 'merMod'
get_varcomp(model, digits = 3)
# S3 method for class 'glmmTMB'
get_varcomp(model, digits = 3)# Simple linear model
lm_model <- lm(mpg ~ wt + hp, data = mtcars)
get_varcomp(lm_model)
#> # A tibble: 1 × 6
#> group effect var var_p var_prop sd
#> <chr> <chr> <dbl> <chr> <dbl> <dbl>
#> 1 Residual NA 6.73 100.0% 1 2.59
# Format for reports using docx_tab()
if (FALSE) { # \dontrun{
get_varcomp(lm_model) %>% docx_tab()
get_varcomp(lm_model) %>% docx_tab(lang = "ger")
# Mixed model with formatted output
if (requireNamespace("lme4", quietly = TRUE)) {
mm_model <- lme4::lmer(mpg ~ wt + hp + (1 | cyl), data = mtcars)
get_varcomp(mm_model) %>% docx_tab(lang = "ger")
}
} # }