# Re: st: -margins- vs -adjust-

 From jpitblado@stata.com (Jeff Pitblado, StataCorp LP) To statalist@hsphsun2.harvard.edu Subject Re: st: -margins- vs -adjust- Date Thu, 05 Nov 2009 09:57:53 -0600

```Fred Wolfe <fwolfe@arthritis-research.org> asks for a clarification of the
description in the -[R] margins- manual:

> Jeff's post and explanation was very helpful. I have been struggling
> with the manual on margins since Stata 11 came out. Sometime I think I
> understand it it, sometimes not. Perhaps Jeff could clarify this
> further for me.
>
> The manual states:
>
> A margin is a statistic based on a fitted model calculated over a
> dataset in which some of or
> all the covariates are fixed at values different from what they really
> are. For instance, after a linear regression fit on males and females,
> the marginal mean (margin of mean) for males is the predicted mean of
> the dependent variable, where every observation is treated as if it
> represents a male; thus those observations that in fact do represent
> males are included, as well as those observations that represent
> females. The marginal mean for female would be similarly obtained by
> treating all observations as if they represented females.
>
> A simple question, what does it mean (precisely) "...where every
> observation is treated as if it represents a male <snip>  treating all
> observations as if they represented females." What actually is done so
> they are "treated as if?"

Let's look at a specific example, using the auto data:

. sysuse auto
. regress mpg turn i.foreign

After typing the above commands in Stata, we have a linear fit of 'mpg' on
the continuous covariate 'turn' and factor covariate 'foreign'.  Now we can
use the -margins- command to compute the margins of the linear prediction for
each level of 'foreign':

. margins foreign

We can reproduce the predictive margins point estimates reported by the above
-margins- command with the following:

. preserve

Get the linear prediction, but treat all cars as domestic:

. replace foreign = 0
. predict xb_dom

Get the linear prediction, but treat all cars as foreign:

. replace foreign = 1
. predict xb_for

Compute the mean (margin) of our linear predictions:

. summarize xb_dom xb_for

. restore

The means reported by -summarize- are our predictive margins.

In an earlier post to this thread, Jeph mentioned that he fit some models
using indicator variables but without using the 'i.' operator.  Jeph can still
compute predictive margins in this case, he just has to use the -at()- option
instead of the varlist syntax.  Using the above model, but without the 'i.'
operator, we would type

. regress mpg turn foreign

. margins, at(foreign=(0 1))

The row titles in the table require a legend, but the computational concept is
the same as the prior use of -margins-.

In my previous post, I showed how the -over()- option computes margins for
groups of observations defined by the combinations of values in the -over()-
variables.  Thus

. margins, over(foreign)

will compute a different type of margin.  We can reproduce these point
estimates by:

. predict xb
. bysort foreign: summarize xb

--Jeff

*** Stata output from the above examples:

. sysuse auto
(1978 Automobile Data)

. regress mpg turn i.foreign

Source |       SS       df       MS              Number of obs =      74
-------------+------------------------------           F(  2,    71) =   38.97
Model |  1278.68109     2  639.340545           Prob > F      =  0.0000
Residual |  1164.77837    71  16.4053292           R-squared     =  0.5233
Total |  2443.45946    73  33.4720474           Root MSE      =  4.0503

------------------------------------------------------------------------------
mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
turn |  -1.029205    .138914    -7.41   0.000    -1.306192   -.7522186
1.foreign |  -1.263614   1.328003    -0.95   0.345    -3.911577    1.384349
_cons |   62.47956   5.784252    10.80   0.000     50.94609    74.01303
------------------------------------------------------------------------------

. margins foreign

Predictive margins                                Number of obs   =         74
Model VCE    : OLS

Expression   : Linear prediction, predict()

------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
foreign |
0  |   21.67297   .6144672    35.27   0.000     20.46863     22.8773
1  |   20.40935   1.045246    19.53   0.000     18.36071      22.458
------------------------------------------------------------------------------

.
. preserve

. // Get the linear prediction, but treat all cars as domestic:
. replace foreign = 0

. predict xb_dom
(option xb assumed; fitted values)

. // Get the linear prediction, but treat all cars as foreign:
. replace foreign = 1

. predict xb_for
(option xb assumed; fitted values)

. // Compute the mean (margin) of our linear predictions:
. summarize xb_dom xb_for

Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
xb_dom |        74    21.67297    4.527838   9.990096    30.5742
xb_for |        74    20.40935    4.527838   8.726482   29.31059

. restore

.
. regress mpg turn foreign

Source |       SS       df       MS              Number of obs =      74
-------------+------------------------------           F(  2,    71) =   38.97
Model |  1278.68109     2  639.340545           Prob > F      =  0.0000
Residual |  1164.77837    71  16.4053292           R-squared     =  0.5233
Total |  2443.45946    73  33.4720474           Root MSE      =  4.0503

------------------------------------------------------------------------------
mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
turn |  -1.029205    .138914    -7.41   0.000    -1.306192   -.7522186
foreign |  -1.263614   1.328003    -0.95   0.345    -3.911577    1.384349
_cons |   62.47956   5.784252    10.80   0.000     50.94609    74.01303
------------------------------------------------------------------------------

. margins, at(foreign=(0 1))

Predictive margins                                Number of obs   =         74
Model VCE    : OLS

Expression   : Linear prediction, predict()

1._at        : foreign         =           0

2._at        : foreign         =           1

------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
_at |
1  |   21.67297   .6144672    35.27   0.000     20.46863     22.8773
2  |   20.40935   1.045246    19.53   0.000     18.36071      22.458
------------------------------------------------------------------------------

.
. margins, over(foreign)

Predictive margins                                Number of obs   =         74
Model VCE    : OLS

Expression   : Linear prediction, predict()
over         : foreign

------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
foreign |
0  |   19.82692   .5616824    35.30   0.000     18.72605     20.9278
1  |   24.77273   .8635374    28.69   0.000     23.08023    26.46523
------------------------------------------------------------------------------

.
. predict xb
(option xb assumed; fitted values)

. bysort foreign: summarize xb

-------------------------------------------------------------------------------
-> foreign = Domestic

Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
xb |        52    19.82692    4.083456   9.990096    30.5742

-------------------------------------------------------------------------------
-> foreign = Foreign

Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
xb |        22    24.77273    1.544921   22.10615   28.28138

<end>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```