Moving Window Principal Component Analysis


I'm finishing my thesis we're I'm forming currency-hedge investment portfolios out of the PCA on the currencies.

I need to do a PCA using a "moving-window" of the previous 60 months of data, throughout my entire data-set.

If you want the "pseudo-code" is:
-Run PCA using previous 60m of data.
-Store the principal components
-Standardize principal components
-Average the standardized principal components according to each component's proportion.
-Export Array with the averaged standardized principal components, for each month.

Basically, an "OOS" PCA with 60 month rolling window.

Any thoughts?

I tried using the formula:

rolling e(L), window(60): pca AUD CAD EUR JPY NZD SGD SEK CHF GBP USD

But I end up having the mismatch r(109) error, and I can't understand why.

For those not acquainted with the rolling formula (, e(L) is supposed specification of the stored components, 60 the number of periods to use, and pca the command we are using.

Thanks for ANY help you can provide me with this.

PS: If anyone knows how to do this in other statistical software (R, for instance), some tips would also be welcomed!

I believe it's because -e(L)- is a matrix. The following reproduces your error:

webuse lutkepohl2, clear
tsset qtr
rolling r(table), window(10): regress dln_inv dln_inc dln_consump
list in 1/10, abbrev(14)
So it seems you can't use -rolling- directly to store a matrix. Try searching the Statalist archives ( to see if this has been brought up before.

Please update this thread if you find a solution and/or spell out any links if cross-posting on different sites.