R stargazer, lme4 and lmerTest incompatibility
Asked Answered
B

2

11

R novice here. I'm having issues working with lmerTest and stargazer. I was following the tutorial here to get stargazer to work with lme4 in R.

http://svmiller.com/blog/2015/02/quasi-automating-the-inclusion-of-random-effects-in-rs-stargazer-package/

I ran this example with no issues.

library(lme4)
library(stargazer)
data(cake)
summary(M1 <- lmer(angle ~ temp + (1 | replicate) + (1|recipe:replicate), cake, REML= FALSE))
summary(M2 <- lmer(angle ~ factor(temperature) + (1 | replicate) + (1|recipe:replicate), cake, REML= FALSE))
anova(M1,M2)
stargazer(M1, M2, style="ajps", title="An Illustrative Model Using Cake Data", dep.var.labels.include = FALSE, 
          covariate.labels=c( "Temperature (Continuous)", "Temperature (Factor $<$ 185)", "Temperature (Factor $<$ 195)", "Temperature (Factor $<$ 205)", "Temperature (Factor $<$ 215)", "Temperature (Factor $<$ 225)")
)

While that works, if I include the lmerTest package, stargazer no longer works.

library(lme4)
library(lmerTest)
library(stargazer)
data(cake)
summary(M1 <- lmer(angle ~ temp + (1 | replicate) + (1|recipe:replicate), cake, REML= FALSE))
summary(M2 <- lmer(angle ~ factor(temperature) + (1 | replicate) + (1|recipe:replicate), cake, REML= FALSE))
anova(M1,M2)
stargazer(M1, M2, style="ajps", title="An Illustrative Model Using Cake Data", dep.var.labels.include = FALSE, 
          covariate.labels=c( "Temperature (Continuous)", "Temperature (Factor $<$ 185)", "Temperature (Factor $<$ 195)", "Temperature (Factor $<$ 205)", "Temperature (Factor $<$ 215)", "Temperature (Factor $<$ 225)")
)

Error in objects[[i]]$zelig.call : 
  $ operator not defined for this S4 class

I really would like to use stargazer but my experiments require the use of merModLmerTest objects, which stargazer doesn't support. Does someone know of a workaround? How difficult would it be to cast a merModLmerTest object into a lmerMod object that would be compatible?

Bluecollar answered 9/7, 2015 at 13:31 Comment(0)
G
14

This is an easy fix. Convert the output of the lmerTest (which is in class merModLmerTest) to the lmerMod class. This will be compatible with stargazer.

class(model) <- "lmerMod"
Goahead answered 27/4, 2018 at 22:9 Comment(2)
For anyone surfing the web, this does work. Nicely done.Tungstite
This also helps if you are getting Error: $ operator not defined for this S4 class if you are giving stargaze model from lmer. Complete working code: lmm.data <- read.table("http://bayes.acs.unt.edu:8083/BayesContent/class/Jon/R_SC/Module9/lmm.data.txt", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE) MLexamp.6 <- lmer(extro ~ open + agree + social + (1|school), data=lmm.data) class(MLexamp.6) <- "lmerMod" stargazer(MLexamp.6, type = "text")Colet
U
9

Just looking through that code, I think it's a stargazer issue. stargazer can read objects of class lmerMod, not merModLmerTest. Since that hack the author describes requires the stargazer package in order to include random effects via stargazer, I think you're stuck.

If you really need lmerTest for your job (looks like for the anova function, right?), I'd recommend the following.

  1. Run your model and get your anova. Look it as you see fit.
  2. Run your models again, but specify you want lme4 estimating the models. You do this by typing lme4::lmer(y + x1, Data). Since you loaded the lmerTest package after lme4, lmerTest actually becomes the default package for running lmer functions. This is why your object became a class that stargazer couldn't read. At the end of the day, it's the same model, just stored differently.
  3. Finally, use xtable to create the anova results in LaTeX, if that's what you were wanting.

This revised code should help you.

library(lme4)
library(lmerTest)
library(stargazer)
library(xtable)
data(cake)

# Get the table first.
summary(M1 <- lme4::lmer(angle ~ temp + (1 | replicate) + (1|recipe:replicate), cake, REML= FALSE))
summary(M2 <- lme4::lmer(angle ~ factor(temperature) + (1 | replicate) + (1|recipe:replicate), cake, REML= FALSE))

stargazer(M1, M2, style="ajps", title="An Illustrative Model Using Cake Data", dep.var.labels.include = FALSE, 
      covariate.labels=c( "Temperature (Continuous)", "Temperature (Factor $<$ 185)", "Temperature (Factor $<$ 195)", "Temperature (Factor $<$ 205)", "Temperature (Factor $<$ 215)", "Temperature (Factor $<$ 225)")
)

# now for lmerTest
summary(M1a <- lmer(angle ~ temp + (1 | replicate) + (1|recipe:replicate), cake, REML= FALSE))
summary(M2a <- lmer(angle ~ factor(temperature) + (1 | replicate) + (1|recipe:replicate), cake, REML= FALSE))
anovadf <- data.frame(anova(M1a,M2a))
xtable(anovadf)
Uranography answered 15/7, 2015 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.