I have an xts of 1033 daily returns points for 5 currency pairs on which I want to run a rolling window regression, but rollapply is not working for my defined function which us
There are several problems here:
rollapply passes a matrix but lm requires a data.frame.rollapply applies the function to each column separately unless we
specify by.column=FALSE. rollapplyr :1) Incorporating the above we have:
dolm <- function(x) coef(lm(USDZAR ~ ., data = as.data.frame(x))))
rollapplyr(fxr, 62, dolm, by.column = FALSE)
2) An alternative to the lm in the dolm above is to use lm.fit which directly works with matrices and is also faster:
dolm <- function(x) coef(lm.fit(cbind(Intercept = 1, x[,-1]), x[,1]))