How to Use Lagged Time-Sereis Variables in a Python Pandas Regression Model?
<p>I'm  creating time-series econometric regression models. The data is stored in a Pandas data frame.</p>

<p><strong>How can I do lagged time-series econometric analysis using Python</strong>? 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:</p>

<pre><code>equation eq1.ls log(usales) c log(usales(-1)) log(price(-1)) tv_spend radio_spend
</code></pre>

<p>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).</p>

<p>Once I've created a model I'd like to perform tests and use the model to forecast.</p>

<p>I'm not interested in doing ARIMA, Exponential Smoothing, or Holt Winters time-series projections - I'm mainly interested in time-series OLS.</p>

<p>pandas allows you to shift your data without moving the index such has</p>

<pre><code>df.shift(-1)
</code></pre>

<p>will create a 1 index lag behing</p>

<p>or</p>

<pre><code>df.shift(1)
</code></pre>

<p>will create a forward lag of 1 index</p>

<p>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</p>

<pre><code>df['lagprice'] = df['price'].shift(1)
</code></pre>

<p>after that if you want to do OLS you can look at scipy module here :</p>

<p><a href="http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.linregress.html" rel="nofollow">http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.linregress.html</a></p>

python pandas time-series regression