Announcement

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

  • Storing results after sort

    Hi there,

    I'm using Stat but i'm new to this software and i have some issues with storing data and then exporting it. Just to create a context, I have sorted by a variable, then i have to repat the same regression for each value of this variable. So I used

    sort Tgt_sect
    by Tgt_sect: reg ...

    Now i want to store all the results in one table, but i can't find the right way.

    Thanks to everyone that will help

  • #2
    One way to do that is to use the statsby prefix. See the example below:
    Code:
    // store the coefficients in a file
    sysuse nlsw88, clear
    statsby _b _se df_r=e(df_r), by(industry) : ///
     reg wage ttl_exp grade
    Here is one way you could use that new dataset (This requires the labmask command written by Nick Cox. To install it type in Stata search labmask and follow the links from there):
    Code:
    // create confidence intervals
     gen lb = _b_grade - invttail(_eq2_df_r, 0.025)*_se_grade
    gen ub = _b_grade + invttail(_eq2_df_r, 0.025)*_se_grade
     
     
    // create a new industry variable sorted by the effect of grade
    egen order = rank(-_b_grade), unique
    labmask order, val(industry) decode
    
    // make a graph
    twoway rspike lb ub order, horizontal || ///
           scatter order _b_grade,           ///
           ylab(1/12, val angle(0) grid)     ///
           ytitle("") xline(0)               ///
           scheme(s1color)  legend(off)      ///
           title("Effect of years of education on hourly wage" ///
                 "with 95% confidence interval")
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	80.7 KB
ID:	1681411
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      You cannot save results after executing a command -by group-.
      Another, and slower way compared to what Maarten proposed is to do it with a loop. The loop looks more or less like this:

      Code:
      gen b = .
      gen se = .
      
      levelsof Tgt_sect, local(tgtlevs)
      
      foreach i of local tgtlevs {
      
      reg y x if Tgt_sect ==`i'
      
      replace b = _b[x] if Tgt_sect ==`i'
      replace se = _se[x] if Tgt_sect ==`i'
      
      }
      and then you will have the results stored as new variables.

      Comment


      • #4
        The advantage of what Maarten proposed is that it is faster. The disadvantage is that it collapses your dataset into one observation per -by groupvariable- value, so you might have to merge this back to your original dataset.

        Comment

        Working...
        X