R 食谱http://www.cookbook-r.com/Statistical_analysis/ANOVA/有一个使用 aov() 进行混合设计 ANOVA 的示例。
我会在这里复制它:
data <- read.table(header=T, con <- textConnection('
subject sex age before after
1 F old 9.5 7.1
2 M old 10.3 11.0
3 M old 7.5 5.8
4 F old 12.4 8.8
5 M old 10.2 8.6
6 M old 11.0 8.0
7 M young 9.1 3.0
8 F young 7.9 5.2
9 F old 6.6 3.4
10 M young 7.7 4.0
11 M young 9.4 5.3
12 M old 11.6 11.3
13 M young 9.9 4.6
14 F young 8.6 6.4
15 F young 14.3 13.5
16 F old 9.2 4.7
17 M young 9.8 5.1
18 F old 9.9 7.3
19 F young 13.0 9.5
20 M young 10.2 5.4
21 M young 9.0 3.7
22 F young 7.9 6.2
23 M old 10.1 10.0
24 M young 9.0 1.7
25 M young 8.6 2.9
26 M young 9.4 3.2
27 M young 9.7 4.7
28 M young 9.3 4.9
29 F young 10.7 9.8
30 M old 9.3 9.4
'))
close(con)
然后重塑它:
library(reshape2)
# Make sure subject column is a factor
data$subject <- factor(data$subject)
# Convert it to long format
data.long <- melt(data, id = c("subject","sex","age"), # keep these columns the same
measure = c("before","after"), # Put these two columns into a new column
variable.name="time") # Name of the new column
# subject sex age time value
# 1 F old before 9.5
# 2 M old before 10.3
#...
现在使用混合方差分析:
aov.after.age.time <- aov(value ~ age*time + Error(subject/time), data=data.long)
summary(aov.after.age.time)
但是当有两个以上的预测变量时,R 示例表明,在误差项之后再次添加了主体因子之间的关系:
#e.g., from R cookbook
#aov.bww <- aov(y ~ b1*b2*w1 + Error(subject/(w1)) + b1*b2, data=data.long)
# which would translate in our case as:
aov.bww <- aov(value ~ sex*age*time + Error(subject/time) + sex*age, data=data.long)
summary(aov.bww)
但是为什么 b1*b2,或者在我们的例子中,sex*age,被指定了两次呢?当我们在 Error() 术语之后删除它们似乎没有什么区别:
aov.bww2 <- aov(value ~ sex*age*time + Error(subject/time), data=data.long)
summary(aov.bww2)
谁能解释为什么这些例子有这些额外的术语?R手册只有这个例子,其中没有指定两次之间的因素:
# fm <- aov(yield ~ v + n*p*k + Error(farms/blocks), data=farm.data)
编辑:
我检查了 R Cookbook 中的参考资料,发现其他网站也在他们的混合设计示例中两次指定了这些术语。请参阅此处: http ://www.personality-project.org/R/r.anova.html 他们有示例:
aov.ex5 = aov.ex5 = aov(Recall ~ (Task*Valence*Gender*Dosage) +
Error(Subject/(Task*Valence)) + (Gender*Dosage), data.example5 )
并在此处查看 http://www.statmethods.net/stats/anova.html 及其示例:
# Two Within Factors W1 W2, Two Between Factors B1 B2
fit <- aov(y ~ (W1*W2*B1*B2) + Error(Subject/(W1*W2)) + (B1*B2),
data=mydataframe)
这大概是食谱从那里获得信息的地方。