*! version 1.1 2025-12-31

Title

mrgtbl2 -- A margin-based approach to table 2 from a regname regression

Syntax

mrgtbl2
[allowed_regression_command (default is mixed)]
[if]
[,
options]

Required
outcome(varname) Specify the outcome for the report table 2

exposure(varname) Specify the exposure for the report table 2

Optional
by1(varname) Specify the eventual first stratification variable for the report table 2

by2(varname) Specify the eventual second stratification variable for the report table 2

adjustments(varlist) Specify the eventual adjustment variables for the report table 2

cluster(varname) Specify the eventual random intercept variable for the report table 2

constraints(numlist) apply specified linear constraints

bootstrap(string) Specify bootstrap estimation

vce(string) Specify vce estimation

regopts(passthru) Additional regression options

mrgopts(passthru) Additional margin options

btext(string) Alternative title for the reported margins

roweq(string) Add row equation text when possible

nolabel Ignore variable and value labels in the report table 2

EForm Report table 2 in exponential form, see lincom

wide Switch to wide format for the report table 2

noquietly Show the log output for the used regression and margin
commands

Description

The output from the mrgtbl2 command is based on margins
from the regression model: outcome = constant + i.exposure[#i.by1][#i.by2] +
adjustment variables.

The user specifies the regression method as one of regress,
cnsreg, mixed, glm, poisson,
nbreg, logit, probit,
cloglog, binreg, meprobit,
melogit, mepoisson, menbreg,
meglm, or mecloglog

Margins calculates metrics, such as linear predictions, probabilities, or
risk differences, based on a fitted model's response over a dataset where
some or all covariates are fixed at specific values.
Margins answers the question: "What does my model have to say about such-and-such scenario?"
The scenario in mrgtbl2 is the standard table 2, including confounding by the
option adjustments and stratification by the options by1 and by2.

There is a conceptual and numerical relationship between the margins
command and the Mantel–Haenszel method, as both tools are used to adjust for
confounding factors and provide comparable estimates across stratified groups.

In the absence of adjustments and stratifications, the output table is
applicable for reporting in RCTs.

Examples

Analyzing the effect of whether mothers are smoking on the birthweight
stratified by race and hypertension using the mixed regression command
(the default), adjusted for age, and with robust variance estimation in wide
format. A row equation label is added.

`". webuse lbw, clear"'

`". mrgtbl2, o(bwt) e(smoke) by(race) by2(ht) a(age) vce(robust) roweq(First) w"'

The same as above, with the report in long format (the default) and the
Option noquietly to display the regression and margin logging behind the Table 2 report.

`". mrgtbl2, o(bwt) e(smoke) by(race) by2(ht) a(age) vce(robust) noq"'

The command returns a list of allowed regressions, the applied regression
command, the applied margins commands, and the Table 2 matrix

`". return list"'

The estimates of the margins command are stored in _mrgtbl2 for further analysis.
A marginsplot of Table 2 is, for example, easily produced

`". marginsplot, x(race ht) horiz"'

If the result estimates _mrgtbl2 isn't active, it can be retrieved

`". estimates restore _mrgtbl2"'

And a list of estimates are found using estimates dir

`". estimates dir"'

Reporting the risk difference from smoking of giving birth to a child
with low birthweight based on a logistic regression adjusted for the mother's
age using bootstrap with 100 replications in the long report format

`". mrgtbl2 logit, o(low) e(smoke) a(age) bootstrap(reps(100) bca nodots) noq"'

Reporting the risk difference from smoking of giving birth to a child
with low birthweight based on a probit regression adjusted for the mother's
age, stratified by race, and using robust variance estimation

`". mrgtbl2 probit, o(low) e(smoke) by(race) a(age) vce(robust)"'

The same as above, using binreg and stratifying further for the prevalence
of hypertension

`". mrgtbl2 binreg, o(low) e(smoke) by(race) by2(ht) a(age) vce(robust)"'

Analyzing the effect of maternal smoking on birthweight using logistic
regression with robust variance estimation, reporting odds and odds ratios

`". mrgtbl2 logit, o(low) e(smoke) vce(robust) mrgopts(predict(xb)) eform roweq(crd)"'

Table 2 can be saved for further reporting

`". matrix tbl = r(mrgtbl2)"'

The same Table 2 as before, but now adjusted for maternal age.

`". mrgtbl2 logit, o(low) e(smoke) a(age) vce(robust) mrgopts(predict(xb)) eform roweq(age adj)"'

Combining the two Table 2's

`". matrix tbl = tbl \ r(mrgtbl2)"'

The combined Table 2

`". matlist tbl"'

Reporting incidence rates and rate differences on the effect of smoking on
dying using a Poisson regression, adjusting for age groups, and having
person-years in the exposure option

`". webuse dollhill3, clear"'

`". mrgtbl2 poisson, o(deaths) e(smokes) a(i.agecat) regopts(exposure(pyears)) mrgopts(predict(ir))"'

To get the incidence rate ratio of smoking on dying, use the returned
estimates (estimates _mrgtbl2 is active)

`". nlcom (IRR: _b[1.smokes] / _b[0.smokes])"'

An RCT adjusted for baseline outcome measurements is often modelled as a
random intercept mixed regression with no baseline treatment effect.
This can also be reported using the mrgtbl2.

A dataset is simulated for the demonstration.
The baseline outcome is Gaussian with a mean of 50 and a standard deviation of 3.
The time effect is 25, the correlation between baseline and follow-up is 0.6,
and the treatment effect is 5. The standard deviation at follow-up is 2.

`". clear"'

`". set seed 123"'

`". set obs 500"'

`". g id = _n"'

Generating the baseline and follow-up values of the outcome

`". g y0 = 50 + rnormal(0, 3)"'

`". g treat = rbinomial(1, 0.5)"'

`". label define treat 0 "ctrl" 1 "treatment (+5)""'

`". label values treat treat"'

`". g y1 = 25 + 0.6 * y0 + 5 * treat + rnormal(0, 2)"'

Making the dataset long for the mixed random intercept regression

`". reshape long y, i(id treat) j(tm)"'

`". label define tm 0 "BA" 1 "FU""'

`". label values tm tm"'

The constraint is no treatment effect at baseline

`". constraint 1 _b[1.treat#0.tm] = 0"'

`". mrgtbl2, o(y) e(treat) by1(tm) constraints(1) cluster(id)"'

The Stata versions 15.1 and 16.1 can only do the constrained random
intercept regression using the glm command for this analysis.
The glm command with constraints is around 50-60 times slower than the mixed
regression command with constraints.

`". mrgtbl2 meglm, o(y) e(treat) by1(tm) constraints(1) cluster(id)"'

Alternatively, this simplified analysis, based on cnsreg, returns similar but faster results

`". mrgtbl2 cnsreg, o(y) e(treat) by1(tm) constraints(1) vce(cluster id)"'

Stored results

Locals

r(poscmds) A list of accepted regressions

r(mrgcmd) The base margin command

r(regcmd) The base regression command

Matrices

r(mrgtbl2) The tabel 2 matrix

Authors and support

Author:

Niels Henrik Bruun,

Aalborg University Hospital

Support:

niels.henrik.bruun@gmail.com