Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 470
user2558053 Avatar asked Apr 04 '16 10:04

user2558053


People also ask

How do you find the confidence interval for the slope of a regression line?

Each confidence interval is calculated using an estimate of the slope plus and/or minus a quantity that represents the distance from the mean to the edge of the interval. For two-sided confidence intervals, this distance is sometimes called the precision, margin of error, or half-width.

Which of the 95% confidence intervals for a regression line's slope indicates that the linear relationship is not significant at the 5% level select all that apply?

Note another option is also correct. Which of the 95% confidence intervals for a regression line's slope indicates that the linear relationship is NOT significant at the 5% level? The range between -9.85 and 5.26 contains zero, which indicates that the linear relationship is not significant at the 5% level.

Which of the following is a 95 percent confidence interval for the slope of the population regression line?

There are degrees of freedom. In other words, we are 95% confident that in the population the slope is between 0.523 and 1.084. For every one point increase in Test 3 the predicted value of Test 4 increases between 0.523 and 1.084 points.

How do you construct a 95 percent confidence interval for the slope of the least squares regression line?

To construct 95% confidence intervals for , we simply take the coefficient and add/subtract \displaystyle 1.96\times the\ standard\ error\ of\ \beta.


2 Answers

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
like image 117
Ulrich Stern Avatar answered Nov 12 '22 07:11

Ulrich Stern


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
like image 36
CoMartel Avatar answered Nov 12 '22 07:11

CoMartel