如何在 Python Pandas 回归模型中使用滞后时间序列变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39840890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Use Lagged Time-Series Variables in a Python Pandas Regression Model?
提问by Steve Maughan
I'm creating time-series econometric regression models. The data is stored in a Pandas data frame.
我正在创建时间序列计量经济学回归模型。数据存储在 Pandas 数据框中。
How can I do lagged time-series econometric analysis using Python? I have used Eviews in the past (which is a standalone econometric program i.e. not a Python package). To estimate an OLS equation using Eviews you can write something like:
如何使用 Python 进行滞后时间序列计量经济学分析?我过去使用过 Eviews(它是一个独立的计量经济学程序,即不是 Python 包)。要使用 Eviews 估计 OLS 方程,您可以编写如下内容:
equation eq1.ls log(usales) c log(usales(-1)) log(price(-1)) tv_spend radio_spend
Note the lagged dependent and lagged price terms. It's these lagged variables which seem to be difficult to handle using Python e.g. using scikit or statmodels (unless I've missed something).
请注意滞后相关和滞后价格条款。正是这些滞后变量似乎很难使用 Python 来处理,例如使用 scikit 或 statmodels(除非我遗漏了一些东西)。
Once I've created a model I'd like to perform tests and use the model to forecast.
创建模型后,我想执行测试并使用该模型进行预测。
I'm not interested in doing ARIMA, Exponential Smoothing, or Holt Winters time-series projections - I'm mainly interested in time-series OLS.
我对做 ARIMA、指数平滑或 Holt Winters 时间序列投影不感兴趣 - 我主要对时间序列 OLS 感兴趣。
回答by Steven G
pandas allows you to shift your data without moving the index such has
pandas 允许您在不移动索引的情况下移动数据
df.shift(-1)
will create a 1 index lag behing
将创建 1 个索引滞后 behing
or
或者
df.shift(1)
will create a forward lag of 1 index
将创建 1 个索引的前向滞后
so if you have a daily time series, you could use df.shift(1) to create a 1 day lag in you values of price such has
所以如果你有一个每日时间序列,你可以使用 df.shift(1) 在你的价格值中创建一个 1 天的滞后,例如
df['lagprice'] = df['price'].shift(1)
after that if you want to do OLS you can look at scipy module here :
之后,如果你想做 OLS,你可以在这里查看 scipy 模块:
http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.linregress.html
http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.linregress.html

