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:
Your rollapply
call should call a function that returns the t-values. Right now it returns a lm
object, which is not a valid value for the coredata
of a zoo object.
Make your function only return the t-values and it will work.
library(quantmod)
getSymbols("SPY;IEF")
x <- merge(Cl(SPY),Cl(IEF))
f <- function (x) {
res <- coef(summary(lm(x ~ time(x))))
sapply(res, "[" ,"(Intercept)" ,"t value")
}
r <- rollapplyr(x, 100, f, by.column=FALSE)