This vignette shows examples for using tab_model()
to create HTML tables for mixed models. Basically, tab_model()
behaves in a very similar way for mixed models as for other, simple regression models, as shown in this vignette.
# load required packages
library(sjPlot)
library(lme4)
library(nlme)
data("sleepstudy")
data("Orthodont")
Unlike tables for non-mixed models, tab_models()
adds additional information on the random effects to the table output for mixed models. You can hide these information with show.icc = FALSE
and show.re.var = FALSE
. Furthermore, the R-squared values are marginal and conditional R-squared statistics, based on Nakagawa et al. 2017.
m1 <- lmer(distance ~ age + Sex + (1 | Subject), data = Orthodont)
m2 <- lmer(Reaction ~ Days + (1 + Days | Subject), data = sleepstudy)
tab_model(m1, m2)
distance | Reaction | |||||
---|---|---|---|---|---|---|
Predictors | Estimates | CI | p | Estimates | CI | p |
(Intercept) | 17.71 | 16.07 – 19.34 | <0.001 | 251.41 | 238.03 – 264.78 | <0.001 |
age | 0.66 | 0.54 – 0.78 | <0.001 | |||
SexFemale | -2.32 | -3.81 – -0.83 | 0.002 | |||
Days | 10.47 | 7.44 – 13.50 | <0.001 | |||
Random Effects | ||||||
σ2 | 2.05 | 654.94 | ||||
τ00 | 3.27 Subject | 612.09 Subject | ||||
τ11 | 35.07 Subject.Days | |||||
ρ01 | 0.07 Subject | |||||
ICC | 0.61 Subject | 0.48 Subject | ||||
Observations | 108 | 180 | ||||
Marginal R2 / Conditional R2 | 0.398 / 0.768 | 0.279 / 0.799 |
The marginal R-squared considers only the variance of the fixed effects, while the conditional R-squared takes both the fixed and random effects into account.
The p-value is a simple approximation, based on the t-statistics and using the normal distribution function. A more precise p-value can be computed using p.val = "kr"
. In this case, which only applies to linear mixed models, the computation of p-values is based on conditional F-tests with Kenward-Roger approximation for the degrees of freedom (using the using the pbkrtest-package). Note that here the computation is more time consuming and thus not used as default. You can also display the approximated degrees of freedom with show.df
.
tab_model(m1, p.val = "kr", show.df = TRUE)
distance | ||||
---|---|---|---|---|
Predictors | Estimates | CI | p | df |
(Intercept) | 17.71 | 16.34 – 19.08 | <0.001 | 99.00 |
age | 0.66 | 0.56 – 0.76 | <0.001 | 80.00 |
SexFemale | -2.32 | -3.57 – -1.07 | 0.005 | 25.00 |
Random Effects | ||||
σ2 | 2.05 | |||
τ00 Subject | 3.27 | |||
ICC Subject | 0.61 | |||
Observations | 108 | |||
Marginal R2 / Conditional R2 | 0.398 / 0.768 |
tab_model()
can also print and combine models with different link-functions.
data("efc")
efc$neg_c_7d <- ifelse(efc$neg_c_7 < median(efc$neg_c_7, na.rm = TRUE), 0, 1)
efc$cluster <- as.factor(efc$e15relat)
m3 <- glmer(
neg_c_7d ~ c160age + c161sex + e42dep + (1 | cluster),
data = efc,
family = binomial(link = "logit")
)
tab_model(m1, m3)
distance | neg c 7 d | |||||
---|---|---|---|---|---|---|
Predictors | Estimates | CI | p | Odds Ratios | CI | p |
(Intercept) | 17.71 | 16.07 – 19.34 | <0.001 | 0.02 | 0.01 – 0.05 | <0.001 |
age | 0.66 | 0.54 – 0.78 | <0.001 | |||
SexFemale | -2.32 | -3.81 – -0.83 | 0.002 | |||
carer’age | 1.01 | 0.99 – 1.02 | 0.355 | |||
carer’s gender | 1.83 | 1.30 – 2.59 | 0.001 | |||
elder’s dependency | 2.37 | 1.99 – 2.81 | <0.001 | |||
Random Effects | ||||||
σ2 | 2.05 | 3.29 | ||||
τ00 | 3.27 Subject | 0.24 cluster | ||||
ICC | 0.61 Subject | 0.07 cluster | ||||
Observations | 108 | 888 | ||||
Marginal R2 / Conditional R2 | 0.398 / 0.768 | 0.181 / 0.237 |
Finally, an example from the glmmTMB-package to show how easy it is to print zero-inflated generalized linear mixed models as HTML table.
library(glmmTMB)
data("Salamanders")
m4 <- glmmTMB(
count ~ spp + mined + (1 | site),
ziformula = ~ spp + mined,
family = truncated_poisson(link = "log"),
data = Salamanders
)
tab_model(m1, m3, m4, show.ci = FALSE)
#> Warning: mu of 2.5 is too close to zero, estimate may be unreliable.
distance | neg c 7 d | count | ||||
---|---|---|---|---|---|---|
Predictors | Estimates | p | Odds Ratios | p | Incidence Rate Ratios | p |
(Intercept) | 17.71 | <0.001 | 0.02 | <0.001 | 0.94 | 0.745 |
age | 0.66 | <0.001 | ||||
SexFemale | -2.32 | 0.002 | ||||
carer’age | 1.01 | 0.355 | ||||
carer’s gender | 1.83 | 0.001 | ||||
elder’s dependency | 2.37 | <0.001 | ||||
sppPR | 0.59 | 0.062 | ||||
sppDM | 1.25 | 0.121 | ||||
sppEC-A | 0.82 | 0.331 | ||||
sppEC-L | 1.91 | <0.001 | ||||
sppDES-L | 1.83 | <0.001 | ||||
sppDF | 1.05 | 0.765 | ||||
minedno | 2.76 | <0.001 | ||||
Zero-Inflated Model | ||||||
(Intercept) | 5.79 | <0.001 | ||||
sppPR | 5.36 | <0.001 | ||||
sppDM | 0.65 | 0.223 | ||||
sppEC-A | 3.02 | 0.003 | ||||
sppEC-L | 0.65 | 0.223 | ||||
sppDES-L | 0.51 | 0.056 | ||||
sppDF | 0.65 | 0.223 | ||||
minedno | 0.09 | <0.001 | ||||
Random Effects | ||||||
σ2 | 2.05 | 3.29 | 0.10 | |||
τ00 | 3.27 Subject | 0.24 cluster | 0.05 site | |||
ICC | 0.61 Subject | 0.07 cluster | 0.34 site | |||
Observations | 108 | 888 | 644 | |||
Marginal R2 / Conditional R2 | 0.398 / 0.768 | 0.181 / 0.237 | 0.724 / 0.819 |
By default, the ICC for nested or cross-classified models only reports the proportion of variance explained for each grouping level. Use show.adj.icc = TRUE
to calculate the adjusted ICC, which takes all sources of uncertainty (of all random effects) into account.
If random effects are not nested and not cross-classified, the adjusted and unadjusted ICC are identical (see m1
in table below).
set.seed(2)
sleepstudy$mygrp <- sample(1:30, size = 180, replace = TRUE)
m5 <- lmer(Reaction ~ Days + (1 | mygrp) + (1 | Subject), sleepstudy)
tab_model(m1, m5, show.ci = FALSE, show.adj.icc = TRUE)
distance | Reaction | |||
---|---|---|---|---|
Predictors | Estimates | p | Estimates | p |
(Intercept) | 17.71 | <0.001 | 251.16 | <0.001 |
age | 0.66 | <0.001 | ||
SexFemale | -2.32 | 0.002 | ||
Days | 10.42 | <0.001 | ||
Random Effects | ||||
σ2 | 2.05 | 867.68 | ||
τ00 | 3.27 Subject | 98.07 mygrp | ||
1371.09 Subject | ||||
ICC | 0.61 Subject | 0.04 mygrp | ||
0.59 Subject | ||||
ICC adjusted | 0.61 | 0.63 | ||
Observations | 108 | 180 | ||
Marginal R2 / Conditional R2 | 0.398 / 0.768 | 0.278 / 0.732 |
Nakagawa S, Johnson P, Schielzeth H (2017) The coefficient of determination R2 and intra-class correlation coefficient from generalized linear mixed-effects models revisted and expanded. J. R. Soc. Interface 14. doi: 10.1098/rsif.2017.0213