-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 output file or -putexcel-.

Installation

To install use the command: ssc install matrixtools

Demonstration

Background

The data is from Hosmer and Lemeshow, Applied logistic regression, 1989 and the description from Rachel MacKay Altman's old homepage

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), ///
        caption(Comparing confounders by exposure (*smoke*) )


Comparing confounders by exposure (*smoke*):
-------------------------------------------------------------------------------------------------------------------------
Columns by: smoked during pregnancy                                           No             Yes           Total  P-value
-------------------------------------------------------------------------------------------------------------------------
n (%)                                                                 115 (60.8)       74 (39.2)     189 (100.0)         
age of mother, mean (sd)                                              23.4 (5.5)      22.9 (5.0)      23.2 (5.3)     0.54
number of visits to physician during 1st trimester, median (iqi)  1.0 (0.0; 1.0)  0.0 (0.0; 1.0)  0.0 (0.0; 1.0)     0.30
number of visits to physician during 1st trimester, n (%)                                                                
  0                                                                    55 (47.8)       45 (60.8)      100 (52.9)         
  1                                                                    35 (30.4)       12 (16.2)       47 (24.9)         
  2                                                                    19 (16.5)       11 (14.9)       30 (15.9)         
  3-                                                                     6 (5.2)         6 (8.1)        12 (6.3)     0.12
presence, uterine irritability (Yes), n (%)                            15 (13.0)       13 (17.6)       28 (14.8)     0.39
-------------------------------------------------------------------------------------------------------------------------

gives the following summary table for exposure smoke

basetable smoke age(%6.1f) ftv(%6.1f, iqi) ftv(c) ui(Yes), ///
        caption(Comparing confounders by exposure (*smoke*) )


Comparing confounders by exposure (*smoke*):
-------------------------------------------------------------------------------------------------------------------------
Columns by: smoked during pregnancy                                           No             Yes           Total  P-value
-------------------------------------------------------------------------------------------------------------------------
n (%)                                                                 115 (60.8)       74 (39.2)     189 (100.0)         
age of mother, mean (sd)                                              23.4 (5.5)      22.9 (5.0)      23.2 (5.3)     0.54
number of visits to physician during 1st trimester, median (iqi)  1.0 (0.0; 1.0)  0.0 (0.0; 1.0)  0.0 (0.0; 1.0)     0.30
number of visits to physician during 1st trimester, n (%)                                                                
  0                                                                    55 (47.8)       45 (60.8)      100 (52.9)         
  1                                                                    35 (30.4)       12 (16.2)       47 (24.9)         
  2                                                                    19 (16.5)       11 (14.9)       30 (15.9)         
  3-                                                                     6 (5.2)         6 (8.1)        12 (6.3)     0.12
presence, uterine irritability (Yes), n (%)                            15 (13.0)       13 (17.6)       28 (14.8)     0.39
-------------------------------------------------------------------------------------------------------------------------

And likewise for exposure ht.

basetable ht age(%6.1f) ftv(%6.1f, iqi) ftv(c) ui(Yes), ///
        caption(Comparing confounders by exposure (*ht*) )


Comparing confounders by exposure (*ht*):
-------------------------------------------------------------------------------------------------------------------------
Columns by: has history of hypertension                                       No             Yes           Total  P-value
-------------------------------------------------------------------------------------------------------------------------
n (%)                                                                 177 (93.7)        12 (6.3)     189 (100.0)         
age of mother, mean (sd)                                              23.3 (5.4)      22.9 (4.4)      23.2 (5.3)     0.83
number of visits to physician during 1st trimester, median (iqi)  0.0 (0.0; 1.0)  0.0 (0.0; 1.0)  0.0 (0.0; 1.0)     0.32
number of visits to physician during 1st trimester, n (%)                                                                
  0                                                                    92 (52.0)        8 (66.7)      100 (52.9)         
  1                                                                    44 (24.9)        3 (25.0)       47 (24.9)         
  2                                                                    30 (16.9)         0 (0.0)       30 (15.9)         
  3-                                                                    11 (6.2)         1 (8.3)        12 (6.3)     0.46
presence, uterine irritability (Yes), n (%)                            28 (15.8)         0 (0.0)       28 (14.8)     0.14
-------------------------------------------------------------------------------------------------------------------------

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.ht) adjustments("" "age i.ftv i.ui") /// 
        title(Crude and adjusted estimates of the effect of *smoke' and *ht* on *bwt*) ///
        : regress


Crude and adjusted estimates of the effect of *smoke' and *ht* on *bwt*:
-----------------------------------------------------------------------------------------------------------------------------------
               Adjustment 1                                               Adjustment 2                                             
                          b   se(b)  Lower 95% CI  Upper 95% CI  P value             b   se(b)  Lower 95% CI  Upper 95% CI  P value
-----------------------------------------------------------------------------------------------------------------------------------
bwt  smoke(1)       -282.66  106.95       -493.65        -71.67     0.01       -228.92  105.10       -436.28        -21.55     0.03
     ht(1)          -435.17  215.71       -860.71         -9.62     0.04       -511.21  208.79       -923.18        -99.25     0.01
-----------------------------------------------------------------------------------------------------------------------------------

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.ht) 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 *ht* on *bwt*) ///
        labels base drop(se p): regress


Crude (Adjustment 1) and adjusted (Adjustment 2: *age*, *ftv* and *ui*) estimates of the effect of *smoke' and *ht* on *bwt*:
----------------------------------------------------------------------------------------------------------------------------------------
                                                      Adjustment 1                              Adjustment 2                            
                                                                 b  Lower 95% CI  Upper 95% CI             b  Lower 95% CI  Upper 95% CI
