Announcement

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

  • Graph using e(.) results.

    Dear All, I use (please ssc install) -xtqreg- command
    Code:
    webuse grunfeld, clear
    xtqreg invest mvalue kstock, q(0.1(0.1)0.9)
    *ereturn list
    forvalues q = 1(1)9 { 
      matrix list e(b_`q')
      matrix list e(V_`q')
    }
    and obtain results as
    Code:
    . xtqreg invest mvalue kstock, q(0.1(0.1)0.9)
    
    
    
                                  MM-QR regression results
    Number of obs = 200
    .1 Quantile regression
    ------------------------------------------------------------------------------
                 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          mvalue |    .084099    .029583     2.84   0.004     .0261174    .1420805
          kstock |   .2646024   .0552066     4.79   0.000     .1563994    .3728054
    ------------------------------------------------------------------------------
    
    .2 Quantile regression
    ------------------------------------------------------------------------------
                 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          mvalue |   .0915338   .0237915     3.85   0.000     .0449033    .1381644
          kstock |   .2775904   .0444176     6.25   0.000     .1905334    .3646474
    ------------------------------------------------------------------------------
    
    .3 Quantile regression
    ------------------------------------------------------------------------------
                 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          mvalue |   .0978504   .0224984     4.35   0.000     .0537543    .1419465
          kstock |   .2886248     .04201     6.87   0.000     .2062867    .3709629
    ------------------------------------------------------------------------------
    
    .4 Quantile regression
    ------------------------------------------------------------------------------
                 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          mvalue |   .1031741   .0244971     4.21   0.000     .0551607    .1511875
          kstock |   .2979249   .0457377     6.51   0.000     .2082807    .3875691
    ------------------------------------------------------------------------------
    
    .5 Quantile regression
    ------------------------------------------------------------------------------
                 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          mvalue |    .112539   .0329018     3.42   0.001     .0480526    .1770254
          kstock |   .3142844   .0614081     5.12   0.000     .1939268     .434642
    ------------------------------------------------------------------------------
    
    .6 Quantile regression
    ------------------------------------------------------------------------------
                 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          mvalue |   .1164492   .0373915     3.11   0.002     .0431631    .1897353
          kstock |   .3211153   .0697806     4.60   0.000     .1843478    .4578827
    ------------------------------------------------------------------------------
    
    .7 Quantile regression
    ------------------------------------------------------------------------------
                 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          mvalue |   .1249394   .0484885     2.58   0.010     .0299037    .2199751
          kstock |   .3359468   .0904895     3.71   0.000     .1585906    .5133029
    ------------------------------------------------------------------------------
    
    .8 Quantile regression
    ------------------------------------------------------------------------------
                 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          mvalue |   .1335321   .0603947     2.21   0.027     .0151606    .2519036
          kstock |   .3509575   .1127063     3.11   0.002     .1300572    .5718578
    ------------------------------------------------------------------------------
    
    .9 Quantile regression
    ------------------------------------------------------------------------------
                 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          mvalue |    .149272   .0832815     1.79   0.073    -.0139567    .3125008
          kstock |   .3784536   .1554208     2.44   0.015     .0738345    .6830727
    ------------------------------------------------------------------------------
    
    
    . *ereturn list
    . forvalues q = 1(1)9 { 
      2.   matrix list e(b_`q')
      3.   matrix list e(V_`q')
      4. }
    
    e(b_1)[1,2]
           mvalue     kstock
    y1  .08409896  .26460241
    
    symmetric e(V_1)[2,2]
                mvalue      kstock
    mvalue   .00087515
    kstock  -6.865e-06   .00304777
    
    e(b_2)[1,2]
           mvalue     kstock
    y1  .09153382  .27759041
    
    symmetric e(V_2)[2,2]
               mvalue     kstock
    mvalue  .00056604
    kstock  5.352e-06  .00197293
    
    e(b_3)[1,2]
           mvalue     kstock
    y1  .09785037  .28862482
    
    symmetric e(V_3)[2,2]
               mvalue     kstock
    mvalue  .00050618
    kstock  .00001066  .00176484
    
    e(b_4)[1,2]
           mvalue     kstock
    y1  .10317409  .29792486
    
    symmetric e(V_4)[2,2]
               mvalue     kstock
    mvalue  .00060011
    kstock  .00001112  .00209193
    
    e(b_5)[1,2]
           mvalue     kstock
    y1  .11253896  .31428441
    
    symmetric e(V_5)[2,2]
                mvalue      kstock
    mvalue   .00108253
    kstock  -3.224e-06   .00377095
    
    e(b_6)[1,2]
           mvalue     kstock
    y1  .11644922  .32111525
    
    symmetric e(V_6)[2,2]
                mvalue      kstock
    mvalue   .00139813
    kstock  -.00002525   .00486933
    
    e(b_7)[1,2]
           mvalue     kstock
    y1  .12493937  .33594676
    
    symmetric e(V_7)[2,2]
                mvalue      kstock
    mvalue   .00235113
    kstock  -.00003722   .00818835
    
    e(b_8)[1,2]
           mvalue     kstock
    y1  .13353212  .35095748
    
    symmetric e(V_8)[2,2]
                mvalue      kstock
    mvalue   .00364752
    kstock  -.00007772   .01270271
    
    e(b_9)[1,2]
           mvalue     kstock
    y1  .14927204  .37845363
    
    symmetric e(V_9)[2,2]
                mvalue      kstock
    mvalue   .00693581
    kstock  -.00013575   .02415561
    How can I use e(b) to draw a line across different quantile (0.1 to 0.9), along with 95% confidence intervals (approximately 2 standard errors) from e(V)?
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    This will get you starting by exporting the coefficients and variance estimates in the different matrices to a Stata data set. Your task is not difficult, just time-consuming, so your best bet is to invest the time yourself to complete it.

    Code:
    webuse grunfeld, clear
    xtqreg invest mvalue kstock, q(0.1(0.1)0.9)
    mat R= J(9, 4, .)
    local i=1
    forval j = 1/9{
    mat b= e(b_`j')
    mat V=e(V_`j')
    matrix R[`i',1]= b[1,1]
    matrix R[`i',2]= b[1,2]
    matrix R[`i',3]= V[1,1]
    matrix R[`i',4]= V[2,2]
    local ++i
    }
     
    forvalues i=1/9{
    local rnames  "`rnames' quantile`i'"
    }
    local cnames  "b_mvalue b_kstock V_mvalue V_kstock"
    mat rownames R= `rnames'
    mat colnames R= `cnames'
    mat list R
    
    svmat double R, names(col)
    Matrix of coefficients and variances

    Code:
    . mat list R
    
    R[9,4]
                b_mvalue   b_kstock   V_mvalue   V_kstock
    quantile1  .08409896  .26460241  .00087515  .00304777
    quantile2  .09153382  .27759041  .00056604  .00197293
    quantile3  .09785037  .28862482  .00050618  .00176484
    quantile4  .10317409  .29792486  .00060011  .00209193
    quantile5  .11253896  .31428441  .00108253  .00377095
    quantile6  .11644922  .32111525  .00139813  .00486933
    quantile7  .12493937  .33594676  .00235113  .00818835
    quantile8  .13353212  .35095748  .00364752  .01270271
    quantile9  .14927204  .37845363  .00693581  .02415561

    Comment


    • #3
      My first instinct is to use -coefplot- (by Ben Jann, available on the SSC website), which is capable of plotting from a matrix. The complication here is that the matrix of results that Andrew compiled doesn't have the values of the confidence interval bounds in it. I know you can calculate those from the variance, but it does strike me that each time -xtqreg- runs, it outputs to r(table) - for those who don't know, r(table) is a list of regression results inlcuding betas, their standard errors, the Z score, p-value, and lower and upper 95% CI bounds. It essentially feeds what you see in the command window. However, when you run the -xtqreg- syntax that River typed, r(table) only has the 90th percentile results and none of the preceding ones. Here's one way I can think of to do things:

      Code:
      ssc install coefplot
      webuse grunfeld, clear
      tempname a b
      forvalues quantile = 0.1(0.1)0.9 {
          xtqreg invest mvalue kstock, q(`quantile')
          matrix `a' = r(table)
          matrix `b' = `a'[1,1] \ `a'[5..6,1]
          matrix colnames `b' = "q`quantile'"
          matrix result1 = nullmat(result1), `b'
          }
      coefplot matrix(result1[1]), ci((2 3)) vertical recast(connected)
      The downsides are that this will require manual operation for each covariate you want to plot. Also, for some unknown reason, the names for the 80th and 90th percentiles are messed up (on my computer, they render as q.8999999999 and q.9999999999999, although you could rename the columns using Andrew's method). And finally, the plot currently lacks labels. Hopefully someone can improve this code!

      For those new to matrix operations, here's an annotated version:
      Code:
      forvalues quantile = 0.1(0.1)0.9 {
          xtqreg invest mvalue kstock, q(`quantile')
          *Give the temporary matrix a the contents of r(table)
          matrix `a' = r(table)
          *Give the temporary matrix b the first row and first column of a, then column join (the \ operator) with the 5th and 6th row of a, first column
          matrix `b' = `a'[1,1] \ `a'[5..6,1]
          *Give temp matrix b the column name "q" plus the current value of quantile (which is referred to as a local macro)
          matrix colnames `b' = "q`quantile'"
          *If the matrix result1 doesn't exist, create it and assign the contents of temp matrix b; if it does exist, column join the contents of b
          matrix result1 = nullmat(result1), `b'
          }
      Last edited by Weiwen Ng; 09 Oct 2018, 09:54.
      Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

      When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

      Comment


      • #4
        @Andrew Musau and @Weiwen Ng, Many thanks for your help suggestions.
        Ho-Chuan (River) Huang
        Stata 19.0, MP(4)

        Comment


        • #5
          Dear Andrew, Suppose that I'd like to collection quantile coefficients, say, from 0.05, 0.10, 0.15, ..., 0.95, how can I adjust the code to this end? Thanks.
          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment


          • #6
            Code:
            webuse grunfeld, clear
            xtqreg invest mvalue kstock, q(0.1(0.1)0.9)
            ereturn list
            forvalues q = 1(1)9 {
              matrix list e(b_`q')
              matrix list e(V_`q')
            }
            
            webuse grunfeld, clear
            xtqreg invest mvalue kstock, q(0.05(0.05)0.95)
            forvalues q = 1(1)9 {
              local j=`q'*10
              matrix  B_`j'=e(b_`q')
              matrix  V_`j'=e(V_`q')
              mat list B_`j'
              matrix list V_`j'
            }
            
            forvalues q = 5(10)95 {
              matrix  B_`q'=e(b_`q')
              matrix  V_`q'=e(V_`q')
              mat list B_`q'
              matrix list V_`q'
            }
            
            mat R= J(19, 4, .)
            local i=1
            forval j = 5(5)95 {
            mat b=B_`j'
            mat V=V_`j'
            matrix R[`i',1]= b[1,1]
            matrix R[`i',2]= b[1,2]
            matrix R[`i',3]= V[1,1]
            matrix R[`i',4]= V[2,2]
            local ++i
            }
             
            forvalues i=1/19{
            local rnames  "`rnames' quantile`i'"
            }
            local cnames  "b_mvalue b_kstock V_mvalue V_kstock"
            mat rownames R= `rnames'
            mat colnames R= `cnames'
            mat list R
            
            svmat double R, names(col)
            I tried to code if you change the quantile range to 0.05(0.05)0.95. It is quite strange that the macro matrix should report quantile10 as quantile1 which brings difficulty in looping. Hope this can help you dear Huang.
            2B or not 2B, that's a question!

            Comment


            • #7
              Dear Liu, Thank you for this helpful suggestion.
              Ho-Chuan (River) Huang
              Stata 19.0, MP(4)

              Comment

              Working...
              X