What's the best way to turn a POSIX datefield into some time index that is accepted by plm?
I trying out the plm package for some first difference estimation in R. As stated in the title I wonder how I can turn my POSIX dates like "2002-10-01" into values that are understood by pl开发者_如何学编程m's time index. I guess this means using integer values?
Here's what I tried so far:
panel.fd <- plm(y~X,index=c("datefield","id"),model="fd",data=z)
# returns
Error in pdim.default(index[[1]], index[[2]]) :
# when I convert my datefield to a factor, I get the same error...
z$t_idx <-as.factor(z$datefield)
Section 4.1 of the Overview tha tis part of the plm package says:
As observed above, the current version of plm is capable of working with a regular data.frame without any further transformation, provided that the individual and time indexes are in the first two columns, as in all the example datasets but Wages. If this weren’t the case, an index optional argument would have to be passed on to the estimating and testing functions.
To my reading this implies that POSIXct dates should work fine.
You can turn it into an integer value with unclass
.
> unclass(Sys.time())
[1] 1296686673
If you divide that number by 86400 you would get the number of days since 1970-1-1.
The plm package does not use the time variable for much else than indexing purposes, so you are right in only needing an integer value here.
EDIT:
How about formating it and then converting:
> x <- as.POSIXlt("2011-02-02")
> x
[1] "2011-02-02"
> as.integer(format(x, "%Y%m%d"))
[1] 20110202
精彩评论