2023-03-08

# Post-estimation command

• Stata “estimation” commands are primarily those which fit models.
• E.g. regress, logit, mixed, xtreg.
• Stata “stores” the most recent estimation command.
• margins is a post-estimation command; meaning it will use the most recently run estimation.
• margins itself is not an estimation command.

# Categorical variables

. sysuse nlsw88
(NLSW, 1988 extract)

. list in 1

+----------------------------------------------------------------+
1. | idcode | age  |  race  | married  |    never_married  | grade  |
|      1 |  37  | Black  |  Single  | Has been married  |    12  |
|----------------------------------------------------------------|
|          collgrad  |      south  |  smsa  |            c_city  |
|  Not college grad  |  Not south  |  SMSA  |  Not central city  |
|----------------------------------------------------------------|
|               industry | occupation | union |     wage | hours |
| Transport/Comm/Utility | Operatives | Union | 11.73913 |    48 |
|----------------------------------------------------------------|
|            ttl_exp            |              tenure            |
|           10.33333            |            5.333333            |
+----------------------------------------------------------------+


# Fitting the model

. regress wage i.race

Source |       SS           df       MS      Number of obs   =     2,246
-------------+----------------------------------   F(2, 2243)      =     10.28
Model |  675.510282         2  337.755141   Prob > F        =    0.0000
Residual |  73692.4571     2,243  32.8544169   R-squared       =    0.0091
-------------+----------------------------------   Adj R-squared   =    0.0082
Total |  74367.9674     2,245  33.1260434   Root MSE        =    5.7319

------------------------------------------------------------------------------
wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
race |
Black  |  -1.238442   .2764488    -4.48   0.000    -1.780564   -.6963193
Other  |   .4677818   1.133005     0.41   0.680    -1.754067    2.689631
|
_cons |   8.082999   .1416683    57.06   0.000     7.805185    8.360814
------------------------------------------------------------------------------

Group Average   Comparison Diff. in Averages
White 8.083 White vs Black -1.238
Black ??? White vs Other 0.468
Other ??? Black vs Other ???

# Calculating group effects and comparisons

. regress, noheader
------------------------------------------------------------------------------
wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
race |
Black  |  -1.238442   .2764488    -4.48   0.000    -1.780564   -.6963193
Other  |   .4677818   1.133005     0.41   0.680    -1.754067    2.689631
|
_cons |   8.082999   .1416683    57.06   0.000     7.805185    8.360814
------------------------------------------------------------------------------


$wage = \beta_0 + \beta_1\textrm{Black} + \beta_2\textrm{Other} + \epsilon$

Group Average
White 8.083
Black 8.083 + -1.238 = 6.845
Other 8.083 + 0.468 = 8.551
Comparison Diff. in Averages
White vs Black -1.238
White vs Other 0.468
Black vs Other 0.468 - -1.238 = 1.706

# Changing reference category

. regress wage ib2.race, noheader
------------------------------------------------------------------------------
wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
race |
White  |   1.238442   .2764488     4.48   0.000     .6963193    1.780564
Other  |   1.706223   1.148906     1.49   0.138    -.5468071    3.959254
|
_cons |   6.844558   .2373901    28.83   0.000     6.379031    7.310085
------------------------------------------------------------------------------

. regress wage ib3.race, noheader
------------------------------------------------------------------------------
wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
race |
White  |  -.4677818   1.133005    -0.41   0.680    -2.689631    1.754067
Black  |  -1.706223   1.148906    -1.49   0.138    -3.959254    .5468071
|
_cons |   8.550781   1.124114     7.61   0.000      6.34637    10.75519
------------------------------------------------------------------------------


# Estimated means

margins does this for us!

Group Average
White 8.083
Black 8.083 + -1.238 = 6.845
Other 8.083 + 0.468 = 8.551
. margins race

Adjusted predictions                                     Number of obs = 2,246
Model VCE: OLS

Expression: Linear prediction, predict()

------------------------------------------------------------------------------
|            Delta-method
|     Margin   std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
race |
White  |   8.082999   .1416683    57.06   0.000     7.805185    8.360814
Black  |   6.844558   .2373901    28.83   0.000     6.379031    7.310085
Other  |   8.550781   1.124114     7.61   0.000      6.34637    10.75519
------------------------------------------------------------------------------


# Differences in estimated means

Comparison Diff. In Averages
White vs Black -1.238
White vs Other 0.468
Black vs Other 0.468 - -1.238 = 1.706
. margins race, pwcompare

Pairwise comparisons of adjusted predictions             Number of obs = 2,246
Model VCE: OLS

Expression: Linear prediction, predict()

-----------------------------------------------------------------
|   Contrast   std. err.     [95% conf. interval]
----------------+------------------------------------------------
race |
Black vs White  |  -1.238442   .2764488     -1.780564   -.6963193
Other vs White  |   .4677818   1.133005     -1.754067    2.689631
Other vs Black  |   1.706223   1.148906     -.5468071    3.959254
-----------------------------------------------------------------


# Syntax for estimated means

Average outcome per level

margins [categorical variable]

Pairwise comparisons between groups

margins [categorical variable], pwcompare(ci) // Produce confidence intervals, default
margins [categorical variable], pwcompare(pv) // Produce p-values
• Do not preface the categorical variable with i..
• In general, binary (0/1) variables can be treated as continuous or categorical in the model. The model is identical either way, but treating them as categorical lets margins operate in this easy fashion.

# In the presence of covariates

. regress wage age i.married, noheader
------------------------------------------------------------------------------
wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
age |  -.0692705   .0396596    -1.75   0.081     -.147044    .0085029
|
married |
Married  |  -.4958806   .2530888    -1.96   0.050    -.9921934    .0004321
_cons |   10.79748   1.568569     6.88   0.000     7.721481    13.87348
------------------------------------------------------------------------------

. margins married

Predictive margins                                       Number of obs = 2,246
Model VCE: OLS

Expression: Linear prediction, predict()

------------------------------------------------------------------------------
|            Delta-method
|     Margin   std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
married |
Single  |   8.085319   .2027826    39.87   0.000     7.687658    8.482981
Married  |   7.589439    .151412    50.12   0.000     7.292517    7.886361
------------------------------------------------------------------------------


# Visualization of margins

margins married