Reporting margins in nested tables


This post shows how to nest several regression / margins models in a single table. In case you are interested in reporting a detailed table for a single model, you can read this post.
For creating a nested table of margins after any regression command, we shall first estimate the regression model. In the next command where we run the margins command, we shall add asdocx to the beginning of the line. Also, we can add other asdocx options after comma, such the option nest and replace. In the following example, I create a nested table of margins for three regression models.

* Estimate regression without using asdocx
webuse nhanes2d
probit highbp age age2 race   [pw=weight], robust

* add asdocx to the margin command: replace, nest and tzok belong to asdocx
asdocx margins , dydx(*) atmeans replace nest tzok 

* Add more regressions to the nested table
probit highbp age age2 race hct  [pw=weight], robust
asdocx margins , dydx(*) atmeans nest tzok

probit highbp age age2 race hct diabetes [pw=weight], robust
asdocx margins , dydx(*) atmeans nest tzok
Table: Regression results
(1) (2) (3)
highbp highbp highbp
age 0.019*** 0.019*** 0.019***
(0.002) (0.002) (0.002)
age2 -0.000*** -0.000*** -0.000***
(0.000) (0.000) (0.000)
race 0.081*** 0.099*** 0.096***
(0.013) (0.013) (0.013)
hct 0.017*** 0.017***
(0.001) (0.001)
diabetes 0.092***
(0.025)
Observations 10351 10351 10349
R2 0.096 0.106 0.107
Notes: Standard errors are in parentheses. *** p<.01, ** p<.05, * p<.1

Report additional statistics


Option stat() can be used to report additional regression statistics that are stored in macro e(). For example, stat(rmse, rss) will report the RMSE and RSS of the model. Please note that these statistics will be reported only if they are available in the e() macro. If they are not available, asdocx will highlight it as an error and the given statistic will be reported as missing. The e() macro can be listed with the command ereturn list. See the following example where I report the log-likelihood , Chi2 , and the RMSE statistics. These statistics are available the e() macro after ppmlhdfe command (available on the SSC).

* Load example data
 sysuse auto , clear 
 
* Run the first regression
 ppmlhdfe price mpg trunk weight
 
 * Create a nested table of margins and report
 * Chi2 and log-likelihood with the stat(chi2 ll) option

 asdocx margins , dydx(*) nest stat(chi2 ll rmse)
Table: Regression results
(1)
price
mpg -85.793
(110.308)
trunk -77.016
(87.486)
weight 1.831**
(0.917)
Observations 74
Pseudo R2 0.352
Chi2 51.205
Log likelihood -28485.307
RMSE 0.373
Notes: Standard errors are in parentheses. *** p<.01, ** p<.05, * p<.1
 * Add second model and report its margins
 ppmlhdfe price mpg trunk weight length 
 
 * Add margins of the model to the nested table
  asdocx margins , dydx(*) nest stat(chi2 ll rmse)

Table: Regression results
(1) (2)
price price
mpg -85.793 -117.416
(110.308) (99.751)
trunk -77.016 26.577
(87.486) (74.411)
weight 1.831** 3.824***
(0.917) (1.447)
length -96.741**
(47.846)
Observations 74 74
Pseudo R2 0.352 0.409
Chi2 51.205 58.819
Log likelihood -28485.307 -25981.841
RMSE 0.373 0.355
Notes: Standard errors are in parentheses. *** p<.01, ** p<.05, * p<.1

margins command with marginsplot


* Load the example dataset

* Estimate regression without using asdocx
webuse nhanes2d , clear

probit highbp age age2 race hct diabetes [pw=weight], robust

* Add a nested margin table with asdocx
asdocx margins , dydx(*) atmeans nest tzok replace

* Add the margins plot
asdocx marginsplot , margins_command(margins) append

asdocx clear

Stata Graph – Graph 0 .05 .1 .15 Effects on Pr(Highbp) age age2 race hct diabetes Effects with Respect to Conditional Marginal Effects with 95% CIs

Using 3 models


And, happily, this will also run as asdocx is using the necessary marging option post ‘below the surface’.

* Load the example dataset
webuse nhanes2d , clear

* Estimate regression without using asdocx
probit highbp age age2 race [pw=weight], robust

* Add asdocx to the margin command: replace, nest and tzok belong to asdocx
asdocx margins , dydx(*) atmeans replace nest tzok
estimates store M1

* Add more regressions to the nested table
probit highbp age age2 race hct [pw=weight], robust
asdocx margins , dydx(*) atmeans nest tzok append
estimates store M2

probit highbp age age2 race hct diabetes [pw=weight], robust
asdocx margins , dydx(*) atmeans nest tzok append
estimates store M3

* Include a marginsplot for each model (further comments, titles, could be included in the report)
estimates restore M1
asdocx marginsplot , margins_command(margins) append
estimates restore M2
asdocx marginsplot , margins_command(margins) append
estimates restore M3
asdocx marginsplot , margins_command(margins) append
asdocx clear