Announcement

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

  • Collecting Confidence Intervals from regress using statsby

    Hi. I want to use statsby to collect the lower and upper limits of the 95% confidence interval of a regression coefficient generated with regress. I'm using the following command to collect the coefficient and the standard error, but can't find a way to collect the confidence interval limits too. How can I collect the confidence interval limits?

    Code:
     statsby _b _se, by(state): reg y x, nocons r
    Last edited by Juan Pablo Mesa; 22 Jun 2021, 14:36.

  • #2
    Here's a riff on the manual examples for -statsby-. Official regression commands return results in -r()- like the confidence interval in a matrix called -r(table)-. Here I show what that might look like following a linear regression.

    Code:
    use "https://www.stata-press.com/data/r17/auto2", clear
    regress price weight length mpg if foreign==1, nohead
    mat list r(table)
    Result

    Code:
    . regress price weight length mpg if foreign==1, nohead
    ------------------------------------------------------------------------------
           price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
          weight |   4.784841   1.670006     2.87   0.010     1.276288    8.293394
          length |   13.39052   50.70229     0.26   0.795    -93.13104    119.9121
             mpg |   -18.4072   59.37442    -0.31   0.760    -143.1482    106.3338
           _cons |   -6497.49   6337.952    -1.03   0.319    -19813.03    6818.052
    ------------------------------------------------------------------------------
    
    . mat list r(table)
    
    r(table)[9,4]
                weight      length         mpg       _cons
         b   4.7848411   13.390522  -18.407204  -6497.4902
        se   1.6700063   50.702293   59.374424   6337.9515
         t   2.8651635   .26410092  -.31001907   -1.025172
    pvalue   .01029028    .7947022   .76010709   .31886286
        ll   1.2762881  -93.131042  -143.14824  -19813.032
        ul   8.2933942   119.91209   106.33383   6818.0518
        df          18          18          18          18
      crit    2.100922    2.100922    2.100922    2.100922
     eform           0           0           0           0
    In recent versions of Stata you can access matrix results by their row and column names, or else you can always used the row and column number. Adding this to -statsby-, you can do something like this:

    Code:
    statsby weight_b=_b[weight] weight_ll=(r(table)["ll","weight"]) weight_ul=(r(table)["ul","weight"]), by(foreign) verbose nodots: regress price weight length mpg
    Result

    Code:
         +-------------------------------------------+
         |  foreign   weight_b   weigh~ll   weigh~ul |
         |-------------------------------------------|
      1. | Domestic   6.767233   4.301539   9.232928 |
      2. |  Foreign   4.784841   1.276288   8.293394 |
         +-------------------------------------------+

    Comment


    • #3
      Thank you very much, Leonardo. Your code does just what I needed.

      Here I leave a simplified version of your code for reference. It collects the coefficient, the standard error, the lower limit and the upper limit of a univariate linear regression:
      Code:
       statsby (r(table)[1,1]) (r(table)[2,1]) (r(table)[5,1]) (r(table)[6,1]), by(state): reg y x, nocons r

      Comment

      Working...
      X