I would like to calculate the moving average of _b_LogSize _b_LogBM _b_MOM12 _b_cons by months of the year over the last 10 years. For instance, I would like to average:
[_b_LogSize(Jan2020)+_b_LogSize(Jan2019)+... + _b_LogSize(Jan2010)]/10
I would like to do it for each month of the year and the 4 variables above.
Currently, I am using "tsegen" but it is only performing a basic 10 years rolling average without taking into account months. Please can you help? Thanks!
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(date _b_LogSize _b_LogBM _b_MOM12 _b_cons month) 493 .09046634 4.1176205 2.5190666 -1.2977983 2 494 -2.0309033 .22475155 -9.759151 30.883093 3 495 -1.0832306 .406494 1.2204374 12.85686 4 496 -2.059664 -.24260585 -2.1624966 17.363857 5 497 .0960215 1.4607705 .5428929 1.1389401 6 498 -.51043904 1.9077493 2.975317 3.2279406 7 499 1.4333166 1.5936514 8.690335 -22.944815 8 500 -3.742916 -2.0467317 -10.620685 37.35859 9 501 -1.5358274 -.21344493 -11.50268 21.56925 10 502 -1.3188688 1.5866338 -1.571547 17.318272 11 503 .4737448 1.0378938 5.982695 -3.458642 12 504 .6831618 2.8478656 2.8225315 -3.0741665 1 505 -1.461657 -.15056618 -7.833986 19.39885 2 506 .15322176 3.042314 8.148885 -1.4166797 3 507 1.3517747 1.4202607 3.542518 -12.55804 4 508 .5102485 2.402631 9.466259 -8.92338 5 509 .14730057 -2.0097933 3.9253964 -12.569436 6 510 -.13011447 2.1992843 -1.5208116 4.3657007 7 511 -.7876962 -1.2948427 12.139635 -1.489981 8 512 .11407951 -.9536515 -10.651284 3.702368 9 513 -3.1914134 3.209994 -19.96686 39.4386 10 514 .485197 2.1473858 10.600106 -6.581267 11 515 -.3188047 .3255054 -1.9797972 .2980764 12 516 .1177141 -.3202377 1.3013583 -2.6397576 1 517 -1.1778133 1.3565993 -.6570513 12.859035 2 518 -.06110432 .7163003 -13.77926 7.564542 3 519 -3.706227 .1257015 -21.8051 38.16873 4 520 .013643764 .7770253 -.7633198 1.8524495 5 521 -2.111613 -.4619206 -2.8396015 22.133587 6 522 -.6336941 .363702 1.210958 9.975827 7 523 -1.014817 .5368967 -2.220904 9.022312 8 524 -.5406848 -.6548046 4.404526 10.582654 9 525 -.7146962 .26931196 -1.1149786 9.302611 10 526 .6385671 2.2701182 -1.6060712 1.2781098 11 527 -.7188457 .57633734 3.1786885 8.57283 12 528 -.4600639 .9384994 -2.588896 8.649567 1 529 -.8613429 .9671511 1.099575 8.608756 2 530 -.07247534 -.1537994 -4.3157163 .8893291 3 531 -.56535167 .07716002 3.332622 5.861691 4 532 -.4648149 1.094499 2.3134766 7.434675 5 533 .27500623 2.5805945 -2.3379977 -2.46115 6 534 -.12708639 1.0634447 -2.841303 3.477974 7 535 -.8708178 .23912176 3.3763766 10.540322 8 536 -.54018456 .7189706 .036027018 8.435553 9 537 -1.4168496 1.6745526 3.554173 19.626165 10 538 -.8309463 -1.2100756 -3.173917 11.207634 11 539 .4382522 1.5968238 3.6161275 -5.716834 12 540 .09637992 2.2883317 2.479093 3.72967 1 541 -.2498437 1.620358 .7389261 2.624726 2 542 -.4497205 -.22138956 -4.454505 2.4982936 3 543 -1.2707784 .2922744 2.3344254 16.399544 4 544 -1.0866243 1.5483017 4.93187 12.820357 5 545 -.6698218 -.3213176 1.0515611 11.332736 6 546 -.04462296 1.1982578 4.1905856 .09844476 7 547 -.0022533806 .58677596 4.4705863 .9089295 8 548 -.26722765 -2.316635 -1.8966275 -.8938839 9 549 -.7312025 -1.262661 2.9972234 9.407964 10 550 -.25243318 .7990872 2.407081 3.20534 11 551 -1.7431985 -.8312908 8.716989 18.789204 12 552 -.6822688 -.11647125 -5.275173 7.973406 1 553 -1.329878 -.7738516 2.598626 13.546588 2 554 .6247546 1.2956816 .6266102 -3.531033 3 555 -.072735175 -.015340515 -3.187542 -1.636328 4 556 .23074944 -.3995629 2.8622246 -2.874943 5 557 .8129722 3.236957 -.8909066 -4.6846304 6 558 -.6832631 -1.0202931 -7.254835 9.369593 7 559 -.58940864 -1.5918627 -4.2435346 7.06904 8 560 -1.2256618 -.6868519 -.5801456 15.051693 9 561 -.8219826 .3530865 -4.62138 11.96707 10 562 .6590861 1.541397 2.6057794 -4.902538 11 563 -.8354015 -.8621215 2.812942 9.531097 12 564 -.9848793 1.3190557 -1.2905746 10.60272 1 565 .1217109 .48999825 1.0703242 .04173471 2 566 .1686769 -.4838383 -1.5908992 1.8760736 3 567 -.05383684 .5829975 -.28491843 4.785616 4 568 .03835373 -.19093046 -.13494432 -2.3227618 5 569 .23461634 -2.2530422 -.7020459 -7.838483 6 570 -.4224021 -.7332852 .83166 4.854007 7 571 .7889878 -.4763553 6.132439 -5.976163 8 572 -.4769273 .21490236 5.495591 6.372781 9 573 .6893386 .7729292 2.860333 -10.344966 10 574 -.12302468 .3361884 7.454767 -.4429162 11 575 -.11429738 3.273526 -10.505454 .959588 12 576 -.8703886 .2214929 7.45836 5.192826 1 577 .08055088 -.8899767 -1.0208174 -1.7982605 2 578 -1.0253339 .5415361 4.950213 16.10081 3 579 -1.318757 -.4902536 4.123805 15.095932 4 580 -.04682831 -1.1088916 11.664658 -10.094048 5 581 -1.4003263 1.8397713 -5.47766 16.4739 6 582 -.8646321 .17430896 -8.737585 10.405688 7 583 -.5431101 2.2606583 -1.1048132 -.7501085 8 584 .9161993 .5219336 2.973937 -27.47846 9 585 .62261 -.6762373 4.851254 -14.31035 10 586 -1.4068698 1.9171596 -9.508333 15.039592 11 587 -.7277468 -2.5405514 5.02278 -1.5999113 12 588 -.4634641 -3.9733584 -.3435096 -8.650168 1 589 .1495674 -.18204223 -20.42709 1.6548795 2 590 -4.0047 1.1798627 -55.92088 30.824076 3 591 1.038709 3.684857 -13.146856 -6.878941 4 592 -.822459 .1430246 9.767742 11.020597 5 end format %tm date
Code:
local t 1 local model1 "LogSize LogBM MOM12" *** 10 year rolling window *** tsegen r_cons = rowmean(L(1/120).(_b_cons)) drop _b_cons foreach var in `model`t''{ tsegen r_`var' = rowmean(L(1/120).(_b_`var')) drop _b_`var' }
0 Response to "tsegen" by group
Post a Comment