Page 1 of 1

rolling PCA and saving the proprtion of total variance

Posted: Sun Aug 04, 2013 1:10 pm
by wolly77
Dear guys,

I would like to perform the principal component analysis in a dynamic manner. The aim is to save the proportion of total variance accounted for by each principal components using a rolling PCA in a vector. Any idea how this can be done? Thank you in advance.

I attache the code used:

' run rolling pca

' set window size
!window = 61

' set step size
!step = 1

' get size of workfile
!length = @obsrange

' declare group for pca
group x CHN_BM COL_BM CZE_BM HKG_BM HUN_BM IND_BM ISR_BM MAL_BM MEX_BM PAK_BM PER_BM PHI_BM POL_BM SAF_BM SNG_BM TAW_BM THA_BM TUR_BM USA_BM CHI_BM BRZ_BM ARG_BM

'calculate number of rolls
!nrolls = @round((!length-!window)/!step)

'variable keeping track of how many rolls we've done
!j=0

' move sample !step obs at a time
for !i = 1 to !length-!window+1-!step step !step
!j=!j+1

' set sample to estimation period
smpl @first+!i-1 @first+!i+!window-2

' store first eigenvalue of PCA in PC1
x.pcomp(n=1)