Adjusting the ereturn coefficients

Suppose you have a model in which you want to adjust some of the coefficients, in order to display in a table alongside the original coefficients.

As an example, consider a simple regression model. We will adjust the model by recentering the independent variable. In a simple model, the slope remains unchanged and we just alter the constant.

Set up:

. sysuse auto
(1978 Automobile Data)

. summarize mpg

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
         mpg |         74     21.2973    5.785503         12         41

. scalar mu = r(mean)

Run the model, store the results for later display.

. regress 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
------------------------------------------------------------------------------

. estimates store base

Next, let me alter the coefficient matrix "by hand". This is just to make it clear what alteration I am actually making. In this simple example, the constant will shift to the mean of price.

. matrix A = e(b)

. matrix A[rownumb(A,"y1"), colnumb(A,"_cons")] = _b[_cons] + _b[mpg]*mu

. *matrix A[1, 2] = _b[_cons] + _b[mpg]*mu // alternate form
. matrix list A

A[1,2]
           mpg       _cons
y1  -238.89435   6165.2568

Now we define a program that makes this alteration (it is only useable with this one regression!).

. capture program drop recenter

. program define recenter, eclass  
  1.   version 12
  2.   syntax , matrix(name) mu(real)
  3.   
.   matrix `matrix'[rownumb(`matrix',"y1"), colnumb(`matrix',"_cons")] = _b[_co
> ns] + _b[mpg]*mu
  4.   
.   ereturn repost b = `matrix'
  5. end

The key elements of the above program are the ugly expression that replaces the constant, and the ereturn repost b, which gives us back the altered e(b) as well as the rest of the ereturn results from the previous regression. (Please note that the e(V) is now actually wrong!)

Calculate the altered coefficients, using the program. (Note we convert our scalar mu, into a macro.)

. recenter , matrix(A) mu(`=mu')

And finally, table the results:

. estimates table base .

----------------------------------------
    Variable |    base        active    
-------------+--------------------------
         mpg | -238.89435   -238.89435  
       _cons |  11253.061    6165.2568  
----------------------------------------