Michael A
Michael A

Reputation: 4613

How do I create a fitted value with a subset of regression coefficients in place of all coefficients?

I run a simple regression and find the fitted value like this:

sysuse auto, clear
reg price mpg c.mpg#foreign i.rep78 headroom trunk
predict fitted_price, xb

This gives me these coefficients:

-------------------------------------------------------------------------------
        price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
--------------+----------------------------------------------------------------
          mpg |  -306.1891   77.01548    -3.98   0.000     -460.243   -152.1352
              |
foreign#c.mpg |
     Foreign  |   60.58403   37.24129     1.63   0.109    -13.90964    135.0777
              |
        rep78 |
           2  |   999.7779   2150.269     0.46   0.644      -3301.4    5300.956
           3  |   1200.741   2001.853     0.60   0.551    -2803.561    5205.043
           4  |   1032.778   2070.513     0.50   0.620    -3108.864     5174.42
           5  |   2081.128   2200.998     0.95   0.348    -2321.523    6483.779
              |
     headroom |  -611.7201   502.3401    -1.22   0.228     -1616.55    393.1097
        trunk |   134.4143   110.8262     1.21   0.230    -87.27118    356.0998
        _cons |   10922.46   2803.271     3.90   0.000     5315.082    16529.84
-------------------------------------------------------------------------------

For purposes of a counterfactual (especially important in time series), I might want to find the fitted value using a subset of the coefficients from this regression. For example, I might want to find the fitted value using all the coefficients from this regression except the coefficient(s) from the interaction between mpg and foreign, i.e. c.mpg#foreign. (Note that this is different from simply running the regression again without the interaction, because that will yield different coefficients).

As of now, I do this:

sysuse auto, clear
reg price mpg c.mpg#foreign i.rep78 headroom trunk
matrix betas = e(b)
local names: colnames betas
foreach name of local names {
    if strpos("`name'", "#") > 0 {
        scalar define col_idx = colnumb(betas, "`name'")
        matrix betas[1, col_idx] = 0
    }
}
matrix score fitted_price_no_interact = betas

This isn't a robust solution because it relies on the naming convention of # in the column names of the coefficient matrix, and breaks down if I want to include one set of interactions but not another. I can code something like this for a specific regression, by manually specifying the names, but if I change the regression, I have to manually change the code.

Is there a more robust way to do this, e.g.

predict fitted_price, xb exclude(c.mpg#foreign trunk)

that will simplify this process for me?

Upvotes: 2

Views: 1797

Answers (1)

Steve Samuels
Steve Samuels

Reputation: 908

Edit 2015-03-29: Use the original method on one subset of interactions, but retain others

A great advantage of your original method is that it can handle interactions of any complexity. The major defect is that it won't ignore interactions that you want to keep in the model. But if you use xi to create these, # won't appear in their names.

 sysuse auto, clear
 recode rep78  1 = 2 //combine small categories
 xi, prefix("") i.rep78*mpg  // mpg*i.rep78 won't work
 des _I*


 reg price mpg  foreign c.mpg#foreign  _I* headroom trunk
 matrix betas = e(b)
 local names: colnames betas
 foreach name of local names {
     if strpos("`name'", "#") > 0 {
         scalar define col_idx = colnumb(betas, "`name'")
         matrix betas[1, col_idx] = 0
     }
 matrix score fit_sans_mpgXforeign = betas

Edit 2015-03-28

The xi prefix wasn't needed, so, for example, this works in Stata 13.

sysuse auto, clear
gen intx = c.mpg#foreign
reg price mpg  foreign i.rep78 headroom trunk intx
predict mhat
gen fitted_sans_interaction = mhat -_b[intx]*intx

Previous Answer

sysuse auto, clear
xi: gen intx = c.mpg#foreign
reg price mpg  foreign i.rep78 headroom trunk intx
predict mhat
gen fitted_sans_interaction = mhat -_b[intx]*intx

or even

sysuse auto, clear

xi: gen intx = c.mpg#foreign
reg price c.mpg##foreign i.rep78 headroom trunk intx
predict mhat
gen fitted_sans_interaction = mhat -_b[intx]*intx

I've supplied the main effect of foreign which was omitted in your example.

Upvotes: 3

Related Questions