Is anyone aware of a package that allows one to perform a rolling
regression?
For instance, if I have a 1000 x 10 matrix and I want to loop through the
rows of the matrix repeating the regression on a constant sample of 100
rows:
x <- matrix(rnorm(1000*10),ncol=10)
rolling.regression <- function(x,window.width=100) {
ans <- matrix(NA,ncol=ncol(x),nrow=(nrow(x)-window.width+1))
for (i in window.width:nrow(x) ) {
start.row <- (i - window.width + 1)
end.row <- i
result <- lm(x[start.row:end.row,1]~x[start.row:end.row,-1])
# store result
ans[i-window.width+1,] <- as.numeric(result$coef)
}
ans
}
tmp <- rolling.regression(x)
I'm sure there are more efficient ways to perform this analysis (using
lm.fit for instance), but is anyone aware of a package that does this type
of operation for fixed windows and expanding windows?
Thanks,
Whit Armstrong
[[alternative HTML version deleted]]