Announcement

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

  • Loop regression: store constants to calculate median

    Hi,

    I have the following code and would like to store the constants being produced from the loop regression so that I can calculate the median. Any help?

    Code:
    encode GVKEY, gen(GVKEY_num)
    estimates clear
    levelsof GVKEY_num, local(groups)
    
    local start_group1 1
    local end_group1 88
    
    scalar sum_alpha1 = 0  // Initialize a sum variable for alpha1
    scalar count_alpha1 = 0  // Initialize a count variable for alpha1
    
    foreach group of local groups {
        if `group' >= `start_group1' & `group' <= `end_group1' {
            regress dealexcessreturns marketexcessreturns if GVKEY_num == `group', robust
            estimates store capm_`group'
            scalar alpha1 = _b[_cons] - (rf + benchmarkyrret * marketexcessreturns)
            display "`group' alpha1: " alpha1
            
            // Update the sum and count for alpha1
            scalar sum_alpha1 = sum_alpha1 + alpha1
            scalar count_alpha1 = count_alpha1 + 1
        }
    }

  • #2
    Code:
    . // reset the frames
    . frames reset
    
    .
    . // open example data
    . sysuse nlsw88, clear
    (NLSW, 1988 extract)
    
    .
    . // We are going to overwrite the data with the coefficients
    . // So doing it in a separate frame allows us to keep the original data
    . frame copy default result
    
    . frame change result
    
    .
    . // some categories are too small
    . numlabel occlbl, add
    
    . tab occupation
    
                   Occupation |      Freq.     Percent        Cum.
    --------------------------+-----------------------------------
    1. Professional/Technical |        317       14.17       14.17
            2. Managers/Admin |        264       11.80       25.97
                     3. Sales |        726       32.45       58.43
        4. Clerical/Unskilled |        102        4.56       62.99
                 5. Craftsmen |         53        2.37       65.36
                6. Operatives |        246       11.00       76.35
                 7. Transport |         28        1.25       77.60
                  8. Laborers |        286       12.78       90.39
                   9. Farmers |          1        0.04       90.43
            10. Farm laborers |          9        0.40       90.84
                  11. Service |         16        0.72       91.55
        12. Household workers |          2        0.09       91.64
                    13. Other |        187        8.36      100.00
    --------------------------+-----------------------------------
                        Total |      2,237      100.00
    
    . recode occupation (9=10) (12=13)
    (3 changes made to occupation)
    
    .
    . // estimate the regression for each occupation and store the coefficients and
    . // standard errors
    . statsby _b _se , by(occupation) clear : regress wage i.south, vce(robust)
    (running regress on estimation sample)
    
          Command: regress wage i.south, vce(robust)
               By: occupation
    
    Statsby groups
    ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5
    ...........
    
    .
    . // find the median
    . sum _b_cons, detail
    
                              _b[_cons]
    -------------------------------------------------------------
          Percentiles      Smallest
     1%     3.519447       3.519447
     5%     3.519447       3.744918
    10%     3.744918       5.535785       Obs                  11
    25%     5.535785       6.570047       Sum of wgt.          11
    
    50%     6.942338                      Mean           7.323996
                            Largest       Std. dev.      2.667199
    75%     9.384748       7.760388
    90%     11.01399       9.384748       Variance        7.11395
    95%     11.93449       11.01399       Skewness       .2739177
    99%     11.93449       11.93449       Kurtosis       2.260087
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment

    Working...
    X