----------------------------------------------------------------------------------------------------------------------------------------
birthweight(grams)  smoked during pregnancy (No)              0.00                                      0.00                            
                    smoked during pregnancy (Yes)          -282.66       -493.65        -71.67       -228.92       -436.28        -21.55
                    has history of hypertension (No)          0.00                                      0.00                            
                    has history of hypertension (Yes       -435.17       -860.71         -9.62       -511.21       -923.18        -99.25
----------------------------------------------------------------------------------------------------------------------------------------

The return list contains the adjustments and the estimates in a matrix.

return list

macros:
       r(Adjustment_2) : "age i.ftv i.ui"
       r(Adjustment_1) : "Crude"

matrices:
             r(regmat) :  4 x 6

The estimates in the matrix looks like:

matprint r(regmat), decimals(3)


----------------------------------------------------------------------------------------------------------------------------------------
                                                      Adjustment 1                              Adjustment 2                            
                                                                 b  Lower 95% CI  Upper 95% CI             b  Lower 95% CI  Upper 95% CI
----------------------------------------------------------------------------------------------------------------------------------------
birthweight(grams)  smoked during pregnancy (No)             0.000                                     0.000                            
                    smoked during pregnancy (Yes)         -282.659      -493.652       -71.667      -228.915      -436.279       -21.552
                    has history of hypertension (No)         0.000                                     0.000                            
                    has history of hypertension (Yes      -435.165      -860.706        -9.624      -511.214      -923.175       -99.254
----------------------------------------------------------------------------------------------------------------------------------------

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 ///
        title(Crude (Adjustment 1) and adjusted (Adjustment 2: *age*, *ftv* and *ui*) ///
        estimates of the effect of *smoke' and *ht* on *bwt*) ///
        drop(se p): logit, vce(robust) or


Crude (Adjustment 1) and adjusted (Adjustment 2: *age*, *ftv* and *ui*) estimates of the effect of *smoke' and *ht* on *bwt*:
--------------------------------------------------------------------------------------------------------------------------------------
                                                    Adjustment 1                              Adjustment 2                            
                                                               b  Lower 95% CI  Upper 95% CI             b  Lower 95% CI  Upper 95% CI
--------------------------------------------------------------------------------------------------------------------------------------
birthweight<2500g    smoked during pregnancy (No)           0.00                                      0.00                            
                     smoked during pregnancy (Yes)          0.70          0.08          1.33          0.60         -0.06          1.26
                     race(white)                            0.00                                      0.00                            
                     race(black)                            0.84         -0.07          1.76          0.78         -0.18          1.74
                     race(other)                            0.64         -0.05          1.32          0.50         -0.21          1.21
birthweight < 1500g  smoked during pregnancy (No)           0.00                                      0.00                            
                     smoked during pregnancy (Yes)          0.04         -1.78          1.85         -0.05         -2.31          2.22
                     race(white)                            0.00                                      0.00                            
                     race(black)                            1.34         -1.48          4.15          1.11         -2.26          4.47
                     race(other)                            1.49         -0.80          3.78          1.61         -1.31          4.54
--------------------------------------------------------------------------------------------------------------------------------------

Estimates from primary and secondary outcomes can be aggregated by matrix operations.

matrix tbl2 = tbl2 \ r(regmat)
matprint tbl2, decimals(3) title(Crude and adjusted estimates of ///
        the effect of *smoke* and *ht* on both primary outcome (*bwt*) and ///
        secondary outcomes (*bwlt2500*, *bwlt1500*) )


Crude and adjusted estimates of the effect of *smoke* and *ht* on both primary outcome (*bwt*) and secondary outcomes (*bwlt2500*, *bwlt1500*):
-----------------------------------------------------------------------------------------------------------------------------------------
                                                       Adjustment 1                              Adjustment 2                            
                                                                  b  Lower 95% CI  Upper 95% CI             b  Lower 95% CI  Upper 95% CI
-----------------------------------------------------------------------------------------------------------------------------------------
birthweight(grams)   smoked during pregnancy (No)             0.000                                     0.000                            
                     smoked during pregnancy (Yes)         -282.659      -493.652       -71.667      -228.915      -436.279       -21.552
                     has history of hypertension (No)         0.000                                     0.000                            
                     has history of hypertension (Yes      -435.165      -860.706        -9.624      -511.214      -923.175       -99.254
birthweight<2500g    smoked during pregnancy (No)             0.000                                     0.000                            
                     smoked during pregnancy (Yes)            0.704         0.076         1.332         0.600        -0.060         1.259
                     race(white)                              0.000                                     0.000                            
                     race(black)                              0.845        -0.066         1.755         0.780        -0.183         1.743
                     race(other)                              0.636        -0.047         1.320         0.497        -0.214         1.209
birthweight < 1500g  smoked during pregnancy (No)             0.000                                     0.000                            
                     smoked during pregnancy (Yes)            0.036        -1.782         1.855        -0.045        -2.313         2.222
                     race(white)                              0.000                                     0.000                            
                     race(black)                              1.335        -1.479         4.149         1.106        -2.255         4.468
                     race(other)                              1.494        -0.798         3.785         1.613        -1.311         4.537
-----------------------------------------------------------------------------------------------------------------------------------------

The do file for this document

Last update: 2022-04-22, Stata version 17