Displaying 1 result from an estimated 1 matches for "movingwindow".
2004 Apr 24
4
Moving window regressions - how can I improve this code?
...ive, but that's rocket science to me!!
For a regression with K explanatory variables, I make a matrix with
2*K+2 columns, where I capture:
K coefficients and K standard errors
the residual sigma
R^2
My code is:
# ------------------------------------------------------------
movingWindowRegression <- function(data, T, width, model, K) {
results = matrix(nrow=T, ncol=2*K+2)
for (i in width:T) {
details <- summary.lm(lm(as.formula(model), data[(i-width+1):i,]))
n=1;
for (j in 1:K) {
results[i, n] = details$coefficients[j, 1]
re...