简体   繁体   English

R / Python置信区间

[英]R / Python confidence interval

Just trying to figure out a R -> Python thing: Why are these two items not giving the same results? 只是想弄清楚R-> Python问题:为什么这两个项目没有给出相同的结果?

Calculating 95% confidence interval for sample data with mean = 65, s = 22, n = 121. 计算平均值= 65,s = 22,n = 121的样本数据的95%置信区间。

R: tsum.test(nx=121, mean.x=65, sx=22) R: tsum.test(nx=121, mean.x=65, sx=22)

gives 95% confidence interval of 61.04014 68.95986 给出95%的置信区间61.04014 68.95986

Python: stats.norm.interval(alpha=0.95, loc=65, scale=22/np.sqrt(121)) Python: stats.norm.interval(alpha=0.95, loc=65, scale=22/np.sqrt(121))

gives 95% confidence interval of (61.080072030919894, 68.9199279690801) 给出95%的置信区间(61.080072030919894, 68.9199279690801)

I thought that these should be identical results, or am I not using the appropriate equivalent Python function for R's tsum.test ? 我认为这些应该是相同的结果,还是我不对R的tsum.test使用适当的等效Python函数?

Upon further investigation I can see that I was wrong to assume to use stats.norm for this. 经过进一步的调查,我发现我认为使用stats.norm是错误的。

scipy.stats.t allows for the degree of freedom calculation that R's tsum.test is doing automatically: scipy.stats.t允许计算R的tsum.test自动执行的自由度:

stats.t.interval(alpha = 0.95, df = 121-1, loc = 65, scale= 22/np.sqrt(121)) returns (61.04013918989445, 68.95986081010555) stats.t.interval(alpha = 0.95, df = 121-1, loc = 65, scale= 22/np.sqrt(121))返回(61.04013918989445, 68.95986081010555)

which round at 5 decimal points to the answers given by tsum.test in R. 将R的tsum.test给出的答案舍入到5个小数点。

The general function I am using, if helpful, is this: 我正在使用的常规功能(如果有帮助)是:

def get_conf_interval_from_sample(n, mean, sigma, alpha = 0.95) :
    """Get confidence interval from sample data with sample of n, mean, sigma, where df = n-1
    Equivalent to getting confidence interval using t.test / tsum.test in R"""
    df = n-1
    scale = sigma / np.sqrt(n)
    return stats.t.interval(alpha=alpha, df=df, loc=mean, scale=scale)````

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

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