简体   繁体   English

使用R进行多主体重复测量方差分析的基于仿真的功率分析

[英]Using R for simulation based power analysis of Multi-Factor Within-Subjects Repeated Measures ANOVA

I have been trying to run a power analysis for study design that will be analyzed by a 4 factor Repeated Measures ANOVA where all factors are within-subjects. 我一直在尝试对研究设计进行功效分析,该分析将通过4因子重复测量方差分析进行分析,其中所有因素均在受试者内。 After a lot of searching and some help on Cross Validated, it is clear that I need to 经过大量搜索和有关交叉验证的一些帮助之后,很明显我需要

  • Do a simulation where I generate data based on some pilot data I have 做一个模拟,根据我拥有的一些试验数据生成数据
  • Run it through the appropriate anova model 通过适当的方差分析模型运行它
  • And then iterate to find the average power. 然后迭代以找到平均功率。

The code pasted below was found at this link , and it seems to do exactly what I want to do, albeit for only a 2 factor within-subjects ANOVA. 此链接中找到了下面粘贴的代码,它似乎完全可以实现我想要的功能,尽管对于对象内部方差分析只有2个因素。

However, I am having trouble understanding all of the details and what specific lines do because I am very new to R. I would be very grateful if anyone could shed some light on these lines of code: 但是,由于我对R非常陌生,因此我难以理解所有细节和特定行的功能。如果有人可以对这些代码行有所了解,我将不胜感激:

My questions: 我的问题:

  1. Why is the intercept for all conditions set to -1? 为什么将所有条件的截距设置为-1? Is that standard to this model? 这是该模型的标准吗? intercept = rep(-1, nconds) 截距= rep(-1,nconds)

  2. Do the values in these columns represent the levels of the x1 and x2 factors or are they SDs etc? 这些列中的值代表x1和x2因子的水平还是SD等等? If they are the levels, shouldn't the levels for x2 be (0, .5, 0, .5) ? 如果它们是级别,则x2的级别不应该是(0, .5, 0, .5)吗?

     true.effect.x1 = c(0, 0, .5, .5) true.effect.x2 = c(0, .5, .5, .5) 
  3. What mean and SDs do I take from my pilot data (from multiple subjects) for these three lines of code? 我从这三行代码的试验数据(来自多个主题)中获取什么意思和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) 

I know this is a very long question but I would really appreciate any help anyone could provide! 我知道这是一个很长的问题,但是我非常感谢任何人都可以提供的任何帮助! Thank you so so much! 非常感谢!

FULL CODE 完整代码

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'))

1) Because -1 = -sum(true.effect.x1) and the author that you were incompletely copying only had one "true.effect". 1)因为-1 = -sum(true.effect.x1),而您未完全复制的作者只有一个“ true.effect”。 They wanted a sum-type of contrast. 他们想要一种求和式的对比。 This is suggesting to me that you don't have a sufficient background in statistics to really understand this project. 这向我暗示您没有足够的统计学背景来真正理解该项目。

2) No, they represent covariate values against which coefficients will be estimated. 2)不,它们表示协变量值,将根据该协变量值来估计系数。 You could use contrasts like c(-2,0,1,1) and the beta estimate would have been one-half what it would be for c(-1,0,-.5,-.5). 您可以使用c(-2,0,1,1)之类的对比,而beta估计值将是c(-1,0,-。5,-。5)的一半。 Think of them as categorical indicators. 将它们视为分类指标。 "Dummy variables" is one term that is used. “虚拟变量”是使用的一个术语。

3) The question is confusing and I think it is you that are confused and not me. 3)这个问题令人困惑,我认为是您而不是我感到困惑。 The sd specified is twice the effect, and I think you have copied the comment above from another source without understanding. 指定的sd是效果的两倍,我认为您是在不理解的情况下从其他来源复制了上面的注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM