9

R新手在这里。我在使用lmerTestand时遇到问题stargazer。我正在按照这里的教程开始stargazer使用lme4R。

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

我运行这个例子没有问题。

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)")
)

虽然这有效,但如果我包含该lmerTest软件包,则stargazer不再有效。

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

我真的很想使用stargazer,但我的实验需要使用 不支持的merModLmerTest对象。stargazer有人知道解决方法吗?将一个merModLmerTest对象转换成一个lmerMod兼容的对象有多难?

4

2 回答 2

11

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"
于 2018-04-27T22:09:39.417 回答
9

只是查看该代码,我认为这是一个stargazer问题。 stargazer可以读取类的对象lmerMod,而不是merModLmerTest. 由于作者描述的那个黑客需要stargazer包才能通过 包含随机效果stargazer,我认为你被卡住了。

如果你真的需要lmerTest你的工作(看起来像anova功能,对吧?),我推荐以下。

  1. 运行您的模型并获取您的方差分析。看你认为合适的。
  2. 再次运行您的模型,但指定您要lme4估计模型。您可以通过键入 来执行此操作lme4::lmer(y + x1, Data)。由于您在 之后加载了lmerTestlme4lmerTest因此实际上成为运行lmer功能的默认包。这就是为什么您的对象成为stargazer无法读取的类的原因。归根结底,它是相同的模型,只是存储方式不同。
  3. 最后,使用xtable在 LaTeX 中创建方差分析结果,如果那是您想要的。

修改后的代码应该对您有所帮助。

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)
于 2015-07-15T19:32:31.623 回答