Rolling window regression

14 vues (au cours des 30 derniers jours)
Fred
Fred le 22 Mai 2011
Commenté : cyril le 5 Mai 2014
Hi there,
I would like to perform a simple regression of the type y = a + bx with a rolling window. That is, I have a time series for y and a time series for x, each with approximately 50 years of observations and I want to estimate a first sample period of 5 years, and then rolling that window by one observation, re-estimate, and repeat the process to obtain a time-varying series of the coefficient b.
What is the best and most simple way to do this? Does anybody have a sample of code to do that?
Thanks very much for your help

Réponses (2)

Daniel Shub
Daniel Shub le 22 Mai 2011
Since you are talking about 6000 data points (50 years x 12 months) optimization for speed is not a huge concern.
N = 50*12;
x = 1:N;
y = randn(1, N);
p = cell(1, N-60);
for ix = 1:N-60
p{ix} = polyfit(x((0:59)+ix), y((0:59)+ix), 1)';
end
p = cell2mat(p)';
Each row of p is the slope (b) and intercept (a) for a 60 month window.
  4 commentaires
Oleg Komarov
Oleg Komarov le 22 Mai 2011
use regstats instead of polyfit if you have the stats tb to easily get R^2 and p values.
cyril
cyril le 5 Mai 2014
or even use conv for shorter code

Connectez-vous pour commenter.


John D'Errico
John D'Errico le 22 Mai 2011
If your data series is equally spaced, then this is easy enough to do using filter. It is often called a Savitsky-Golay filter, of which a simple implementation is found in my movingslope code on the file exchange. That code uses filter to to the hard work, then patches the estimates at the ends of the series if necessary, since it uses a sliding central window.
If your data series is NOT equally spaced in time, then the solution is a bit more work since filter cannot be employed. Simplest here is just a loop, perhaps using polyfit with the appropriate data points. One can get trickier, using a QR updating scheme to add and delete points from the model, but that hardly seems worth it for a linear model, and I doubt that it would be any more efficient.
  2 commentaires
Fred
Fred le 22 Mai 2011
The data is indeed equally spaced. I have one observation every month. Thank you for your code. I had a look at it but it looks quite complicated for what I need to do. Is there a way to do a simple OLS regression on 5 years, and then repeat that regression by moving the window month by month, until the end of the sample?
John D'Errico
John D'Errico le 22 Mai 2011
Why is using that exact code, calling it directly, complicated????? Why do you need to write it yourself, rather than using code that has already been tested and debugged to do the job?
This is like saying that using ANY built-in tool in MATLAB (backslash for example, to do a regression) is complicated, because that code is more complex than you like under the hood.

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by