# Re: st: marginal effects and interactions

 From May Boggess To statalist@hsphsun2.harvard.edu Subject Re: st: marginal effects and interactions Date 01 Apr 2004 19:35:33 -0600

```On Thursday, Scott Merryman wrote:

>  one can easily obtain the marginal effects and standard errors for linear models
> using -lincom-
>
> For example:
>
> . sysuse auto
> (1978 Automobile Data)
>
> . gen mpgXprice = mpg*price
>
> . gen mpgXgear = mpg*gear
>
> . qui reg weight mpg price gear mpgX*
>
> . qui sum price if e(sample)
>
> . local mean1 = r(mean)
>
> . qui sum gear if e(sample)
>
> . local mean2 = r(mean)
>
> . lincom mpg + mpgXp*`mean1' + mpgXg*`mean2'
>
>  ( 1)  mpg + 6165.257 mpgXprice + 3.014865 mpgXgear = 0
>
> ------------------------------------------------------------------------------
>       weight |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
> -------------+----------------------------------------------------------------
>          (1) |  -73.55893   10.69451    -6.88   0.000    -94.89949   -52.21836
> ------------------------------------------------------------------------------
>
>
We can use this for any function of the covariates and coefficients,
after an estimation, as long as we can differentiate the function by
hand, as was done in the example above.

Let's go over the example above in more detail. Here the function of
the covariates and coefficients was

f = b0 + b1*mpg + b2*price + b3*gear + b4* mpg*price + b5 * mpg*gear

If we differentiate this with respect to mpg we obtain

df/dx = b1 + 0 + 0 + b4*price + b5*gear.

In the example, this derivative (ie. marginal effect) is evaluated at
average price and average gear. They were called mean1 nad mean2. This
is the function that was evaluated by -lincom-.

The extra milage that -lincom- gets us is the standard error of this
marginal effect. You can use -predictnl- in the same way, since it is
also designed to use the delta method to obtain standard errors:

clear
sysuse auto
gen mpgXprice = mpg*price
gen mpgXgear = mpg*gear
reg weight mpg price gear mpgX*
qui sum price if e(sample)
local mean1 = r(mean)
qui sum gear if e(sample)
local mean2 = r(mean)
di _b[mpg] + _b[mpgXp]*`mean1' + _b[mpgXg]*`mean2'
lincom mpg + mpgXp*`mean1' + mpgXg*`mean2'
predictnl dfdx =_b[mpg] + _b[mpgXp]*`mean1' + _b[mpgXg]*`mean2' in 1,
se(se)
list dfdx se in 1

Here is one more example, using -probit-, as requested by Javier. I
haven't used an interaction in this example, because I want to be able

clear
sysuse auto
probit for  price gear
qui sum price if e(sample)
local mean1 = r(mean)
qui sum gear if e(sample)
local mean2 = r(mean)
predict xb,xb
predict p,p
gen myxb = _b[price]* price + _b[gear]*gear + _b[_cons]
gen myp = norm(myxb)
sum myp p  xb myxb

di  normd(_b[price]* `mean1' + _b[gear]*`mean2' + _b[_cons])*_b[price]
*lincom  lincom  normd(_b[price]* `mean1' + _b[gear]*`mean2' +
_b[_cons])*_b[price]
predictnl dfdp =  normd(_b[price]* `mean1' + _b[gear]*`mean2' +
_b[_cons])*_b[price] in 1, se(sep)
list dfdp sep in 1
predictnl dfdg =  normd(_b[price]* `mean1' + _b[gear]*`mean2' +
_b[_cons])*_b[gear] in 1, se(seg)
list dfdg seg in 1
mfx, predict(p)

After I run the model, I check that I understand the function for which
I want the marginal effects. In this example I am interested in p, the
probability of success. So, I check I know how  to compute it as Stata
would.

Then I have done the derivative by hand using the chain rule. Note that
the derivative of the cumulative distribution fuction -norm- is the
density function -normd-.

I have commented out -lincom- because, as you will see, it's equation
parser isn't quite as clever as -predictnl-'s (either that or I haven't
found the correct way to enter it into -lincom-). However, -predictnl-
handles it well. I do it twice, to get both marginal effects. I confirm

-- May
mmb@stata.com

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```