Announcement

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

  • Store the confidence interval of coefficient after OLS regression

    Hey All

    Suppose I run a regression by the command:
    Code:
    reg var1 var2
    .
    Now If I can use: "_b[var2]" to store the coefficient from the regression, for example:
    Code:
    scalar beta1 = _b[var2]
    And if I want to store the standard error of the coefficient I can write:
    Code:
    scalar stderr = _se[var2]
    But if I want to the same for the 95% confidence interval that Stata calculate automatically, what should I write? I haven't managed to find anything when I used "help reg"

    Thanks,

    Fitzgerald
    Last edited by FitzGerald Blindman; 18 Feb 2025, 03:06.

  • #2
    Code:
        clear
        set obs 100
        gene var1 = rnormal()
        gene var2 = rnormal()
        regress var1 var2
        
        *! Manual calculation
        matrix X = r(table)
        matrix list X
        dis X[1,1]-invt(X[7,1],0.975)*X[2,1]
        dis X[1,1]+invt(X[7,1],0.975)*X[2,1]
        
        *! Automated calculation
        lincom var2
        return list

    Comment


    • #3
      Originally posted by Tiago Pereira View Post
      Code:
      clear
      set obs 100
      gene var1 = rnormal()
      gene var2 = rnormal()
      regress var1 var2
      
      *! Manual calculation
      matrix X = r(table)
      matrix list X
      dis X[1,1]-invt(X[7,1],0.975)*X[2,1]
      dis X[1,1]+invt(X[7,1],0.975)*X[2,1]
      
      *! Automated calculation
      lincom var2
      return list
      Thank you.

      Actually it looks quit complex, isn't any way as _ci[var2] or something like that?

      Thank again,

      Fitz

      Comment


      • #4
        Hi, Fitz.

        I believe those are the easiest approaches.

        You can also use:
        Code:
        dis _b[var2]-invt( e(df_r),0.975)*_se[var2]
        dis _b[var2]+invt( e(df_r),0.975)*_se[var2]

        Comment


        • #5
          There is nothing of the form you want, but you do not need to reconstruct confidence limits as they are already in r(table). You can access the values directly as follows.
          Code:
          sysuse auto
          regress price mpg
          mat list r(table)
          display r(table)[5,1]
          display r(table)[6,1]
          This is not ideal because we are referring to the variable through a column of r(table). So if we change the order of variables in our regress command, we need to remember to change the column number (unless there is something I’ve forgotten). With _b[mpg] and _se[mpg] you do not have to remember this because it directly refers to the variable name.

          Comment


          • #6
            Originally posted by FitzGerald Blindman View Post
            Actually it looks quit complex, isn't any way as _ci[var2] or something like that?
            A confidence interval is by definition described by two numbers not one; a lower bound and a upper bound. So something like _ci[var] would not work. Some like _cilb[var2] and _ciub[var2] might work but at some point you just leave too many scalars around.

            If too many scalars is a problem, than the solution is to pack them neatly in a matrix. That is what r(table) is, and it does leave the lower and upper bounds behind in the rows ll and ul.

            Code:
            . // get example data and run a regression
            . sysuse auto, clear
            (1978 automobile data)
            
            . reg price i.foreign mpg
            
                  Source |       SS           df       MS      Number of obs   =        74
            -------------+----------------------------------   F(2, 71)        =     14.07
                   Model |   180261702         2  90130850.8   Prob > F        =    0.0000
                Residual |   454803695        71  6405685.84   R-squared       =    0.2838
            -------------+----------------------------------   Adj R-squared   =    0.2637
                   Total |   635065396        73  8699525.97   Root MSE        =    2530.9
            
            ------------------------------------------------------------------------------
                   price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
            -------------+----------------------------------------------------------------
                 foreign |
                Foreign  |   1767.292    700.158     2.52   0.014     371.2169    3163.368
                     mpg |  -294.1955   55.69172    -5.28   0.000    -405.2417   -183.1494
                   _cons |   11905.42   1158.634    10.28   0.000     9595.164    14215.67
            ------------------------------------------------------------------------------
            
            .
            . // look at the matrix r(table)
            . matlist r(table)
            
                         |        0b.         1.                      
                         |   foreign    foreign        mpg      _cons
            -------------+-------------------------------------------
                       b |         0   1767.292  -294.1955   11905.42
                      se |         .    700.158   55.69172   1158.634
                       t |         .   2.524134  -5.282572   10.27539
                  pvalue |         .   .0138363   1.33e-06   1.09e-15
                      ll |         .   371.2169  -405.2417   9595.164
                      ul |         .   3163.368  -183.1494   14215.67
                      df |        71         71         71         71
                    crit |  1.993943   1.993943   1.993943   1.993943
                   eform |         0          0          0          0
            
            .
            . // if you want to store things in scalars,
            . // it is best to give them temporary names
            . tempname lb ub
            
            .
            . // since I will repeated refer to r(table) I create a little shortcut
            . local tab = "r(table)"
            
            .
            . // get the lower bound of the ci of mpg
            . // the el() function extracts a scalar from a matrix (the first argument)
            . // the second argument is the row, and the third argument is the column
            . // The rownumb() and colnumb() functions give you row or column number from the
            . // matrix specified in the first argument associated
            . // with the row/column name given in the second argument
            . local r = rownumb(`tab',"ll")
            
            . local c = colnumb(`tab', "mpg")
            
            . scalar `lb' = el(`tab', `r', `c' )
            
            .
            . // you can put this in one command, but I find it more difficult to read
            . // scalar `lb' = el(r(table), rownumb(r(table),"ll"), colnumb(r(table), "mpg"))
            .
            . // or refer directly to row and column numbers, rather than names
            . // but I find that I just make too many errors, and when I do, it is an absolute
            . // pain finding those erros
            . // scalar `lb' = el(r(table), 5,3)
            .
            . // get the upper bound of the ci of mpg
            . local r = rownumb(`tab',"ul")
            
            . scalar `ub' = el(`tab', `r', `c')
            
            . di `lb'
            -405.24167
            
            . di `ub'
            -183.1494
            In real code I don't need that much explanation, so my .do file would look something like this:

            Code:
            // get example data and run a regression
            sysuse auto, clear
            reg price i.foreign mpg
            
            // get lb and ub of 95% CI of mpg
            tempname lb ub
            local tab = "r(table)"
            
            local r = rownumb(`tab',"ll")
            local c = colnumb(`tab', "mpg")
            scalar `lb' = el(`tab', `r', `c' )
            
            local r = rownumb(`tab',"ul")
            scalar `ub' = el(`tab', `r', `c')
            Last edited by Maarten Buis; 18 Feb 2025, 03:50.
            ---------------------------------
            Maarten L. Buis
            University of Konstanz
            Department of history and sociology
            box 40
            78457 Konstanz
            Germany
            http://www.maartenbuis.nl
            ---------------------------------

            Comment


            • #7
              Actually, Fitz:

              Code:
              help _b
              Code:
              dis _r_lb[var2]
              dis _r_ub[var2]
              I did not know that. Just found out.

              Comment


              • #8
                Tiago Pereira What! I did not realise this either. Thanks.

                Comment


                • #9
                  The confidence intervals are also found in the return list:

                  Code:
                  set level 95
                  reg var1 var2 
                  mat D = r(table)
                  mat list D
                  display D[5,1]
                  display D[6,1]

                  Comment


                  • #10
                    Hey guys,

                    Thank you all. I think that the last answer from Frode is the one that fits my needs the best.

                    Thanks again,

                    Fitz

                    Comment


                    • #11
                      This can also be done with the user command regsave:

                      Code:
                      sysuse auto, clear
                      reg price mpg
                      regsave, ci
                      list
                      
                           +----------------------------------------------------------------------+
                           |   var        coef     stderr    ci_lower    ci_upper    N         r2 |
                           |----------------------------------------------------------------------|
                        1. |   mpg   -238.8943   53.07669   -344.7008   -133.0879   74   .2195829 |
                        2. | _cons    11253.06   1170.813    8919.088    13587.03   74   .2195829 |
                           +----------------------------------------------------------------------+
                      Associate Professor of Finance and Economics
                      University of Illinois
                      www.julianreif.com

                      Comment

                      Working...
                      X