Announcement

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

  • Francois Durant
    started a topic If condition with loops

    If condition with loops

    Hi,

    I would like to fill out the out of sample stock returns based on factor values (LogSize DY MOM36) and their corresponding lagged estimated coefficients (r_* ). I want the lag of the estimated coefficient used to be 1 if the lag 1 period is in the same regime (crisis=1 and not crisis=0). If not, go back 1 more lag etc. until the same regime is found. I would like to write the condition for 30 periods or more with a loop. I have written what I want below but I do not know how to program the loop:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str24 Name double(date LogSize DY MOM36 r_LogSize r_DY r_MOM36 crisis F1_ret_out)
    "JETBLUE AIRWAYS" 650 -1.9281090176391644 -1.8181114098681626   -.8627962799540776   -.11740106122124609  -.3716441653324519    .22409716210708738 0  1.8540404037749423
    "JETBLUE AIRWAYS" 651  -1.953333532727173 -1.8551743275315165    -.836440855462065   -.09929143504086753  -.2846929347412533     .1429385566198428 0  1.8860335692110155
    "JETBLUE AIRWAYS" 652  -2.040089494317819 -1.8167224914402094  -.39804164897049843    -.1660916613095861  -.4176857431335997    .13354416700763644 0  1.7866539099625167
    "JETBLUE AIRWAYS" 653 -1.9376887783114682  -1.855146244777546  -1.0022940764570223    -.2008035729740716  -.3884040095597731    .07710266777053551 0  1.8446875128967026
    "JETBLUE AIRWAYS" 654   -1.89139700197255 -1.8845053642981344    -.952873175679264   -.05772417729581753  -.4192784135066334    .07334977451495205 0  1.8383769361076068
    "JETBLUE AIRWAYS" 655 -1.8750128106669015 -1.8892477138558288  -.18088973509881917   -.08051653544275483  -.4548024605016906    .10042437629398587 0  1.7081340454007965
    "JETBLUE AIRWAYS" 656  -1.814540662259903  -1.878219069758983 -.015018040948759314   .030316591395971204  -.5606721059659836    .06462305718704353 0   1.717937254222506
    "JETBLUE AIRWAYS" 657 -1.8899958196512705 -1.8870379822576429   .12845071225998037   -.09828050321507352  -.5374306078947759    .14756611896577362 0  1.6786710939498055
    "JETBLUE AIRWAYS" 658 -1.8734666247067702 -1.8100311501651383    .2426269691686575   -.19568526172467035  -.5830696282936368      .305543890196478 0  1.8580393179908445
    "JETBLUE AIRWAYS" 659 -1.7461645514070612 -1.8512383210197532    1.217992661720144   -.21535605636043811  -.7959341990641041     .2900867371940641 0   2.116588445820907
    "JETBLUE AIRWAYS" 660  -1.692094299715763  -1.908987603343651   .20408207441227527    -.2407048915345397  -.4638913615448051    .13906911628290866 0   2.290282442680227
    "JETBLUE AIRWAYS" 661 -1.6028292798451296 -1.8294354869631928   .11371528200956979    -.3462294065508215  -.5301924329690795    .15008203037380277 0  2.0473785439661367
    "JETBLUE AIRWAYS" 662 -1.5894642158894488  -1.817253754292456    .4632291367853355    -.4305278652742395  -.5310748548930454    .07142088346766136 0  2.0931007632563756
    "JETBLUE AIRWAYS" 663  -1.502412234256875  -1.869958475944973    .5510200145917438   -.39478613732698586  -.5735449758828703    .06762865927749417 0  1.9865583234814765
    "JETBLUE AIRWAYS" 664 -1.4381325587041185  -1.872899546189993     .290459580093873    -.2637125252213425  -.6050211260507602    -.0791073861886854 0  1.9923069106552418
    "JETBLUE AIRWAYS" 665 -1.4346107054749733  -1.853383997628375    .4626815973038487    -.3347989257777495  -.7415560149350192   -.24551711474135118 0  2.1426702014101564
    "JETBLUE AIRWAYS" 666 -1.3837307184634124 -1.8603434268013155    .7842136954824868    -.3557117562921646  -.8508032516613332    -.1728824671510134 0  1.9277463540377373
    "JETBLUE AIRWAYS" 667 -1.3095279184574133 -1.8810853784564083    .6522721892414917   -.49767411971986547  -.7885927941452933    -.1386465455571269 0   1.982904380926538
    "JETBLUE AIRWAYS" 668  -1.297060717502879  -1.871271052629726   1.2607102400567876    -.5604188200170354  -.7793884205409202   -.13010930791954625 0   1.758176279994274
    "JETBLUE AIRWAYS" 669 -1.1857513056668958  -1.847046860910674    .9887769258002137   -.33929683816169876  -.7835090531290422   .050015702797166856 0   1.759595469622606
    "JETBLUE AIRWAYS" 670 -1.2374135289405173  -1.806675763042542    .8442581624745737    -.3294514167560277   -.696769393160383   -.05034272028810181 0  1.9177682264675857
    "JETBLUE AIRWAYS" 671  -1.222139244006631 -1.8813265586754884    1.591537244372993   -.15344172760858385 -.41968700810903276     .0862684369257555 0  1.8919047731041647
    "JETBLUE AIRWAYS" 672 -1.2481252096525723 -1.9619519415807867   1.6631747723192434   -.13854266428408865  -.2467082440978814    -.0664816047923369 0  1.7471333203153496
    "JETBLUE AIRWAYS" 673   -1.25705129270388  -1.963839454410656    2.183010014591035   -.23933375565368648 -.23337260176994834   -.03910357445320117 0  1.4191474589565207
    "JETBLUE AIRWAYS" 674 -1.2494336578892438  -2.004047520466713    2.344961476976875    -.2083672532479426 -.13959484455152432   -.04125995615692998 0  1.4371343566057324
    "JETBLUE AIRWAYS" 675 -1.3292223763674835 -2.0119035102762193   2.7841695903411563    -.2133127189404055  -.2525793127197275   -.09543774347431311 0  1.5020704372325804
    "JETBLUE AIRWAYS" 676  -1.357535078661709 -2.0497516666491196    3.480456785840916    -.3318323076231331 -.05127774626055727   -.11311312215010828 0  1.4249541070904717
    "JETBLUE AIRWAYS" 677 -1.4235611631074985  -2.201618343920832    4.227544657318321    -.3398356862642233   .1065961578154545   -.15874074006626535 0  1.4263592684667348
    "JETBLUE AIRWAYS" 678 -1.4727884698765796 -2.1825070176310244     4.21535042602119   -.32169456752576925   .1277570797801772   -.12394074050485275 0  1.4812872940742856
    "JETBLUE AIRWAYS" 679 -1.4219814780262425 -2.1176223293386385    4.631131961457205    -.2626086622384509 .025476944155852183   -.17139773258202934 0    1.42298753854275
    "JETBLUE AIRWAYS" 680 -1.4991990586928854 -2.1230880184103302    4.590675197821155    -.2780658037155568  .14169773586447532   -.10705273608177726 0   1.547996435072859
    "JETBLUE AIRWAYS" 681  -1.459294981027219 -2.0983917444659266    5.189699666284187   -.27364692741971064  .17003316433984927   -.03674752851021042 0   1.453059258286882
    "JETBLUE AIRWAYS" 682 -1.4498797668533787  -1.989732268987346    4.747501752585942    -.3497173048793914  .12605542239321968   -.01966143223115133 0  1.3256424725958094
    "JETBLUE AIRWAYS" 683 -1.3902569074269073 -2.0057577749140085   3.8415939639770946   -.33600703487677414   .1695058296638096    .05881047399930028 0  1.3825073167976898
    "JETBLUE AIRWAYS" 684  -1.322118232951108  -1.959022411519761    3.624189086826994   -.28618917018963014   .1695551883605059    .10479023544742615 0  1.4116943076178747
    "JETBLUE AIRWAYS" 685  -1.396220645894086 -1.9567223380334045   3.5641777773442094    -.2778948819508057  .19372954771215292    .11950262133484794 0   1.440820766522946
    "JETBLUE AIRWAYS" 686  -1.402520955538576   -1.96953622537209    3.922943126913067    -.2097371279756402  .17683181444387097     .1484652336983575 0  1.4666120879115756
    "JETBLUE AIRWAYS" 687 -1.3604687369468886      -1.94604014298   3.5810931323301567   -.18294187851077567  .06594503284838997      .157001879621086 0   1.330908500065616
    "JETBLUE AIRWAYS" 688  -1.319997263135289 -1.9576414629312204    4.060685405064835   -.13779516046777654   .2704736910257452    .21183077741844625 0  1.3054366135685358
    "JETBLUE AIRWAYS" 689 -1.2924569194734041 -1.9122486980719648   2.6019674119778933   -.09711875123087865  .09175957756789314    .22921610263177578 0  1.2399792997863297
    "JETBLUE AIRWAYS" 690 -1.3069321768783775 -1.9574346890828307   1.3956800209247344   -.11124536140824151  .22908799260152718    .22231107376262743 0  1.1987920359049031
    "JETBLUE AIRWAYS" 691 -1.3266485926740674 -1.9861920700965066   1.8143747534014256   -.09153782709876308  .24236888868905038     .1788774198632877 0  1.3238250700066507
    "JETBLUE AIRWAYS" 692 -1.4033396930449857 -1.9584059562482576    .6867533055484274   -.16236667788236372  .23724800963099923     .0878773221439432 0  1.2025845429581148
    "JETBLUE AIRWAYS" 693 -1.4696288994795015 -1.9675881730680882   1.9168138240196126   .049001451634333545   .1968145358553358   .009561823629454413 0  1.2861002113788877
    "JETBLUE AIRWAYS" 694 -1.4608459697658476 -1.9314128179586356   1.9890551338782092    .07006572347107917  .23609188152602176    .08747432879810034 0  1.1939271272083103
    "JETBLUE AIRWAYS" 695   -1.41398000794931  -1.849392019834293    1.533027074104009    .12857185548787506  .06425068804878765   -.05429653827299268 0  1.1935234417137695
    "JETBLUE AIRWAYS" 696 -1.3845351416791354 -1.8222556414336153    1.550916343665786     .1926999528234059 -.09706666944893436     -.128644805130466 0  1.1937384980071082
    "JETBLUE AIRWAYS" 697 -1.4403580823922357 -1.8213327682781757     .061691130331108    .12924227501156713 -.05438472508749682    -.1184746118258624 0    1.39217144439771
    "JETBLUE AIRWAYS" 698 -1.3983125575496569 -1.8600349461553258  -.07602823348285048    .07598602415707684 .028185520587709574    -.1408917533150811 0  1.1246199731374733
    "JETBLUE AIRWAYS" 699 -1.4462318832439784 -1.8764180924066283   -.5100018484801856    .04827935754069952 .022522384968607777   -.11014895224027146 0  1.1976240152003148
    "JETBLUE AIRWAYS" 700 -1.4845166918958868 -1.8479358058574682   -.5926658768989383   .007320485407972707   .0103733288824484   -.05826941581894475 0  1.1947879161219679
    "JETBLUE AIRWAYS" 701 -1.5021753535811029 -1.9419077651887453  -.41818584508419177  -.015486205054111675   .2953959676741235    .08437497469163702 0  1.2074444179652768
    "JETBLUE AIRWAYS" 702 -1.4941390021617675 -1.9080564578549488   -.6619739308617701   .045078937437599546   .2774267697019265    .06791036421380109 0  1.3829369024091536
    "JETBLUE AIRWAYS" 703   -1.55816195669197  -1.855829341350433  -1.1616632331050794    .14889986869875438  .20412988751902583   .037560910598434735 0   1.433891787479454
    "JETBLUE AIRWAYS" 704 -1.5219018007469973 -1.8643560214743238   -1.594995551307098     .1699076026481305   .1978521715904848    .02738490907248949 0  1.7229575362100982
    "JETBLUE AIRWAYS" 705 -1.5120691385628433 -1.9259632607061428   -2.198007159126804    .13188295937244132  .35944292922900456   -.09382291749754358 0   1.722175404048602
    "JETBLUE AIRWAYS" 706 -1.5682608979799764  -1.926632896936461  -1.8638461360272345   .048830402792163136  .40767458459099637 -.0001143745855458626 0  1.3110221049311839
    "JETBLUE AIRWAYS" 707 -1.5116511042014564 -1.9889598094806196  -1.7259706636585337    -.8909783289412979   .4213275369004427    -.7710631663158782 1  1.4759140268763835
    "JETBLUE AIRWAYS" 708 -1.5567477143333293 -1.9814266375528284   -1.387162786495836  -.027662298985861696   .3659707141624182   -.07862129908399942 0   .7608203564024189
    "JETBLUE AIRWAYS" 709 -1.5398110620101853  -2.001655857271038  -1.2923183178692352 -.0031592206010508943  .22722470555671467    .12873914655122895 0  1.2069979283450059
    "JETBLUE AIRWAYS" 710 -1.6194647472886576 -1.9610020554491814  -1.1094500452606715    .17807936584891618  .26550846933740413    .08676581709554265 0   1.498527751848327
    "JETBLUE AIRWAYS" 711 -1.6470375328647275 -1.9538426612469215   -1.004774216215306    .24857361012872486  .03412064951785949     .0767210543960463 0  1.4324886902948895
    "JETBLUE AIRWAYS" 712 -1.5810820951147067  -1.946242714668633  -1.0731286058429657    .34108835545803856  .03635476557533664     .0962384384273429 0   1.263467801209109
    "JETBLUE AIRWAYS" 713 -1.5617632465481632 -1.9469389957448986   -.7822480558681837     .2867848525512256  .03232698693559846    .13514392811620166 0  1.0119751290615115
    "JETBLUE AIRWAYS" 714 -1.5698706181755224 -1.9420050666498048   -.5243303474120904    .31291520535218864 -.20557452230064133    .15920143732662864 0  1.2371467346333476
    "JETBLUE AIRWAYS" 715 -1.5595283891147056 -1.9006776147940263  -1.0202053078519882       .41806081171742  -.1930815205692955     .1099229371745441 0   1.233145162364478
    "JETBLUE AIRWAYS" 716 -1.5846928225314447   -1.97280010989225   -.5568678205838594    .21666116924643736 -.04300997963870179    .13813675281402457 0  1.0569243981003704
    "JETBLUE AIRWAYS" 717  -1.657392728913543 -1.9419222342850389   -.7445227753239338    .27030405485046066 -.08943737555179268    .13693414380619232 0  1.1957166106768116
    "JETBLUE AIRWAYS" 718 -1.5587659145191604 -1.9237117710266296   -1.116351224479294     .2912440565888479 -.09254763256055343      .087682139526089 0  1.1923008466537928
    "JETBLUE AIRWAYS" 719  -1.561663761699428 -1.9373688073786115  -1.1810026441487442      .289736360226523   .1742948071460107    .09255905021599896 0  1.2359567147579076
    "JETBLUE AIRWAYS" 720 -1.5968662183470914 -1.8429929281659625  -1.5753536637468164    .30248978673891525  .15068564152357172 -.0007846378235321622 0  1.1512098959829702
    "JETBLUE AIRWAYS" 721 -1.5451900274062849 -1.8173779987325835   -1.142597832968991    .30160991203273346  .15077414010443677    .07463246232835952 0   1.070451579922707
    "JETBLUE AIRWAYS" 722 -1.6032595645739733 -1.4653024908754302  -1.3879868169926164   -1.0050490769240181  .39218638525544486    -.6451287804629928 1   .6417595491275315
    "JETBLUE AIRWAYS" 723 -1.7775177389789087 -1.5694282512723101  -1.6409309491110946    -.9331713959894647   .5707161364241239    -.6502110052431429 1  1.1333392153210944
    "JETBLUE AIRWAYS" 724 -1.7686958944433848 -1.4423549186589482  -1.5844855812878076    -.9272944980694869  .43631036990620764    -.5016372520061669 1  1.5400427253172186
    "JETBLUE AIRWAYS" 725  -1.766388461164701  -1.452353851970067   -1.548652561338926   -1.0056204186623565   .2530694988293223   -.46432116098724996 1  1.6744076644938812
    "JETBLUE AIRWAYS" 726 -1.7435162839119513 -1.3909598812627135  -1.5940293149780636   -1.0000248246823393   .2880828005366245    -.3659202687630539 1  1.6620585732231892
    "JETBLUE AIRWAYS" 727 -1.7898376334109156 -1.3451342182576946   -1.470324581563863     .5271992034678995  .16363851689386766    .06225267748471568 0  1.7301947948971343
    "JETBLUE AIRWAYS" 728 -1.7617118503638505 -1.2961110481102112   -1.323203333089691    -.9758479969485518 -.13773473241099948    -.5152350599253908 1  -.0651646111123104
    "JETBLUE AIRWAYS" 729 -1.7559000989249036 -1.2646077365663073  -1.2338579872684814    -1.419702497641784 -.09128677208665681   -.48798444866308377 1  1.9437710219473865
    "JETBLUE AIRWAYS" 730  -1.770284124854819 -1.2965867028555096   -.8260260837721991    .49720032672231473  .15675241072955048   .041097811094588356 0  1.9353438907392708
    "JETBLUE AIRWAYS" 731  -1.706609261470894 -1.3151127557941884  -1.1055407399058685    .48941266577006426  .17804434602521038    .04147415177070184 0 -.03937037049866469
    "JETBLUE AIRWAYS" 732 -1.6551115577415698 -1.3431887559333195  -1.3106217350560523    -1.393671541682524 -.21022237989480286    -.8170797365948982 1 .031276087771243924
    "JETBLUE AIRWAYS" 733 -1.6601665551600997 -1.3985731329612159   -.7432081223715259                     .                   .                     . 0  2.3978724800699753
    end
    format %tm date

    Code:
    global M7 LogSize DY MOM36
    local t 7
    local m`t' ${M`t'}
    
        foreach var in `m`t'' {
            replace F1_ret_out = F1_ret_out + (L1.r_`var')*`var' if (crisis==0 & L1.crisis==0)
            replace F1_ret_out = F1_ret_out + (L2.r_`var')*`var' if (crisis==0 & L1.crisis==1 & L2.crisis==0)
            replace F1_ret_out = F1_ret_out + (L3.r_`var')*`var' if (crisis==0 & L1.crisis==1 & L2.crisis==1 & L3.crisis==0)
            replace F1_ret_out = F1_ret_out + (L4.r_`var')*`var' if (crisis==0 & L1.crisis==1 & L2.crisis==1 & L3.crisis==1 & L4.crisis==0)
            replace F1_ret_out = F1_ret_out + (L5.r_`var')*`var' if (crisis==0 & L1.crisis==1 & L2.crisis==1 & L3.crisis==1 & L4.crisis==1 & L5.crisis==0)
            replace F1_ret_out = F1_ret_out + (L6.r_`var')*`var' if (crisis==0 & L1.crisis==1 & L2.crisis==1 & L3.crisis==1 & L4.crisis==1 & L5.crisis==1 & L6.crisis==0)
            replace F1_ret_out = F1_ret_out + (L7.r_`var')*`var' if (crisis==0 & L1.crisis==1 & L2.crisis==1 & L3.crisis==1 & L4.crisis==1 & L5.crisis==1 & L6.crisis==1 & L7.crisis==0)
            replace F1_ret_out = F1_ret_out + (L8.r_`var')*`var' if (crisis==0 & L1.crisis==1 & L2.crisis==1 & L3.crisis==1 & L4.crisis==1 & L5.crisis==1 & L6.crisis==1 & L7.crisis==1 & L8.crisis==0)
            replace F1_ret_out = F1_ret_out + (L9.r_`var')*`var' if (crisis==0 & L1.crisis==1 & L2.crisis==1 & L3.crisis==1 & L4.crisis==1 & L5.crisis==1 & L6.crisis==1 & L7.crisis==1 & L8.crisis==1 & L9.crisis==0)
            replace F1_ret_out = F1_ret_out + (L10.r_`var')*`var' if (crisis==0 & L1.crisis==1 & L2.crisis==1 & L3.crisis==1 & L4.crisis==1 & L5.crisis==1 & L6.crisis==1 & L7.crisis==1 & L8.crisis==1 & L9.crisis==1 & L10.crisis==0)
            replace F1_ret_out = F1_ret_out + (L11.r_`var')*`var' if (crisis==0 & L1.crisis==1 & L2.crisis==1 & L3.crisis==1 & L4.crisis==1 & L5.crisis==1 & L6.crisis==1 & L7.crisis==1 & L8.crisis==1 & L9.crisis==1 & L10.crisis==1 & L11.crisis==0)
            replace F1_ret_out = F1_ret_out + (L12.r_`var')*`var' if (crisis==0 & L1.crisis==1 & L2.crisis==1 & L3.crisis==1 & L4.crisis==1 & L5.crisis==1 & L6.crisis==1 & L7.crisis==1 & L8.crisis==1 & L9.crisis==1 & L10.crisis==1 & L11.crisis==1 & L12.crisis==0)
        }

    Does anyone know how to program that more efficiently? Thanks.


  • Clyde Schechter
    replied
    The code I marked up in #13 doesn't produce output--because it's riddled with errors. I included some comments to indicate what those errors are so that you can make corrections.

    Leave a comment:


  • Francois Durant
    replied
    are the 2 codes producing the same output? Any mistake you found in mine? Thanks Clyde.

    Leave a comment:


  • Clyde Schechter
    replied
    That's a hot mess. See my annotations as end of line comments in all caps:

    Code:
    preserve
        tempfile working
        keep Name date crisis
        save `working'
    restore
    
    *global M7 LogSize DY MOM36 // WHY ARE THESE COMMENTED OUT?  IT SEEMS YOU NEED TO USE THEM LATER
    *global M_crisis_7 LogSize DY MOM12
    
    //  FIND THE DATE OF THE LATEST PRIOR OBSERVATION IN SAME CRISIS STATE
    rangejoin date . -1 using `working', by(Name)
    replace date_U = . if crisis != crisis_U
    gen byte has_date_U = !missing(date_U)
    by Name date (has_date_U date_U), sort: keep if _n == _N
    
    //  NOW GET CORRESPONDING LAGGED COEFFICIENTS
    rangestat (min) b_cons = r_cons , interval(date date_U date_U) by(Name)
    
    local union : list global(M`t') | global(M_crisis_`t') // LOCAL MACRO t IS UNDEFINED, HENCE SO ARE GLOBAL MACROS M`t' AND M_crisis_`t'
    foreach v of varlist `union' { // ONCE YOU FIX THE COMMAND CREATING LOCAL MACRO union THIS WILL RUN AND GIVE CORRECT RESULTS, BUT IT IS SOMEWHAT INEFFICIENT TO KEEP CALLING rangestat WHEN, THE APPROACH IN #11
                                                // ONLY NEEDS TO CALL IT ONCE.  THE DIFFERENCE IN SPEED WILL BE VERY SMALL, HOWEVER, PROBABLY NOT NOTICEABLE.
        rangestat (min) b_`v' = r_`v', interval(date date_U date_U) by(Name)
    }
                    
    //  NOW LOOP OVER VARIABLES AND INCREMENT F1_ret_out
    gen F1_ret_out = b_cons
    *local t 7
    local m ${M`t'} // AGAIN, SINCE LOCAL MACRO t IS UNDEFINED, LOCAL MACRO M WILL CONTAIN THE CONTENTS OF GLOBAL MACRO M, WHICH IS ALSO UNDEFINED. 
    foreach v of varlist `m' { // HENCE THIS LOOP WILL BE SKIPPED
        replace F1_ret_out = F1_ret_out + b_`v'*`v' if crisis == 0
    }
    
    local m_crisis ${M_crisis_`t'}  // AGAIN, WITH LOCAL MACRO t UNDEFINED, LOCAL MACRO m_crisis WILL CONTAIN NOTHING
    foreach v of varlist `m_crisis' { // SO THIS LOOP, ALSO WILL BE SKIPPED
        replace F1_ret_out = F1_ret_out + b_`v'*`v' if crisis == 1
    }

    Leave a comment:


  • Francois Durant
    replied
    is there anything wrong programming it the following way instead?

    Code:
    preserve
        tempfile working
        keep Name date crisis
        save `working'
    restore
    
    *global M7 LogSize DY MOM36 
    *global M_crisis_7 LogSize DY MOM12 
    
    //  FIND THE DATE OF THE LATEST PRIOR OBSERVATION IN SAME CRISIS STATE
    rangejoin date . -1 using `working', by(Name)
    replace date_U = . if crisis != crisis_U
    gen byte has_date_U = !missing(date_U)
    by Name date (has_date_U date_U), sort: keep if _n == _N
    
    //  NOW GET CORRESPONDING LAGGED COEFFICIENTS
    rangestat (min) b_cons = r_cons , interval(date date_U date_U) by(Name)
    
    local union : list global(M`t') | global(M_crisis_`t')
    foreach v of varlist `union' {
        rangestat (min) b_`v' = r_`v', interval(date date_U date_U) by(Name)
    }
                    
    //  NOW LOOP OVER VARIABLES AND INCREMENT F1_ret_out
    gen F1_ret_out = b_cons
    *local t 7
    local m ${M`t'}
    foreach v of varlist `m' {
        replace F1_ret_out = F1_ret_out + b_`v'*`v' if crisis == 0
    }
    
    local m_crisis ${M_crisis_`t'}
    foreach v of varlist `m_crisis' {
        replace F1_ret_out = F1_ret_out + b_`v'*`v' if crisis == 1
    }
    Thanks a lot Clyde, really appreciated

    Leave a comment:


  • Clyde Schechter
    replied
    Code:
    local conversions b_cons = r_cons
    global all_vars: list M1 | M_crisis_1
    
    foreach v of varlist $all_vars {
        local conversions `conversions' b_`v' = r_`v'
    }
    rangestat (min) `conversions', interval(date date_U date_U) by(Name)

    Leave a comment:


  • Francois Durant
    replied
    how do I reprogram that part though:

    Code:
    //  NOW GET CORRESPONDING LAGGED COEFFICIENTS
    rangestat (min) b_cons = r_cons ///
                    b_LogSize = r_LogSize ///
                    b_DY = r_DY ///
                    b_MOM36 = r_MOM36, interval(date date_U date_U) by(Name)
    By the way:
    - no crisis -> crisis=0 and $M1
    - crisis -> crisis=1 and $M_crisis_1

    thanks a lot for all this help Clyde!

    Leave a comment:


  • Clyde Schechter
    replied
    You don't state which of those macros corresponds to crisis == 1 and which to crisis == 0, and it isn't obvious to me from the names, since both look like they are oriented towards something being 1. In the code below, I assume that $M1 contains the variables for crisis == 0, and $M_crisis_1 for crisis == 1. Only the code starting at // NOW LOOP OVER VARIABLES AND INCREMENT F1_ret_out is affected:
    Code:
    //  NOW LOOP OVER VARIABLES AND INCREMENT F1_ret_out
    replace F1_ret_out =F1_ret_out + b_cons
    
    foreach v of varlist $M1 {
        replace F1_ret_out = F1_ret_out + b_`v'*`v' if crisis == 0
    }
    
    foreach v of varlist $M_crisis_1 {
        replace F1_ret_out = F1_retout + b_`v'*`v' if crisis == 1
    }
    Also, eliminate the -replace b_LogSize = . if crisis == 1- from the code shown in #7.

    Leave a comment:


  • Francois Durant
    replied
    In the real code, the 2 lists of factors are widely different between the crisis and no crisis regime. They may or may not contain common factors (variables) in their respective varlist. They each are defined by a global variable with includes between 20-30 factors. Hence, I cannot do the following:
    Code:
     
     replace b_LogSize = . if crisis == 1
    Is there any way to amend the previous code with the 2 following macro variables:
    Code:
    * No crisis varlist
    $M1
    $M_crisis_1
    Thanks a lot in advance

    Leave a comment:


  • Clyde Schechter
    replied
    Code:
    preserve
    tempfile working
    keep Name date crisis
    save `working'
    restore
    
    //  FIND THE DATE OF THE LATEST PRIOR OBSERVATION IN SAME CRISIS STATE
    rangejoin date . -1 using `working', by(Name)
    replace date_U = . if crisis != crisis_U
    gen byte has_date_U = !missing(date_U)
    by Name date (has_date_U date_U), sort: keep if _n == _N
    
    //  NOW GET CORRESPONDING LAGGED COEFFICIENTS
    rangestat (min) b_cons = r_cons ///
                    b_LogSize = r_LogSize ///
                    b_DY = r_DY ///
                    b_MOM36 = r_MOM36, interval(date date_U date_U) by(Name)
    replace b_LogSize = . if crisis == 1
    
    //  NOW LOOP OVER VARIABLES AND INCREMENT F1_ret_out
    replace F1_ret_out =F1_ret_out + b_cons
    foreach v of varlist LogSize DY MOM36 {
        replace F1_ret_out = F1_ret_out + b_`v'*`v' if !missing(b_`v')
    }
    should do it.

    Notes: The example data you showed does not contain the constants from the regressions. The code above assumes that you do have such a variable, and that it is called r_cons.

    As for the meaning of the first block of code, it is exactly what the comment preceding it says: for each observation, it finds the most recent preceding date among the observations of the same firm (Name). It does this by pairing each observation with all observations of the same firm whose date precedes the date of the current observation. The value of date from the paired observations appears in a new variable, date_U. It then removes all observations where crisis and crisis_U (the value of crisis in the paired observation) don't match, unless there were no paired observations, in which case the original observation is retained but has date_U reset to missing. (This "unless" clause prevents any of the original observations from being deleted altogether.)

    Leave a comment:


  • Francois Durant
    replied
    In addition, I use different factors in crisis time relative to normal times. I thought I could integrate it easily afterward, but I'm not too sure now. Assume that the list of predictor in now "DY MOM36" for simplicity if crisis periods (and remains "LogSize DY MOM36" in normal times crisis==0). Could you help me integrate all that? Thank you so much again!

    Leave a comment:


  • Francois Durant
    replied
    Hi Clyde,

    I'm not sure if I understand what the first block does. Could you give me some details step by step?
    Code:
     
     //  FIND THE DATE OF THE LATEST PRIOR OBSERVATION IN SAME CRISIS STATE rangejoin date . -1 using `working', by(Name) replace date_U = . if crisis != crisis_U gen byte has_date_U = !missing(date_U) by Name date (has_date_U date_U), sort: keep if _n == _N
    Also, I realize that I originally defined F1_ret_out as the intercept of the regression:
    Code:
    gen F1_ret_out = L1.r_cons
    I would like the intercept to follow the same logic as well. Could you help me integrate it? Thanks.



    Leave a comment:


  • Clyde Schechter
    replied
    You are welcome, but I realized that the code is not quite correct nonetheless. The removal I alluded to in #2 can result in taking out an observation that is itself the correct lag for a later one. (This does not, I think, happen in your example, but, in principle, it could.) The following code corrects this error:

    Code:
    preserve
    tempfile working
    keep Name date crisis
    save `working'
    restore
    
    //  FIND THE DATE OF THE LATEST PRIOR OBSERVATION IN SAME CRISIS STATE
    rangejoin date . -1 using `working', by(Name)
    replace date_U = . if crisis != crisis_U
    gen byte has_date_U = !missing(date_U)
    by Name date (has_date_U date_U), sort: keep if _n == _N
    
    //  NOW GET CORRESPONDING LAGGED COEFFICIENTS
    rangestat (min) b_LogSize = r_LogSize ///
                    b_DY = r_DY ///
                    b_MOM36 = r_MOM36, interval(date date_U date_U) by(Name)
    
    //  NOW LOOP OVER VARIABLES AND INCREMENT F1_ret_out
    foreach v of varlist LogSize DY MOM36 {
        replace F1_ret_out = F1_ret_out + b_`v'*`v' if !missing(b_`v')
    }
    This doe leaves F1_ret_out unchanged in the event that the observation has no earlier observation that matches it on crisis, and does not delete the observation.

    Leave a comment:


  • Francois Durant
    replied
    Crisis are defined at the macro level so there are the same for each stock. Thanks a lot Clyde, it really helps!

    Leave a comment:


  • Clyde Schechter
    replied
    I think this will do it:

    Code:
    preserve
    tempfile working
    keep Name date crisis
    save `working'
    restore
    
    //  FIND THE DATE OF THE LATEST PRIOR OBSERVATION IN SAME CRISIS STATE
    rangejoin date . -1 using `working', by(Name)
    by Name date (date_U), sort: keep if crisis == crisis_U
    by Name date (date_U), sort: keep if _n == _N
    
    //  NOW GET CORRESPONDING LAGGED COEFFICIENTS
    rangestat (min) b_LogSize = r_LogSize ///
                    b_DY = r_DY ///
                    b_MOM36 = r_MOM36, interval(date date_U date_U) by(Name)
    
    //  NOW LOOP OVER VARIABLES AND INCREMENT F1_ret_out
    foreach v of varlist LogSize DY MOM36 {
        replace F1_ret_out = F1_ret_out + b_`v'*`v'
    }
    -rangestat- is by Robert Picard, Nick Cox, and Roberto Ferrer. -rangejoin- is by Robert Picard. Both are available from SSC. (Note, -rangejoin- requires -rangestat-)

    Edit: There is one gap in the pose of your question. You don't say what you want to do if for some observation, no matter how far back you go, you never find a match on crisis status. The code above drops such observations.
    Last edited by Clyde Schechter; 03 Mar 2021, 20:19.

    Leave a comment:

Working...
X