This document shows examples for using the sjt.lmer()
function of the sjPlot package.
# load required packages
library(sjPlot)
library(sjmisc)
library(sjlabelled)
library(lme4)
The sjt.lmer()
function prints summaries of linear mixed models (fitted with the lmer()
function of the lme4-package) as nicely formatted html-tables.
Before starting, sample data is loaded and sample models are fitted:
# load sample data
data(efc)
# prepare grouping variables
efc$grp = as.factor(efc$e15relat)
levels(x = efc$grp) <- get_labels(efc$e15relat)
efc$care.level <- rec(efc$n4pstu, rec = "0=0;1=1;2=2;3:4=4", val.labels = c("none", "I", "II", "III"))
# data frame for fitted model
mydf <- data.frame(
neg_c_7 = efc$neg_c_7,
sex = to_factor(efc$c161sex),
c12hour = efc$c12hour,
barthel = efc$barthtot,
education = to_factor(efc$c172code),
grp = efc$grp,
carelevel = to_factor(efc$care.level)
)
# fit sample models
fit1 <- lmer(neg_c_7 ~ sex + c12hour + barthel + (1 | grp), data = mydf)
fit2 <- lmer(neg_c_7 ~ sex + c12hour + education + barthel + (1 | grp), data = mydf)
fit3 <- lmer(neg_c_7 ~ sex + c12hour + education + barthel +
(1 | grp) + (1 | carelevel), data = mydf)
The simplest way of producing the table output is by passing the fitted models as parameter. By default, estimates (B), confidence intervals (CI) and p-values (p) are reported. The models are named Model 1 and Model 2.
The resulting table is divided into three parts:
sjt.lmer(fit1, fit2)
Negative impact with 7 items | Negative impact with 7 items | |||||||
B | CI | p | B | CI | p | |||
Fixed Parts | ||||||||
(Intercept) | 14.14 | 13.15 – 15.12 | <.001 | 13.75 | 12.63 – 14.87 | <.001 | ||
carer’s gender (Female) | 0.48 | -0.07 – 1.03 | .091 | 0.67 | 0.10 – 1.25 | .023 | ||
average number of hours of care per week | 0.00 | -0.00 – 0.01 | .238 | 0.00 | -0.00 – 0.01 | .218 | ||
Total score BARTHEL INDEX | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | ||
carer’s level of education (intermediate level of education) | 0.19 | -0.43 – 0.80 | .550 | |||||
carer’s level of education (high level of education) | 0.80 | 0.03 – 1.58 | .045 | |||||
Random Parts | ||||||||
σ2 | 12.189 | 12.320 | ||||||
τ00, grp | 0.279 | 0.267 | ||||||
Ngrp | 8 | 8 | ||||||
ICCgrp | 0.022 | 0.021 | ||||||
Observations | 872 | 815 | ||||||
R2 / Ω02 | .185 / .185 | .193 / .193 |
You can specify the ‘model’ label via depvar.labels
parameter:
sjt.lmer(fit1, fit2,
depvar.labels = c("Negative Impact", "Negative Impact"))
Negative Impact | Negative Impact | |||||||
B | CI | p | B | CI | p | |||
Fixed Parts | ||||||||
(Intercept) | 14.14 | 13.15 – 15.12 | <.001 | 13.75 | 12.63 – 14.87 | <.001 | ||
carer’s gender (Female) | 0.48 | -0.07 – 1.03 | .091 | 0.67 | 0.10 – 1.25 | .023 | ||
average number of hours of care per week | 0.00 | -0.00 – 0.01 | .238 | 0.00 | -0.00 – 0.01 | .218 | ||
Total score BARTHEL INDEX | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | ||
carer’s level of education (intermediate level of education) | 0.19 | -0.43 – 0.80 | .550 | |||||
carer’s level of education (high level of education) | 0.80 | 0.03 – 1.58 | .045 | |||||
Random Parts | ||||||||
σ2 | 12.189 | 12.320 | ||||||
τ00, grp | 0.279 | 0.267 | ||||||
Ngrp | 8 | 8 | ||||||
ICCgrp | 0.022 | 0.021 | ||||||
Observations | 872 | 815 | ||||||
R2 / Ω02 | .185 / .185 | .193 / .193 |
Here is an example how to change the other labels. Note that show.header
makes the two labels on top and top left corner appear in the table.
sjt.lmer(fit1, fit2, show.header = TRUE, string.est = "Estimate",
string.ci = "Conf. Int.", string.p = "p-value",
string.dv = "Response", string.pred = "Coefficients",
string.interc = "Konstante",
depvar.labels = c("Negative Impact", "Negative Impact"))
Coefficients | Response | Negative Impact | Negative Impact | |||||
Estimate | Conf. Int. | p-value | Estimate | Conf. Int. | p-value | |||
Fixed Parts | ||||||||
Konstante | 14.14 | 13.15 – 15.12 | <.001 | 13.75 | 12.63 – 14.87 | <.001 | ||
carer’s gender (Female) | 0.48 | -0.07 – 1.03 | .091 | 0.67 | 0.10 – 1.25 | .023 | ||
average number of hours of care per week | 0.00 | -0.00 – 0.01 | .238 | 0.00 | -0.00 – 0.01 | .218 | ||
Total score BARTHEL INDEX | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | ||
carer’s level of education (intermediate level of education) | 0.19 | -0.43 – 0.80 | .550 | |||||
carer’s level of education (high level of education) | 0.80 | 0.03 – 1.58 | .045 | |||||
Random Parts | ||||||||
σ2 | 12.189 | 12.320 | ||||||
τ00, grp | 0.279 | 0.267 | ||||||
Ngrp | 8 | 8 | ||||||
ICCgrp | 0.022 | 0.021 | ||||||
Observations | 872 | 815 | ||||||
R2 / Ω02 | .185 / .185 | .193 / .193 |
You can change the table style with specific parameters, e.g. to include CI into the same table cell as the estimates, print asterisks instead of numeric p-values etc.
sjt.lmer(fit1, fit2,
separate.ci.col = FALSE, # ci in same cell as estimates
show.std = TRUE, # also show standardized beta values
p.numeric = FALSE, # "*" instead of numeric values
show.re.var = FALSE, # no random effect variances
show.aic = TRUE, # AIC
show.dev = FALSE, # no deviance
show.r2 = FALSE) # no Pseudo-R2
#> Warning: Some models were fit with REML. To get meaningful AIC values for
#> comparison, refit models with ML (`REML = FALSE`).
Negative impact with 7 items | Negative impact with 7 items | |||||
B (CI) | std. Beta (CI) | B (CI) | std. Beta (CI) | |||
Fixed Parts | ||||||
(Intercept) |
14.14 (13.15 – 15.12) *** |
13.75 (12.63 – 14.87) *** |
||||
carer’s gender (Female) |
0.48 (-0.07 – 1.03) |
0.05 (-0.01 – 0.11) |
0.67 (0.10 – 1.25) * |
0.07 (0.01 – 0.14) |
||
average number of hours of care per week |
0.00 (-0.00 – 0.01) |
0.04 (-0.03 – 0.12) |
0.00 (-0.00 – 0.01) |
0.05 (-0.03 – 0.12) |
||
Total score BARTHEL INDEX |
-0.05 (-0.06 – -0.04) *** |
-0.37 (-0.44 – -0.30) |
-0.05 (-0.06 – -0.04) *** |
-0.37 (-0.44 – -0.30) |
||
carer’s level of education (intermediate level of education) |
0.19 (-0.43 – 0.80) |
0.02 (-0.05 – 0.10) |
||||
carer’s level of education (high level of education) |
0.80 (0.03 – 1.58) * |
0.08 (0.00 – 0.16) |
||||
Random Parts | ||||||
Ngrp | 8 | 8 | ||||
ICCgrp | 0.022 | 0.021 | ||||
Observations | 872 | 815 | ||||
AIC | 4691.393 | 4397.996 | ||||
Notes | * p<.05 ** p<.01 *** p<.001 |
In the above example, the original variable labels are long and not much pretty. You can change variable labels either with sjmisc::set_label()
, which will affect all future plots and tables, or pass own labels via pred.labels
.
sjt.lmer(fit1, fit2, pred.labels = c("Carer's Sex",
"Hours of Care", "Elder's Dependency",
"Mid Educational Level", "High Educational Level"))
Negative impact with 7 items | Negative impact with 7 items | |||||||
B | CI | p | B | CI | p | |||
Fixed Parts | ||||||||
(Intercept) | 14.14 | 13.15 – 15.12 | <.001 | 13.75 | 12.63 – 14.87 | <.001 | ||
Carer’s Sex | 0.48 | -0.07 – 1.03 | .091 | 0.67 | 0.10 – 1.25 | .023 | ||
Hours of Care | 0.00 | -0.00 – 0.01 | .238 | 0.00 | -0.00 – 0.01 | .218 | ||
Elder’s Dependency | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | ||
Mid Educational Level | 0.19 | -0.43 – 0.80 | .550 | |||||
High Educational Level | 0.80 | 0.03 – 1.58 | .045 | |||||
Random Parts | ||||||||
σ2 | 12.189 | 12.320 | ||||||
τ00, grp | 0.279 | 0.267 | ||||||
Ngrp | 8 | 8 | ||||||
ICCgrp | 0.022 | 0.021 | ||||||
Observations | 872 | 815 | ||||||
R2 / Ω02 | .185 / .185 | .193 / .193 |
Categorical variables with more than two levels can be grouped in the table output. Grouping means, that a row with the variable label is inserted before these variables, and a value label for each category (i.e. factor level) is printed with a small margin.
sjt.lmer(fit3, fit2, fit1, group.pred = TRUE)
Negative impact with 7 items | Negative impact with 7 items | Negative impact with 7 items | ||||||||||
B | CI | p | B | CI | p | B | CI | p | ||||
Fixed Parts | ||||||||||||
(Intercept) | 13.76 | 12.63 – 14.88 | <.001 | 13.75 | 12.63 – 14.87 | <.001 | 14.14 | 13.15 – 15.12 | <.001 | |||
carer’s gender (Female) | 0.65 | 0.08 – 1.22 | .029 | 0.67 | 0.10 – 1.25 | .023 | 0.48 | -0.07 – 1.03 | .091 | |||
average number of hours of care per week | 0.00 | -0.00 – 0.01 | .208 | 0.00 | -0.00 – 0.01 | .218 | 0.00 | -0.00 – 0.01 | .238 | |||
carer’s level of education | ||||||||||||
intermediate level of education | 0.16 | -0.46 – 0.79 | .606 | 0.19 | -0.43 – 0.80 | .550 | ||||||
high level of education | 0.79 | 0.01 – 1.57 | .052 | 0.80 | 0.03 – 1.58 | .045 | ||||||
Total score BARTHEL INDEX | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | |||
Random Parts | ||||||||||||
σ2 | 12.413 | 12.320 | 12.189 | |||||||||
τ00, grp | 0.264 | 0.267 | 0.279 | |||||||||
τ00, carelevel | 0.000 | |||||||||||
Ngrp | 8 | 8 | 8 | |||||||||
Ncarelevel | 4 | |||||||||||
ICCgrp | 0.021 | 0.021 | 0.022 | |||||||||
ICCcarelevel | 0.000 | |||||||||||
Observations | 807 | 815 | 872 | |||||||||
R2 / Ω02 | .190 / .190 | .193 / .193 | .185 / .185 |
Note that in the above example, the order of fitted model was changed. This is sometimes necessary, because grouping categorical predictors does not always work properly when multiple models with different amount and order of predictors are printed in one table.
When models have different random intercepts, the sjt.lmer()
function tries to detect these information from each model. In the Random parts section of the table, information on multiple grouping levels and ICC’s are printed then.
sjt.lmer(fit1, fit2, fit3)
Negative impact with 7 items | Negative impact with 7 items | Negative impact with 7 items | ||||||||||
B | CI | p | B | CI | p | B | CI | p | ||||
Fixed Parts | ||||||||||||
(Intercept) | 14.14 | 13.15 – 15.12 | <.001 | 13.75 | 12.63 – 14.87 | <.001 | 13.76 | 12.63 – 14.88 | <.001 | |||
carer’s gender (Female) | 0.48 | -0.07 – 1.03 | .091 | 0.67 | 0.10 – 1.25 | .023 | 0.65 | 0.08 – 1.22 | .029 | |||
average number of hours of care per week | 0.00 | -0.00 – 0.01 | .238 | 0.00 | -0.00 – 0.01 | .218 | 0.00 | -0.00 – 0.01 | .208 | |||
Total score BARTHEL INDEX | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | |||
carer’s level of education (intermediate level of education) | 0.19 | -0.43 – 0.80 | .550 | 0.16 | -0.46 – 0.79 | .606 | ||||||
carer’s level of education (high level of education) | 0.80 | 0.03 – 1.58 | .045 | 0.79 | 0.01 – 1.57 | .052 | ||||||
Random Parts | ||||||||||||
σ2 | 12.189 | 12.320 | 12.413 | |||||||||
τ00, grp | 0.279 | 0.267 | 0.264 | |||||||||
τ00, carelevel | 0.000 | |||||||||||
Ngrp | 8 | 8 | 8 | |||||||||
Ncarelevel | 4 | |||||||||||
ICCgrp | 0.022 | 0.021 | 0.021 | |||||||||
ICCcarelevel | 0.000 | |||||||||||
Observations | 872 | 815 | 807 | |||||||||
R2 / Ω02 | .185 / .185 | .193 / .193 | .190 / .190 |
Note that in certain cases, depending on the order of fitted models with several random intercepts, the group label might be incorrect.
Especially when fitting and summarizing some more models, it might help to increase the distance between the columns that separate the models. This can be done by tweaking the css.separatorcol
style-sheet:
sjt.lmer(fit1, fit2, fit3,
CSS = list(css.separatorcol = 'padding-right:1.5em; padding-left:1.5em;'),
show.re.var = FALSE,
show.icc = FALSE,
show.r2 = FALSE)
Negative impact with 7 items | Negative impact with 7 items | Negative impact with 7 items | ||||||||||
B | CI | p | B | CI | p | B | CI | p | ||||
Fixed Parts | ||||||||||||
(Intercept) | 14.14 | 13.15 – 15.12 | <.001 | 13.75 | 12.63 – 14.87 | <.001 | 13.76 | 12.63 – 14.88 | <.001 | |||
carer’s gender (Female) | 0.48 | -0.07 – 1.03 | .091 | 0.67 | 0.10 – 1.25 | .023 | 0.65 | 0.08 – 1.22 | .029 | |||
average number of hours of care per week | 0.00 | -0.00 – 0.01 | .238 | 0.00 | -0.00 – 0.01 | .218 | 0.00 | -0.00 – 0.01 | .208 | |||
Total score BARTHEL INDEX | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | |||
carer’s level of education (intermediate level of education) | 0.19 | -0.43 – 0.80 | .550 | 0.16 | -0.46 – 0.79 | .606 | ||||||
carer’s level of education (high level of education) | 0.80 | 0.03 – 1.58 | .045 | 0.79 | 0.01 – 1.57 | .052 | ||||||
Random Parts | ||||||||||||
Ngrp | 8 | 8 | 8 | |||||||||
Ncarelevel | 4 | |||||||||||
Observations | 872 | 815 | 807 |
With remove.estmates
, specific estimates can be removed from the table output. This may make sense in case you have stepwise regression models and only want to compare the varying predictors but not the controls. remove.estmates
either accepts the row indices of the rows of the table output that should be removed, or the coefficient’s names.
When using numeric indices, the estimates’ index number relates to the same order as coef(fit)
.
Here you have the complete table output. This helps you identify the row index numbers. Especially when you have multiple models with different predictors, the estimate’s position in the last model may differ from this estimate’s position in the table output.
sjt.lmer(fit1, fit2, fit3,
show.re.var = FALSE,
show.icc = FALSE)
Negative impact with 7 items | Negative impact with 7 items | Negative impact with 7 items | ||||||||||
B | CI | p | B | CI | p | B | CI | p | ||||
Fixed Parts | ||||||||||||
(Intercept) | 14.14 | 13.15 – 15.12 | <.001 | 13.75 | 12.63 – 14.87 | <.001 | 13.76 | 12.63 – 14.88 | <.001 | |||
carer’s gender (Female) | 0.48 | -0.07 – 1.03 | .091 | 0.67 | 0.10 – 1.25 | .023 | 0.65 | 0.08 – 1.22 | .029 | |||
average number of hours of care per week | 0.00 | -0.00 – 0.01 | .238 | 0.00 | -0.00 – 0.01 | .218 | 0.00 | -0.00 – 0.01 | .208 | |||
Total score BARTHEL INDEX | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | |||
carer’s level of education (intermediate level of education) | 0.19 | -0.43 – 0.80 | .550 | 0.16 | -0.46 – 0.79 | .606 | ||||||
carer’s level of education (high level of education) | 0.80 | 0.03 – 1.58 | .045 | 0.79 | 0.01 – 1.57 | .052 | ||||||
Random Parts | ||||||||||||
Ngrp | 8 | 8 | 8 | |||||||||
Ncarelevel | 4 | |||||||||||
Observations | 872 | 815 | 807 | |||||||||
R2 / Ω02 | .185 / .185 | .193 / .193 | .190 / .190 |
sjt.lmer(fit1, fit2, fit3,
remove.estimates = 2,
show.re.var = FALSE,
show.icc = FALSE)
Negative impact with 7 items | Negative impact with 7 items | Negative impact with 7 items | ||||||||||
B | CI | p | B | CI | p | B | CI | p | ||||
Fixed Parts | ||||||||||||
(Intercept) | 14.14 | 13.15 – 15.12 | <.001 | 13.75 | 12.63 – 14.87 | <.001 | 13.76 | 12.63 – 14.88 | <.001 | |||
average number of hours of care per week | 0.00 | -0.00 – 0.01 | .238 | 0.00 | -0.00 – 0.01 | .218 | 0.00 | -0.00 – 0.01 | .208 | |||
Total score BARTHEL INDEX | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | |||
carer’s level of education (intermediate level of education) | – | 0.19 | -0.43 – 0.80 | .550 | 0.16 | -0.46 – 0.79 | .606 | |||||
carer’s level of education (high level of education) | – | 0.80 | 0.03 – 1.58 | .045 | 0.79 | 0.01 – 1.57 | .052 | |||||
Random Parts | ||||||||||||
Ngrp | 8 | 8 | 8 | |||||||||
Ncarelevel | 4 | |||||||||||
Observations | 872 | 815 | 807 | |||||||||
R2 / Ω02 | .185 / .185 | .193 / .193 | .190 / .190 |
sjt.lmer(fit1, fit2, fit3,
remove.estimates = c("c12hour", "sex2"),
show.re.var = FALSE,
show.icc = FALSE)
Negative impact with 7 items | Negative impact with 7 items | Negative impact with 7 items | ||||||||||
B | CI | p | B | CI | p | B | CI | p | ||||
Fixed Parts | ||||||||||||
(Intercept) | 14.14 | 13.15 – 15.12 | <.001 | 13.75 | 12.63 – 14.87 | <.001 | 13.76 | 12.63 – 14.88 | <.001 | |||
Total score BARTHEL INDEX | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | -0.05 | -0.06 – -0.04 | <.001 | |||
carer’s level of education (intermediate level of education) | – | 0.19 | -0.43 – 0.80 | .550 | 0.16 | -0.46 – 0.79 | .606 | |||||
carer’s level of education (high level of education) | – | 0.80 | 0.03 – 1.58 | .045 | 0.79 | 0.01 – 1.57 | .052 | |||||
Random Parts | ||||||||||||
Ngrp | 8 | 8 | 8 | |||||||||
Ncarelevel | 4 | |||||||||||
Observations | 872 | 815 | 807 | |||||||||
R2 / Ω02 | .185 / .185 | .193 / .193 | .190 / .190 |