R stargazer, lme4 and lmerTest incompatibility

杀马特。学长 韩版系。学妹 提交于 2019-12-21 13:11:41

问题


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?


回答1:


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"



回答2:


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)


来源:https://stackoverflow.com/questions/31319030/r-stargazer-lme4-and-lmertest-incompatibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!