2

我最近在 SAS 中学习了 ANOVA 课程,并正在用 R 重写我的代码。到目前为止,将随机效应(和混合效应)模型从 SAS 转换为 R 让我望而却步。我从 R 得到的输出与 SAS 非常不同:SS 和 F 值不同,我无法获得随机效应的 F 检验。我能得到的最接近的是 Chi-sq,使用 rand()。所以也许我在 R 中做错了。

以下是 SAS 代码和输出,然后是我在 R 中所做的尝试。

数据集下载,<1kb

*Two-Way ANOVA, with one random effect and interaction term;
*import dataset as "pesticide";
proc glm data=pesticide;
    class locations chemicals;
    model numberkilled = locations chemicals locations*chemicals / solution;
    random locations locations*chemicals / test;
run; quit;

SAS 输出

以下是尝试的 R 代码。

#data step
pesticide <- read.csv("ex17-10.txt")
colnames(pesticide) <- c("location", "chemical", "number_killed")
pesticide$location <- as.factor(pesticide$location)
pesticide$chemical <- as.factor(pesticide$chemical)

#ANOVA
library(lmerTest); library(car)
model <- lmer(number_killed ~ chemical + (1|location) + (1|chemical:location), data=pesticide)
Anova(model, type=3, test="F")

接下来是输出。随机效应和交互项(也是随机的)没有 F 检验,SS 和 F 值与 SAS 不同。

Analysis of Deviance Table (Type III Wald F tests with Kenward-Roger df)

Response: number_killed
                  F Df Df.res    Pr(>F)    
(Intercept) 587.069  1     16 4.879e-14 ***
chemical     48.108  3     12 5.800e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

总之,我不知道如何在 R 中正确地做混合效应模型。固定效应模型都可以。

4

1 回答 1

1

您可以按照此处给出的建议重现 SAS 代码的输出:

fit <- lm(number_killed ~ location * chemical, data=pesticide)
results <- anova(fit)
Df <- results$Df
SumSq <- results$"Sum Sq"
MeanSq <- results$"Mean Sq"
Fvalue <- results$"F value"
Pvalue <- results$"Pr(>F)"
Error.Term <- MeanSq[3]
df.error <- Df[3]

Fvalue[1] <- MeanSq[1]/Error.Term
Pvalue[1] <- 1 - pf(Fvalue[1], Df[1], df.error)

Fvalue[2] <- MeanSq[2]/Error.Term
Pvalue[2] <- 1 - pf(Fvalue[2], Df[2], df.error)

Ftable <- cbind(Df, SumSq, MeanSq, Fvalue, Pvalue)
rownames(Ftable) <- c("Locations", "Chemicals", "Locations:Chemicals", "Residuals")
print(Ftable)

#                     Df    SumSq    MeanSq     Fvalue       Pvalue
# Locations            4   3.8115  0.952875  0.7076461 6.020037e-01
# Chemicals            3 180.1327 60.044250 44.5914534 8.797523e-07
# Locations:Chemicals 12  16.1585  1.346542  3.8889290 3.652306e-03
# Residuals           20   6.9250  0.346250         NA           NA
于 2018-03-22T11:32:32.103 回答