首页 > 解决方案 > StatsModels:返回没有截距的线性回归的预测区间

问题描述

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

import statsmodels.api as sm 
import numpy as np  
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) # for testing purposes
N = len(x1)

fitted = sm.OLS(y, x2).fit() # with an intercept for testing purposes
sdev, lower_pred, upper_pred = wls_prediction_std(fitted, exog=x2, alpha=0.95)
# everything works until here
fitted1 = sm.OLS(y, x1).fit() # without an intercept
sdev1, lower_pred1, upper_pred1 = wls_prediction_std(fitted, exog=x1, alpha=0.95)
# ValueError: wrong shape of exog

这里出了什么问题?

标签: pythonlinear-regressionstatsmodels

解决方案


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

推荐阅读