python - 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
这里出了什么问题?
解决方案
@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)
推荐阅读
- ruby-on-rails - f 选择多项选择在表单中不起作用
- node.js - 当服务器应用程序在 EC2 上运行时如何获取用户的浏览器 ID
- highcharts - highcharts,没有数据位置
- ios - 如何在目标 c 中通过 POST 请求捕获标准错误输出并将消息发送到远程日志文件?
- php - 检查数组是否包含与值匹配的数组,然后将结果存储到新数组中
- amazon-web-services - 在亚马逊 AWS 上托管模型
- ios - 离线支持的 Firebase 数据检索问题
- search - AspNetCore.OData 7.1.0“$search”查询选项不起作用
- scikit-learn - 使用类权重的网格搜索和 XGBClassifier
- docker - Docker Swarm - 守护进程重启后不启动服务