Rolling window regression

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

1 vote

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

Fred
Fred le 22 Mai 2011
Thanks Daniel for your answer. Is there a way I can get p values and R square coefficients for each beta with your method ?
Daniel Shub
Daniel Shub le 22 Mai 2011
Yes, but now you are just asking me to write code for you. Why don't you try calculating p and R square values in MATLAB from a single simple regression (hint doc corrcoef). Once you have done that, see if you can figure out where it would go in the for loop I provided.
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

0 votes

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.

Question posée :

le 22 Mai 2011

Commenté :

le 5 Mai 2014

Community Treasure Hunt

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

Start Hunting!

Translated by