0
votes

Is it possible to get the regression upper and lower bound as variables so that I can predict both upper and lower bound of the regression without hard coding.

I.e

reg y x
gen ypred = _b[_cons] + _b[x]*48 \\ prediction for x = 48
gen ypred_beta95u= 0.8401741 + 0.0202769*48 \\ prediction upper bound
gen ypred_beta95l= 0.550594 + 0.0097727*48 \\ prediction lower bound

I can get the coefficients of the regression without hard coding and I want to know if it is possible to do the same thing with upper and lower bound in stata.

1
Read help regress postestimation and look at saved results after regression using ereturn list. - Nick Cox

1 Answers

0
votes

Here are two ways of doing this that rely on Stata's margins, which calculates predictions of a previously fit model at fixed values of some covariates. The first is a bit more from-first-principles. The second relies on pulling out stored estimation results that margins leaves behind.

. sysuse auto, clear 
(1978 Automobile Data)

. reg price mpg 

      Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(1, 72)        =     20.26
       Model |   139449474         1   139449474   Prob > F        =    0.0000
    Residual |   495615923        72  6883554.48   R-squared       =    0.2196
-------------+----------------------------------   Adj R-squared   =    0.2087
       Total |   635065396        73  8699525.97   Root MSE        =    2623.7

------------------------------------------------------------------------------
       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         mpg |  -238.8943   53.07669    -4.50   0.000    -344.7008   -133.0879
       _cons |   11253.06   1170.813     9.61   0.000     8919.088    13587.03
------------------------------------------------------------------------------

. margins, at(mpg=48) level(95)

Adjusted predictions                            Number of obs     =         74
Model VCE    : OLS

Expression   : Linear prediction, predict()
at           : mpg             =          48

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _cons |  -213.8679   1449.736    -0.15   0.883    -3103.864    2676.128
------------------------------------------------------------------------------

. /* Mostly By Hand */
. gen ll1 = _b[_cons] - invttail(`e(df_r)',0.025)*_se[_cons] 

. gen ub1 = _b[_cons] + invttail(`e(df_r)',0.025)*_se[_cons] 

. /* From Stata's own stored output */ 
. matrix E = r(table)

. matrix list E

E[9,1]
             _cons
     b  -213.86793
    se   1449.7361
     t  -.14752197
pvalue   .88313237
    ll   -3103.864
    ul   2676.1282
    df          72
  crit   1.9934636
 eform           0

. gen ll2  =  E[5,1]

. gen ub2  =  E[6,1]

. list ll2 ub1 ll2 ub2 in 1/2, clean noobs

          ll2        ub1         ll2        ub2  
    -3103.864   2676.128   -3103.864   2676.128  
    -3103.864   2676.128   -3103.864   2676.128