How would I use given data to generate lags? For example, I am given the variable DMortgage. How would I generate lags of DMortgage(t): DMortgage (t-1), DMortgage (t-2), DMortgage (t-3), and DMortgage (t-4)?
In STATA
Create lag (or lead) variables using subscripts.
. gen lag1 = x[_n-1] . gen lag2 = x[_n-2] . gen lead1 = x[_n+1]
You can create lag (or lead) variables for different subgroups using the by prefix. For example,
. sort state year . by state: gen lag1 = x[_n-1]
In R
Lag a Time Series
Description
Compute a lagged version of a time series, shifting the time base back by a given number of observations.
lag is a generic function; this page documents its default method.
Usage
lag(x, ...) ## Default S3 method: lag(x, k = 1, ...)
Arguments
x |
A vector or matrix or univariate or multivariate time series |
k |
The number of lags (in units of observations). |
... |
further arguments to be passed to or from methods. |
lag {stats} | R Documentation |
Get Answers For Free
Most questions answered within 1 hours.