How to extract LME4 output to latex tables?

1k views Asked by At

I usually use modelsummary() or stargazer() for extracting tables to latex in R. However, they seem to not work with lme4. Does someone know a way to extract this reproducible example to a nice latex table?

Here is my model:

df <- tibble(
      y = rnorm(100000),
      x1 = rnorm(100000),
      x2 = rnorm(100000),
      school =sample.int(300,size=100000,replace=TRUE)-1,
      classes =sample.int(100,size=100000,replace=TRUE)-1
      )
    
    df$school = as.factor(df$school)
    df$classes = as.factor(df$classes)
    
    library(lme4)
    model1 <- lmer(y~ x1 + x2 +  
                  (x1 + x2 |classes) +
                  (x1 + x2 |school), data=df)
2

There are 2 answers

1
Vincent On BEST ANSWER

As of version 0.6.4 (on CRAN now), modelsummary supports lme4::lmer models out of the box. You simply need to update your package and try again.

update.packages("modelsummary")

library(lme4)
library(modelsummary)

N <- 1000
df <- data.frame(
      y = rnorm(N),
      x1 = rnorm(N),
      x2 = rnorm(N),
      school =sample.int(300,size=N,replace=TRUE)-1,
      classes =sample.int(100,size=,replace=TRUE)-1)
    
df$school = as.factor(df$school)
df$classes = as.factor(df$classes)
    
model1 <- lmer(y~ x1 + x2 +  
              (x1 + x2 |classes) +
              (x1 + x2 |school), data=df)

modelsummary(model1, "markdown")

|            |  Model 1  |
|:-----------|:---------:|
|(Intercept) |   0.009   |
|            |  (0.036)  |
|x1          |  -0.037   |
|            |  (0.032)  |
|x2          |   0.042   |
|            |  (0.033)  |
|Num.Obs.    |   1000    |
|R2 Marg.    |   0.003   |
|R2 Cond.    |           |
|AIC         |  2858.0   |
|BIC         |  2936.6   |
|Log.Lik.    | -1413.013 |
0
Werner Hertzog On

Package exists for that.

library(texreg)
texreg(model1)

Alternatively, you could use tab_model() and convert the html output to latex (untested).

library(sjPlot)
model_html <- tab_model(model1)

library(rrtable)
HTMLcode2latex(model_html$page.content)