How to get residuals from Repeated measures ANOVA model in R

好久不见. 提交于 2019-12-02 00:57:32

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:

> npk.pr <- proj(npk.aovE) 
> npk.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"]
toto_tico

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)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!