简体   繁体   中英

How to get residuals from Repeated measures ANOVA model in R

Normally from aov() you can get residuals after using summary() function on it.

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?

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(). 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:

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

And that's it...

...but for completeness:

1 - The summaries of the model

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

2 - The Tukey test with repeated measures anova (3 hours looking for this!!). 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.

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

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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