从StatsModels获得OLS拟合的预测
我试图从下面的 ols 拟合中进行样本预测,
import numpy as np
import pandas as pd
import statsmodels.api as sm
macrodata = sm.datasets.macrodata.load_pandas().data
macrodata.index = pd.period_range('1959Q1', '2009Q3', freq='Q')
mod = sm.OLS(macrodata['realgdp'], sm.add_constant(macrodata[['realdpi', 'realinv', 'tbilrate', 'unemp']])).fit()
mod.get_prediction(sm.add_constant(macrodata[['realdpi', 'realinv', 'tbilrate', 'unemp']])).summary_frame(0.95).head()
这很好。但是,如果我在mod.get_prediction
中更改回归器的位置,我会得到不同的估计,
mod.get_prediction(sm.add_constant(macrodata[['tbilrate', 'unemp', 'realdpi', 'realinv']])).summary_frame(0.95).head()
这是令人惊讶的。 mod.get_prediction
无法根据列名识别回归剂吗?
I am trying to get in sample predictions from an OLS fit as below,
import numpy as np
import pandas as pd
import statsmodels.api as sm
macrodata = sm.datasets.macrodata.load_pandas().data
macrodata.index = pd.period_range('1959Q1', '2009Q3', freq='Q')
mod = sm.OLS(macrodata['realgdp'], sm.add_constant(macrodata[['realdpi', 'realinv', 'tbilrate', 'unemp']])).fit()
mod.get_prediction(sm.add_constant(macrodata[['realdpi', 'realinv', 'tbilrate', 'unemp']])).summary_frame(0.95).head()
This is fine. But if I alter the positions of regressors in mod.get_prediction
, I get different estimates,
mod.get_prediction(sm.add_constant(macrodata[['tbilrate', 'unemp', 'realdpi', 'realinv']])).summary_frame(0.95).head()
This is surprising. Can't mod.get_prediction
identify the regressors based on column names?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如注释中指出的那样,
sm.ols
将您的数据框架转换为拟合的数组,并且同样进行预测,它希望预测变量的顺序相同。如果您希望使用列名,可以使用公式接口,请参见有关更多详细信息。下面我应用您的示例:
按照提供的顺序:
如果我们翻转列,则结果相同:
As noted in the comments,
sm.OLS
will convert your data frame into an array for fitting, and likewise for prediction, it expects the predictors to be in the same order.If you would like the column names to be used, you can use the formula interface, see the documentation for more details. Below I apply your example :
In the order provided :
Results are the same if we flip the columns: