r语言 - 方差分析在多级分析中不起作用 - "$ operator not defined for this S4 class"



尝试进行与多级分析相关的方差分析时,我在 R 中运行代码时遇到问题。我总是收到错误:

$ 未为此 S4 类定义运算符

## Model 0: Model without teams - grand-mean-centered
h2_0_gmc <- lm(PSS_mean ~ PCT_mean_gmc, data = dat_h1_2)
## Model 1: Model with teams - fixed intercept, random slope - grand-mean-centered
h2_1_gmc <- lmer(PSS_mean ~ PCT_mean_gmc + (1 | teamcode), data = dat_h1_2)
## Model 2: Model with teams - random intercept, random slope - grand-mean-centered
h2_2_gmc <- lmer(PSS_mean ~ PCT_mean_gmc + (PCT_mean_gmc | teamcode), data = dat_h1_2)
## Comparison of models
anova(h2_0_gmc, h2_1_gmc, h2_2_gmc)

只需颠倒参数的顺序:

library(lme4)
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
fm0 <- lm(Reaction ~ Days, sleepstudy)
anova(fm0, fm1)
#Error: $ operator not defined for this S4 class
anova(fm1, fm0)
#refitting model(s) with ML (instead of REML)
#Data: sleepstudy
#Models:
#fm0: Reaction ~ Days
#fm1: Reaction ~ Days + (Days | Subject)
#    Df    AIC    BIC  logLik deviance  Chisq Chi Df Pr(>Chisq)    
#fm0  3 1906.3 1915.9 -950.15   1900.3                             
#fm1  6 1763.9 1783.1 -875.97   1751.9 148.35      3  < 2.2e-16 ***
#---
#Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

技术说明:

anova是 S3 泛型。S3 方法调度根据作为第一个参数传递的对象类工作。如果将lm拟合放在第一位,则调用anova.lm,它无法处理"merMod"对象。如果将lmer拟合放在第一位,则调用anova.merMod,此方法也可以处理"lm"对象。

最新更新