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?
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.
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.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)
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With