R 中的 aov() 错误项:bw Error(id) 和 Error(id/timevar) 规范有什么区别? [英] aov() error term in R: what's the difference bw Error(id) and Error(id/timevar) specification?
问题描述
aov(depvar~timevar+Error(id))
和 aov(depvar~timevar+Error(id/timevar))
公式有什么区别规格?这两种变体产生的结果略有不同.
What is the difference between the aov(depvar~timevar+Error(id))
and the aov(depvar~timevar+Error(id/timevar))
formula specifications? These two variants produce slightly different results.
这里曾经有人问过同样的问题:https://stats.stackexchange.com/questions/60108/how-to-write-the-error-term-in-repeated-measures-anova-in-r不过,我想用一个更合适的例子来重复一遍.
The same question was once asked here: https://stats.stackexchange.com/questions/60108/how-to-write-the-error-term-in-repeated-measures-anova-in-r However, I'd like to repeat it with a more appropriate example.
这是我创建的示例:
var=rep(NA,180)
id=rep(1:20,each=180/20)
group=rep(rep(1:2,each=9),180/(9*2))
time1=rep(rep(1:3,each=3),180/(3*3))
time2=rep(c(8,15,20),180/3)
var[group==1&time1==1&time2==8]=runif(10,105,115)
var[group==2&time1==1&time2==8]=runif(10,105,115)
var[group==1&time1==1&time2==15]=runif(10,95,105)
var[group==2&time1==1&time2==15]=runif(10,95,105)
var[group==1&time1==1&time2==20]=runif(10,85,95)
var[group==2&time1==1&time2==20]=runif(10,85,95)
var[group==1&time1==2&time2==8]=runif(10,95,105)
var[group==2&time1==2&time2==8]=runif(10,95,105)
var[group==1&time1==2&time2==15]=runif(10,85,95)
var[group==2&time1==2&time2==15]=runif(10,75,85)
var[group==1&time1==2&time2==20]=runif(10,75,85)
var[group==2&time1==2&time2==20]=runif(10,65,75)
var[group==1&time1==3&time2==8]=runif(10,95,105)
var[group==2&time1==3&time2==8]=runif(10,95,105)
var[group==1&time1==3&time2==15]=runif(10,85,95)
var[group==2&time1==3&time2==15]=runif(10,75,85)
var[group==1&time1==3&time2==20]=runif(10,75,85)
var[group==2&time1==3&time2==20]=runif(10,65,75)
df=data.frame(id,var,group,time1,time2)
df$id=factor(df$id)
df$group=factor(df$group)
df$time1=factor(df$time1)
df$time2=factor(df$time2)
根据 Error() 项规范,对此执行 aov() 会得到略有不同的结果:
Performing aov() on this gets slightly different results depending on Error() term specification:
只限一次:
> summary(aov(var~time1+Error(id),data=df))
Error: id
Df Sum Sq Mean Sq F value Pr(>F)
Residuals 19 958.4 50.44
Error: Within
Df Sum Sq Mean Sq F value Pr(>F)
time1 2 7538 3769 30.41 6.72e-12 ***
Residuals 158 19584 124
> summary(aov(var~time1+Error(id/time1),data=df))
Error: id
Df Sum Sq Mean Sq F value Pr(>F)
Residuals 19 958.4 50.44
Error: id:time1
Df Sum Sq Mean Sq F value Pr(>F)
time1 2 7538 3769 211.5 <2e-16 ***
Residuals 38 677 18
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Error: Within
Df Sum Sq Mean Sq F value Pr(>F)
Residuals 120 18907 157.6
或者对于两个时间项(为了空间起见,不要在此处输入输出,您可以自行检查):
Or for both time terms (don't type output here for the sake of space, your may check it on your own):
summary(aov(var~group*time1*time2+Error(id/(group*time1*time2)),data=df))
summary(aov(var~group*time1*time2+Error(id),data=df))
为什么会这样?哪个变体是正确的?
Why does it happen? Which variant is correct?
推荐答案
这是一篇博文,可帮助分解古典方差分析中的随机效应"部分下的每一个含义.
Here's a blog post that'll help break down what each means under the "Random Effects in Classical ANOVA" section.
从博客中,这里总结了 Error
术语中的划分"的含义.
From the blog, here's a summary for what "dividing" in the Error
term means.
aov(Y ~ Error(A), data=d) # Lone random effect
aov(Y ~ B + Error(A/B), data=d) # A random, B fixed, B nested within A
aov(Y ~ (B*X) + Error(A/(B*X)), data=d) # B and X interact within levels of A
所以从你的问题来看,
aov(depvar~timevar+Error(id/timevar))
意味着你有一个来自 id
的随机效应,然后用 timevar
嵌套在 id
级别而不是修复 timevar
means you have a random effect from id
but then fix timevar
with timevar
nested within id
levels versus
aov(depvar~timevar+Error(id))
这只是将 id
作为对其他变量没有约束的随机效应.
which is just taking id
as the random effects with no constraint on other variables.
来源:http://共轭先验.org/2013/01/formulae-in-r-anova/
这也可能有用,这是一些代码回顾了方差分析,对学习方差分析有一些建议.
This might prove useful as well, which is some code going over analysis of variance that has some recommendations on learning ANOVA.
这篇关于R 中的 aov() 错误项:bw Error(id) 和 Error(id/timevar) 规范有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!