Announcement

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

  • Calculating average of looped regression coefficients

    Hello everyone,

    I'm currently facing an issue with my code, and I need some assistance. I have a dataset that contains daily returns of 136 mutual funds and 5 Fama-French factors over a span of 23 years. My objective is to run a looped regression for each fund, regressing it on the 5 Fama-French factors. Afterwards, I want to calculate the average coefficients, standard errors, and t-tests for the constant and the Fama-French factors. Essentially, I aim to obtain the same output as a regular regression, but for the average of looped regressions.

    The following code represents my loop, which is currently working successfully:
    Code:
    local num_funds = 136
    local start_col = 2
    local end_col = 137
    local coef_start_col = 138
    local coef_end_col = 142
    
    foreach fund of numlist `start_col'/`end_col' {
      local dep_var = "m" + string(`fund' - `start_col' + 1)
      regress `dep_var' MktRF SMB HML RMW CMA
    }
    However, I'm encountering issues when attempting to store the coefficients in a matrix for calculating the averages. I've tried the following code, but it is not working, and I'm unsure about what's causing the problem:

    Code:
    local num_funds = 136
    local start_col = 2
    local end_col = 137
    local coef_start_col = 138
    local coef_end_col = 142
    
    matrix coef_matrix = J(`num_funds', `coef_end_col' - `coef_start_col' + 1, .) // Create a new matrix to store the coefficients
    
    foreach fund of numlist `start_col'/`end_col' {
      local dep_var = "m" + string(`fund' - `start_col' + 1)
      regress `dep_var' MktRF SMB HML RMW CMA
      matrix coef_vector = e(b)
      local row = `fund' - `start_col' + 1
      forval i = 1/(`coef_end_col' - `coef_start_col' + 1) {
        local col = `i'
        matrix coef_matrix[`row', `col'] = coef_vector[1, `coef_start_col' + `i' - 1]
      }
    }
    
    egen mean_results = rowmean(results)
    egen se_results = rowstd(results) / sqrt(`num_funds')
    
    matrix list mean_results
    matrix list se_results

    I would greatly appreciate any help and advice regarding this issue. Thank you in advance for your assistance!

  • #2
    You could sum the entries of the needed statistics after extracting them from -r(table)- and then divide by the number of regressions.

    Code:
    regress ...
    mat l r(table)
    But we need a data example to be more specific (a couple of funds and a couple of years). See FAQ Advice #12 on how to provide one using the dataex command.

    Comment


    • #3
      Hi Andrew,

      Thank you for your answer and suggestion. I have tried adding r(table), it gives me the output in a table format now. Which code would you suggest for extracting the statistics and summing it? Should I keep something from the second version of y code?


      Please find the data example, I have reduced the initial 136 funds to 6 funds.

      Code:
      [CODE}
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int Date double(m1 m2 m3 m4 m5 m6 MktRF SMB HML RMW CMA)
      14612 -.37243962 1.3201356 1.1764765 -.386101 2.2058725 -1.4619887 -.71 -.06 -1.42 -1.5 -.63
      14613 -1.3084114 -1.6286612 -1.1627913 -.77519417 -5.0359607 -6.231451 -4.06 .33 2.06 .47 1.45
      14614 -.75757504 -.6622553 -1.512605 .390625 1.5151501 0 -.09 .33 .16 .41 1.11
      14615 0 -.33332705 -.8532405 .38909912 -2.9850721 1.2658238 -.73 -.04 1.26 .65 1.21
      14616 .38167238 0 .34422874 1.9379854 3.8461566 1.8749952 3.21 -.93 -1.42 -.88 -.96
      14619 0 .66889524 3.087473 0 1.8518567 .6134987 1.76 .5 -1.53 -1.98 -.24
      14620 1.1406898 .66444874 1.1647224 -.38022995 -3.272736 -4.5731726 -1.71 .34 .81 .81 1.08
      14621 -.18796921 -.66006184 -1.3157904 .3816843 -1.1278093 -1.5974462 -.69 -.22 .78 .42 .95
      14622 .37665367 0 -.33333302 0 .38022995 3.5714269 1.59 .49 -.91 -1.77 -1.04
      14623 .18761158 -1.6611278 3.8461566 0 0 .9404421 1.15 .26 -.65 -.31 -.62
      14627 .93632936 2.0270228 .80515146 .38022995 0 1.8633485 -.26 2.3 -.49 -1.48 -.12
      14628 .927639 -.6622553 .6389737 -.37878752 -3.030312 2.439022 .44 .64 -.37 -1.11 .47
      14629 -1.4705837 -1.3333261 -5.079365 .38022995 -2.343744 2.976191 -.37 1.73 -1.25 -1.07 -.59
      14630 .9328246 -1.3513565 .33445358 .75757504 -.4000008 -.28901696 .23 1.24 -.52 -1.31 .4
      14633 0 -1.7123282 .16666651 -.37593842 0 -1.1594176 -2.59 1.4 .76 .49 .9
      14634 -.92421174 0 -1.3311148 .377357 -1.2048244 .87976456 .49 -.77 -.1 -.59 -.62
      14635 .55969954 -2.787459 -.16863346 .37593842 0 .8720875 -.44 .31 .09 .35 .96
      14636 .5565882 2.8673887 0 0 1.219511 -.86455345 -.44 -.23 -.68 .15 -.01
      14637 -2.2140205 -2.0905912 -2.1959484 .37453175 0 -2.9069781 -2.82 .2 1.63 1 .39
      14640 -.37736297 -.3558755 -1.2089789 -.373137 1.6064167 0 1.5 -3.35 -.43 .75 .44
      14641 .56818724 -1.0714293 -.34965277 .7490635 -1.1857629 .5988002 1.29 -.06 -.1 -.39 -1.11
      14642 -.56497455 .7220149 .87718964 -.7434964 4.400003 0 .16 1.23 -.22 -1.06 .12
      14643 .94697475 1.0752678 1.565218 1.1235952 .76628923 1.1904716 1.49 .94 -1.29 -.98 -.67
      14644 .75047016 11.702132 .8561611 0 .38022995 .29411316 -.05 1.17 -.62 -.32 -.16
      14647 0 .95238686 .5093336 0 1.1363506 -.29325485 .35 1.28 -1.2 -1.1 -.11
      14648 1.6759753 -.31446218 -.33783913 .37037134 2.2471905 .58823824 1.23 .08 -1.73 -1.58 -.82
      14649 -.7326007 2.8391123 -.33898354 0 0 .5847931 -1.83 1.91 -.41 -.36 .28
      14650 0 1.8404961 .8503437 .36900043 1.8314958 .8720875 .57 .81 -.57 -1.84 -.43
      14651 -2.0295203 -2.409643 -1.3490736 .36764145 -1.4388442 .86455345 -1.74 1.25 .94 -.61 .43
      14654 -.37665367 -1.2345612 1.025641 0 -2.9197156 -.5714297 .24 .75 -.16 .09 -.5
      14655 0 -.3125012 -.84602237 -.7326007 -3.3834577 -1.1494279 .7 -.75 .04 -.05 .89
      14656 -1.5122831 -.6269574 -.1706481 0 .38911104 2.0348787 -.58 1.94 -.57 -2.11 .76
      14657 .95968246 -.31545758 .68376064 -.3690064 .77519417 -2.5641024 .46 1.78 -1.26 -2.24 -.16
      14658 -1.5209079 1.5822768 .33955574 -.37037134 -2.3076952 -4.9707594 -2.69 1.23 .49 -.46 .35
      14662 .96524954 -.934577 .507617 .37174225 -1.968497 .3076911 .11 -1.08 .38 .47 .35
      14663 .57361126 -1.886791 -.33670068 -.37037134 2.409637 2.7607322 1.24 .32 -1.45 -1.04 -1.2
      14664 .19011497 -2.5641024 .33783913 .18587112 -2.745092 -.29850602 .01 .82 -.55 -.83 -1.22
      14665 .37950277 -1.9736826 -.33670068 -.18553138 1.2096763 0 -1.04 1.83 -.14 -.87 .24
      14668 -.75613856 -.6711364 -.67567825 0 -2.7888536 -.89820623 .76 -.2 -.06 .17 .65
      14669 .95238686 -.67567825 .6802678 0 1.2295127 .3021121 1.96 1.47 -.1 -1.8 .77
      14670 .18867254 1.0204077 0 -.3717482 6.4777374 .6024122 1.39 .84 -.35 -1.33 -.15
      14671 2.448213 -.33669472 .8445978 .373137 3.4220457 -.2994001 -.09 -.04 .62 .88 .62
      14672 .91911554 -.67567825 -.50251484 0 1.838243 .60060024 2.3 -.17 -1.21 -1.02 -.49
      14675 -.18214583 -.6802678 .33669472 0 -2.527082 -.59701204 -.77 1.57 .32 -1.27 .49
      14676 -1.2773752 4.1095853 -.3355682 0 -4.8148155 -1.5015006 -2.2 .8 .94 -.62 .01
      14677 .7393718 -1.3157904 2.8619528 -.3717482 1.9455314 -2.439022 .69 -.58 -.79 .3 -1.52
      14678 0 .99999905 -1.4729977 0 -.38167834 .6250024 2.45 -.73 -.35 .68 -1.13
      14679 .5504608 -2.3102283 1.6611338 .746274 -1.9157112 -.31055808 -.43 .29 .11 .38 .06
      14682 -.91241 -.33783913 -.49019456 -.37037134 -3.90625 -.6230533 -1.48 -.78 .95 1.56 .1
      14683 .5524874 -1.0169506 .49260855 -.7434964 -3.2520294 .31348467 -2.33 -.92 1.2 2.36 -.28
      14684 1.098907 -.3424704 -1.4705896 .7490635 3.3613443 0 .83 -4.16 1 3.27 1.39
      14685 4.1666746 -.34363866 1.65838 -.3717482 0 1.8749952 4.25 -3.27 .2 .85 -.24
      14686 -.52173734 -3.4482777 1.1419296 .373137 -.81300735 -.6134987 .62 .12 -.23 -.05 -.52
      14689 -.34965277 1.4285684 .6451607 0 -2.4590135 -.6172836 -1.71 -2.56 2.24 3.18 .61
      14690 1.05263 1.0563374 2.4038434 0 1.6806602 .62111616 1.95 -2.47 -.37 .25 -1.25
      14691 .17360449 -1.7421663 0 0 .82644224 1.5432119 1.31 2.19 -1.08 -2.18 -.57
      14692 2.2530437 -1.0638297 1.7214417 .37174225 -.81967115 -2.1276593 1.48 -.74 -.14 1.02 -.1
      14693 -.5084753 -.7168412 -.6153822 0 .41322708 0 .11 -.4 .2 .1 -.25
      14696 -.8517921 .7220149 .30959845 -.37037134 -.4115224 -1.2422383 -.39 .22 -.45 -.14 .31
      14697 2.2336721 0 -.30864477 .7434964 -.8264363 .31446218 -1.34 -1.02 1.03 .82 -.05
      14698 -1.512605 -2.1505356 -.92879534 -.73800683 2.916658 -.9404361 -1 -2.2 2.26 2.74 .93
      14699 1.0238886 -.36629438 -.15624762 .7434964 -3.6437213 .63290596 -1.76 -1.38 1.57 1.54 .74
      14700 1.0135174 1.8382311 -1.4084518 -.3690064 .42016506 -1.886791 1.19 .24 .27 -1.7 .17
      14703 -.16722083 -1.8050492 .79364777 .37037134 -5.439335 -1.2820542 -1.67 -3.26 1.71 4.52 2.11
      14704 -3.182578 2.573526 -2.2047222 -1.1070132 -4.8672557 -1.9480526 -1.2 -1.86 .12 1.67 .45
      14705 -.5190313 -1.4336884 .32205582 0 -.46511889 .99338293 .01 1.61 -.93 -1.16 .2
      14706 1.565218 -1.090908 .6420493 .746274 -.934577 .98360777 1.47 1.42 -.38 -1.59 .73
      14707 .5136967 1.1029363 2.0733595 0 8.490562 -1.9480526 1.42 1.08 -.16 -.86 -1.29
      14710 .17035007 .36363602 .9374976 0 1.7391324 -1.986754 -1.95 -2.53 1.68 3.27 1.17
      14711 -.17006397 0 -1.2383878 0 -2.136749 .33783913 -1.01 -1.74 1.9 2.08 1.23
      14712 -.170362 -3.6231816 -.6269574 -.37037134 -3.9301336 -4.7138033 -2.97 -1.79 2.53 2 1.5
      14713 -2.3890734 -.7518828 -2.208203 -.3717482 -1.3636351 4.946995 -1.8 .07 .95 .29 .72
      14714 -5.2447557 -.75757504 -5.161291 0 -8.294928 0 -6.72 -1.11 2.17 2.93 .78
      14717 -.3690064 -3.0534327 0 -.373137 -1.5075386 .33669472 2.94 -2.5 -1.66 .25 -2.15
      14718 2.0370364 -1.968509 1.3605475 0 2.5510192 -1.6778529 3.84 2.86 -2.44 -2.95 -.92
      14719 .90743303 .40160418 1.5100718 -.7490635 0 2.0477772 -.8 1.68 -.06 -.54 1.07
      14720 -1.0791361 .40000677 -.66115856 .754714 0 -1.0033429 .09 -.92 1 .73 .41
      14724 -1.090908 1.1952162 -.6655574 -.37453175 -4.477608 -2.0270288 -1.01 -1.95 1.1 2.21 1.28
      14725 2.389717 -1.1811018 1.1725307 0 2.6041627 -2.0689666 3.74 .19 -.9 -1.96 -1.17
      14726 -.5385995 -.79681873 .33112764 -.37593842 -2.538067 3.5211325 -1.23 .59 1.13 .2 .59
      14727 -.18051267 0 0 -.754714 -1.562506 .6802678 .76 1 -.51 -1.69 -1.22
      14728 .54249763 -1.2048185 .16502142 0 5.291009 1.0135174 .07 2.27 .07 -2.1 -.28
      14731 1.0791421 1.6260147 .4942298 0 3.517592 4.0133834 1.4 1.27 -1.03 -.91 -.42
      14732 -.17793179 5.5999994 .32787323 0 2.9126167 .96462965 -2.07 -.48 1.68 1.42 .74
      14733 -.71300864 -1.1363626 -1.4705896 0 -2.3584962 1.9108295 -2.09 .29 -.37 .6 .36
      14734 -.8976698 1.9157171 -.497514 0 1.449275 0 0 .94 .34 -1.12 .31
      14735 .7246375 -1.1278212 .33333302 -.76045394 .47619343 1.5625 1.48 .5 -.44 -.26 -.29
      14738 .71942806 -1.1406839 1.4950156000000001 -.7662833 0 -1.5384614 -.98 -1.25 1.42 .99 .85
      14739 .1785636 0 -.1636684 0 -1.4218032 -1.5625 -1.19 -.59 .6 1.54 .03
      14740 -.89126825 1.5384555 -.81967115 0 -3.8461506 -.6349206 -2.7 -.8 .93 1.38 1.09
      14741 .17986298 -1.8939376 0 -.386101 -1.000005 1.5974402 2.08 .3 .23 -.91 -1.04
      14742 .17952919 -.77219605 -.8264482 0 0 -.62893033 .81 -.52 .36 .06 -.16
      14745 1.0752678 -.7782161 .16666651 -.19379854 0 0 2.13 -.91 -.71 -.43 -.38
      14746 .8865237 -2.352941 .99834204 .5825281 2.020204 2.8481007 1.3 .05 -.81 -1.16 -.58
      14747 -2.8119564 -1.2048185 -.16474724 .38609505 -4.950494 .9230733 -1.38 .42 .75 .29 .3
      14748 .18084049 0 -.33003092 -.38461685 .5208373 1.219511 -1.09 -.76 .86 .95 .97
      14749 -.7220268 0 -.6622493 0 -1.0362744 0 -2.39 -.01 .86 .79 .54
      14752 -1.6363621 0 -.99999905 -.386101 -3.6649227 1.2048244 -.73 -1.34 .42 .61 -.21
      14753 -.18483996 .8130193 0 0 -2.7173877 1.7857194 -2.38 -.33 1.3 2.04 .94
      14754 -.37037134 -2.4193525 -.8417487 0 2.7933002 .29239655 1.55 -1.68 -1.2 .47 -1.05
      end
      format %td Date
      [/CODE]

      Comment


      • #4
        My reading is that you want the following:

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input int Date double(m1 m2 m3 m4 m5 m6 MktRF SMB HML RMW CMA)
        14612 -.37243962  1.3201356          1.1764765   -.386101  2.2058725 -1.4619887  -.71  -.06 -1.42  -1.5  -.63
        14613 -1.3084114 -1.6286612         -1.1627913 -.77519417 -5.0359607  -6.231451 -4.06   .33  2.06   .47  1.45
        14614 -.75757504  -.6622553          -1.512605    .390625  1.5151501          0  -.09   .33   .16   .41  1.11
        14615          0 -.33332705          -.8532405  .38909912 -2.9850721  1.2658238  -.73  -.04  1.26   .65  1.21
        14616  .38167238          0          .34422874  1.9379854  3.8461566  1.8749952  3.21  -.93 -1.42  -.88  -.96
        14619          0  .66889524           3.087473          0  1.8518567   .6134987  1.76    .5 -1.53 -1.98  -.24
        14620  1.1406898  .66444874          1.1647224 -.38022995  -3.272736 -4.5731726 -1.71   .34   .81   .81  1.08
        14621 -.18796921 -.66006184         -1.3157904   .3816843 -1.1278093 -1.5974462  -.69  -.22   .78   .42   .95
        14622  .37665367          0         -.33333302          0  .38022995  3.5714269  1.59   .49  -.91 -1.77 -1.04
        14623  .18761158 -1.6611278          3.8461566          0          0   .9404421  1.15   .26  -.65  -.31  -.62
        14627  .93632936  2.0270228          .80515146  .38022995          0  1.8633485  -.26   2.3  -.49 -1.48  -.12
        14628    .927639  -.6622553           .6389737 -.37878752  -3.030312   2.439022   .44   .64  -.37 -1.11   .47
        14629 -1.4705837 -1.3333261          -5.079365  .38022995  -2.343744   2.976191  -.37  1.73 -1.25 -1.07  -.59
        14630   .9328246 -1.3513565          .33445358  .75757504  -.4000008 -.28901696   .23  1.24  -.52 -1.31    .4
        14633          0 -1.7123282          .16666651 -.37593842          0 -1.1594176 -2.59   1.4   .76   .49    .9
        14634 -.92421174          0         -1.3311148    .377357 -1.2048244  .87976456   .49  -.77   -.1  -.59  -.62
        14635  .55969954  -2.787459         -.16863346  .37593842          0   .8720875  -.44   .31   .09   .35   .96
        14636   .5565882  2.8673887                  0          0   1.219511 -.86455345  -.44  -.23  -.68   .15  -.01
        14637 -2.2140205 -2.0905912         -2.1959484  .37453175          0 -2.9069781 -2.82    .2  1.63     1   .39
        14640 -.37736297  -.3558755         -1.2089789   -.373137  1.6064167          0   1.5 -3.35  -.43   .75   .44
        14641  .56818724 -1.0714293         -.34965277   .7490635 -1.1857629   .5988002  1.29  -.06   -.1  -.39 -1.11
        14642 -.56497455   .7220149          .87718964  -.7434964   4.400003          0   .16  1.23  -.22 -1.06   .12
        14643  .94697475  1.0752678           1.565218  1.1235952  .76628923  1.1904716  1.49   .94 -1.29  -.98  -.67
        14644  .75047016  11.702132           .8561611          0  .38022995  .29411316  -.05  1.17  -.62  -.32  -.16
        14647          0  .95238686           .5093336          0  1.1363506 -.29325485   .35  1.28  -1.2  -1.1  -.11
        14648  1.6759753 -.31446218         -.33783913  .37037134  2.2471905  .58823824  1.23   .08 -1.73 -1.58  -.82
        14649  -.7326007  2.8391123         -.33898354          0          0   .5847931 -1.83  1.91  -.41  -.36   .28
        14650          0  1.8404961           .8503437  .36900043  1.8314958   .8720875   .57   .81  -.57 -1.84  -.43
        14651 -2.0295203  -2.409643         -1.3490736  .36764145 -1.4388442  .86455345 -1.74  1.25   .94  -.61   .43
        14654 -.37665367 -1.2345612           1.025641          0 -2.9197156  -.5714297   .24   .75  -.16   .09   -.5
        14655          0  -.3125012         -.84602237  -.7326007 -3.3834577 -1.1494279    .7  -.75   .04  -.05   .89
        14656 -1.5122831  -.6269574          -.1706481          0  .38911104  2.0348787  -.58  1.94  -.57 -2.11   .76
        14657  .95968246 -.31545758          .68376064  -.3690064  .77519417 -2.5641024   .46  1.78 -1.26 -2.24  -.16
        14658 -1.5209079  1.5822768          .33955574 -.37037134 -2.3076952 -4.9707594 -2.69  1.23   .49  -.46   .35
        14662  .96524954   -.934577            .507617  .37174225  -1.968497   .3076911   .11 -1.08   .38   .47   .35
        14663  .57361126  -1.886791         -.33670068 -.37037134   2.409637  2.7607322  1.24   .32 -1.45 -1.04  -1.2
        14664  .19011497 -2.5641024          .33783913  .18587112  -2.745092 -.29850602   .01   .82  -.55  -.83 -1.22
        14665  .37950277 -1.9736826         -.33670068 -.18553138  1.2096763          0 -1.04  1.83  -.14  -.87   .24
        14668 -.75613856  -.6711364         -.67567825          0 -2.7888536 -.89820623   .76   -.2  -.06   .17   .65
        14669  .95238686 -.67567825           .6802678          0  1.2295127   .3021121  1.96  1.47   -.1  -1.8   .77
        14670  .18867254  1.0204077                  0  -.3717482  6.4777374   .6024122  1.39   .84  -.35 -1.33  -.15
        14671   2.448213 -.33669472           .8445978    .373137  3.4220457  -.2994001  -.09  -.04   .62   .88   .62
        14672  .91911554 -.67567825         -.50251484          0   1.838243  .60060024   2.3  -.17 -1.21 -1.02  -.49
        14675 -.18214583  -.6802678          .33669472          0  -2.527082 -.59701204  -.77  1.57   .32 -1.27   .49
        14676 -1.2773752  4.1095853          -.3355682          0 -4.8148155 -1.5015006  -2.2    .8   .94  -.62   .01
        14677   .7393718 -1.3157904          2.8619528  -.3717482  1.9455314  -2.439022   .69  -.58  -.79    .3 -1.52
        14678          0  .99999905         -1.4729977          0 -.38167834   .6250024  2.45  -.73  -.35   .68 -1.13
        14679   .5504608 -2.3102283          1.6611338    .746274 -1.9157112 -.31055808  -.43   .29   .11   .38   .06
        14682    -.91241 -.33783913         -.49019456 -.37037134   -3.90625  -.6230533 -1.48  -.78   .95  1.56    .1
        14683   .5524874 -1.0169506          .49260855  -.7434964 -3.2520294  .31348467 -2.33  -.92   1.2  2.36  -.28
        14684   1.098907  -.3424704         -1.4705896   .7490635  3.3613443          0   .83 -4.16     1  3.27  1.39
        14685  4.1666746 -.34363866            1.65838  -.3717482          0  1.8749952  4.25 -3.27    .2   .85  -.24
        14686 -.52173734 -3.4482777          1.1419296    .373137 -.81300735  -.6134987   .62   .12  -.23  -.05  -.52
        14689 -.34965277  1.4285684           .6451607          0 -2.4590135  -.6172836 -1.71 -2.56  2.24  3.18   .61
        14690    1.05263  1.0563374          2.4038434          0  1.6806602  .62111616  1.95 -2.47  -.37   .25 -1.25
        14691  .17360449 -1.7421663                  0          0  .82644224  1.5432119  1.31  2.19 -1.08 -2.18  -.57
        14692  2.2530437 -1.0638297          1.7214417  .37174225 -.81967115 -2.1276593  1.48  -.74  -.14  1.02   -.1
        14693  -.5084753  -.7168412          -.6153822          0  .41322708          0   .11   -.4    .2    .1  -.25
        14696  -.8517921   .7220149          .30959845 -.37037134  -.4115224 -1.2422383  -.39   .22  -.45  -.14   .31
        14697  2.2336721          0         -.30864477   .7434964  -.8264363  .31446218 -1.34 -1.02  1.03   .82  -.05
        14698  -1.512605 -2.1505356         -.92879534 -.73800683   2.916658  -.9404361    -1  -2.2  2.26  2.74   .93
        14699  1.0238886 -.36629438         -.15624762   .7434964 -3.6437213  .63290596 -1.76 -1.38  1.57  1.54   .74
        14700  1.0135174  1.8382311         -1.4084518  -.3690064  .42016506  -1.886791  1.19   .24   .27  -1.7   .17
        14703 -.16722083 -1.8050492          .79364777  .37037134  -5.439335 -1.2820542 -1.67 -3.26  1.71  4.52  2.11
        14704  -3.182578   2.573526         -2.2047222 -1.1070132 -4.8672557 -1.9480526  -1.2 -1.86   .12  1.67   .45
        14705  -.5190313 -1.4336884          .32205582          0 -.46511889  .99338293   .01  1.61  -.93 -1.16    .2
        14706   1.565218  -1.090908           .6420493    .746274   -.934577  .98360777  1.47  1.42  -.38 -1.59   .73
        14707   .5136967  1.1029363          2.0733595          0   8.490562 -1.9480526  1.42  1.08  -.16  -.86 -1.29
        14710  .17035007  .36363602           .9374976          0  1.7391324  -1.986754 -1.95 -2.53  1.68  3.27  1.17
        14711 -.17006397          0         -1.2383878          0  -2.136749  .33783913 -1.01 -1.74   1.9  2.08  1.23
        14712   -.170362 -3.6231816          -.6269574 -.37037134 -3.9301336 -4.7138033 -2.97 -1.79  2.53     2   1.5
        14713 -2.3890734  -.7518828          -2.208203  -.3717482 -1.3636351   4.946995  -1.8   .07   .95   .29   .72
        14714 -5.2447557 -.75757504          -5.161291          0  -8.294928          0 -6.72 -1.11  2.17  2.93   .78
        14717  -.3690064 -3.0534327                  0   -.373137 -1.5075386  .33669472  2.94  -2.5 -1.66   .25 -2.15
        14718  2.0370364  -1.968509          1.3605475          0  2.5510192 -1.6778529  3.84  2.86 -2.44 -2.95  -.92
        14719  .90743303  .40160418          1.5100718  -.7490635          0  2.0477772   -.8  1.68  -.06  -.54  1.07
        14720 -1.0791361  .40000677         -.66115856    .754714          0 -1.0033429   .09  -.92     1   .73   .41
        14724  -1.090908  1.1952162          -.6655574 -.37453175  -4.477608 -2.0270288 -1.01 -1.95   1.1  2.21  1.28
        14725   2.389717 -1.1811018          1.1725307          0  2.6041627 -2.0689666  3.74   .19   -.9 -1.96 -1.17
        14726  -.5385995 -.79681873          .33112764 -.37593842  -2.538067  3.5211325 -1.23   .59  1.13    .2   .59
        14727 -.18051267          0                  0   -.754714  -1.562506   .6802678   .76     1  -.51 -1.69 -1.22
        14728  .54249763 -1.2048185          .16502142          0   5.291009  1.0135174   .07  2.27   .07  -2.1  -.28
        14731  1.0791421  1.6260147           .4942298          0   3.517592  4.0133834   1.4  1.27 -1.03  -.91  -.42
        14732 -.17793179  5.5999994          .32787323          0  2.9126167  .96462965 -2.07  -.48  1.68  1.42   .74
        14733 -.71300864 -1.1363626         -1.4705896          0 -2.3584962  1.9108295 -2.09   .29  -.37    .6   .36
        14734  -.8976698  1.9157171           -.497514          0   1.449275          0     0   .94   .34 -1.12   .31
        14735   .7246375 -1.1278212          .33333302 -.76045394  .47619343     1.5625  1.48    .5  -.44  -.26  -.29
        14738  .71942806 -1.1406839 1.4950156000000001  -.7662833          0 -1.5384614  -.98 -1.25  1.42   .99   .85
        14739   .1785636          0          -.1636684          0 -1.4218032    -1.5625 -1.19  -.59    .6  1.54   .03
        14740 -.89126825  1.5384555         -.81967115          0 -3.8461506  -.6349206  -2.7   -.8   .93  1.38  1.09
        14741  .17986298 -1.8939376                  0   -.386101  -1.000005  1.5974402  2.08    .3   .23  -.91 -1.04
        14742  .17952919 -.77219605          -.8264482          0          0 -.62893033   .81  -.52   .36   .06  -.16
        14745  1.0752678  -.7782161          .16666651 -.19379854          0          0  2.13  -.91  -.71  -.43  -.38
        14746   .8865237  -2.352941          .99834204   .5825281   2.020204  2.8481007   1.3   .05  -.81 -1.16  -.58
        14747 -2.8119564 -1.2048185         -.16474724  .38609505  -4.950494   .9230733 -1.38   .42   .75   .29    .3
        14748  .18084049          0         -.33003092 -.38461685   .5208373   1.219511 -1.09  -.76   .86   .95   .97
        14749  -.7220268          0          -.6622493          0 -1.0362744          0 -2.39  -.01   .86   .79   .54
        14752 -1.6363621          0         -.99999905   -.386101 -3.6649227  1.2048244  -.73 -1.34   .42   .61  -.21
        14753 -.18483996   .8130193                  0          0 -2.7173877  1.7857194 -2.38  -.33   1.3  2.04   .94
        14754 -.37037134 -2.4193525          -.8417487          0  2.7933002  .29239655  1.55 -1.68  -1.2   .47 -1.05
        end
        format %td Date
        
        clear matrix
        forval i=1/6{
            regress m`i' MktRF SMB HML RMW CMA
            if `i'==1{
                mat wanted= r(table)[1..3, 1...]
            }
            if inrange(`i', 2, 6){
                mat wanted= wanted+ r(table)[1..3, 1...]
            }
        }
        mat wanted= wanted/6
        mat l wanted
        If a regression fails, the code will fail. Otherwise, matrix wanted will be the averaged coefficients, standard errors and t-statistics from the six regressions with the same regressors and outcomes m1 through m6.

        Res.:

        Code:
        . mat wanted= wanted/6
        
        .
        . mat l wanted
        
        wanted[3,6]
                 MktRF         SMB         HML         RMW         CMA       _cons
         b   .34683593   .22093612  -.04894403   .14021731   .04204172   -.0509192
        se   .15164387   .22913342   .29038258    .2774704    .2678931   .14724091
         t   2.5883626   1.0966643  -.09205493   .72349196   .24011036  -.14324462
        Code:
        clear
        local colnames= ustrregexra("`:colnames wanted'", "\b_cons\b", "cons")
        mat colnames wanted= `colnames'
        svmat wanted, names(col)
        gen which= cond(_n==1, "Coefficient", cond(_n==2, "Std. Error", "t-statistic")), before(MktRF)
        to convert this matrix to a dataset.

        Res.:

        Code:
        . l
        
             +---------------------------------------------------------------------------------+
             |       which      MktRF        SMB         HML        RMW        CMA        cons |
             |---------------------------------------------------------------------------------|
          1. | Coefficient   .3468359   .2209361    -.048944   .1402173   .0420417   -.0509192 |
          2. |  Std. Error   .1516439   .2291334    .2903826   .2774704   .2678931    .1472409 |
          3. | t-statistic   2.588363   1.096664   -.0920549    .723492   .2401104   -.1432446 |
             +---------------------------------------------------------------------------------+
        Last edited by Andrew Musau; 15 Jun 2023, 18:19.

        Comment


        • #5
          Hi Andrew,

          Just wanted to say a huge thank you for your help! The code you gave me is working, and I couldn't be happier. I've been struggling with this code for the past days, so your assistance is a huge relief. I do have a quick question though: Is there a way to add a p-value to the output? And can I use outreg2 to display this table?

          Thanks again for everything!

          Comment


          • #6
            Due to rounding, if you pursue #4, I recommend that you calculate the t-statistics based on your averaged coefficients and standard errors. As you can see, the t-statistics are off as a result of the rounding. Another approach is to use suest and average and output the estimates using margins. However, I do not know the maximum number of estimates that can be combined using suest. If you have estimates in the low 100s, then you may be fine. This should be more accurate for the regressions in #4. Install estout from SSC which will allow you to output the averaged estimates, same as a single regression. If this fails, let me know and I can expand on #4.


            Code:
            ssc install estout, replace
            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input int Date double(m1 m2 m3 m4 m5 m6 MktRF SMB HML RMW CMA)
            14612 -.37243962  1.3201356          1.1764765   -.386101  2.2058725 -1.4619887  -.71  -.06 -1.42  -1.5  -.63
            14613 -1.3084114 -1.6286612         -1.1627913 -.77519417 -5.0359607  -6.231451 -4.06   .33  2.06   .47  1.45
            14614 -.75757504  -.6622553          -1.512605    .390625  1.5151501          0  -.09   .33   .16   .41  1.11
            14615          0 -.33332705          -.8532405  .38909912 -2.9850721  1.2658238  -.73  -.04  1.26   .65  1.21
            14616  .38167238          0          .34422874  1.9379854  3.8461566  1.8749952  3.21  -.93 -1.42  -.88  -.96
            14619          0  .66889524           3.087473          0  1.8518567   .6134987  1.76    .5 -1.53 -1.98  -.24
            14620  1.1406898  .66444874          1.1647224 -.38022995  -3.272736 -4.5731726 -1.71   .34   .81   .81  1.08
            14621 -.18796921 -.66006184         -1.3157904   .3816843 -1.1278093 -1.5974462  -.69  -.22   .78   .42   .95
            14622  .37665367          0         -.33333302          0  .38022995  3.5714269  1.59   .49  -.91 -1.77 -1.04
            14623  .18761158 -1.6611278          3.8461566          0          0   .9404421  1.15   .26  -.65  -.31  -.62
            14627  .93632936  2.0270228          .80515146  .38022995          0  1.8633485  -.26   2.3  -.49 -1.48  -.12
            14628    .927639  -.6622553           .6389737 -.37878752  -3.030312   2.439022   .44   .64  -.37 -1.11   .47
            14629 -1.4705837 -1.3333261          -5.079365  .38022995  -2.343744   2.976191  -.37  1.73 -1.25 -1.07  -.59
            14630   .9328246 -1.3513565          .33445358  .75757504  -.4000008 -.28901696   .23  1.24  -.52 -1.31    .4
            14633          0 -1.7123282          .16666651 -.37593842          0 -1.1594176 -2.59   1.4   .76   .49    .9
            14634 -.92421174          0         -1.3311148    .377357 -1.2048244  .87976456   .49  -.77   -.1  -.59  -.62
            14635  .55969954  -2.787459         -.16863346  .37593842          0   .8720875  -.44   .31   .09   .35   .96
            14636   .5565882  2.8673887                  0          0   1.219511 -.86455345  -.44  -.23  -.68   .15  -.01
            14637 -2.2140205 -2.0905912         -2.1959484  .37453175          0 -2.9069781 -2.82    .2  1.63     1   .39
            14640 -.37736297  -.3558755         -1.2089789   -.373137  1.6064167          0   1.5 -3.35  -.43   .75   .44
            14641  .56818724 -1.0714293         -.34965277   .7490635 -1.1857629   .5988002  1.29  -.06   -.1  -.39 -1.11
            14642 -.56497455   .7220149          .87718964  -.7434964   4.400003          0   .16  1.23  -.22 -1.06   .12
            14643  .94697475  1.0752678           1.565218  1.1235952  .76628923  1.1904716  1.49   .94 -1.29  -.98  -.67
            14644  .75047016  11.702132           .8561611          0  .38022995  .29411316  -.05  1.17  -.62  -.32  -.16
            14647          0  .95238686           .5093336          0  1.1363506 -.29325485   .35  1.28  -1.2  -1.1  -.11
            14648  1.6759753 -.31446218         -.33783913  .37037134  2.2471905  .58823824  1.23   .08 -1.73 -1.58  -.82
            14649  -.7326007  2.8391123         -.33898354          0          0   .5847931 -1.83  1.91  -.41  -.36   .28
            14650          0  1.8404961           .8503437  .36900043  1.8314958   .8720875   .57   .81  -.57 -1.84  -.43
            14651 -2.0295203  -2.409643         -1.3490736  .36764145 -1.4388442  .86455345 -1.74  1.25   .94  -.61   .43
            14654 -.37665367 -1.2345612           1.025641          0 -2.9197156  -.5714297   .24   .75  -.16   .09   -.5
            14655          0  -.3125012         -.84602237  -.7326007 -3.3834577 -1.1494279    .7  -.75   .04  -.05   .89
            14656 -1.5122831  -.6269574          -.1706481          0  .38911104  2.0348787  -.58  1.94  -.57 -2.11   .76
            14657  .95968246 -.31545758          .68376064  -.3690064  .77519417 -2.5641024   .46  1.78 -1.26 -2.24  -.16
            14658 -1.5209079  1.5822768          .33955574 -.37037134 -2.3076952 -4.9707594 -2.69  1.23   .49  -.46   .35
            14662  .96524954   -.934577            .507617  .37174225  -1.968497   .3076911   .11 -1.08   .38   .47   .35
            14663  .57361126  -1.886791         -.33670068 -.37037134   2.409637  2.7607322  1.24   .32 -1.45 -1.04  -1.2
            14664  .19011497 -2.5641024          .33783913  .18587112  -2.745092 -.29850602   .01   .82  -.55  -.83 -1.22
            14665  .37950277 -1.9736826         -.33670068 -.18553138  1.2096763          0 -1.04  1.83  -.14  -.87   .24
            14668 -.75613856  -.6711364         -.67567825          0 -2.7888536 -.89820623   .76   -.2  -.06   .17   .65
            14669  .95238686 -.67567825           .6802678          0  1.2295127   .3021121  1.96  1.47   -.1  -1.8   .77
            14670  .18867254  1.0204077                  0  -.3717482  6.4777374   .6024122  1.39   .84  -.35 -1.33  -.15
            14671   2.448213 -.33669472           .8445978    .373137  3.4220457  -.2994001  -.09  -.04   .62   .88   .62
            14672  .91911554 -.67567825         -.50251484          0   1.838243  .60060024   2.3  -.17 -1.21 -1.02  -.49
            14675 -.18214583  -.6802678          .33669472          0  -2.527082 -.59701204  -.77  1.57   .32 -1.27   .49
            14676 -1.2773752  4.1095853          -.3355682          0 -4.8148155 -1.5015006  -2.2    .8   .94  -.62   .01
            14677   .7393718 -1.3157904          2.8619528  -.3717482  1.9455314  -2.439022   .69  -.58  -.79    .3 -1.52
            14678          0  .99999905         -1.4729977          0 -.38167834   .6250024  2.45  -.73  -.35   .68 -1.13
            14679   .5504608 -2.3102283          1.6611338    .746274 -1.9157112 -.31055808  -.43   .29   .11   .38   .06
            14682    -.91241 -.33783913         -.49019456 -.37037134   -3.90625  -.6230533 -1.48  -.78   .95  1.56    .1
            14683   .5524874 -1.0169506          .49260855  -.7434964 -3.2520294  .31348467 -2.33  -.92   1.2  2.36  -.28
            14684   1.098907  -.3424704         -1.4705896   .7490635  3.3613443          0   .83 -4.16     1  3.27  1.39
            14685  4.1666746 -.34363866            1.65838  -.3717482          0  1.8749952  4.25 -3.27    .2   .85  -.24
            14686 -.52173734 -3.4482777          1.1419296    .373137 -.81300735  -.6134987   .62   .12  -.23  -.05  -.52
            14689 -.34965277  1.4285684           .6451607          0 -2.4590135  -.6172836 -1.71 -2.56  2.24  3.18   .61
            14690    1.05263  1.0563374          2.4038434          0  1.6806602  .62111616  1.95 -2.47  -.37   .25 -1.25
            14691  .17360449 -1.7421663                  0          0  .82644224  1.5432119  1.31  2.19 -1.08 -2.18  -.57
            14692  2.2530437 -1.0638297          1.7214417  .37174225 -.81967115 -2.1276593  1.48  -.74  -.14  1.02   -.1
            14693  -.5084753  -.7168412          -.6153822          0  .41322708          0   .11   -.4    .2    .1  -.25
            14696  -.8517921   .7220149          .30959845 -.37037134  -.4115224 -1.2422383  -.39   .22  -.45  -.14   .31
            14697  2.2336721          0         -.30864477   .7434964  -.8264363  .31446218 -1.34 -1.02  1.03   .82  -.05
            14698  -1.512605 -2.1505356         -.92879534 -.73800683   2.916658  -.9404361    -1  -2.2  2.26  2.74   .93
            14699  1.0238886 -.36629438         -.15624762   .7434964 -3.6437213  .63290596 -1.76 -1.38  1.57  1.54   .74
            14700  1.0135174  1.8382311         -1.4084518  -.3690064  .42016506  -1.886791  1.19   .24   .27  -1.7   .17
            14703 -.16722083 -1.8050492          .79364777  .37037134  -5.439335 -1.2820542 -1.67 -3.26  1.71  4.52  2.11
            14704  -3.182578   2.573526         -2.2047222 -1.1070132 -4.8672557 -1.9480526  -1.2 -1.86   .12  1.67   .45
            14705  -.5190313 -1.4336884          .32205582          0 -.46511889  .99338293   .01  1.61  -.93 -1.16    .2
            14706   1.565218  -1.090908           .6420493    .746274   -.934577  .98360777  1.47  1.42  -.38 -1.59   .73
            14707   .5136967  1.1029363          2.0733595          0   8.490562 -1.9480526  1.42  1.08  -.16  -.86 -1.29
            14710  .17035007  .36363602           .9374976          0  1.7391324  -1.986754 -1.95 -2.53  1.68  3.27  1.17
            14711 -.17006397          0         -1.2383878          0  -2.136749  .33783913 -1.01 -1.74   1.9  2.08  1.23
            14712   -.170362 -3.6231816          -.6269574 -.37037134 -3.9301336 -4.7138033 -2.97 -1.79  2.53     2   1.5
            14713 -2.3890734  -.7518828          -2.208203  -.3717482 -1.3636351   4.946995  -1.8   .07   .95   .29   .72
            14714 -5.2447557 -.75757504          -5.161291          0  -8.294928          0 -6.72 -1.11  2.17  2.93   .78
            14717  -.3690064 -3.0534327                  0   -.373137 -1.5075386  .33669472  2.94  -2.5 -1.66   .25 -2.15
            14718  2.0370364  -1.968509          1.3605475          0  2.5510192 -1.6778529  3.84  2.86 -2.44 -2.95  -.92
            14719  .90743303  .40160418          1.5100718  -.7490635          0  2.0477772   -.8  1.68  -.06  -.54  1.07
            14720 -1.0791361  .40000677         -.66115856    .754714          0 -1.0033429   .09  -.92     1   .73   .41
            14724  -1.090908  1.1952162          -.6655574 -.37453175  -4.477608 -2.0270288 -1.01 -1.95   1.1  2.21  1.28
            14725   2.389717 -1.1811018          1.1725307          0  2.6041627 -2.0689666  3.74   .19   -.9 -1.96 -1.17
            14726  -.5385995 -.79681873          .33112764 -.37593842  -2.538067  3.5211325 -1.23   .59  1.13    .2   .59
            14727 -.18051267          0                  0   -.754714  -1.562506   .6802678   .76     1  -.51 -1.69 -1.22
            14728  .54249763 -1.2048185          .16502142          0   5.291009  1.0135174   .07  2.27   .07  -2.1  -.28
            14731  1.0791421  1.6260147           .4942298          0   3.517592  4.0133834   1.4  1.27 -1.03  -.91  -.42
            14732 -.17793179  5.5999994          .32787323          0  2.9126167  .96462965 -2.07  -.48  1.68  1.42   .74
            14733 -.71300864 -1.1363626         -1.4705896          0 -2.3584962  1.9108295 -2.09   .29  -.37    .6   .36
            14734  -.8976698  1.9157171           -.497514          0   1.449275          0     0   .94   .34 -1.12   .31
            14735   .7246375 -1.1278212          .33333302 -.76045394  .47619343     1.5625  1.48    .5  -.44  -.26  -.29
            14738  .71942806 -1.1406839 1.4950156000000001  -.7662833          0 -1.5384614  -.98 -1.25  1.42   .99   .85
            14739   .1785636          0          -.1636684          0 -1.4218032    -1.5625 -1.19  -.59    .6  1.54   .03
            14740 -.89126825  1.5384555         -.81967115          0 -3.8461506  -.6349206  -2.7   -.8   .93  1.38  1.09
            14741  .17986298 -1.8939376                  0   -.386101  -1.000005  1.5974402  2.08    .3   .23  -.91 -1.04
            14742  .17952919 -.77219605          -.8264482          0          0 -.62893033   .81  -.52   .36   .06  -.16
            14745  1.0752678  -.7782161          .16666651 -.19379854          0          0  2.13  -.91  -.71  -.43  -.38
            14746   .8865237  -2.352941          .99834204   .5825281   2.020204  2.8481007   1.3   .05  -.81 -1.16  -.58
            14747 -2.8119564 -1.2048185         -.16474724  .38609505  -4.950494   .9230733 -1.38   .42   .75   .29    .3
            14748  .18084049          0         -.33003092 -.38461685   .5208373   1.219511 -1.09  -.76   .86   .95   .97
            14749  -.7220268          0          -.6622493          0 -1.0362744          0 -2.39  -.01   .86   .79   .54
            14752 -1.6363621          0         -.99999905   -.386101 -3.6649227  1.2048244  -.73 -1.34   .42   .61  -.21
            14753 -.18483996   .8130193                  0          0 -2.7173877  1.7857194 -2.38  -.33   1.3  2.04   .94
            14754 -.37037134 -2.4193525          -.8417487          0  2.7933002  .29239655  1.55 -1.68  -1.2   .47 -1.05
            end
            format %td Date
            
            estimates clear
            local tosuest
            forval i=1/6{
                eststo m`i': regress m`i' MktRF SMB HML RMW CMA
                local tosuest "`tosuest' m`i'"
            }
            suest `tosuest'
            
            foreach est in MktRF SMB HML RMW CMA _cons{
                local lincom`est' _b[m1_mean:`est']
            }
            
            foreach est in MktRF SMB HML RMW CMA _cons{
                forval i=2/6{
                    local lincom`est' "`lincom`est'' +_b[m`i'_mean:`est']"
                }
            }
            est sto suest
            foreach est in MktRF SMB HML RMW CMA _cons{
                est restore suest
                eststo: margins, expression((`lincom`est'')/6) post
                
            }
            
            esttab est*, cells(b se t p) mlab(MktRF SMB HML RMW CMA _cons) coeflab(_cons " ") label
            
            *OR IN THE USUAL FORMAT WITH COEF AND T-STATS WITH STARS INDICATING SIGNIFICANCE
            
            esttab est*,  mlab(MktRF SMB HML RMW CMA _cons) coeflab(_cons " ") label  starlevels(* 0.10 ** 0.05 *** 0.01) compress
            Res.:

            Code:
            . esttab est*, cells(b se t p) mlab(MktRF SMB HML RMW CMA _cons) coeflab(_cons " ") label
            
            --------------------------------------------------------------------------------------------------
                                          (1)          (2)          (3)          (4)          (5)          (6)
                                        MktRF          SMB          HML          RMW          CMA        _cons
                                     b/se/t/p     b/se/t/p     b/se/t/p     b/se/t/p     b/se/t/p     b/se/t/p
            --------------------------------------------------------------------------------------------------
                                     .3468359     .2209361     -.048944     .1402173     .0420417    -.0509192
                                     .0790755      .125907      .147845     .1412035     .1273206     .0742135
                                     4.386134     1.754756    -.3310495     .9930156     .3302037    -.6861177
                                     .0000115      .079301     .7406071     .3207024      .741246     .4926389
            --------------------------------------------------------------------------------------------------
            Observations                  100          100          100          100          100          100
            --------------------------------------------------------------------------------------------------
            
             
            . esttab est*,  mlab(MktRF SMB HML RMW CMA _cons) coeflab(_cons " ") label  starlevels(* 0.10 ** 0.05 *** 0.01) compress
            
            ----------------------------------------------------------------------------------------------
                                   (1)          (2)          (3)          (4)          (5)          (6)  
                                 MktRF          SMB          HML          RMW          CMA        _cons  
            ----------------------------------------------------------------------------------------------
                                 0.347***     0.221*     -0.0489        0.140       0.0420      -0.0509  
                                (4.39)       (1.75)      (-0.33)       (0.99)       (0.33)      (-0.69)  
            ----------------------------------------------------------------------------------------------
            Observations           100          100          100          100          100          100  
            ----------------------------------------------------------------------------------------------
            t statistics in parentheses
            * p<0.10, ** p<0.05, *** p<0.01
            Last edited by Andrew Musau; 16 Jun 2023, 18:21.

            Comment


            • #7
              I have tried to run the updated version of the code. Indeed it fails since I have 136 dependent variables, in the sample I cut it to 6.

              It gives me the following error:
              Code:
              warning: option expression() does not contain option predict() or xb().
              warning: prediction constant over observations.
              After I have to restart Stata since it refuses to continue running any other codes due to large number of results.

              Comment


              • #8
                I have checked and suest can combine up to 300 sets of estimates. It is constrained by the limit of estimates store. Indeed, if you get to the margins part, it means that suest has combined all 136 estimates. On the warnings:

                warning: option expression() does not contain option predict() or xb().
                warning: prediction constant over
                These are not errors which cause the program to terminate. They are standard warnings issued by Stata if you use margins as lincom via the expression option. Consider:

                Code:
                sysuse auto, clear
                regress price mpg weight disp
                lincom _b[mpg]+_b[weight]+_b[displacement]
                margins, expression(_b[mpg]+_b[weight]+_b[displacement])
                Res.:

                Code:
                 
                . lincom _b[mpg]+_b[weight]+_b[displacement]
                
                 ( 1)  mpg + weight + displacement = 0
                
                ------------------------------------------------------------------------------
                       price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
                -------------+----------------------------------------------------------------
                         (1) |  -47.46103    87.2304    -0.54   0.588    -221.4366    126.5145
                ------------------------------------------------------------------------------
                
                .
                . margins, expression(_b[mpg]+_b[weight]+_b[displacement])
                warning: option expression() does not contain option predict() or xb().
                warning: prediction constant over observations.
                
                Predictive margins                                          Number of obs = 74
                Model VCE: OLS
                
                Expression: _b[mpg]+_b[weight]+_b[displacement]
                
                ------------------------------------------------------------------------------
                             |            Delta-method
                             |     Margin   std. err.      z    P>|z|     [95% conf. interval]
                -------------+----------------------------------------------------------------
                       _cons |  -47.46103    87.2304    -0.54   0.586    -218.4295    123.5074
                ------------------------------------------------------------------------------
                Now, notice that despite the warnings, both margins and lincom yield identical results. No doubt that this may take time with 136 sets of estimates, but I will advocate this over simple averaging of coefficients and standard errors. The reason is that to calculate the standard errors of the averaged coefficients, you will need to combine their variances and this is not as simple as taking an arithmetic average of the individual standard errors. So if the program terminates with an error, copy and paste the exact error. Otherwise, just give it time to combine the estimates and calculate the standard errors (e.g., leave it to run overnight).
                Last edited by Andrew Musau; 18 Jun 2023, 16:40.

                Comment


                • #9
                  Okay, I see. This code seems a little complicated, the problem is that I have to run a lot of regressions on multiple samples. Thus, the first option seems the easiest and most optimal one

                  Is it possible to add p-value and R-squared using the previous code?

                  Code:
                  clear matrix
                  forval i=1/6{
                  regress m`i' MktRF SMB HML RMW CMA
                  if `i'==1{
                  mat wanted= r(table)[1..3, 1...]
                  }
                  if inrange(`i', 2, 6){
                  mat wanted= wanted+ r(table)[1..3, 1...]
                  }
                  }
                  mat wanted= wanted/6
                  mat l wanted
                  Last edited by Liuda Fedosenko; 19 Jun 2023, 08:49.

                  Comment


                  • #10
                    Just as long as you understand that averaging the standard errors does not lead to the correct standard errors for the averaged coefficients, the z-statistic is calculated as $$z=\frac{\text{Coefficient}}{\text{Std. Error}}$$ and the p-value is calculated as \(2\times \text{normal(-abs(z))}\).

                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input int Date double(m1 m2 m3 m4 m5 m6 MktRF SMB HML RMW CMA)
                    14612 -.37243962  1.3201356          1.1764765   -.386101  2.2058725 -1.4619887  -.71  -.06 -1.42  -1.5  -.63
                    14613 -1.3084114 -1.6286612         -1.1627913 -.77519417 -5.0359607  -6.231451 -4.06   .33  2.06   .47  1.45
                    14614 -.75757504  -.6622553          -1.512605    .390625  1.5151501          0  -.09   .33   .16   .41  1.11
                    14615          0 -.33332705          -.8532405  .38909912 -2.9850721  1.2658238  -.73  -.04  1.26   .65  1.21
                    14616  .38167238          0          .34422874  1.9379854  3.8461566  1.8749952  3.21  -.93 -1.42  -.88  -.96
                    14619          0  .66889524           3.087473          0  1.8518567   .6134987  1.76    .5 -1.53 -1.98  -.24
                    14620  1.1406898  .66444874          1.1647224 -.38022995  -3.272736 -4.5731726 -1.71   .34   .81   .81  1.08
                    14621 -.18796921 -.66006184         -1.3157904   .3816843 -1.1278093 -1.5974462  -.69  -.22   .78   .42   .95
                    14622  .37665367          0         -.33333302          0  .38022995  3.5714269  1.59   .49  -.91 -1.77 -1.04
                    14623  .18761158 -1.6611278          3.8461566          0          0   .9404421  1.15   .26  -.65  -.31  -.62
                    14627  .93632936  2.0270228          .80515146  .38022995          0  1.8633485  -.26   2.3  -.49 -1.48  -.12
                    14628    .927639  -.6622553           .6389737 -.37878752  -3.030312   2.439022   .44   .64  -.37 -1.11   .47
                    14629 -1.4705837 -1.3333261          -5.079365  .38022995  -2.343744   2.976191  -.37  1.73 -1.25 -1.07  -.59
                    14630   .9328246 -1.3513565          .33445358  .75757504  -.4000008 -.28901696   .23  1.24  -.52 -1.31    .4
                    14633          0 -1.7123282          .16666651 -.37593842          0 -1.1594176 -2.59   1.4   .76   .49    .9
                    14634 -.92421174          0         -1.3311148    .377357 -1.2048244  .87976456   .49  -.77   -.1  -.59  -.62
                    14635  .55969954  -2.787459         -.16863346  .37593842          0   .8720875  -.44   .31   .09   .35   .96
                    14636   .5565882  2.8673887                  0          0   1.219511 -.86455345  -.44  -.23  -.68   .15  -.01
                    14637 -2.2140205 -2.0905912         -2.1959484  .37453175          0 -2.9069781 -2.82    .2  1.63     1   .39
                    14640 -.37736297  -.3558755         -1.2089789   -.373137  1.6064167          0   1.5 -3.35  -.43   .75   .44
                    14641  .56818724 -1.0714293         -.34965277   .7490635 -1.1857629   .5988002  1.29  -.06   -.1  -.39 -1.11
                    14642 -.56497455   .7220149          .87718964  -.7434964   4.400003          0   .16  1.23  -.22 -1.06   .12
                    14643  .94697475  1.0752678           1.565218  1.1235952  .76628923  1.1904716  1.49   .94 -1.29  -.98  -.67
                    14644  .75047016  11.702132           .8561611          0  .38022995  .29411316  -.05  1.17  -.62  -.32  -.16
                    14647          0  .95238686           .5093336          0  1.1363506 -.29325485   .35  1.28  -1.2  -1.1  -.11
                    14648  1.6759753 -.31446218         -.33783913  .37037134  2.2471905  .58823824  1.23   .08 -1.73 -1.58  -.82
                    14649  -.7326007  2.8391123         -.33898354          0          0   .5847931 -1.83  1.91  -.41  -.36   .28
                    14650          0  1.8404961           .8503437  .36900043  1.8314958   .8720875   .57   .81  -.57 -1.84  -.43
                    14651 -2.0295203  -2.409643         -1.3490736  .36764145 -1.4388442  .86455345 -1.74  1.25   .94  -.61   .43
                    14654 -.37665367 -1.2345612           1.025641          0 -2.9197156  -.5714297   .24   .75  -.16   .09   -.5
                    14655          0  -.3125012         -.84602237  -.7326007 -3.3834577 -1.1494279    .7  -.75   .04  -.05   .89
                    14656 -1.5122831  -.6269574          -.1706481          0  .38911104  2.0348787  -.58  1.94  -.57 -2.11   .76
                    14657  .95968246 -.31545758          .68376064  -.3690064  .77519417 -2.5641024   .46  1.78 -1.26 -2.24  -.16
                    14658 -1.5209079  1.5822768          .33955574 -.37037134 -2.3076952 -4.9707594 -2.69  1.23   .49  -.46   .35
                    14662  .96524954   -.934577            .507617  .37174225  -1.968497   .3076911   .11 -1.08   .38   .47   .35
                    14663  .57361126  -1.886791         -.33670068 -.37037134   2.409637  2.7607322  1.24   .32 -1.45 -1.04  -1.2
                    14664  .19011497 -2.5641024          .33783913  .18587112  -2.745092 -.29850602   .01   .82  -.55  -.83 -1.22
                    14665  .37950277 -1.9736826         -.33670068 -.18553138  1.2096763          0 -1.04  1.83  -.14  -.87   .24
                    14668 -.75613856  -.6711364         -.67567825          0 -2.7888536 -.89820623   .76   -.2  -.06   .17   .65
                    14669  .95238686 -.67567825           .6802678          0  1.2295127   .3021121  1.96  1.47   -.1  -1.8   .77
                    14670  .18867254  1.0204077                  0  -.3717482  6.4777374   .6024122  1.39   .84  -.35 -1.33  -.15
                    14671   2.448213 -.33669472           .8445978    .373137  3.4220457  -.2994001  -.09  -.04   .62   .88   .62
                    14672  .91911554 -.67567825         -.50251484          0   1.838243  .60060024   2.3  -.17 -1.21 -1.02  -.49
                    14675 -.18214583  -.6802678          .33669472          0  -2.527082 -.59701204  -.77  1.57   .32 -1.27   .49
                    14676 -1.2773752  4.1095853          -.3355682          0 -4.8148155 -1.5015006  -2.2    .8   .94  -.62   .01
                    14677   .7393718 -1.3157904          2.8619528  -.3717482  1.9455314  -2.439022   .69  -.58  -.79    .3 -1.52
                    14678          0  .99999905         -1.4729977          0 -.38167834   .6250024  2.45  -.73  -.35   .68 -1.13
                    14679   .5504608 -2.3102283          1.6611338    .746274 -1.9157112 -.31055808  -.43   .29   .11   .38   .06
                    14682    -.91241 -.33783913         -.49019456 -.37037134   -3.90625  -.6230533 -1.48  -.78   .95  1.56    .1
                    14683   .5524874 -1.0169506          .49260855  -.7434964 -3.2520294  .31348467 -2.33  -.92   1.2  2.36  -.28
                    14684   1.098907  -.3424704         -1.4705896   .7490635  3.3613443          0   .83 -4.16     1  3.27  1.39
                    14685  4.1666746 -.34363866            1.65838  -.3717482          0  1.8749952  4.25 -3.27    .2   .85  -.24
                    14686 -.52173734 -3.4482777          1.1419296    .373137 -.81300735  -.6134987   .62   .12  -.23  -.05  -.52
                    14689 -.34965277  1.4285684           .6451607          0 -2.4590135  -.6172836 -1.71 -2.56  2.24  3.18   .61
                    14690    1.05263  1.0563374          2.4038434          0  1.6806602  .62111616  1.95 -2.47  -.37   .25 -1.25
                    14691  .17360449 -1.7421663                  0          0  .82644224  1.5432119  1.31  2.19 -1.08 -2.18  -.57
                    14692  2.2530437 -1.0638297          1.7214417  .37174225 -.81967115 -2.1276593  1.48  -.74  -.14  1.02   -.1
                    14693  -.5084753  -.7168412          -.6153822          0  .41322708          0   .11   -.4    .2    .1  -.25
                    14696  -.8517921   .7220149          .30959845 -.37037134  -.4115224 -1.2422383  -.39   .22  -.45  -.14   .31
                    14697  2.2336721          0         -.30864477   .7434964  -.8264363  .31446218 -1.34 -1.02  1.03   .82  -.05
                    14698  -1.512605 -2.1505356         -.92879534 -.73800683   2.916658  -.9404361    -1  -2.2  2.26  2.74   .93
                    14699  1.0238886 -.36629438         -.15624762   .7434964 -3.6437213  .63290596 -1.76 -1.38  1.57  1.54   .74
                    14700  1.0135174  1.8382311         -1.4084518  -.3690064  .42016506  -1.886791  1.19   .24   .27  -1.7   .17
                    14703 -.16722083 -1.8050492          .79364777  .37037134  -5.439335 -1.2820542 -1.67 -3.26  1.71  4.52  2.11
                    14704  -3.182578   2.573526         -2.2047222 -1.1070132 -4.8672557 -1.9480526  -1.2 -1.86   .12  1.67   .45
                    14705  -.5190313 -1.4336884          .32205582          0 -.46511889  .99338293   .01  1.61  -.93 -1.16    .2
                    14706   1.565218  -1.090908           .6420493    .746274   -.934577  .98360777  1.47  1.42  -.38 -1.59   .73
                    14707   .5136967  1.1029363          2.0733595          0   8.490562 -1.9480526  1.42  1.08  -.16  -.86 -1.29
                    14710  .17035007  .36363602           .9374976          0  1.7391324  -1.986754 -1.95 -2.53  1.68  3.27  1.17
                    14711 -.17006397          0         -1.2383878          0  -2.136749  .33783913 -1.01 -1.74   1.9  2.08  1.23
                    14712   -.170362 -3.6231816          -.6269574 -.37037134 -3.9301336 -4.7138033 -2.97 -1.79  2.53     2   1.5
                    14713 -2.3890734  -.7518828          -2.208203  -.3717482 -1.3636351   4.946995  -1.8   .07   .95   .29   .72
                    14714 -5.2447557 -.75757504          -5.161291          0  -8.294928          0 -6.72 -1.11  2.17  2.93   .78
                    14717  -.3690064 -3.0534327                  0   -.373137 -1.5075386  .33669472  2.94  -2.5 -1.66   .25 -2.15
                    14718  2.0370364  -1.968509          1.3605475          0  2.5510192 -1.6778529  3.84  2.86 -2.44 -2.95  -.92
                    14719  .90743303  .40160418          1.5100718  -.7490635          0  2.0477772   -.8  1.68  -.06  -.54  1.07
                    14720 -1.0791361  .40000677         -.66115856    .754714          0 -1.0033429   .09  -.92     1   .73   .41
                    14724  -1.090908  1.1952162          -.6655574 -.37453175  -4.477608 -2.0270288 -1.01 -1.95   1.1  2.21  1.28
                    14725   2.389717 -1.1811018          1.1725307          0  2.6041627 -2.0689666  3.74   .19   -.9 -1.96 -1.17
                    14726  -.5385995 -.79681873          .33112764 -.37593842  -2.538067  3.5211325 -1.23   .59  1.13    .2   .59
                    14727 -.18051267          0                  0   -.754714  -1.562506   .6802678   .76     1  -.51 -1.69 -1.22
                    14728  .54249763 -1.2048185          .16502142          0   5.291009  1.0135174   .07  2.27   .07  -2.1  -.28
                    14731  1.0791421  1.6260147           .4942298          0   3.517592  4.0133834   1.4  1.27 -1.03  -.91  -.42
                    14732 -.17793179  5.5999994          .32787323          0  2.9126167  .96462965 -2.07  -.48  1.68  1.42   .74
                    14733 -.71300864 -1.1363626         -1.4705896          0 -2.3584962  1.9108295 -2.09   .29  -.37    .6   .36
                    14734  -.8976698  1.9157171           -.497514          0   1.449275          0     0   .94   .34 -1.12   .31
                    14735   .7246375 -1.1278212          .33333302 -.76045394  .47619343     1.5625  1.48    .5  -.44  -.26  -.29
                    14738  .71942806 -1.1406839 1.4950156000000001  -.7662833          0 -1.5384614  -.98 -1.25  1.42   .99   .85
                    14739   .1785636          0          -.1636684          0 -1.4218032    -1.5625 -1.19  -.59    .6  1.54   .03
                    14740 -.89126825  1.5384555         -.81967115          0 -3.8461506  -.6349206  -2.7   -.8   .93  1.38  1.09
                    14741  .17986298 -1.8939376                  0   -.386101  -1.000005  1.5974402  2.08    .3   .23  -.91 -1.04
                    14742  .17952919 -.77219605          -.8264482          0          0 -.62893033   .81  -.52   .36   .06  -.16
                    14745  1.0752678  -.7782161          .16666651 -.19379854          0          0  2.13  -.91  -.71  -.43  -.38
                    14746   .8865237  -2.352941          .99834204   .5825281   2.020204  2.8481007   1.3   .05  -.81 -1.16  -.58
                    14747 -2.8119564 -1.2048185         -.16474724  .38609505  -4.950494   .9230733 -1.38   .42   .75   .29    .3
                    14748  .18084049          0         -.33003092 -.38461685   .5208373   1.219511 -1.09  -.76   .86   .95   .97
                    14749  -.7220268          0          -.6622493          0 -1.0362744          0 -2.39  -.01   .86   .79   .54
                    14752 -1.6363621          0         -.99999905   -.386101 -3.6649227  1.2048244  -.73 -1.34   .42   .61  -.21
                    14753 -.18483996   .8130193                  0          0 -2.7173877  1.7857194 -2.38  -.33   1.3  2.04   .94
                    14754 -.37037134 -2.4193525          -.8417487          0  2.7933002  .29239655  1.55 -1.68  -1.2   .47 -1.05
                    end
                    format %td Date
                    
                    local R2
                    clear matrix
                    forval i=1/6{
                        regress m`i' MktRF SMB HML RMW CMA
                        if `i'==1{
                            mat wanted= r(table)[1..2, 1...]
                            local R2= e(r2)
                        }
                        if inrange(`i', 2, 6){
                            mat wanted= wanted+ r(table)[1..2, 1...]
                            local R2= `R2'+e(r2)
                        }
                    }
                    mat wanted= wanted/6
                    local R2= `R2'/6
                    mat l wanted
                    
                    clear
                    local colnames= ustrregexra("`:colnames wanted'", "\b_cons\b", "cons")
                    mat colnames wanted= `colnames'
                    svmat wanted, names(col)
                    set obs 4
                    gen which="", before(MktRF)
                    gen R2=`R2', before(which)
                    replace which= cond(_n==1, "Coefficient", cond(_n==2, "Std. Error", cond(_n==3, "z-statistic", "p-value")))
                    foreach var of varlist MktRF SMB HML RMW CMA cons{
                        replace `var'= `var'[1]/`var'[2] in 3
                        replace `var'=2*normal(-abs(`var'[3])) in 4
                    }
                    Code:
                    . l
                    
                         +--------------------------------------------------------------------------------------------+
                         |       R2         which      MktRF        SMB         HML        RMW        CMA        cons |
                         |--------------------------------------------------------------------------------------------|
                      1. | .1896801   Coefficient   .3468359   .2209361    -.048944   .1402173   .0420417   -.0509192 |
                      2. | .1896801    Std. Error   .1516439   .2291334    .2903826   .2774704   .2678931    .1472409 |
                      3. | .1896801   z-statistic   2.287174   .9642248   -.1685501   .5053415   .1569347   -.3458224 |
                      4. | .1896801       p-value   .0221857   .3349332    .8661505    .613319   .8752964    .7294762 |
                         +--------------------------------------------------------------------------------------------+
                    Last edited by Andrew Musau; 19 Jun 2023, 20:22.

                    Comment


                    • #11
                      Alright, thank you so much!

                      Comment

                      Working...
                      X