我一直在尝试对研究设计进行功效分析,该分析将通过4因子重复测量方差分析进行分析,其中所有因子都在受试者内。经过大量的搜索和Cross Validated的一些帮助后,显然我需要
下面粘贴的代码是在this link找到的,它似乎完全符合我的要求,尽管只有2个因子内的ANOVA。
但是,我无法理解所有细节和具体细节,因为我对R来说很新。如果有人能够对这些代码行有所了解,我将非常感激:
我的问题:
为什么所有条件的截距都设置为-1?这个型号的标准是什么? intercept = rep(-1,nconds)
这些列中的值是代表x1和x2因子的水平还是SD等?如果它们是级别,那么x2
的级别不应该是(0, .5, 0, .5)
吗?
true.effect.x1 = c(0, 0, .5, .5)
true.effect.x2 = c(0, .5, .5, .5)
对于这三行代码,我从我的试验数据(来自多个科目)中得到了什么意思和SD?
#relatively large subject-specific variance in intercepts
sub.intercept = rep(rnorm(nsub, mean=0, sd=2), times=1, each=nconds)
#relatively small by-subject adjustments to the effects
sub.effect = rep(rnorm(nsub, mean=0, sd=0.05), times=1, each=nconds)
#unexplained error
error = rnorm(nsub*nconds, mean=0, sd=1)
我知道这是一个很长的问题,但我真的很感激任何人都可以提供帮助!非常感谢你!
完整代码
library(ez)
nsub = 30
nconds = 4
nsims = 100
#create an empty matrix that will be populated with p-values
p = matrix(NA, nrow=nsims, ncol=3)
#subject vector
sub = sort(rep(1:nsub, nconds))
#2x2 factorial design
cond = data.frame(x1=c('a','a','b','b'), x2=c('c','d','c','d'))
# fixed effects
intercept = rep(-1, nconds)
true.effect.x1 = c(0, 0, .5, .5)
true.effect.x2 = c(0, 0.5, .5, .5)
X = rep((intercept + true.effect.x1 + true.effect.x2),nsub)
#simulation loop
for (x in 1:nsims)
{
#random effects
#relatively large subject-specific variance in intercepts
sub.intercept = rep(rnorm(nsub, mean=0, sd=2), times=1, each=nconds)
#relatively small by-subject adjustments to the effects
sub.effect = rep(rnorm(nsub, mean=0, sd=0.05), times=1, each=nconds)
#unexplained error
error = rnorm(nsub*nconds, mean=0, sd=1)
#simulated dependent variable
observed.y = X + (sub.intercept + sub.effect + error)
#place everything in a data frame
df = cbind(sub, cond, observed.y)
names(df) = c('sub','x1','x2','y')
df$sub = as.factor(df$sub)
#extract the p-values for each effect from a repeated measure ANOVA (ezANOVA from 'ez' package)
p[x,1] = ezANOVA(data=df, dv=.(y), wid=.(sub), within=.(x1, x2))$ANOVA[1,5]
p[x,2] = ezANOVA(data=df, dv=.(y), wid=.(sub), within=.(x1, x2))$ANOVA[2,5]
p[x,3] = ezANOVA(data=df, dv=.(y), wid=.(sub), within=.(x1, x2))$ANOVA[3,5]
}
###### p-values < .05 ? ######
sig.x1 = ifelse(p[,1] <= .05, 1, 0)
sig.x2 = ifelse(p[,2] <= .05, 1, 0)
sig.int = ifelse(p[,3] <= .05, 1, 0)
###### Histograms ######
par(mfrow=c(3,1))
hist(p[,1], 20, xaxp=c(0, 1, 20), col=2, main = paste('power X1:', mean(sig.x1 * 100), '% with ', nsub, 'subjects'))
hist(p[,2], 20, xaxp=c(0, 1, 20), col=2, main = paste('power X2:', mean(sig.x2 * 100), '% with ', nsub, 'subjects'))
hist(p[,3], 20, xaxp=c(0, 1, 20), col=2, main = paste('power interaction:', mean(sig.int * 100), '% with ', nsub, 'subjects'))
答案 0 :(得分:0)
1)因为-1 = -sum(true.effect.x1)并且您不完全复制的作者只有一个“true.effect”。他们想要一种总和型对比。这表明你没有足够的统计背景来真正理解这个项目。
2)不,它们表示将估计系数的协变量值。你可以使用像c(-2,0,1,1)那样的对比,而beta的估计值将是c(-1,0, - 。5, - 。5)的一半。将它们视为分类指标。 “虚拟变量”是一个使用的术语。
3)这个问题令人困惑,我认为是你感到困惑而不是我。指定的sd是效果的两倍,我认为你已经从另一个来源复制了上面的评论而没有理解。