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

28,446

Solution 1

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

Solution 2

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
Share:
28,446
user2558053
Author by

user2558053

Updated on April 05, 2020

Comments

  • user2558053
    user2558053 about 4 years

    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).

  • user2558053
    user2558053 about 8 years
    How to calculate the 99% confidence interval for the slope using r/p value and standard error?
  • CoMartel
    CoMartel about 8 years
    If I'm not mistaken, the 99% confidence interval correspond to a 2.58*stderr. source : en.wikipedia.org/wiki/Confidence_interval
  • CoMartel
    CoMartel about 8 years
    The r value can be used as an indicator of regression "quality" : the more it is close to 1, the better the regression is.
  • Ulrich Stern
    Ulrich Stern about 8 years
    2.58*stderr is correct only for large samples. The Basic Steps section of the Confidence Interval Wikipedia page gives 2.58 only for known standard deviation.
  • Ulrich Stern
    Ulrich Stern about 8 years
    As follow-up for my last comment, the N Multiplier table on GraphPad's Confidence interval of a mean page shows how the 1.96 (for a 95% confidence interval) is adjusted for small sample sizes.
  • Desta Haileselassie Hagos
    Desta Haileselassie Hagos almost 7 years
    I would appreciate if you could have a look at this and thank you: stackoverflow.com/questions/44923808/…