简体   繁体   English

如何从R中的重复测量方差分析模型中获得残差

[英]How to get residuals from Repeated measures ANOVA model in R

Normally from aov() you can get residuals after using summary() function on it. 通常,在aov()使用summary()函数后,可以得到残差。

But how can I get residuals when I use Repeated measures ANOVA and formula is different? 但是,当我使用重复测量方差分析和公式不同时,如何得到残差?

## as a test, not particularly sensible statistically
npk.aovE <- aov(yield ~  N*P*K + Error(block), npk)
npk.aovE
summary(npk.aovE)
Error: block
          Df Sum Sq Mean Sq F value Pr(>F)
N:P:K      1   37.0   37.00   0.483  0.525
Residuals  4  306.3   76.57               

Error: Within
          Df Sum Sq Mean Sq F value  Pr(>F)   
N          1 189.28  189.28  12.259 0.00437 **
P          1   8.40    8.40   0.544 0.47490   
K          1  95.20   95.20   6.166 0.02880 * 
N:P        1  21.28   21.28   1.378 0.26317   
N:K        1  33.14   33.14   2.146 0.16865   
P:K        1   0.48    0.48   0.031 0.86275   
Residuals 12 185.29   15.44                   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Intuitial summary(npk.aovE)$residuals return NULL .. Can anyone can help me with this? 直观summary(npk.aovE)$residuals返回NULL ..有人可以帮助我吗?

Look at the output of 看一下输出

 
 
 
  
  > names(npk.aovE)
 
  

and try 并尝试

 
 
 
  
  > npk.aovE$residuals
 
  

EDIT: I apologize I read your example way too quickly. 编辑:对不起,我阅读您的榜样太快了。 What I suggested is not possible with multilevel models with aov(). 我建议的内容对于带有aov()的多层模型是不可能的。 Try the following: 请尝试以下操作:

x1 <- gl(8, 4)                                                                 
block <- gl(2, 16)                                                             
y <- as.numeric(x1) + rnorm(length(x1))                                        
d <- data.frame(block, x1, y)                                                  

m <- aov(y ~ x1 + Error(block), d)                                             
m.pr <- proj(m)                                                                  
m.pr[[3]][, "Residuals"]

Here's a simpler reproducible anyone can mess around with if they run into the same issue: 如果遇到相同的问题,那么任何人都可以使用它来简化再现:

 x1 <- gl(8, 4) block <- gl(2, 16) y <- as.numeric(x1) + rnorm(length(x1)) d <- data.frame(block, x1, y) m <- aov(y ~ x1 + Error(block), d) m.pr <- proj(m) m.pr[[3]][, "Residuals"] 

The other option is with lme: 另一个选择是使用lme:

require(MASS) ## for oats data set
require(nlme) ## for lme()
require(multcomp) ## for multiple comparison stuff

Aov.mod <- aov(Y ~ N * V + Error(B/V), data = oats)
the_residuals <- aov.out.pr[[3]][, "Residuals"]

Lme.mod <- lme(Y ~ N * V, random = ~1 | B/V, data = oats)
the_residuals <- residuals(Lme.mod)

The original example came without the interaction ( Lme.mod <- lme(Y ~ N * V, random = ~1 | B/V, data = oats) ) but it seems to be working with it (and producing different results, so it is doing something). 原始示例没有交互( Lme.mod <- lme(Y ~ N * V, random = ~1 | B/V, data = oats)Lme.mod <- lme(Y ~ N * V, random = ~1 | B/V, data = oats) ),但似乎正在使用它(并产生了不同的结果,所以它正在做某事)。

And that's it... 就是这样...

...but for completeness: ...但出于完整性考虑:

1 - The summaries of the model 1-模型摘要

summary(Aov.mod)
anova(Lme.mod)

2 - The Tukey test with repeated measures anova (3 hours looking for this!!). 2-重复测量方差分析的Tukey测试(需要3个小时!)。 It does raises a warning when there is an interaction ( * instead of + ), but it seems to be safe to ignore it . 当存在交互( *而不是+ )时,它的确会发出警告,但是忽略它似乎是安全的。 Notice that V and N are factors inside the formula. 注意, VN是公式中的因子。

summary(Lme.mod)
summary(glht(Lme.mod, linfct=mcp(V="Tukey")))
summary(glht(Lme.mod, linfct=mcp(N="Tukey")))

3 - The normality and homoscedasticity plots 3-正态性和均方差图

par(mfrow=c(1,2)) #add room for the rotated labels
aov.out.pr <- proj(aov.mod)                                            
#oats$resi <- aov.out.pr[[3]][, "Residuals"]
oats$resi <- residuals(Lme.mod)
qqnorm(oats$resi, main="Normal Q-Q") # A quantile normal plot - good for checking normality
qqline(oats$resi)
boxplot(resi ~ interaction(N,V), main="Homoscedasticity", 
        xlab = "Code Categories", ylab = "Residuals", border = "white", 
        data=oats)
points(resi ~ interaction(N,V), pch = 1, 
       main="Homoscedasticity",  data=oats)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM