# Summary of Mixed Models as HTML Table

#### Daniel Lüdecke

#### 2021-01-10

    ## Warning in checkMatrixPackageVersion(): Package version inconsistency detected.
    ## TMB was built with Matrix version 1.2.18
    ## Current Matrix version is 1.3.2
    ## Please re-install 'TMB' from source using install.packages('TMB', type = 'source') or ask CRAN for a binary version of 'TMB' matching CRAN's 'Matrix' package

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)
    data("sleepstudy")
    data("efc")
    efc$cluster <- as.factor(efc$e15relat)

## Mixed models summaries as HTML table

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(neg_c_7 ~ c160age + c161sex + e42dep + (1 | cluster), data = efc)
    m2 <- lmer(Reaction ~ Days + (1 + Days | Subject), data = sleepstudy)

    tab_model(m1, m2)

| Negative impact with 7 items | Reaction
--- | --- | ---
Predictors | Estimates | CI | p | Estimates | CI | p
(Intercept) | 6.55 | 4.87 – 8.23 | <0.001 | 251.41 | 238.03 – 264.78 | <0.001
carer’age | -0.00 | -0.03 – 0.02 | 0.802 |  |  |
carer’s gender | 0.47 | -0.08 – 1.02 | 0.094 |  |  |
elder’s dependency | 1.45 | 1.20 – 1.71 | <0.001 |  |  |
Days |  |  |  | 10.47 | 7.44 – 13.50 | <0.001
Random Effects
σ2 | 12.61 | 654.94
τ00 | 0.50 cluster | 612.10 Subject
τ11 |  | 35.07 Subject.Days
ρ01 |  | 0.07 Subject
ICC | 0.04 | 0.72
N | 8 cluster | 18 Subject
Observations | 888 | 180
Marginal R2 / Conditional R2 | 0.127 / 0.160 | 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)

| Negative impact with 7 items
--- | ---
Predictors | Estimates | CI | p | df
(Intercept) | 6.55 | 4.82 – 8.28 | <0.001 | 136.86
carer’age | -0.00 | -0.03 – 0.02 | 0.810 | 255.72
carer’s gender | 0.47 | -0.08 – 1.02 | 0.095 | 881.58
elder’s dependency | 1.45 | 1.19 – 1.71 | <0.001 | 883.58
Random Effects
σ2 | 12.61
τ00 cluster | 0.50
ICC | 0.04
N cluster | 8
Observations | 888
Marginal R2 / Conditional R2 | 0.127 / 0.160

## Generalized linear mixed models

`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)

| Negative impact with 7 items | neg c 7 d
--- | --- | ---
Predictors | Estimates | CI | p | Odds Ratios | CI | p
(Intercept) | 6.55 | 4.87 – 8.23 | <0.001 | 0.02 | 0.01 – 0.05 | <0.001
carer’age | -0.00 | -0.03 – 0.02 | 0.802 | 1.01 | 0.99 – 1.02 | 0.355
carer’s gender | 0.47 | -0.08 – 1.02 | 0.094 | 1.83 | 1.30 – 2.59 | 0.001
elder’s dependency | 1.45 | 1.20 – 1.71 | <0.001 | 2.37 | 1.99 – 2.81 | <0.001
Random Effects
σ2 | 12.61 | 3.29
τ00 | 0.50 cluster | 0.24 cluster
ICC | 0.04 | 0.07
N | 8 cluster | 8 cluster
Observations | 888 | 888
Marginal R2 / Conditional R2 | 0.127 / 0.160 | 0.181 / 0.237

## More complex models

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
    )
    #> Warning in Matrix::sparseMatrix(dims = c(0, 0), i = integer(0), j = integer(0), : 'giveCsparse' has been deprecated; setting 'repr = "T"' for you

    #> Warning in Matrix::sparseMatrix(dims = c(0, 0), i = integer(0), j = integer(0), : 'giveCsparse' has been deprecated; setting 'repr = "T"' for you

    #> Warning in Matrix::sparseMatrix(dims = c(0, 0), i = integer(0), j = integer(0), : 'giveCsparse' has been deprecated; setting 'repr = "T"' for you

    tab_model(m1, m3, m4, show.ci = FALSE)

| Negative impact with 7 items | neg c 7 d | count
--- | --- | --- | ---
Predictors | Estimates | p | Odds Ratios | p | Incidence Rate Ratios | p
(Intercept) | 6.55 | <0.001 | 0.02 | <0.001 | 0.94 | 0.745
carer’age | -0.00 | 0.802 | 1.01 | 0.355 |  |
carer’s gender | 0.47 | 0.094 | 1.83 | 0.001 |  |
elder’s dependency | 1.45 | <0.001 | 2.37 | <0.001 |  |
spp: PR |  |  |  |  | 0.59 | 0.062
spp: DM |  |  |  |  | 1.25 | 0.121
spp: EC-A |  |  |  |  | 0.82 | 0.331
spp: EC-L |  |  |  |  | 1.91 | <0.001
spp: DES-L |  |  |  |  | 1.83 | <0.001
spp: DF |  |  |  |  | 1.05 | 0.765
mined: no |  |  |  |  | 2.76 | <0.001
Zero-Inflated Model
(Intercept) |  |  |  |  | 5.79 | <0.001
spp: PR |  |  |  |  | 5.36 | <0.001
spp: DM |  |  |  |  | 0.65 | 0.223
spp: EC-A |  |  |  |  | 3.02 | 0.003
spp: EC-L |  |  |  |  | 0.65 | 0.223
spp: DES-L |  |  |  |  | 0.51 | 0.056
spp: DF |  |  |  |  | 0.65 | 0.223
mined: no |  |  |  |  | 0.09 | <0.001
Random Effects
σ2 | 12.61 | 3.29 | 0.10
τ00 | 0.50 cluster | 0.24 cluster | 0.05 site
ICC | 0.04 | 0.07 | 0.34
N | 8 cluster | 8 cluster | 23 site
Observations | 888 | 888 | 644
Marginal R2 / Conditional R2 | 0.127 / 0.160 | 0.181 / 0.237 | 0.724 / 0.819

## References

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