Rolling regression xts object in R

前端 未结 1 1626
再見小時候
再見小時候 2020-12-19 23:58

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:

相关标签:
1条回答
  • 2020-12-20 00:44

    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)
    
    0 讨论(0)
提交回复
热议问题