用于预测值的Python置信区间

时间:2017-08-07 13:15:22

标签: python statsmodels

我正在拟合线性回归,并希望对超出适合值范围的点投射置信带。

another post开始,有一个示例可以获取适合范围内值的波段:

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

但是,这不允许我将频段扩展到预测值。我如何将这些边界扩展到拟合值范围之外的值,例如:对于x=5

0 个答案:

没有答案