返回介绍

Example: Simulations to estimate power

发布于 2025-02-25 23:43:56 字数 1010 浏览 0 评论 0 收藏 0

What sample size is needed for the t-test to have a power of 0.8 with an effect size of 0.5?

This is a toy example, since you can just use a pakcage to calculate it, but the simulation approach works for everything, including arbitrarily complex experimental designs, correcting for multiple comparisons and so on(assuming infinite computational resources and you have some prior knowledge of the likely distribution of simulation parameters).

# Run nresps simulations
# The power is simply the fraction of reps where
# the p-value is less than 0.05

nreps = 10000
d = 0.5

n = 50
power = 0
while power < 0.8:
    n1 = n2 = n
    x = np.random.normal(0, 1, (n1, nreps))
    y = np.random.normal(d, 1, (n2, nreps))
    t, p = st.ttest_ind(x, y)
    power = (p < 0.05).sum()/nreps
    print n, power
    n += 1
50 0.7002
51 0.706
52 0.7119
53 0.7181
54 0.7344
55 0.7351
56 0.7405
57 0.7583
58 0.761
59 0.7647
60 0.775
61 0.7878
62 0.7865
63 0.7913
64 0.8004

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文