Generating a lagged time series cross sectional variable in R

谁说我不能喝 提交于 2019-12-05 01:24:27

问题


I am a new R user. I have a time series cross sectional dataset and, although I have found ways to lag time series data in R, I have not found a way to create lagged time-series cross sectional variables so that I can use them in my analysis.


回答1:


Here's how you could use the lag() function with zoo (and panel series data):

> library(plm)
> library(zoo)
> data("Produc")
> dnow <- pdata.frame(Produc)
> x.Date <- as.Date(paste(rownames(t(as.matrix(dnow$pcap))), "-01-01", sep=""))
> x <- zoo(t(as.matrix(dnow$pcap)), x.Date)
> x[1:3,1:3]
            ALABAMA  ARIZONA ARKANSAS
1970-01-01 15032.67 10148.42  7613.26
1971-01-01 15501.94 10560.54  7982.03
1972-01-01 15972.41 10977.53  8309.01

Lag forward by 1:

> lag(x[1:3,1:3],1)
            ALABAMA  ARIZONA ARKANSAS
1970-01-01 15501.94 10560.54  7982.03
1971-01-01 15972.41 10977.53  8309.01

Lag backward by 1:

> lag(x[1:3,1:3],k=-1)
            ALABAMA  ARIZONA ARKANSAS
1971-01-01 15032.67 10148.42  7613.26
1972-01-01 15501.94 10560.54  7982.03

As Dirk mentioned, be careful with the meaning of lag in the different time series packages. Notice how xts treats this differently:

> lag(as.xts(x[1:3,1:3]),k=1)
            ALABAMA  ARIZONA ARKANSAS
1970-01-01       NA       NA       NA
1971-01-01 15032.67 10148.42  7613.26
1972-01-01 15501.94 10560.54  7982.03



回答2:


For cross-sectional time-series data the package plm is very useful. It has a lag function that takes into account the panel nature of the data.

library(plm)
data("Produc", package="plm")
dnow <- pdata.frame(Produc)
head(lag(dnow$pcap,1))
             ALABAMA-1970 ALABAMA-1971 ALABAMA-1972 ALABAMA-1973 ALABAMA-1974 
          NA     15032.67     15501.94     15972.41     16406.26     16762.67 

One problem with the package is that using with (or within or transform) gives you the wrong answer.

head(with(dnow, lag(pcap,1)))
15032.67 15501.94 15972.41 16406.26 16762.67 17316.26

So be careful.



来源:https://stackoverflow.com/questions/1971461/generating-a-lagged-time-series-cross-sectional-variable-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!