Regression with a rolling window

I would like to run a regression with a rolling window. The number of observation is 10000, so my code took a long time. Could you let me know how to improve my code so that I can save time?
for i=1:(k-m)/12-1
for j=1:n
whichstats = {'all'};
stats = regstats(a(1+12*(i-1):12*(i+1),j), b(1+12*(i-1):12*(i+1)), 'linear',whichstats);
c(i,j) = std(stats.r);
end
end

댓글 수: 3

jonas
jonas 2018년 7월 17일
편집: jonas 2018년 7월 17일
Can you provide part of the data set? I would suggest preallocating the variable c, but I don't think it will help much.
Also, what is k, m and n?
jonas
jonas 2018년 7월 17일
편집: jonas 2018년 7월 17일
I think you will have better chances of getting a good answer if you provide a part of your data. At least to me, there is no obvious way to reduce the time other than pre-allocation. Having the data and code, I would try substituting the regstats function for something else.
Grouping the segments and calling regstats using splitapply instead of the loop could also help..
Image Analyst
Image Analyst 2018년 7월 17일
I'm not sure my answer below would change. Did you even try it?

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2018년 7월 17일
편집: Image Analyst 2018년 7월 17일

0 개 추천

"Regression with a rolling window" <== this is exactly what the Savitzky-Golay filter is. If you have the Signal Processing Toolbox, use sgolayfilt().
I also don't know why you chose not to do Jonas's request (twice) "Can you provide part of the data set?". Come on, make it EASY for us to help you, not hard.

댓글 수: 2

Image Analyst
Image Analyst 2018년 7월 17일
편집: Image Analyst 2018년 7월 17일
Attach your "a" (please pick a more descriptive name!) and say which column of the 1000 columns is "b" (again, a better name would be good) in a .mat file with the paper clip icon. Say which row or column is supposed to be filtered with a sliding polynomial regression. And say what order of regression (linear, quadratic, cubic, etc.).
Image Analyst
Image Analyst 2018년 7월 17일
Say which row or column is supposed to be filtered with a sliding polynomial regression. And say what order of regression (linear, quadratic, cubic, etc.).

댓글을 달려면 로그인하십시오.

태그

질문:

2018년 7월 17일

댓글:

2018년 7월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by