如何模拟lm模型的自定义功率分析(使用R)


13

根据最近的问题,我们在这里

我希望知道是否有人遇到或可以共享R代码以执行基于线性模型仿真的自定义功率分析?

后来我显然想将其扩展到更复杂的模型,但是lm似乎是正确的起点。谢谢。

Answers:


4

我不确定您是否需要模拟一个简单的回归模型。例如,请参见便携式电源一文。对于更复杂的模型,特别是混合效果,R中的pamm包通过仿真执行功率分析。另请参阅Todd Jobe的帖子,其中包含用于仿真的R代码。


1
便携式电源链接断开。如果有人可以更新链接,那就太好了。谢谢。
Brian P

3

这是R中一些仿真代码的来源。我不确定是否专门针对线性模型,但是也许它们提供了足够的示例来说明要点:

以下站点还有另外两个模拟示例:


0

摘自R中的Bolker 2009生态模型和数据。您需要声明要测试的趋势的强度(即斜率)。直观上,强趋势和低可变性将需要较小的样本量,弱趋势和较大的可变性将需要较大的样本量。

a = 2  #desired slope
b = 1  #estimated intercept
sd = 20  #estimated variability defined by standard deviation
nsim = 400  #400 simulations
pval = numeric(nsim)  #placeholder for the second for loop output
Nvec = seq(25, 100, by = 1)  #vector for the range of sample sizes to be tested
power.N = numeric(length(Nvec))   #create placeholder for first for loop output
for (j in 1:length(Nvec)) {
  N = Nvec[j]  
  x = seq(1, 20, length = Nvec[j])  #x value length needs to match sample size (Nvec) length
  for (i in 1:nsim) {   #for this value of N, create random error 400 times
    y_det = a + b * x
    y = rnorm(N, mean = y_det, sd = sd)
    m = lm(y ~ x)
    pval[i] = coef(summary(m))["x", "Pr(>|t|)"]  #all the p values for 400 sims
  }  #cycle through all N values
  power.N[j] = sum(pval < 0.05)/nsim  #the proportion of correct p-values (i.e the power)
}
power.N
plot(Nvec, power.N)  #need about 90 - 100 samples for 80% power

您还可以模拟在给定样本量下可以测试的最小趋势是什么,如书中所示

bvec = seq(-2, 2, by = 0.1)
power.b = numeric(length(bvec))
for (j in 1:length(bvec)) {
  b = bvec[j]
   for (i in 1:nsim) {
     y_det = a + b * x
     y = rnorm(N, mean = y_det, sd = sd)
     m = lm(y ~ x)
     pval[i] = coef(summary(m))["x", "Pr(>|t|)"]
     }
   power.b[j] = sum(pval < 0.05)/nsim
  }
 power.b
 plot(bvec, power.b)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.