繁体   English   中英

scipy.stats.linregress - 获取截距的 p 值

[英]scipy.stats.linregress - get p-value of intercept

scipy.stats.linregress返回与斜率对应的 p 值,但没有截距的 p 值。 考虑文档中的以下示例:

>>> from scipy import stats
>>> import numpy as np
>>> x = np.random.random(10)
>>> y = np.random.random(10)
>>> slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
>>> p_value
0.40795314163864016

根据文档, p-value是“假设检验的双边 p 值,其零假设是斜率为零。” 我想获得相同的统计数据,但是对于截距而不是斜率。

statsmodels.regression.linear_model.OLS 立即返回两个系数的 p 值:

>>> import numpy as np

>>> import statsmodels.api as sm

>>> X = sm.add_constant(x)
>>> model = sm.OLS(y,X)
>>> results = model.fit()
>>> results.pvalues
array([ 0.00297559,  0.40795314])    

仅使用 scipy,如何获得截距的 p 值(0.40795314163864016)?

要计算截距的 pvalue,请执行以下操作:

  • 从 tvalue 开始,该 tvalue 从截距的均值和 stderr 开始计算(参见下面的函数tvalue
  • 然后使用 t 分布和自由度的生存函数计算 pvalue(参见下面的函数pvalue

scipy 案例的 Python 代码:

import scipy.stats
from scipy import stats
import numpy as np

def tvalue(mean, stderr):
    return mean / stderr

def pvalue(tvalue, dof):
    return 2*scipy.stats.t.sf(abs(tvalue), dof)

np.random.seed(42)
x = np.random.random(10)
y = np.random.random(10)
scipy_results = stats.linregress(x,y)
print(scipy_results)
dof = 1.0*len(x) - 2
print("degrees of freedom = ", dof)
tvalue_intercept = tvalue(scipy_results.intercept, scipy_results.intercept_stderr)
tvalue_slope = tvalue(scipy_results.slope, scipy_results.stderr)
pvalue_intercept = pvalue(tvalue_intercept, dof)
pvalue_slope = pvalue(tvalue_slope, dof)
print(f"""tvalues(intercept, slope) = {tvalue_intercept, tvalue_slope}
pvalues(intercept, slope) = {pvalue_intercept, pvalue_slope}
""")

输出:

LinregressResult(slope=0.6741948478345656, intercept=0.044594333294114996, rvalue=0.7042846127289285, pvalue=0.02298486740535295, stderr=0.24027039310814322, intercept_stderr=0.14422953722007206)
degrees of freedom =  8.0
tvalues(intercept, slope) = (0.30919001858870915, 2.8059838713924172)
pvalues(intercept, slope) = (0.7650763497698203, 0.02298486740535295)

与您使用statsmodels获得的结果进行比较:

import statsmodels.api as sm
import math

X = sm.add_constant(x)
model = sm.OLS(y,X)
statsmodels_results = model.fit()
print(f"""intercept, slope = {statsmodels_results.params}
rvalue = {math.sqrt(statsmodels_results.rsquared)}
tvalues(intercept, slope) = {statsmodels_results.tvalues}
pvalues(intercept, slope) = {statsmodels_results.pvalues}""")

输出:

intercept, slope = [0.04459433 0.67419485]
rvalue = 0.7042846127289285
tvalues(intercept, slope) = [0.30919002 2.80598387]
pvalues(intercept, slope) = [0.76507635 0.02298487]

笔记

  • 修复随机种子以获得可重复的结果
  • 使用LinregressResult对象,其中还包含intercept_stderr

参考

  • 如何计算 tvalue 和 pvalue: https ://online.stat.psu.edu/stat501/lesson/2/2.12
  • 如何在 python 中从 tvalue 计算 pvalue: https ://www.statology.org/p-value-from-t-score-python/

来自 SciPy.org 文档: https ://docs.scipy.org/doc/scipy-.14.0/reference/generated/scipy.stats.linregress.html

print "r-squared:", r_value**2

输出

r平方:0.15286643777

对于其他参数,请尝试:

print ('Intercept is: ', (intercept))
print ('Slope is: ', (slope))
print ('R-Value is: ', (r_value))
print ('Std Error is: ', (std_err))
print ('p-value is: ', (p_value))

暂无
暂无

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

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