How to calculate the 99% confidence interval for the slope in a linear regression model in python?
Asked Answered
R

2

14

We have following linear regression: 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).

Removable answered 4/4, 2016 at 10:42 Comment(0)
K
14

StatsModels' RegressionResults has a conf_int() method. 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
Kenton answered 4/4, 2016 at 14:24 Comment(0)
R
7

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

EDIT : as underlines by Brian, I had the code from scipy documentation:

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
Radioscope answered 4/4, 2016 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.