[R] Rolling window linear regression

Achim Zeileis Achim.Zeileis at uibk.ac.at
Thu Aug 19 09:42:07 CEST 2010


On Thu, 19 Aug 2010, siddharth.garg85 at gmail.com wrote:

> Hi
>
> That's right, but that isn't what I am trying to achieve. When we have a 
> rolling window, the only difference between two neighboring windows is 
> first and the last point.

Yes, this is what the example does. Consider the following artificial 
example:

## artificial bivariate series of length 5
set.seed(1)
z <- zoo(matrix(rnorm(10), ncol = 2))
colnames(z) <- c("y", "x")

## rolling regression of width 4
rollapply(z, width = 4,
   function(x) coef(lm(y ~ x, data = as.data.frame(x))),
   by.column = FALSE, align = "right")

## result is identical to
coef(lm(y ~ x, data = z[1:4,]))
coef(lm(y ~ x, data = z[2:5,]))

> First window indexes from i to i+w and second 
> window from (i+1) to (i+w+1). Is there a efficient way to run regression 
> on second window if I am given the results of regression on the first 
> window.

Yes, the above computations are not efficient but use a brute-force 
approach. I'm not sure whether there is a rolling regression 
implementation that uses an updating algorithm.
Z

> Regards 
> Sid
> ------Original Message------
> From: Achim Zeileis
> To: siddharth.garg85 at gmail.com
> Cc: Dennis Murphy
> Cc: R-help at r-project.org
> Subject: Re: [R] Rolling window linear regression
> Sent: Aug 19, 2010 12:42 PM
>
> The function rollapply() in package "zoo" can be used to run rolling 
> regressions. See the examples in the manual page for a worked example.
>
> On Thu, 19 Aug 2010, siddharth.garg85 at gmail.com wrote:
>
>> Thanks, I will try it.
>>
>> Regards
>> Sid
>> Sent on my BlackBerry? from Vodafone
>>
>> -----Original Message-----
>> From: Dennis Murphy <djmuser at gmail.com>
>> Date: Wed, 18 Aug 2010 08:46:49
>> To: <siddharth.garg85 at gmail.com>
>> Subject: Re: [R] Rolling window linear regression
>>
>> This is called kernel-based regression; the most popular version is loess.
>> Try
>>
>> library(sos)
>> findFn('loess')
>>
>> to see some of the various implementations available, including graphics
>> functions. The basic function is loess(); the window width is related to the
>> span = parameter of that function.
>>
>> HTH,
>> Dennis
>>
>> On Wed, Aug 18, 2010 at 2:08 AM, <siddharth.garg85 at gmail.com> wrote:
>>
>>> Hi
>>>
>>> Does there exists an efficient way of performing linear regression on
>>> rolling windows in R.
>>>
>>> The exact problem is:
>>>
>>> We have a dataset of length l. The window size is w.
>>>
>>> Now, I perform linear regression on window i to (i+w) . Using this model
>>> can I perform linear regression over window (i+1) to (i+w+1).
>>>
>>> Thanks
>>> Sid
>>> Sent on my BlackBerry? from Vodafone
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>> 	[[alternative HTML version deleted]]
> Sent on my BlackBerry® from Vodafone


More information about the R-help mailing list