/*
shell pandoc -s regmat_demo.md -o regmat_demo.html & timeout 30
shell regmat_demo.html
*/
//OFF
cd "C:\Users\au24026\Documents\stata\StataHacks\docs\My commands\regmat"
if `c(version)' >= 13 cls
use "lbw.dta", clear
//ON
/***
# What is -regmat-
regmat is prefix command to a regression template.
A regression template is simply a regression command.
Each combination of one outcome variable, one exposure variable and one
adjustment set is inserted just after the regression command.
Then all regression estimates of exposures are placed in a matrix ordered by
outcome and exposure variables rowwise and adjustment columnwise.
The resulting matrix is saved in the return list for further usage.
The matprint options makes it easy to integrate the result table into a
log2markup output file.
Together with the basetable the command regmat generates the two typical tables
for reporting epidemiological research.
The resulting tables are easy to integrate into the final text using eg.
[log2markup](../../log2markup/log2markup_demo/) output file or
-putexcel-.
-regmat- is a part of the package matrixtools.
## Syntax
The syntax is: `regmat [using], outcomes(varlist) exposures(varlist) [adjustements(varlist strings) noquietly labels base keep(string) drop(string) Matprint options] , regression template`
## Options
* **outcomes**(varlist): A non-empty varlist of outcome variables.
An outcome is the dependent variable in a regression.
* **exposures**(varlist): A non-empty varlist of outcome variables.
Exposures are variables of whiech estimates are to be reported.
* **adjustments**(string): A set of varlist strings.
A varlist string is a possibly empty set of adjustment variables.
Each varlist string is surrounded in text quotes (").
An empty string ("") means no adjustment.
Adjustment variables are variables needed for the estimation of the exposures,
but it is not necessary to report their estimates.
* **noquietly**: If set, regression outputs are printed in the log.
* **labels**: Use variable and value labels.
* **base**: Include base values at factor variables.
* **keep**: To style output choose which calculations to keep.
Choices are: b(=estimate of exposure in regression), se(=Se(estimate)), ci(=Confidence interval - level is set with set level), and p(=P-value).
* **drop**: To style output choose which calculations to drop.
Choices are: b(=estimate of exposure in regression), se(=Se(estimate)), ci(=Confidence interval - level is set with set level), and p(=P-value).
[To see -matprint- options](../../matprint/matprint_demo/)
## Stored results
-regmat- stores the following in r():
**local**
* **r(Adjustment_#)** Adjustment number #.
**Matrix**
* **r(regmat)** The matrix containing regression estimates of the exposures
## Versions
-regmat- is tested in version 12.1 ic, 13.1 ic, 14.2 ic, and 15.1 ic.
## Installation
To install use the command: `ssc install matrixtools`
# A demonstration of -regmat-
## Background
The data is from [Hosmer and Lemeshow, Applied logistic regression, 1989](https://books.google.dk/books/about/Applied_logistic_regression.html?id=-p5qAAAAMAAJ&redir_esc=y)
and the description from [Rachel MacKay Altman's old homepage](http://www.stat.sfu.ca/~raltman/stat300/DataSet5.doc)
Low birth weight is an outcome that has been of concern to physicians for years.
This is due to the fact that infant mortality rates and birth defect rates are
very high for low birth weight babies.
A woman's behaviour during pregnancy (including diet, smoking habits, and
receiving prenatal care) can greatly alter the chances of carrying the baby to
term and, consequently, of delivering a baby of normal birth weight.
## Objective
The objective here is to see if smoking during pregnancy has an effect on birth
weights.
## The dataset
`webuse lbw, clear`
***/
/**/rename low bwlt2500
/**/generate bwlt1500 = bwt < 1500 if !missing(bwt)
/**/label variable bwlt1500 "birthweight < 1500g"
/**/label define no_yes 0 "No" 1 "Yes"
/**/label values ht ui smoke bwlt* no_yes
/**/replace ftv = 3 if ftv > 3
/**/label define ftv 3 "3-"
label values ftv ftv
/***
## Outcomes, exposures, and confounders
Primary outcome is birthweight in grams (*bwt*).
Two secondary outcomes are whether a newborn weighs less than 2500 grams
(bwlt2500) and weighs less than 1500 grams (bwlt1500).
Exposure is whether the mothers has been smoking or not during pregnancy
(*smoke*) and whether the mothers have had hypertension or not (*ht*).
Confounders to be included are number of visits to physician during 1st
trimester (*ftv*), mothers age (*age*), mothers race (*race*), and presence of
uterine irritability (*ui*).
## The Analysis
Using the -basetable- command
***/
/**/basetable smoke age(%6.1f) ftv(%6.1f, iqi) ftv(c) ui(Yes), style(html) ///
caption(Comparing confounders by exposure (*smoke*) )
/***
gives the following summary table for exposure *smoke*
***/
/****/basetable smoke age(%6.1f) ftv(%6.1f, iqi) ftv(c) ui(Yes), style(html) ///
caption(Comparing confounders by exposure (*smoke*) )
/***
And likewise for exposure *race*.
***/
/****/basetable race age(%6.1f) ftv(%6.1f, iqi) ftv(c) ui(Yes), style(html) ///
caption(Comparing confounders by exposure (*race*) )
/***
Crude and adjusted estimates of the effect of *smoke* and *ht* on the primary
outcome *bwt* can be summarised by the command
***/
/**/regmat, outcomes(bwt) exposure(i.smoke i.race) adjustments("" "age i.ftv i.ui") ///
title(Crude and adjusted estimates of the effect of *smoke' and *race* on *bwt*) ///
style(html): regress
/****/regmat, outcomes(bwt) exposure(i.smoke i.race) adjustments("" "age i.ftv i.ui") ///
title(Crude (Adjustment 1) and adjusted (Adjustment 2: *age*, *ftv* and *ui*) ///
estimates of the effect of *smoke' and *race* on *bwt*) ///
style(html): regress
/***
Option *labels* let one see labels if possible.
Option *base* adds base values in output.
Option *drop(se p)* removes the se and p columns from output.
***/
/**/regmat, outcome(bwt) exposure(i.smoke i.race) adjustments("" "age i.ftv i.ui") ///
title(Crude (Adjustment 1) and adjusted (Adjustment 2: *age*, *ftv* and *ui*) ///
estimates of the effect of *smoke' and *race* on *bwt*) ///
labels base style(html) drop(se p): regress
/****/regmat, outcome(bwt) exposure(i.smoke i.race) adjustments("" "age i.ftv i.ui") ///
title(Crude (Adjustment 1) and adjusted (Adjustment 2: *age*, *ftv* and *ui*) ///
estimates of the effect of *smoke' and *race* on *bwt*) ///
labels base style(html) drop(se p): regress
/***
The return list contains the adjustments and the estimates in a matrix.
***/
return list
/***
The estimates in the matrix looks like:
***/
matprint r(regmat), decimals(3)
/***
This matrix is saved for later use.
***/
matrix tbl2 = r(regmat)
/***
Crude estimates as well as estimates adjusted for *age*, *ftv*, and *ui* of
the effects of *smoke* and *ht* on the two secondary outcomes *bwlt2500* and
*bwlt1500* is presented below.
Note that the same type of regression (template: logit, vce(robust) or) is used.
***/
/**/regmat, outcome(bwlt2500 bwlt1500) exposure(i.smoke i.race) ///
adjustments("" "age i.ftv i.ui") labels base style(html) ///
title(Crude (Adjustment 1) and adjusted (Adjustment 2: *age*, *ftv* and *ui*) ///
estimates of the effect of *smoke' and *race* on *bwt*) ///
drop(se p): logit, vce(robust) or
/****/regmat, outcome(bwlt2500 bwlt1500) exposure(i.smoke i.race) ///
adjustments("" "age i.ftv i.ui") labels base style(html) ///
title(Crude (Adjustment 1) and adjusted (Adjustment 2: *age*, *ftv* and *ui*) ///
estimates of the effect of *smoke' and *race* on *bwt*) ///
drop(se p): logit, vce(robust) or
/***
Estimates from primary and secondary outcomes can be aggregated by matrix
operations.
***/
/**/matrix tbl2 = tbl2 \ r(regmat)
/**/matprint tbl2, decimals(3) style(html) title(Crude and adjusted estimates of ///
the effect of *smoke* and *ht* on both primary outcome (*bwt*) and ///
secondary outcomes (*bwlt2500*, *bwlt1500*) )
/****/matprint tbl2, decimals(3) style(html) ///
title(Crude (Adjustment 1) and adjusted (Adjustment 2: *age*, *ftv* and *ui*) ///
estimates of the effect of *smoke' and *race* on both primary outcome ///
(*bwt*) and secondary outcomes (*bwlt2500*, *bwlt1500*) )