目标:根据重复测量ANOVA计算eta值的95%置信区间。
设计是双因素设计(因子1有3个等级,因子2有7个等级),与30个受试者完全交叉。但是,我不确定我是否正确地进行自举以计算eta的置信区间。我使用boot()函数按主题引导样本,但仅将因子和级别留在因子中。所以我不确定我是否正确这样做 - 我是否需要进行更复杂的自举/重采样,我按照因子/等级进行重新采样,或者只是在主题级别进行。我的代码似乎给出了合理的结果......
library(ez)
library(boot)
library(reshape2)
###create a data.frame for a 2-factor (Factor1-3 levels, Factor2-7 levels) fully crossed design with 30 subjects & fill fake data values
subject.number<-factor(rep(1:30,each=21))
factor1.levels<-rep(rep(c("level1","level2","level3"),each=7),30)
factor2.levels<-rep(rep(c("level1","level2","level3","level4","level5","level6","level7"),3),30)
set.seed(1234)
fake.data<-rnorm(630,mean=3)
dframe<-data.frame(subject.number,factor1.levels,factor2.levels,fake.data)
names(dframe)<-c("Subject","Factor1","Factor2","OutcomeValue")
###to work with boot() convert from long to wide format
dframe.wide<-dcast(dframe,Subject~Factor1+Factor2,value.var="OutcomeValue")
###function to use with boot() to calculate generalized eta value for Factor1, Factor2, and Factor1xFactor2 interaction in a repeated measures ANOVA
generalized_eta<-function(data,indices){
d.wide<-data[indices,] #use boot() indices to sample data
#now that have used indices from boot(), convert data back to long with correct Factor labeling
dframe.long<-melt(d.wide,value.name="OutcomeValue",id="Subject")
dframe.long<-cbind(dframe.long,colsplit(dframe.long$variable,"_",c("Factor1","Factor2")))
dframe.long$Factor1<-factor(dframe.long$Factor1)
dframe.long$Factor2<-factor(dframe.long$Factor2)
dframe.long$Subject<-factor(dframe.long$Subject)
#do repeated measures ANOVA with ezANOVA() which calculates generalized eta
aov.ez = ezANOVA(data = dframe.long, dv = .(OutcomeValue), wid = .(Subject), within = .(Factor1,Factor2), type = 1)
#return the three generalized eta values - Factor1, Factor2, Factor1xFactor2
return(aov.ez[[1]]$ges)
}
###call boot() to do the bootstrap - only 200 to make it fast
results<-boot(data=dframe.wide,statistic=generalized_eta,R=200)
###plot the bootstrap results
plot(results,index=1) #for Factor1
plot(results,index=2) #for Factor2
plot(results,index=3) #for Factor1xFactor2
###create 95%-CI from bootstrap results
boot.ci(results,type="bca",index=1) #for Factor1
boot.ci(results,type="bca",index=2) #for Factor2
boot.ci(results,type="bca",index=3) #for Factor3