Skip to Content

Rolling regression xts object in R

I am attempting to perform a rolling 100 day regression on an xts object and return the t statistic of the slope coefficient for all dates. I have an xts object, prices:

> tail(prices)
             DBC   EEM   EFA    GLD   HYG    IEF   IWM   IYR    MDY    TLT
2012-11-02 27.14 41.60 53.69 162.60 92.41 107.62 81.19 64.50 179.99 122.26
2012-11-05 27.37 41.80 53.56 163.23 92.26 107.88 81.73 64.02 181.10 122.95
2012-11-06 27.86 42.13 54.07 166.30 92.40 107.39 82.34 64.16 182.69 121.79
2012-11-07 27.34 41.44 53.26 166.49 91.85 108.29 80.34 63.84 178.90 124.00
2012-11-08 27.38 40.92 52.78 167.99 91.55 108.77 79.21 63.19 176.37 125.84
2012-11-09 27.60 41.00 52.80 167.82 91.39 108.78 79.38 62.98 176.80 125.98

And a function to generate the t value from a regression of the last 100 prices and return as xts object with the same column names as prices:

compute.regression <- function(x) 
{
  last100 = last(x,100)
  fit <- lm (last100~time(last100))
  tval <- unlist(lapply( coef(summary(fit)), "[" ,"(Intercept)"  ,"t value"))
  tval.mat <-as.matrix(tval)
  tval.mat2 <- matrix(c(tval.mat), nrow=1, ncol=10)
  new2 <- xts(tval.mat2, Sys.Date())
  colnames(new2) <- tickers
  return(new2)
}
 
> compute.regression(prices)
                 IWM       EFA       HYG       EEM       IYR      IEF       TLT            DBC       GLD      MDY
2012-11-10 -7.642781 -14.33474 -16.28911 -14.52982 -15.85337 5.732489 -8.026495 -1.960392 -11.82474 8.686045

However, this function only returns the t values for the current date. I need a xts object that includes the t values for all dates included in the prices xts object. I am attempting to use rollapply but not getting anywhere:

fit.r <- function (x) 
{
      hard <- lm (x~time(x))
      return(hard)
}
 
compute.r <- function(x)
{  
  l100.ra <- rollapply(x, 100, fit.r, fill=NA, align='right')
  tval <- unlist(lapply( coef(summary(l100.ra)), "[" ,"(Intercept)"  ,"t value"))
  tval.mat <-as.matrix(tval)
  tval.mat2 <- matrix(c(tval.mat), nrow=1, ncol=10)
  new2 <- xts(tval.mat2, Sys.Date())
  colnames(new2) <- tickers
  return(new2)
}

But this returns the error:

compute.r(prices)
Error in zoo(rval, index(x)[i]) :
“x” : attempt to define invalid zoo object

I know the problem is in the l100.ra line but cannot fix it. Any thoughts?

EDIT 1

The function provided below:

x <- prices
f <- function (x) {
  res <- coef(summary(lm(x ~ time(x)))
  sapply(res, "[" ,"time(x)"  ,"t value")
}
r <- rollapplyr(x, 100, f, by.column=FALSE)

works well but changes the date/time format to that below:

> last(r,5)
           Response MDY Response TLT
1352160000     15.38380    -7.913764
1352246400     14.81888    -7.957261
1352332800     13.96203    -7.666699
1352419200     13.28624    -7.299532
1352678400     12.75266    -7.100558

I've been reading up on timestamps but dont understand why the conversion took place. Also, when I try:

last(index(r),5)
[1] "2012-11-06 GMT" "2012-11-07 GMT" "2012-11-08 GMT" "2012-11-09 GMT" "2012-11-12 GMT"

It shows the timestamps in POSIXct format. Any clues here? I need to convert back to the format in prices above. Thanks!