Answers:
这是R中一些仿真代码的来源。我不确定是否专门针对线性模型,但是也许它们提供了足够的示例来说明要点:
以下站点还有另外两个模拟示例:
摘自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)