如何基于两组重复的值在python中生成p值?
我对两个不同的样本进行了一系列的三重测量,我想知道这些差异是否具有统计学意义。由于样本量小,我不能使用学生T检验。一位同事使用了一个名为limma()的R包,但我不想调用R。是我在Udacity论坛上发表的一篇文章,展示了如何获得适合各种样本大小的t统计数据 这将为您提供一个与Wikipedia文章底部表格中显示的值类似的t统计值 为了防止第一个链接发生任何情况,下面是代码:如何基于两组重复的值在python中生成p值?,python,statistics,Python,Statistics,我对两个不同的样本进行了一系列的三重测量,我想知道这些差异是否具有统计学意义。由于样本量小,我不能使用学生T检验。一位同事使用了一个名为limma()的R包,但我不想调用R。是我在Udacity论坛上发表的一篇文章,展示了如何获得适合各种样本大小的t统计数据 这将为您提供一个与Wikipedia文章底部表格中显示的值类似的t统计值 为了防止第一个链接发生任何情况,下面是代码: # -*- coding: utf-8 -*- from __future__ import division impo
# -*- coding: utf-8 -*-
from __future__ import division
import math
from scipy.stats import t
def mean(lst):
# μ = 1/N Σ(xi)
return sum(lst) / float(len(lst))
def variance(lst):
"""
Uses standard variance formula (sum of each (data point - mean) squared)
all divided by number of data points
"""
# σ² = 1/N Σ((xi-μ)²)
mu = mean(lst)
return 1.0/len(lst) * sum([(i-mu)**2 for i in lst])
def get_tstat(probability, degrees_of_freedom, tails=2):
"""get the t-statistic required for confidence intetval calculations"""
if tails not in [1,2]:
sys.exit('invalid tails parameter (1 or 2 valid)')
inv_p = 1 - ((1 - probability) / tails)
return t.ppf(inv_p, degrees_of_freedom)
def conf_int(lst=None, p=0, n=0, perc_conf=95, tails=2):
"""
Confidence interval - supply a list OR a probability (p) and sample
size (n) e.g. if you want to know the confidence interval for 1000
coin tosses (0.5 p i.e. a fair coin) then call with (None, 0.5, 1000).
If a list id provided then the relevant stats are calculated on the
list from within the function so no p or n value is required.
The result gives a figure that you can be confident to conf (e.g
95% certain for 0.95) that the result will always be within this
amount +/- from the mean.
e.g. 1000 coin tosses returns ~0.03 on a fair coin (with 0.95 conf)
which means that you can be 95% confident of a getting within 3%
of the expected no of heads if you did this experiement.
"""
if lst:
n, v = len(lst), variance(lst)
t = get_tstat(perc_conf/100, n-1)
return math.sqrt(v/n) * t
else:
if not 0 < p < 1:
sys.exit('p parameter must be >0 and <1 if lst not given')
if n == 0:
sys.exit('n parameter must be >0 if lst not given')
t = get_tstat(perc_conf/100, n-1)
return t*(math.sqrt(p*(1-p)) / math.sqrt(n))
################################################################################
# Example: 1000 coin tosses on a fair coin. What is the range that I can be 95%
# confident the result will fall within.
################################################################################
# get confidence interval
sample_size, probability, perc_conf_req = 1000, 0.5, 95
c_int = conf_int(n=sample_size, p=probability, perc_conf=perc_conf_req)
# show results
exp_heads = probability * sample_size
x = round(sample_size * c_int, 0)
print 'I can be '+str(perc_conf_req)+'% confident that the result of '+ \
str(sample_size)+' coin flips will be within +/- '+ \
str(round(c_int*100,2))+'% of '+str(int(exp_heads)) + \
' i.e. between '+str(int(exp_heads-x))+' and '+str(int(exp_heads+x))+ \
' heads (assuming a probability of '+str(probability)+' for each flip).'
#-*-编码:utf-8-*-
来自未来进口部
输入数学
从scipy.stats导入t
def平均值(lst):
#μ=1/N∑(xi)
返回和(lst)/浮动(len(lst))
def差异(lst):
"""
使用标准方差公式(每个(数据点-平均值)的平方和)
全部除以数据点的数量
"""
#σ²=1/N∑((xi-μ)²)
mu=平均值(lst)
返回1.0/len(lst)*和([(i-mu)**2表示lst中的i])
def get_tstat(概率,自由度,尾部=2):
“”“获取置信区间计算所需的t统计量”“”
如果尾不在[1,2]中:
sys.exit('无效尾部参数(1或2有效)')
投资组合p=1-((1-概率)/尾部)
返回t.ppf(俯仰,自由度)
def conf_int(lst=None,p=0,n=0,perc_conf=95,tails=2):
"""
置信区间-提供列表或概率(p)和样本
大小(n),例如,如果您想知道1000的置信区间
投币(0.5便士,即一枚普通硬币),然后打电话(无,0.5,1000)。
如果提供了列表id,则在
从函数中列出,因此不需要p或n值。
结果给出了一个您可以确信的数字(例如
95%确定(0.95),结果始终在此范围内
平均值的+/-金额。
e、 g.在一枚普通硬币上投掷1000枚硬币,返回~0.03(形态为0.95)
也就是说,你有95%的信心在3%以内
如果你做了这个实验,预期的人头数。
"""
如果是lst:
n、 v=长度(lst),方差(lst)
t=get_tstat(perc_conf/100,n-1)
返回数学sqrt(v/n)*t
其他:
如果不是0
看起来不错,但我不知道如何从以下两组测量值中获得p值分数(以确定第一组测量值是否与第二组测量值有显著差异):(2306407.115917.13968965.79547)与(2603677.74470711.647335000.68534)