StatsModels:返回线性回归的预测间隔而没有截距

时间:2018-07-24 20:57:47

标签: python linear-regression statsmodels

我想获得没有截距的简单线性回归的预测间隔。我有以下代码:

scp -P 2222 -o User=cf:<APP_GUID>/0 ssh.<system domain>:/<path>/<file name> <LOCAL_FILE_SYS>

这是怎么回事?

1 个答案:

答案 0 :(得分:1)

@hunter,第二次调用wlu_prediction_std,exog应该重塑为x1.reshape(-1,1)

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

x1 = np.array( [40, 45, 38, 50, 48, 55, 53, 55, 58, 40, 55, 48, 45, 55, 60, 60, 60, 65, 50, 58] )
y = np.array( [1, 2, 1, 3, 2, 3, 3, 4, 4, 3, 5, 3, 3, 2, 4, 5, 5, 5, 4, 3] )

x2 = sm.add_constant(x1) 

fitted = sm.OLS(y, x2).fit() 
sdev, lower_pred, upper_pred = wls_prediction_std(fitted, exog=x2, alpha=0.95)


fitted1 = sm.OLS(y, x1).fit() # without an intercept
sdev1, lower_pred1, upper_pred1 = wls_prediction_std(fitted1, exog=x1.reshape(-1,1), alpha=0.95)