3

I am fitting a linear regression and want to have confidence bands projected for points that fall out of the range of the values fit.

From another post, there's an example of getting the bands for the values within the fit range:

import numpy as np
import statsmodels.api as sm
from statsmodels.sandbox.regression.predstd import wls_prediction_std

#Generate some data
n = 20
x = np.sort(np.random.normal(size=n))
e = np.random.normal(size=n)
y = 1 + 0.5*x + 1*e
X = sm.add_constant(x)

#Fit the model
re = sm.OLS(y, X).fit()


from statsmodels.stats.outliers_influence import summary_table

st, data, ss2 = summary_table(re, alpha=0.05)

#Get the confidence intervals
fittedvalues = data[:,2]
predict_mean_se  = data[:,3]
predict_mean_ci_low, predict_mean_ci_upp = data[:,4:6].T
predict_ci_low, predict_ci_upp = data[:,6:8].T

#Plot confidence intervals and data points
plt.plot(x, y, 'o')
plt.plot(x, fittedvalues, '-', lw=2)
plt.plot(x, predict_ci_low, 'r--', lw=2)
plt.plot(x, predict_ci_upp, 'r--', lw=2)
plt.plot(x, predict_mean_ci_low, 'r--', lw=2)
plt.plot(x, predict_mean_ci_upp, 'r--', lw=2)
plt.show()

However, this doesn't allow me to extend the bands to predicted values. How would I go about extending these bounds to values outside of the range of the fit values, e.g. for x=5?

Kewl
  • 3,327
  • 5
  • 26
  • 45

0 Answers0