I have a question regarding?the very useful doBy package, and
specifically, the transformBy() function?with the lag() and diff()
functions. ?It is often useful to lag or difference data within a
panel, i.e., within a by-group. ?Is the following code a safe use of
transformBy? Is there an alternative?
First, does the initial "orderBy" statement guarantee that the Time
order will be preserved in the "transformBy" statement?
Second, is there an alternative to explicitly padding the differenced
series with NA's? ?I received an error without c(NA,...) but maybe
there is a better way.
Third, the lag() function generates warnings (but not errors) as the
returned series is truncated to fit within the by-groups.
Again, this appears to be working but I would like to confirm that
it's appropriate use. Thank you very much.
Best regards,
Michael
options(width=140)
library(doBy)
data(dietox)
dietox.ordered <- orderBy(~Evit+Cu+Litter+Time, data=dietox)
dietox.difference <- transformBy(~Evit+Cu+Litter, data=dietox.ordered,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?lWeight=c(NA,lag(Weight,1)),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?l4Weight=c(NA,NA,NA,NA,lag(Weight,4)),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?dWeight=c(NA,diff(Weight,1)),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?d2Weight=c(NA,NA,diff(Weight,2)),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?dFeed=c(NA,diff(Feed,1))
? ? ? ? ? ? ? ? ? ?)
head(dietox.difference,24)
Information on package 'doBy'
Package: doBy
Version: 4.3.0
Packaged: 2011-03-28 08:03:18 UTC; SHD
Repository: CRAN
Date/Publication: 2011-03-28 10:44:03
Built: R 2.13.0; ; 2011-05-02 20:18:22 UTC; unix
> sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: i686-pc-linux-gnu (32-bit)