I have an xts object and I am trying to use period.apply with a function that would return more than one value for each non-overlapping period. For example: I run a regression and would like to return the residuals for each non-overlapping period and thus my function returns all the dates from that period along with the residual at each particular date. It currently seems that xts does not support this behavior. Is this correct? Is there a work around?
> df <- data.frame(x=rnorm(31)+10, y=rnorm(31)+10)
> xts.data <- xts(df, order.by=as.Date(13514:(13544),origin="1970-01-01"))
> f <- function(d) {as.numeric(coredata(d[,"x"]))}
> period.apply(xts.data, INDEX=endpoints(xts.data,"weeks"), FUN=f)
Error in coredata.xts(x) : currently unsupported data type
It just occurred to me that the problem is likely that each non-overlapping period doesn’t have the same number of observations, so there’s no way to easily create a matrix-like structure.
Use something like this instead, and notice how each list element isn’t the same length:
To illustrate how
period.applycan return more than one column per period: