简体   繁体   English

如何使用此等式计算 Python 中的样本量?

[英]How do I calculate sample size in Python using this equation?

The following equation is something that I am having trouble translating into Python.以下等式是我在转换为 Python 时遇到的问题。 When I do it it pops out a ridiculously large number.当我这样做时,它会弹出一个非常大的数字。 My code is 100000/1+((2.575**2)*.38*(1-.38)/(.005**2)*100000) but it's wrong.我的代码是100000/1+((2.575**2)*.38*(1-.38)/(.005**2)*100000)但这是错误的。

Here is the equation-这是等式- 样本量公式

In this formula, let's assume that the population proportion is.38 (38%).在这个公式中,我们假设人口比例为.38(38%)。 The z score is 2.575. z 分数为 2.575。 The margin of error is.005 (.5%).误差幅度为.005 (.5%)。 The population size is 100,000.人口规模为100,000。

Can somebody help me translate this into Python?有人可以帮我把它翻译成 Python 吗? The expected result is 38,549.预期结果为 38,549。

100000/1+((2.575**2)*.38*(1-.38)/(.005**2)*100000)

You have a missing parentheses which is making python carry out the wrong calculation.您缺少括号,这使 python 执行错误的计算。 Remember your PEMDAS.记住你的 PEMDAS。 100000/1 has higher priority than + 100000/1的优先级高于+

Anyway, here's the fixed version:无论如何,这是固定版本:

def the_factor(n, z, p_cap, epsilon, N):
    return n / (1 + (((z**2) * (p_cap * (1 - p_cap)))/((epsilon**2) * N)))

print(the_factor(100000, 2.575, 0.38, 0.005, 100000))

Output: Output:

61543.38122167427

You have a mistake in the parenthesis of division parts!您在除法部分的括号中有错误!

 100000 / (1 + ((2.575**2) * .38 * (1-.38) / ((.005**2) * 100000)))

Besides putting all parts of the division inside the parenthesis, you should put * of the second division inside the parenthesis as well.除了将除法的所有部分放在括号内之外,您还应该将第二个除法的*放在括号内。

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

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