[R] Moving window regressions - how can I improve this code?

Ajay Shah ajayshah at mayin.org
Sat Apr 24 07:09:21 CEST 2004

I wrote a function which does "moving window" regressions. E.g. if
there are 100 observations and the window width is 50, then I first
run the regression for observations 1..50, then for 2..51, and so on.

I am extremely pleased with R in my experience with writing this,
since I was able to pass the model as an argument into the function
:-) Forgive me if I sound naive, 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

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,]))
       for (j in 1:K) {
         results[i, n]   = details$coefficients[j, 1]
         results[i, n+1] = details$coefficients[j, 2]
         n = n + 2
       results[i, n] = details$sigma
       results[i, n+1] = details$r.squared
   # Simulate some data for a linear regression
   T = 20
   x = runif(T); y = 2 + 3*x + rnorm(T);
   D = data.frame(x, y)
   r = movingWindowRegression(D, T=T, width=10, model="y ~ x", K=2)
   # ------------------------------------------------------------ 

I would be very happy if you could look at this and tell me how to do
things better.

I have two specific questions:

  1. I find it silly that I have to manually pass K and T into the
     function. It would be so much nicer to have:

        r = movingWindowRegression(D,      width=10, model="y ~ x")
     instead of the existing
        r = movingWindowRegression(D, T=T, width=10, model="y ~ x", K=2)

     How can the function inspect the data frame D and learn the
     number of rows?

     How can the function inspect the model specification string and
     learn K, the number of explanatory variables?

  2. "The R way" consists of avoiding loops when the code is
     vectorisable. I am using a loop to copy out from
     details$coefficients into the columns of results[i,]. Is there a
     better way?

Ajay Shah                                                   Consultant
ajayshah at mayin.org                      Department of Economic Affairs
http://www.mayin.org/ajayshah           Ministry of Finance, New Delhi

More information about the R-help mailing list