Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PCA on a rolling time window

    Hello!

    I want to compute the dynamic first principal component on time-series data using a rolling window (of 5 months).
    My data consists of several component inflation rates by month (- see below).

    Could somebody help me out with the code? I've tried with loops but haven't succeeded.


    Thanks so much!


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float date double(infl_CP0111 infl_CP0112 infl_CP0113)
    455   1  1.8   3
    456   1  1.8 3.4
    457   1  1.8 3.5
    458   1  1.8 3.7
    459   1  1.8   4
    460   1  1.7 4.3
    461   1  1.6 4.7
    462   1  1.4   5
    463   1  1.2 5.3
    464   1    1 5.5
    465   1   .8 5.6
    466 1.1   .5 5.7
    467 1.1   .2 5.6
    468 1.1  -.2 5.3
    469 1.1  -.5 5.2
    470 1.1  -.8   5
    471   1 -1.1 4.7
    472   1 -1.3 4.4
    473   1 -1.5   4
    474   1 -1.6 3.6
    475  .9 -1.7 3.2
    476  .9 -1.7 2.8
    477  .9 -1.7 2.6
    478  .8 -1.6 2.4
    479  .8 -1.4 2.3
    480  .8 -1.2 2.3
    481  .8  -.9 2.2
    482  .8  -.7 2.2
    483  .8  -.4 2.2
    484  .9  -.1 2.2
    485  .9   .3 2.3
    486  .9   .6 2.4
    487   1    1 2.5
    488   1  1.3 2.8
    489 1.1  1.6 2.9
    490 1.2  1.9 3.2
    491 1.3  2.2 3.3
    492 1.4  2.5 3.5
    493 1.5    3 3.8
    494 1.6  3.5 4.1
    495 1.8  4.1 4.3
    496 1.9  4.7 4.5
    497 2.1  5.2 4.7
    498 2.2  5.8 4.9
    499 2.4  6.2 5.1
    500 2.6  6.7 5.2
    501 2.8  7.1 5.2
    502 2.9  7.4 5.1
    503 3.1  7.6   5
    504 3.2  7.7 4.9
    505 3.3  7.4 4.8
    506 3.4    7 4.5
    507 3.4  6.3 4.3
    508 3.5  5.5 4.3
    509 3.5  4.8 4.3
    510 3.5    4 4.2
    511 3.4  3.3 4.1
    512 3.3  2.6   4
    513 3.2    2   4
    514 3.1  1.5   4
    515   3    1 4.2
    516 2.9   .6 4.3
    517 2.8   .3 4.3
    518 2.7   .2 4.3
    519 2.6   .2 4.4
    520 2.5   .3 4.3
    521 2.5   .4 4.1
    522 2.4   .5 3.8
    523 2.3   .6 3.6
    524 2.3   .8 3.5
    525 2.2    1 3.3
    526 2.2  1.1   3
    527 2.2  1.2 2.7
    528 2.2  1.4 2.4
    529 2.3  1.4 2.1
    530 2.3  1.5 1.8
    531 2.3  1.6 1.6
    532 2.3  1.7 1.4
    533 2.3  1.8 1.3
    534 2.3  1.8 1.2
    535 2.3  1.8 1.2
    536 2.3  1.7   1
    537 2.3  1.6  .9
    538 2.2  1.6  .9
    539 2.2  1.5  .8
    540 2.1  1.5  .8
    541 1.9  1.5  .9
    542 1.8  1.5  .9
    543 1.6  1.5  .9
    544 1.5  1.5  .8
    545 1.4  1.5  .9
    546 1.2  1.4  .9
    547 1.1  1.4   1
    548   1  1.4 1.2
    549  .9  1.4 1.3
    550  .8  1.4 1.4
    551  .7  1.3 1.6
    552  .7  1.3 1.7
    553  .7  1.3 1.8
    554  .8  1.3 2.1
    end
    format %tm date

  • #2
    You should not use PCA with time series data. You can use functional PCA. Stata can't do this, but Python can, and I have the code to do it, if you're interested.

    Comment


    • #3
      If moving correlations and regressions between series varying in time make sense, then I suppose PCA does too. Functional PCA strikes me as just different and aimed at a different view of the data.

      My puzzlement is otherwise:

      You're estimating PCs from samples of 5 observations? If so, I couldn't take correlations based on samples of 5 very seriously myself.

      What is that you want to save and how? PC1 has as many values (scores) as observations and so where do you want to put them given your present data structure? For every observation that is ONE OF the end of a window, the middle of a window, or the start of a window, you need 5 places to put 5 values.

      Comment

      Working...
      X