简体   繁体   English

如何在python的线性回归模型中计算斜率的99%置信区间?

[英]How to calculate the 99% confidence interval for the slope in a linear regression model in python?

We have following linear regression: y ~ b0 + b1 * x1 + b2 * x2. 我们有以下线性回归:y~b0 + b1 * x1 + b2 * x2。 I know that regress function in Matlab does calculate it, but numpy's linalg.lstsq doesn't ( https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html ). 我知道Matlab中的回归函数确实计算了它,但是numpy的linalg.lstsq没有( https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html )。

StatsModels' RegressionResults has a conf_int() method. StatsModels的RegressionResults有一个conf_int()方法。 Here an example using it (minimally modified version of their Ordinary Least Squares example): 这是一个使用它的例子( 普通最小二乘示例的最小修改版本):

import numpy as np, statsmodels.api as sm

nsample = 100
x = np.linspace(0, 10, nsample)
X = np.column_stack((x, x**2))
beta = np.array([1, 0.1, 10])
e = np.random.normal(size=nsample)

X = sm.add_constant(X)
y = np.dot(X, beta) + e

mod = sm.OLS(y, X)
res = mod.fit()
print res.conf_int(0.01)   # 99% confidence interval

You can use scipy's linear regression, which does calculate the r/p value and standard error : http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.linregress.html 您可以使用scipy的线性回归,它可以计算r / p值和标准误差: http//docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.linregress.html

EDIT : as underlines by Brian, I had the code from scipy documentation: 编辑:由Brian强调,我从scipy文档获得了代码:

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)

confidence_interval = 2.58*std_err

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

相关问题 如何计算python中简单线性回归拟合的截距和斜率? - How to calculate intercept and slope of the simple linear regression fit in python? 计算线性回归斜率矩阵(类似于相关矩阵)-Python/Pandas - calculate linear regression slope matrix (analogous to correlation matrix) - Python/Pandas Python中statsmodels线性回归中的obs置信区间是什么 - What does obs confidence interval in statsmodels linear regression in Python 从 python 中的 sklearn 线性回归获取置信区间 - Get confidence interval from sklearn linear regression in python 计算线性回归斜率矩阵(与相关矩阵相同) - Python/Pandas - Calculate linear regression slope matrix (Same to correlation matrix) - Python/Pandas pymc3:如何在多级线性回归中建模相关截距和斜率 - pymc3: how to model correlated intercept and slope in multilevel linear regression python函数计算TLS回归斜率的第5和第95置信区间。 - python function to calculate 5th and 95th confidence intervals of TLS regression slope. 线性回归的斜率 - Slope from linear regression python线性回归的置信度小于0 - Confidence level smaller than 0 with python linear regression 如何使用Python中的numpy.percentile()计算置信区间 - How to calculate a Confidence Interval using numpy.percentile() in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM