gpt4 book ai didi

r - Anova 无法进行多级分析 - "$ operator not defined for this S4 class"

转载 作者:行者123 更新时间:2023-12-05 03:58:27 26 4
gpt4 key购买 nike

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

$ operator not defined for this S4 class

## 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 方法分派(dispatch)根据作为第一个参数传递的对象的类工作。如果您将 lm 放在首位,则会调用 anova.lm 并且它无法处理“merMod”对象。如果将 lmer 放在首位,则会调用 anova.merMod,此方法也可以处理“lm”对象。

关于r - Anova 无法进行多级分析 - "$ operator not defined for this S4 class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57970243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com