在PLM方面滞后 [英] Lagging Forward in plm
问题描述
这是一个非常简单的问题,但是我找不到确切的答案,所以我想问一下.我使用plm
包处理面板数据.我正在尝试使用lag
函数将变量FORWARD滞后(默认是从上一个周期中检索值,并且我希望从NEXT中检索该值).我发现了许多旧文章/问题(大约在2009年),暗示通过使用k=-1
作为参数是可能的.但是,当我尝试这样做时,出现错误.
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.
示例代码:
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"))
滞后:
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
我还读到plm.data
对于plm
中的某些应用程序已替换pdata.frame
.但是,plm.data
似乎根本无法与lag
函数一起使用:
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
我将不胜感激.如果有人对使用某个包的建议有其他建议,我会很不高兴.但是,我很喜欢plm
,因为它会自动处理多个人之间的滞后并跳过时间序列中的间隔.
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.
推荐答案
EDIT2 :在plm
CRAN版本> = 1.6-4中实现了滞后(=前导值).
函数是lead()
或lag()
(后导值带有负整数的后缀).
EDIT2: lagging forward (=leading values) is implemented in plm
CRAN releases >= 1.6-4 .
Functions are either lead()
or lag()
(latter with a negative integer for leading values).
请保管所有使用相同功能名称的其他软件包.可以肯定的是,您可以通过完整的名称空间来引用该函数,例如plm::lead
.
Take care of any other packages attached that use the same function names. To be sure, you can refer to the function by the full namespace, e.g., plm::lead
.
?plm::lead
中的示例:
# First, create a pdata.frame
data("EmplUK", package = "plm")
Em <- pdata.frame(EmplUK)
# Then extract a series, which becomes additionally a pseries
z <- Em$output
class(z)
# compute negative lags (= leading values)
lag(z, -1)
lead(z, 1) # same as line above
identical(lead(z, 1), lag(z, -1)) # TRUE
这篇关于在PLM方面滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!