Announcement

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

  • In need of urgent help: Storing coefficients of looped multiple regression

    Hello,

    I am having issues to estimate and store regression coefficients in a loop. To make myself clear I will describe the step that I want my command in stata to achieve.

    First, I generated a time variable. Next, I reshaped my data from wide to long format 25 stocks with weekly returns in one year.
    I am trying to run a loop 25 multiple regressions for Fama/French and store the constants, t-statistic, and p-value. The loop for regressions works. However,
    when I run the loop for replacing the variables Stata only stores the output of the last regression for all generated variables, instead of for all 25 regressions.
    I think I am missing a step between running the regression and replacing the variables.

    Will you please advise me.

    Thank you in advance!

    This is my dataset in long format:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float time byte stock double(MktRF SMB HML RMW CMA P) float(alpha tstat pvalue)
    1  1 -1.11 -.52 -.03  .23  .05    -.06737918215613387 0 0 0
    1  2 -1.11 -.52 -.03  .23  .05   -.050800609031739934 0 0 0
    1  3 -1.11 -.52 -.03  .23  .05   -.051464549685190326 0 0 0
    1  4 -1.11 -.52 -.03  .23  .05     -.0663709805163947 0 0 0
    1  5 -1.11 -.52 -.03  .23  .05    -.10097517117366342 0 0 0
    1  6 -1.11 -.52 -.03  .23  .05     -.2543554006968641 0 0 0
    1  7 -1.11 -.52 -.03  .23  .05    -.08746087276744614 0 0 0
    1  8 -1.11 -.52 -.03  .23  .05    -.04082163286531462 0 0 0
    1  9 -1.11 -.52 -.03  .23  .05    -.06216782090513769 0 0 0
    1 10 -1.11 -.52 -.03  .23  .05   -.027121697357886245 0 0 0
    1 11 -1.11 -.52 -.03  .23  .05   -.033752327746741057 0 0 0
    1 12 -1.11 -.52 -.03  .23  .05    -.08830022075055183 0 0 0
    1 13 -1.11 -.52 -.03  .23  .05    -.04056097896328891 0 0 0
    1 14 -1.11 -.52 -.03  .23  .05     .03654159869494293 0 0 0
    1 15 -1.11 -.52 -.03  .23  .05    -.10859903381642508 0 0 0
    1 16 -1.11 -.52 -.03  .23  .05    -.08118956394818469 0 0 0
    1 17 -1.11 -.52 -.03  .23  .05    -.09783722599327987 0 0 0
    1 18 -1.11 -.52 -.03  .23  .05    -.13865932047750232 0 0 0
    1 19 -1.11 -.52 -.03  .23  .05   -.035722762876809935 0 0 0
    1 20 -1.11 -.52 -.03  .23  .05    -.08667736757624395 0 0 0
    1 21 -1.11 -.52 -.03  .23  .05   -.019111860595840417 0 0 0
    1 22 -1.11 -.52 -.03  .23  .05   -.051669147352753554 0 0 0
    1 23 -1.11 -.52 -.03  .23  .05    -.08586083853702055 0 0 0
    1 24 -1.11 -.52 -.03  .23  .05   -.025283347863993156 0 0 0
    1 25 -1.11 -.52 -.03  .23  .05    -.05677721701514056 0 0 0
    2  1 -2.14   .4 -.21  .31  .33   -.012812299807815583 0 0 0
    2  2 -2.14   .4 -.21  .31  .33    .000662779301543388 0 0 0
    2  3 -2.14   .4 -.21  .31  .33   -.015295815295815125 0 0 0
    2  4 -2.14   .4 -.21  .31  .33    -.06435923538061318 0 0 0
    2  5 -2.14   .4 -.21  .31  .33    -.03354104161858604 0 0 0
    2  6 -2.14   .4 -.21  .31  .33    -.05140186915887844 0 0 0
    2  7 -2.14   .4 -.21  .31  .33   -.046811945117029866 0 0 0
    2  8 -2.14   .4 -.21  .31  .33   .0018975332068311272 0 0 0
    2  9 -2.14   .4 -.21  .31  .33  -.0017173278378842763 0 0 0
    2 10 -2.14   .4 -.21  .31  .33    -.03374138463121079 0 0 0
    2 11 -2.14   .4 -.21  .31  .33 -.00024090580583004372 0 0 0
    2 12 -2.14   .4 -.21  .31  .33    -.01493139628732853 0 0 0
    2 13 -2.14   .4 -.21  .31  .33   -.011607910576096335 0 0 0
    2 14 -2.14   .4 -.21  .31  .33    -.02533836953100419 0 0 0
    2 15 -2.14   .4 -.21  .31  .33      -.079340992846304 0 0 0
    2 16 -2.14   .4 -.21  .31  .33    -.04626687847498011 0 0 0
    2 17 -2.14   .4 -.21  .31  .33   -.051092983768508314 0 0 0
    2 18 -2.14   .4 -.21  .31  .33    -.02096659559346126 0 0 0
    2 19 -2.14   .4 -.21  .31  .33 -.00012307692307681113 0 0 0
    2 20 -2.14   .4 -.21  .31  .33     .00140597539543055 0 0 0
    2 21 -2.14   .4 -.21  .31  .33   -.015854823304680004 0 0 0
    2 22 -2.14   .4 -.21  .31  .33    -.04573991031390132 0 0 0
    2 23 -2.14   .4 -.21  .31  .33   -.016589412051719925 0 0 0
    2 24 -2.14   .4 -.21  .31  .33    .013416815742397222 0 0 0
    2 25 -2.14   .4 -.21  .31  .33    -.02560672654309198 0 0 0
    3  1  2.08  .14 -.19 -.38 -.76    .005984569904102765 0 0 0
    3  2  2.08  .14 -.19 -.38 -.76    .009903396960280794 0 0 0
    3  3  2.08  .14 -.19 -.38 -.76     .01992966002344665 0 0 0
    3  4  2.08  .14 -.19 -.38 -.76    .009368955512572602 0 0 0
    3  5  2.08  .14 -.19 -.38 -.76   -.008119079837618485 0 0 0
    3  6  2.08  .14 -.19 -.38 -.76   -.004926108374384349 0 0 0
    3  7  2.08  .14 -.19 -.38 -.76   -.010584250635055038 0 0 0
    3  8  2.08  .14 -.19 -.38 -.76    .027056277056277056 0 0 0
    3  9  2.08  .14 -.19 -.38 -.76    .006881128505074808 0 0 0
    3 10  2.08  .14 -.19 -.38 -.76    .020440753752794652 0 0 0
    3 11  2.08  .14 -.19 -.38 -.76    .013493975903614513 0 0 0
    3 12  2.08  .14 -.19 -.38 -.76   .0040966816878329135 0 0 0
    3 13  2.08  .14 -.19 -.38 -.76  -.0030448020878641982 0 0 0
    3 14  2.08  .14 -.19 -.38 -.76    .012271919909575409 0 0 0
    3 15  2.08  .14 -.19 -.38 -.76    -.03319990581586995 0 0 0
    3 16  2.08  .14 -.19 -.38 -.76     .04726212783676875 0 0 0
    3 17  2.08  .14 -.19 -.38 -.76  -.0032951265495437133 0 0 0
    3 18  2.08  .14 -.19 -.38 -.76    .034119782214156125 0 0 0
    3 19  2.08  .14 -.19 -.38 -.76    .015263417035942821 0 0 0
    3 20  2.08  .14 -.19 -.38 -.76   -.008775008775008775 0 0 0
    3 21  2.08  .14 -.19 -.38 -.76      .0283385093167702 0 0 0
    3 22  2.08  .14 -.19 -.38 -.76    .055451127819548855 0 0 0
    3 23  2.08  .14 -.19 -.38 -.76     .03200198461920117 0 0 0
    3 24  2.08  .14 -.19 -.38 -.76     .03383348043542214 0 0 0
    3 25  2.08  .14 -.19 -.38 -.76     .02549519513630118 0 0 0
    4  1  2.57  .59  .37  .18  .47     .08228211009174304 0 0 0
    4  2  2.57  .59  .37  .18  .47     .12422336413712734 0 0 0
    4  3  2.57  .59  .37  .18  .47     .04339080459770109 0 0 0
    4  4  2.57  .59  .37  .18  .47   -.003473261871968456 0 0 0
    4  5  2.57  .59  .37  .18  .47    -.03595217077281109 0 0 0
    4  6  2.57  .59  .37  .18  .47     .08910891089108919 0 0 0
    4  7  2.57  .59  .37  .18  .47    .017971758664955144 0 0 0
    4  8  2.57  .59  .37  .18  .47                      0 0 0 0
    4  9  2.57  .59  .37  .18  .47       .035366478728857 0 0 0
    4 10  2.57  .59  .37  .18  .47     .03599374021909236 0 0 0
    4 11  2.57  .59  .37  .18  .47     .02044698050404183 0 0 0
    4 12  2.57  .59  .37  .18  .47     .02468380252957978 0 0 0
    4 13  2.57  .59  .37  .18  .47   -.028068644560791254 0 0 0
    4 14  2.57  .59  .37  .18  .47     .05854203222204489 0 0 0
    4 15  2.57  .59  .37  .18  .47     .03701899659035565 0 0 0
    4 16  2.57  .59  .37  .18  .47    .052286282306162925 0 0 0
    4 17  2.57  .59  .37  .18  .47     .07497133387457207 0 0 0
    4 18  2.57  .59  .37  .18  .47  -.0028080028080028725 0 0 0
    4 19  2.57  .59  .37  .18  .47   -.040979631425800134 0 0 0
    4 20  2.57  .59  .37  .18  .47     .03045325779036825 0 0 0
    4 21  2.57  .59  .37  .18  .47  -.0005662514156285605 0 0 0
    4 22  2.57  .59  .37  .18  .47     .02827248441674094 0 0 0
    4 23  2.57  .59  .37  .18  .47    .036057692307692304 0 0 0
    4 24  2.57  .59  .37  .18  .47     .02618099032441667 0 0 0
    4 25  2.57  .59  .37  .18  .47     .05354752342704158 0 0 0
    end

    Code:
    gen time =_n
    reshape long P, i(time) j(stock)
    gen alpha=0
    gen tstat=0
    gen pvalue=0
    
    forvalues i=1/25 {
    reg P MktRF SMB HML RMW CMA if stock ==`i'  
    }
    
    forvalues i=1/25 {
    replace alpha=_b[_cons] if stock == `i'
    replace tstat= _b[_cons] / _se[_cons] if stock ==`i'
    replace pvalue= 2*ttail((_N-1),abs(tstat)) if stock == `i'
    }
    This is what happens after I run the last forvalues loop. As you can see Stata only stores coefficients for the last regression output.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float time byte stock double(MktRF SMB HML RMW CMA P) float(alpha tstat pvalue)
    1  1 -1.11 -.52 -.03  .23  .05    -.06737918215613387 .003726126 1.2680005 .205025
    1  2 -1.11 -.52 -.03  .23  .05   -.050800609031739934 .003726126 1.2680005 .205025
    1  3 -1.11 -.52 -.03  .23  .05   -.051464549685190326 .003726126 1.2680005 .205025
    1  4 -1.11 -.52 -.03  .23  .05     -.0663709805163947 .003726126 1.2680005 .205025
    1  5 -1.11 -.52 -.03  .23  .05    -.10097517117366342 .003726126 1.2680005 .205025
    1  6 -1.11 -.52 -.03  .23  .05     -.2543554006968641 .003726126 1.2680005 .205025
    1  7 -1.11 -.52 -.03  .23  .05    -.08746087276744614 .003726126 1.2680005 .205025
    1  8 -1.11 -.52 -.03  .23  .05    -.04082163286531462 .003726126 1.2680005 .205025
    1  9 -1.11 -.52 -.03  .23  .05    -.06216782090513769 .003726126 1.2680005 .205025
    1 10 -1.11 -.52 -.03  .23  .05   -.027121697357886245 .003726126 1.2680005 .205025
    1 11 -1.11 -.52 -.03  .23  .05   -.033752327746741057 .003726126 1.2680005 .205025
    1 12 -1.11 -.52 -.03  .23  .05    -.08830022075055183 .003726126 1.2680005 .205025
    1 13 -1.11 -.52 -.03  .23  .05    -.04056097896328891 .003726126 1.2680005 .205025
    1 14 -1.11 -.52 -.03  .23  .05     .03654159869494293 .003726126 1.2680005 .205025
    1 15 -1.11 -.52 -.03  .23  .05    -.10859903381642508 .003726126 1.2680005 .205025
    1 16 -1.11 -.52 -.03  .23  .05    -.08118956394818469 .003726126 1.2680005 .205025
    1 17 -1.11 -.52 -.03  .23  .05    -.09783722599327987 .003726126 1.2680005 .205025
    1 18 -1.11 -.52 -.03  .23  .05    -.13865932047750232 .003726126 1.2680005 .205025
    1 19 -1.11 -.52 -.03  .23  .05   -.035722762876809935 .003726126 1.2680005 .205025
    1 20 -1.11 -.52 -.03  .23  .05    -.08667736757624395 .003726126 1.2680005 .205025
    1 21 -1.11 -.52 -.03  .23  .05   -.019111860595840417 .003726126 1.2680005 .205025
    1 22 -1.11 -.52 -.03  .23  .05   -.051669147352753554 .003726126 1.2680005 .205025
    1 23 -1.11 -.52 -.03  .23  .05    -.08586083853702055 .003726126 1.2680005 .205025
    1 24 -1.11 -.52 -.03  .23  .05   -.025283347863993156 .003726126 1.2680005 .205025
    1 25 -1.11 -.52 -.03  .23  .05    -.05677721701514056 .003726126 1.2680005 .205025
    2  1 -2.14   .4 -.21  .31  .33   -.012812299807815583 .003726126 1.2680005 .205025
    2  2 -2.14   .4 -.21  .31  .33    .000662779301543388 .003726126 1.2680005 .205025
    2  3 -2.14   .4 -.21  .31  .33   -.015295815295815125 .003726126 1.2680005 .205025
    2  4 -2.14   .4 -.21  .31  .33    -.06435923538061318 .003726126 1.2680005 .205025
    2  5 -2.14   .4 -.21  .31  .33    -.03354104161858604 .003726126 1.2680005 .205025
    2  6 -2.14   .4 -.21  .31  .33    -.05140186915887844 .003726126 1.2680005 .205025
    2  7 -2.14   .4 -.21  .31  .33   -.046811945117029866 .003726126 1.2680005 .205025
    2  8 -2.14   .4 -.21  .31  .33   .0018975332068311272 .003726126 1.2680005 .205025
    2  9 -2.14   .4 -.21  .31  .33  -.0017173278378842763 .003726126 1.2680005 .205025
    2 10 -2.14   .4 -.21  .31  .33    -.03374138463121079 .003726126 1.2680005 .205025
    2 11 -2.14   .4 -.21  .31  .33 -.00024090580583004372 .003726126 1.2680005 .205025
    2 12 -2.14   .4 -.21  .31  .33    -.01493139628732853 .003726126 1.2680005 .205025
    2 13 -2.14   .4 -.21  .31  .33   -.011607910576096335 .003726126 1.2680005 .205025
    2 14 -2.14   .4 -.21  .31  .33    -.02533836953100419 .003726126 1.2680005 .205025
    2 15 -2.14   .4 -.21  .31  .33      -.079340992846304 .003726126 1.2680005 .205025
    2 16 -2.14   .4 -.21  .31  .33    -.04626687847498011 .003726126 1.2680005 .205025
    2 17 -2.14   .4 -.21  .31  .33   -.051092983768508314 .003726126 1.2680005 .205025
    2 18 -2.14   .4 -.21  .31  .33    -.02096659559346126 .003726126 1.2680005 .205025
    2 19 -2.14   .4 -.21  .31  .33 -.00012307692307681113 .003726126 1.2680005 .205025
    2 20 -2.14   .4 -.21  .31  .33     .00140597539543055 .003726126 1.2680005 .205025
    2 21 -2.14   .4 -.21  .31  .33   -.015854823304680004 .003726126 1.2680005 .205025
    2 22 -2.14   .4 -.21  .31  .33    -.04573991031390132 .003726126 1.2680005 .205025
    2 23 -2.14   .4 -.21  .31  .33   -.016589412051719925 .003726126 1.2680005 .205025
    2 24 -2.14   .4 -.21  .31  .33    .013416815742397222 .003726126 1.2680005 .205025
    2 25 -2.14   .4 -.21  .31  .33    -.02560672654309198 .003726126 1.2680005 .205025
    3  1  2.08  .14 -.19 -.38 -.76    .005984569904102765 .003726126 1.2680005 .205025
    3  2  2.08  .14 -.19 -.38 -.76    .009903396960280794 .003726126 1.2680005 .205025
    3  3  2.08  .14 -.19 -.38 -.76     .01992966002344665 .003726126 1.2680005 .205025
    3  4  2.08  .14 -.19 -.38 -.76    .009368955512572602 .003726126 1.2680005 .205025
    3  5  2.08  .14 -.19 -.38 -.76   -.008119079837618485 .003726126 1.2680005 .205025
    3  6  2.08  .14 -.19 -.38 -.76   -.004926108374384349 .003726126 1.2680005 .205025
    3  7  2.08  .14 -.19 -.38 -.76   -.010584250635055038 .003726126 1.2680005 .205025
    3  8  2.08  .14 -.19 -.38 -.76    .027056277056277056 .003726126 1.2680005 .205025
    3  9  2.08  .14 -.19 -.38 -.76    .006881128505074808 .003726126 1.2680005 .205025
    3 10  2.08  .14 -.19 -.38 -.76    .020440753752794652 .003726126 1.2680005 .205025
    3 11  2.08  .14 -.19 -.38 -.76    .013493975903614513 .003726126 1.2680005 .205025
    3 12  2.08  .14 -.19 -.38 -.76   .0040966816878329135 .003726126 1.2680005 .205025
    3 13  2.08  .14 -.19 -.38 -.76  -.0030448020878641982 .003726126 1.2680005 .205025
    3 14  2.08  .14 -.19 -.38 -.76    .012271919909575409 .003726126 1.2680005 .205025
    3 15  2.08  .14 -.19 -.38 -.76    -.03319990581586995 .003726126 1.2680005 .205025
    3 16  2.08  .14 -.19 -.38 -.76     .04726212783676875 .003726126 1.2680005 .205025
    3 17  2.08  .14 -.19 -.38 -.76  -.0032951265495437133 .003726126 1.2680005 .205025
    3 18  2.08  .14 -.19 -.38 -.76    .034119782214156125 .003726126 1.2680005 .205025
    3 19  2.08  .14 -.19 -.38 -.76    .015263417035942821 .003726126 1.2680005 .205025
    3 20  2.08  .14 -.19 -.38 -.76   -.008775008775008775 .003726126 1.2680005 .205025
    3 21  2.08  .14 -.19 -.38 -.76      .0283385093167702 .003726126 1.2680005 .205025
    3 22  2.08  .14 -.19 -.38 -.76    .055451127819548855 .003726126 1.2680005 .205025
    3 23  2.08  .14 -.19 -.38 -.76     .03200198461920117 .003726126 1.2680005 .205025
    3 24  2.08  .14 -.19 -.38 -.76     .03383348043542214 .003726126 1.2680005 .205025
    3 25  2.08  .14 -.19 -.38 -.76     .02549519513630118 .003726126 1.2680005 .205025
    4  1  2.57  .59  .37  .18  .47     .08228211009174304 .003726126 1.2680005 .205025
    4  2  2.57  .59  .37  .18  .47     .12422336413712734 .003726126 1.2680005 .205025
    4  3  2.57  .59  .37  .18  .47     .04339080459770109 .003726126 1.2680005 .205025
    4  4  2.57  .59  .37  .18  .47   -.003473261871968456 .003726126 1.2680005 .205025
    4  5  2.57  .59  .37  .18  .47    -.03595217077281109 .003726126 1.2680005 .205025
    4  6  2.57  .59  .37  .18  .47     .08910891089108919 .003726126 1.2680005 .205025
    4  7  2.57  .59  .37  .18  .47    .017971758664955144 .003726126 1.2680005 .205025
    4  8  2.57  .59  .37  .18  .47                      0 .003726126 1.2680005 .205025
    4  9  2.57  .59  .37  .18  .47       .035366478728857 .003726126 1.2680005 .205025
    4 10  2.57  .59  .37  .18  .47     .03599374021909236 .003726126 1.2680005 .205025
    4 11  2.57  .59  .37  .18  .47     .02044698050404183 .003726126 1.2680005 .205025
    4 12  2.57  .59  .37  .18  .47     .02468380252957978 .003726126 1.2680005 .205025
    4 13  2.57  .59  .37  .18  .47   -.028068644560791254 .003726126 1.2680005 .205025
    4 14  2.57  .59  .37  .18  .47     .05854203222204489 .003726126 1.2680005 .205025
    4 15  2.57  .59  .37  .18  .47     .03701899659035565 .003726126 1.2680005 .205025
    4 16  2.57  .59  .37  .18  .47    .052286282306162925 .003726126 1.2680005 .205025
    4 17  2.57  .59  .37  .18  .47     .07497133387457207 .003726126 1.2680005 .205025
    4 18  2.57  .59  .37  .18  .47  -.0028080028080028725 .003726126 1.2680005 .205025
    4 19  2.57  .59  .37  .18  .47   -.040979631425800134 .003726126 1.2680005 .205025
    4 20  2.57  .59  .37  .18  .47     .03045325779036825 .003726126 1.2680005 .205025
    4 21  2.57  .59  .37  .18  .47  -.0005662514156285605 .003726126 1.2680005 .205025
    4 22  2.57  .59  .37  .18  .47     .02827248441674094 .003726126 1.2680005 .205025
    4 23  2.57  .59  .37  .18  .47    .036057692307692304 .003726126 1.2680005 .205025
    4 24  2.57  .59  .37  .18  .47     .02618099032441667 .003726126 1.2680005 .205025
    4 25  2.57  .59  .37  .18  .47     .05354752342704158 .003726126 1.2680005 .205025
    end
    Last edited by Levi Jano; 10 Jun 2018, 03:04.

  • #2
    Keep it in one loop.

    Code:
    gen time =_n
    reshape long P, i(time) j(stock)
    gen alpha=0
    gen tstat=0
    gen pvalue=0
    
    forvalues i=1/25 {
    reg P MktRF SMB HML RMW CMA if stock ==`i'  
    replace alpha=_b[_cons] if stock == `i'
    replace tstat= _b[_cons] / _se[_cons] if stock ==`i'
    replace pvalue= 2*ttail((_N-1),abs(tstat)) if stock == `i'
    }
    Also, usually better to create missings than 0 values unless you have reason to set this factor at zero afterwards.
    Code:
    gen alpha=.
    gen tstat=.
    gen pvalue=.

    Comment


    • #3
      Jorrit's main point is key, but the use of _N in #1 and #2 is incorrect. The calculation should use the number of observations used in each regression, not the number of observations in the dataset.

      See also rangestat (SSC and mentions here). Here is a start on some code. In turn a main point of rangestat is that it does the looping for you.

      Code:
      gen time =_n
      reshape long P, i(time) j(stock)
      rangestat (reg) P MktRF SMB HML RMW CMA, int(stock 0 0)
      rename b_cons alpha
      gen tstat = alpha / se_cons
      By the way, claims of urgency really don't help. There isn't a queue here that you can jump by claiming urgency. Also, it's folklore that many experienced users ignore questions flagged as urgent. Otherwise put, you would need to explain to everyone else why your question is more important than theirs. The point is made in the FAQ Advice.

      Comment


      • #4
        This message may be taken as redundant, but I just wish to say that, considering this is a "non-stop" forum, where members of goodwill come to grips with solving several queries, even during holidays or weekends (like today), demanding an "urgent" reply a) indicates the sender did not get aware with the forum routine as well as the FAQ advice; b) may be seen as pushing too much the envelope or demanding unreasonable priviledges and, as a consequence, c) this may also prompt some members, busy as a bee, to just give the query a pass.

        Best regards,

        Marcos

        Comment

        Working...
        X