Estimating a Time Series Regression with tstools

Note: The tstools package needs to be installed to run this example. If you’re using the class server, it’s already installed.

For this example, we assume you want to estimate the model \[y_{t} = \beta_{0} + \beta_{1} y_{t-1} + \beta_{2} x_{t-1} + \beta_{3} x_{t-2} + \varepsilon_{t}.\] Let’s generate 100 observations of \(y\) and \(x\) to use to estimate the regression.

library(tstools)
y <- ts(rnorm(100))
x <- ts(rnorm(100))
fit <- tsreg(y, ts.combine(lags(y, 1), lags(x, 1:2)))

The first argument to tsreg is the left hand side variable in the regression. The second is an mts object holding the regressors. lags(y, 1) returns \(y_{t-1}\). lags(x, 1:2) returns \(x_{t-1}\) and \(x_{t-2}\). ts.combine will align all observations so that dates match up, and then drop any incomplete observations.

There’s nothing to stop you from doing this estimation using the lm function. Note that the lm function was not designed for estimating models with time series data, which means:

  • You have to align observations and drop missing observations yourself.
  • Time series properties are stripped. Among other things, your residual series will be a vector of numbers, with no attached dates.

The tsreg function was created to handle both of those issues for you. You can also specify options start and end for the sample start and end dates, if desired. Note that if you specify values for start or end that are not binding (for example, if the sample starts after start) they will have no effect.

fit can be treated as the output of a regular regression estimated by lm:

summary(fit)

But it contains additional information. To plot the residuals as a time series:

plot(fit$resids)