Description Usage Arguments Details References See Also Examples
Function to lag/lead a panel variable in a data frame
with an xtdata
attribute. Based on the
shift
function by TszKin Julian Chan.
1 |
data |
a data frame carrying an |
variable |
the variable to lag/lead. |
k |
the number of lags/leads. |
You might want to use the xtlag
and xtlead
convenience wrappers:
xtlag
will
perform a negative shift of k
lags
xtlag
will perform a positive shift of k
leads
Christopher Gandrud, "Slide: one function for lag/lead variables in data frames, including time-series cross-sectional data", http://christophergandrud.blogspot.com/2013/05/slide-one-function-for-laglead.html
1 2 3 4 5 6 7 8 9 10 11 12 | # Load QOG demo datasets.
data(qog.demo)
# Subset to a short series.
QOG = xtsubset(qog.ts.demo, year %in% 2008:2012 & cname == "United States")
# Lag by one time period.
QOG$L1.wdi_gdpc = xtlag(QOG, "wdi_gdpc", -1)
# Lead by two time periods.
QOG$F2.wdi_gdpc = xtlead(QOG, "wdi_gdpc", 2)
# Check results.
QOG[, c("year", "wdi_gdpc", "L1.wdi_gdpc", "F2.wdi_gdpc")]
# Full method.
cbind(QOG, sapply(-2:2, xtshift, data = QOG, variable = "wdi_hec"))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.