Announcement

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

  • Help with saving the coefficients and confidence intervals of only one variable in multiple regressions

    Dear all,

    I am running -qreg- for 9 deciles separately, and I want to create 3 variables containing the coefficients and confidence intervals of only the educ variable from the nine separate regressions.

    The reason I am running them separately is because -sqreg- does not allow pweights.

    I am aware of e(b), but that contains coefficients from all variables and I am only interested in one.

    Code:
    use http://fmwww.bc.edu/RePEc/bocode/o/oaxaca.dta, clear
    global reg lnwage educ exper tenure age agesq
    qreg $reg [pweight=wt], quantile(0.10) vce(robust,)
    qreg $reg [pweight=wt], quantile(0.20) vce(robust,)
    qreg $reg [pweight=wt], quantile(0.30) vce(robust,)
    qreg $reg [pweight=wt], quantile(0.40) vce(robust,)
    qreg $reg [pweight=wt], quantile(0.50) vce(robust,)
    qreg $reg [pweight=wt], quantile(0.60) vce(robust,)
    qreg $reg [pweight=wt], quantile(0.70) vce(robust,)
    qreg $reg [pweight=wt], quantile(0.80) vce(robust,)
    qreg $reg [pweight=wt], quantile(0.90) vce(robust,)
    What I am aiming for is something like this:
    quantile educoeff edulow eduup
    0.1 .0844962 .0605507 .1084416
    0.2 .0842375 .0718671 .096608
    0.3 .0753538 .0619078 .0887999
    and so on...

    I saw this thread here, but I am still not very clear about what to do (never did loops before):
    https://www.econjobrumors.com/topic/...iable-in-stata

    I can copy and paste from the output, but was wondering if there is a more efficient way to do this.

    Thanks in advance!
    Last edited by Alina Faruk; 22 Jun 2019, 09:56.

  • #2
    Hi Alina
    That is relatively easy:
    Code:
    clear all
    use http://fmwww.bc.edu/RePEc/bocode/o/oaxaca.dta, clear
    global reg lnwage educ exper tenure age agesq
    
    forvalues i=10(10)90 {
    local qq=`i'/100
    qreg $reg [pweight=wt], quantile(`qq') vce(robust,) nolog
    matrix tbl=r(table)
    matrix myqr=nullmat(myqr)\[`qq',tbl["b","educ"],tbl["ll","educ"],tbl["ul","educ"]]
    }
    matrix colname myqr = q beta ul ll
    matrix list myqr
    Hope it helps
    Fernando

    Comment


    • #3
      The following seems to do what you want, with the caveat that my qreg results do not seem to match the ones you show in your example of what you are aiming for.
      Code:
      use http://fmwww.bc.edu/RePEc/bocode/o/oaxaca.dta, clear
      global reg lnwage educ exper tenure age agesq
      postutil clear
      postfile table quantile educoef edulow eduup using results, replace
      foreach q of numlist .01 .02 .03 .04 .05 .06 .07 .08 .09 {
          quietly qreg $reg [pweight=wt], quantile(`q') vce(robust,)
          matrix table = r(table)
          post table (`q') (table[rownumb(table,"b"),colnumb(table,"educ")]) ///
                           (table[rownumb(table,"ll"),colnumb(table,"educ")]) ///
                           (table[rownumb(table,"ul"),colnumb(table,"educ")]) 
          }
      postclose table
      use results, clear
      list, clean noobs
      Code:
      . list, clean noobs 
      
          quantile    educoef      edulow      eduup  
               .01   .0998657    .0009773   .1987542  
               .02   .0852669    .0198226   .1507112  
               .03   .0704674   -.0047377   .1456725  
               .04   .0752415    .0268348   .1236483  
               .05   .0748492    .0354176   .1142807  
               .06   .0710218     .044709   .0973346  
               .07   .0760128    .0491715   .1028542  
               .08   .0716216    .0474834   .0957597  
               .09   .0830426    .0603374   .1057478
      There are a few keys to understanding this code:

      1) The programming command postfile as described in the output of help postfile, which allows you to easily build a Stata dataset from the results of your models

      2) The r(table) matrix returned by most estimation commands, whose documentation is cannily hidden in the Stata Programming Reference Manual PDF included with your Stata installation and accessible through Stata's Help menu.

      3) Use of Stata's matrix (not Mata!) commands as described in the output of help matrix and its links.

      Comment


      • #4
        Originally posted by FernandoRios View Post
        Hi Alina
        That is relatively easy:
        Code:
        clear all
        use http://fmwww.bc.edu/RePEc/bocode/o/oaxaca.dta, clear
        global reg lnwage educ exper tenure age agesq
        
        forvalues i=10(10)90 {
        local qq=`i'/100
        qreg $reg [pweight=wt], quantile(`qq') vce(robust,) nolog
        matrix tbl=r(table)
        matrix myqr=nullmat(myqr)\[`qq',tbl["b","educ"],tbl["ll","educ"],tbl["ul","educ"]]
        }
        matrix colname myqr = q beta ul ll
        matrix list myqr
        Hope it helps
        Fernando
        Hello, this worked perfectly. Thank you so much!

        Just a small correction in line 10:
        matrix colname myqr = q beta ll ul
        Last edited by Alina Faruk; 22 Jun 2019, 14:55.

        Comment


        • #5
          Originally posted by William Lisowski View Post
          The following seems to do what you want, with the caveat that my qreg results do not seem to match the ones you show in your example of what you are aiming for.
          Code:
          use http://fmwww.bc.edu/RePEc/bocode/o/oaxaca.dta, clear
          global reg lnwage educ exper tenure age agesq
          postutil clear
          postfile table quantile educoef edulow eduup using results, replace
          foreach q of numlist .01 .02 .03 .04 .05 .06 .07 .08 .09 {
          quietly qreg $reg [pweight=wt], quantile(`q') vce(robust,)
          matrix table = r(table)
          post table (`q') (table[rownumb(table,"b"),colnumb(table,"educ")]) ///
          (table[rownumb(table,"ll"),colnumb(table,"educ")]) ///
          (table[rownumb(table,"ul"),colnumb(table,"educ")])
          }
          postclose table
          use results, clear
          list, clean noobs
          Code:
          . list, clean noobs
          
          quantile educoef edulow eduup
          .01 .0998657 .0009773 .1987542
          .02 .0852669 .0198226 .1507112
          .03 .0704674 -.0047377 .1456725
          .04 .0752415 .0268348 .1236483
          .05 .0748492 .0354176 .1142807
          .06 .0710218 .044709 .0973346
          .07 .0760128 .0491715 .1028542
          .08 .0716216 .0474834 .0957597
          .09 .0830426 .0603374 .1057478
          There are a few keys to understanding this code:

          1) The programming command postfile as described in the output of help postfile, which allows you to easily build a Stata dataset from the results of your models

          2) The r(table) matrix returned by most estimation commands, whose documentation is cannily hidden in the Stata Programming Reference Manual PDF included with your Stata installation and accessible through Stata's Help menu.

          3) Use of Stata's matrix (not Mata!) commands as described in the output of help matrix and its links.
          Hello, I learned a lot of new things from this. Thanks so much!

          And the reason our results were differing was because the 5th line should be:
          foreach q of numlist .10 .20 .30 .40 .50 .60 .70 .80 .90 {

          But I am wondering why my original dataset vanishes after running these commands. I was hoping to add the variables to the working dataset.

          Comment


          • #6
            But I am wondering why my original dataset vanishes after running these commands.
            It doesn't vanish, it is replaced in memory when my example code does
            Code:
            use results, clear
            That is, you now have two datasets, the original one, whatever its name may be, which still where it was when you used it or imported it or whatever, and the results dataset named results.dta.

            Comment

            Working...
            X