Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R stargazer, lme4 and lmerTest incompatibility

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?

like image 446
Nick Ruiz Avatar asked Jul 09 '15 13:07

Nick Ruiz


2 Answers

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)
like image 64
steve Avatar answered Nov 17 '22 03:11

steve


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"
like image 41
Daniel Yudkin Avatar answered Nov 17 '22 03:11

Daniel Yudkin