Re: [R] Rolling window linear regression
Use ?loess instead. -- Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." Clifford Stoll On Sat, Oct 4, 2014 at 12:09 AM, Grace Shi <1104271...@qq.com> wrote: > > I have to do roll regression based on the Daily data. I use the past three > weeks of daily returns as the estimation window and the regression is > estimated rolling forward one week at a time generating time series > estimates of beta. I know I should use the rollapply in zoo package. but I > am not sure how to do. > > data example: > > stockday week y x > "1" 2009-01-02 2009-01 0.89 2.45 > "1" 2009-01-03 2009-01 1.21 1.90 > "1" 2009-01-04 2009-01 0.12 0.89 > "1" 2009-01-05 2009-01 1.45 2.78 > "1" 2009-01-06 2009-01 1.98 0.98 > "1" 2009-01-09 2009-02 3.34 1.23 > "1" 2009-01-10 2009-02 0.12 0.89 > "1" 2009-01-11 2009-02 1.45 2.78 > "1" 2009-01-13 2009-02 1.98 0.98 > "1" 2009-01-16 2009-03 3.38 0.93 > "1" 2009-01-17 2009-03 6.56 3.90 > "1" 2009-01-18 2009-03 5.09 3.45 > "1" 2009-01-19 2009-03 5.89 3.78 > > > > > [[alternative HTML version deleted]] > > __ > R-help@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. __ R-help@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.
Re: [R] Rolling window linear regression
I have to do roll regression based on the Daily data. I use the past three weeks of daily returns as the estimation window and the regression is estimated rolling forward one week at a time generating time series estimates of beta. I know I should use the rollapply in zoo package. but I am not sure how to do. data example: stockday week y x "1" 2009-01-02 2009-01 0.89 2.45 "1" 2009-01-03 2009-01 1.21 1.90 "1" 2009-01-04 2009-01 0.12 0.89 "1" 2009-01-05 2009-01 1.45 2.78 "1" 2009-01-06 2009-01 1.98 0.98 "1" 2009-01-09 2009-02 3.34 1.23 "1" 2009-01-10 2009-02 0.12 0.89 "1" 2009-01-11 2009-02 1.45 2.78 "1" 2009-01-13 2009-02 1.98 0.98 "1" 2009-01-16 2009-03 3.38 0.93 "1" 2009-01-17 2009-03 6.56 3.90 "1" 2009-01-18 2009-03 5.09 3.45 "1" 2009-01-19 2009-03 5.89 3.78 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Rolling window linear regression
On Thu, 19 Aug 2010, siddharth.gar...@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.gar...@gmail.com Cc: Dennis Murphy Cc: R-help@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.gar...@gmail.com wrote: Thanks, I will try it. Regards Sid Sent on my BlackBerry? from Vodafone -Original Message- From: Dennis Murphy Date: Wed, 18 Aug 2010 08:46:49 To: 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, 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@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__ R-help@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.
Re: [R] Rolling window linear regression
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.gar...@gmail.com wrote: Thanks, I will try it. Regards Sid Sent on my BlackBerry? from Vodafone -Original Message- From: Dennis Murphy Date: Wed, 18 Aug 2010 08:46:49 To: 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, 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@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]] __ R-help@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.
Re: [R] Rolling window linear regression
Thanks, I will try it. Regards Sid Sent on my BlackBerry® from Vodafone -Original Message- From: Dennis Murphy Date: Wed, 18 Aug 2010 08:46:49 To: 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, 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@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]] __ R-help@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.
[R] Rolling window linear regression
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@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.