This is a very simple question, but I haven't been able to find a definitive answer, so I thought I would ask it. I use the plm
package for dealing with panel data. I am attempting to use the lag
function to lag a variable FORWARD in time (the default is to retrieve the value from the previous period, and I want the value from the NEXT). I found a number of old articles/questions (circa 2009) suggesting that this is possible by using k=-1
as an argument. However, when I attempt this, I get an error.
Sample code:
library(plm)
df<-as.data.frame(matrix(c(1,1,1,2,2,3,20101231,20111231,20121231,20111231,20121231,20121231,50,60,70,120,130,210),nrow=6,ncol=3))
names(df)<-c("individual","date","data")
df$date<-as.Date(as.character(df$date),format="%Y%m%d")
df.plm<-pdata.frame(df,index=c("individual","date"))
Lagging:
lag(df.plm$data,0)
##returns
1-2010-12-31 1-2011-12-31 1-2012-12-31 2-2011-12-31 2-2012-12-31 3-2012-12-31
50 60 70 120 130 210
lag(df.plm$data,1)
##returns
1-2010-12-31 1-2011-12-31 1-2012-12-31 2-2011-12-31 2-2012-12-31 3-2012-12-31
NA 50 60 NA 120 NA
lag(df.plm$data,-1)
##returns
Error in rep(1, ak) : invalid 'times' argument
I've also read that plm.data
has replaced pdata.frame
for some applications in plm
. However, plm.data
doesn't seem to work with the lag
function at all:
df.plm<-plm.data(df,indexes=c("individual","date"))
lag(df.plm$data,1)
##returns
[1] 50 60 70 120 130 210
attr(,"tsp")
[1] 0 5 1
I would appreciate any help. If anyone has another suggestion for a package to use for lagging, I'm all ears. However, I do love plm
because it automagically deals with lagging across multiple individuals and skips gaps in the time series.
lag
is a generic from the stats package, so the relevant code will beplm:::lag.pseries
which may not be coded to handle negative values fork
– Trilbielag.pseries
has its second argument assigned to "k", so you should try to name your 'lag' argument (and k will default to 1). – Rubriclag(df.plm$data,k=-1
) results in the same error. GSee - there don't appear to be any restrictions on whatk
can be, but the function does use the length of the vector, so you might be correct. – Lacunar