繁体   English   中英

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

[英]StatsModels: return prediction interval for linear regression without an intercept

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

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

这是怎么了?

@猎人第二次调用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM