Announcement

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

  • Moving Average - Stop at Previous Date

    Good afternoon,

    I am calculating a moving average on the data below using the code...

    bysort coursedirectioncodelto (ddate): gen c_directionlto_nmfp =sum(nmfp[_n-1])/sum(nmfp[_n-1]<.)

    I would like the calculation in column 4 to stop at the previous date.
    In the example provided, the moving average for the ddate (29jan20233) would be calculated to ddate (06dec2022) and not the previous row as it is now.
    I have tried rangestat which works, but as I'm looking back 5000 days in the actual data I'm using its running very slowly.
    Is there a way to modify the command that I'm using above to achieve this quicker.

    Thanks
    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte coursedirectioncodelto float(nmfp ddate c_directionlto_nmfp)
    66    .1741 22209           .
    66     .058 22209       .1741
    66   -.5222 22209      .11605
    66    -.058 22209      -.0967
    66   -.1741 22209    -.087025
    66    .3333 22225     -.10444
    66        0 22225 -.031483334
    66    .5092 22225 -.026985714
    66   -.4472 22225    .0400375
    66        0 22234 -.014100002
    66   -.2928 22257 -.012690002
    66   -.0976 22257  -.03815455
    66    -.488 22257  -.04310834
    66   -.5311 22294  -.07733077
    66   -.5164 22329  -.10974286
    66    .1291 22329  -.13685334
    66      -.5 22339  -.12023125
    66   -.3333 22339  -.14257059
    66       .5 22339  -.15316667
    66    .1491 22339  -.11878947
    66   -.5423 22339    -.105395
    66    .3637 22349      -.1262
    66    .2414 22349  -.10393181
    66    .0976 22359   -.0889174
    66    .0976 22359  -.08114583
    66    -.488 22359    -.073996
    66   -.0976 22368  -.08991923
    66     .488 22368   -.0902037
    66   -.3333 22368  -.06955357
    66    .1667 22386  -.07864828
    66      -.5 22386     -.07047
    66    .4714 22405   -.0843258
    66        0 22405  -.06695937
    66    .3873 22440 -.064930305
    66    .2582 22440  -.05162941
    66    .1491 22554  -.04277714
    66    .4714 22554  -.03744722
    66      -.5 22573 -.023694593
    66    .4714 22573 -.036228947
    66        0 22573  -.02321282
    66    .2357 22573   -.0226325
    66    .1667 22589 -.016331706
    66   -.3333 22589 -.011973809
    66        0 22598  -.01944651
    66    .3333 22598 -.019004544
    66    .3637 22621 -.011175554
    66    .5092 22621 -.003026086
    66        0 22640  .007872341
    66   -.5092 22640  .007708334
    66    .2182 22640 -.002840815
    66        0 22683  .001580001
    66        0 22693 .0015490204
    66        0 22693 .0015192316
    66    .4714 22693 .0014905668
    66    .4472 22703  .010192594
    66   -.4472 22703  .018138183
    66     .488 22713  .009828572
    66   -.4472 22713  .018217545
    66        0 22713  .010193104
    66    .1491 22713   .01002034
    66   -.1491 22713  .012338334
    66        0 22713  .009691804
    66    .2928 22713  .009535485
    66    .4472 22723  .014031746
    66    .2357 22723       .0208
    66        0 22723  .024106154
    66        0 22723   .02374091
    66   -.2182 22733   .02338657
    66   -.1491 22743  .019833824
    66    .4472 22743  .017385507
    66        0 22769  .023525715
    66   -.3873 22769  .023194367
    66   -.5164 22769  .017493056
    66  .363696 22794  .010179453
    66 -.218218 22794  .014956703
    66  .509175 22794  .011847707
    66  .447214 22794  .018391488
    66 -.408248 22804   .02396061
    66  .408248 22804  .018419474
    66 -.447214 22918  .023354014
    66  .447214 22919  .017471913
    66 -.447214 22919   .02277737
    66        0 22919   .01704577
    66 -.447214 22937  .016840398
    66  .408248 22937   .01131594
    66        0 22937  .015985731
    66  .408248 22937   .01579985
    66        0 22953   .02031075
    66 -.129099 22953  .020079944
    66  .258199 22953  .018403776
    66       .5 22953  .021068167
    66 -.516398 22953  .026331155
    66  .421637 22962  .020431925
    66  .471405 22962   .02474596
    66 -.516398 22985   .02949765
    66 -.522233 23039   .02375138
    66  .509175 23039  .018064043
    end
    format %td ddate

  • #2
    I'm not entirely sure I understand what you want, but perhaps it is this:
    Code:
    by coursedirectioncodelto (ddate), sort: gen double numerator = sum(nmfp[_n-1])
    by coursedirectioncodelto (ddate): gen double denominator = sum(!missing(nmfp[_n-1]))
    by coursedirectioncodelto ddate: gen double wanted = numerator[1]/denominator[1]

    Comment


    • #3
      Thanks, Clyde. That worked fine.

      Comment

      Working...
      X