Rolling Window PCA- Save Explained factors for every period

I need to do rolling window PCA and I have a rolling window size=20. But I want to save all explained factors for [coeff,scores,explained]=pcacov(X) function in for loop.
What would be the most simple solution?
Thank you very much.

Answers (1)

One way;
numberOfVars = 100; % Set to however big your data are
numberOfWindows = 10;
explainedArray = zeros(numberOfVars,numberOfRuns);
for nw = 1:numberOfWindows
[coeff,scores,explained]=pcacov(X);
explainedArray(:,nw) = explained;
end

4 Comments

Thank you very much cyclist,
But I need explained factors for every rolling window period and I want to save them in a variable in a workspace.
So for example; for every period of window size=100; I need explained factors and then for other 100 observations I need other explained factors and store them in a workspace.
I assumed you only needed help with the storage in the for loop, since that is all you asked about.
Just modify this line
[coeff,scores,explained]=pcacov(X);
so that you have the rolling window within X each time. Maybe something like
X = biggerX(<define window in here>,:)
Do you need help in how to define a rolling window?
Do you have a chance to define a rolling window, and write a code for window size=20.
Thank you,
Insert a line like this in the loop:
rollingWindowX = fullX(nw:nw+20,:)
and do the PCA on rollingWindowX.

Sign in to comment.

Categories

Asked:

on 19 Dec 2015

Commented:

on 24 Dec 2015

Community Treasure Hunt

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

Start Hunting!