What is -matprint-

The command -matprint- is for styling and printing Stata matrices.

The styles can be smcl for nice outlook in the log (and when eg saved in the using file the output can be used in smcl documentation files), csv for a semicolon separated output html, tex/latex and md (markdown).

The styles can be added non standard style code pre table (option top), post header (option undertop) and post table (option bottom).

A title/caption for the table can be added as well as a column title for roweq names (option rowtitle1)

Number of decimals can be set at cell level in option decimals by specifying a matrix of integers. If the decimal matrix is smaller than the data matrix first the right most column of integers are copied and then the bottom row is also copied to get the same size.
If the decimal matrix is too big only the top left part of the decimals numbers matching the size of the data matrix is used.

The command -matprint- is a mix of matlist and if installed estout for matrices.
The command -matlist- to me is too detailed with respect to controlling the smcl output and too little detailed with respect to styles. The command -estout- has the concept of styles and a simple setting of decimals. However presenting and styling matrices is not the main focus. And also here there are too much functionality.

-matprint- is highly integrated with -log2markup- such that a matrix in a layout of eg html or latex can be merged to the final document generated by -log2markup- in the same style as the end document.

-matprint- is a part of the package matrixtools.

Syntax

The syntax is: matprint matrixname [using] [[, options]

Here "matrixname" is the matrix to print and/or style.

The styled matrix can be save as file using the using modifier.

Options

Versions

-matprint- is tested in version 12.1 ic, 13.1 ic, and 14.2 ic.

Installation

To install use the command: ssc install matrixtools

A demonstration of -matprint-

The example data

We use a subset of the Stata hospid2 dataset:


use low-bwt using "http://www.stata-press.com/data/r12/hospid2.dta", clear
metadata *

-----------------------------------------------------------------------------------------------------------------------------------------------
Name   Index  Label                                               Value Label Name  Format  Value Label Values               n  unique  missing
-----------------------------------------------------------------------------------------------------------------------------------------------
low        1  birth weight<2500g                                                    %8.0g                                  189       2        0
age        2  age of mother                                                         %8.0g                                  189      24        0
lwt        3  weight at last menstrual period                                       %8.0g                                  189      76        0
race       4  race                                                race              %8.0g   1 "white" 2 "black" 3 "other"  189       3        0
smoke      5  smoked during pregnancy                                               %8.0g                                  189       2        0
ptl        6  premature labor history (count)                                       %8.0g                                  189       4        0
ht         7  has history of hypertension                                           %8.0g                                  189       2        0
ui         8  presence, uterine irritability                                        %8.0g                                  189       2        0
ftv        9  number of visits to physician during 1st trimester                    %8.0g                                  189       6        0
bwt       10  birth weight (grams)                                                  %8.0g                                  189     133        0
-----------------------------------------------------------------------------------------------------------------------------------------------

Presenting OR estimates and CI from 2 regressions

Do the first regression, select OR estimates and CI for all explanatory variables, and set roweq for the result to M1:


logit low age lwt smoke ptl ht ui ftv, or nolog cformat(%6.3f)
mat b1 = r(table)
mat b1 = b1["b", "low:age".."low:ftv"] \ b1["ll".."ul", "low:age".."low:ftv"]
mat roweq b1 = M1

And the same for a reduced model (no age and ftv):


logit low lwt smoke ptl ht ui, or nolog cformat(%6.3f)
mat b2 = r(table)
mat b2 = b2["b", "low:lwt".."low:ui"] \ b2["ll".."ul", "low:lwt".."low:ui"]
mat roweq b2 = "M1 minus age, ftv"

Stata has a command -mat_rapp- that merges matrices on rownames:


mat_rapp b12 : b1 b2, miss(.) cons

The merged matrix is transposed:


mat b12 = b12'

Now the results can be presented by matprint:


matprint b12, title(Regression summary table) decimals(3) replace

Regression summary table:
------------------------------------------------------------------
               M1                 M1 minus age, ftv               
                b     ll      ul                  b     ll      ul
------------------------------------------------------------------
low  age    0.958  0.893   1.026                  .      .       .
     lwt    0.986  0.973   0.999              0.985  0.972   0.997
     smoke  1.740  0.886   3.417              1.757  0.898   3.438
     ptl    1.812  0.916   3.586              1.706  0.874   3.330
     ht     6.502  1.679  25.176              6.716  1.751  25.756
     ui     2.095  0.856   5.126              2.159  0.889   5.245
     ftv    1.024  0.729   1.437                  .      .       .
------------------------------------------------------------------

The table can be saved in a file in smcl format:


matprint b12 using Reg_sum_tbl.smcl, title(Regression summary table) d(3) replace

Regression summary table:
------------------------------------------------------------------
               M1                 M1 minus age, ftv               
                b     ll      ul                  b     ll      ul
------------------------------------------------------------------
low  age    0.958  0.893   1.026                  .      .       .
     lwt    0.986  0.973   0.999              0.985  0.972   0.997
     smoke  1.740  0.886   3.417              1.757  0.898   3.438
     ptl    1.812  0.916   3.586              1.706  0.874   3.330
     ht     6.502  1.679  25.176              6.716  1.751  25.756
     ui     2.095  0.856   5.126              2.159  0.889   5.245
     ftv    1.024  0.729   1.437                  .      .       .
------------------------------------------------------------------

The saved file can be seen here

The table can be saved in a file in html format:


matprint b12 using Reg_sum_tbl.html, title(Regression summary table) d(3) style(html) replace

Regression summary table
M1 M1 minus age, ftv
b ll ul b ll ul
lowage 0.9580.893 1.026 . . .
lwt 0.9860.973 0.999 0.9850.972 0.997
smoke1.7400.886 3.417 1.7570.898 3.438
ptl 1.8120.916 3.586 1.7060.874 3.330
ht 6.5021.67925.176 6.7161.75125.756
ui 2.0950.856 5.126 2.1590.889 5.245
ftv 1.0240.729 1.437 . . .

The saved file can be seen here

The table can be saved in a file in latex format:


matprint b12 using Reg_sum_tbl.tex, title(Regression summary table) d(3) style(tex) replace

\begin{table}[h]
\centering
\caption{Regression summary table}
\begin{tabular}{llrrrrrr}
\hline
\hline
    &       &    M1 &       &        & M1 minus age, ftv &       &        \\
    &       &     b &    ll &     ul &                 b &    ll &     ul \\
\hline
low & age   & 0.958 & 0.893 &  1.026 &                 . &     . &      . \\
    & lwt   & 0.986 & 0.973 &  0.999 &             0.985 & 0.972 &  0.997 \\
    & smoke & 1.740 & 0.886 &  3.417 &             1.757 & 0.898 &  3.438 \\
    & ptl   & 1.812 & 0.916 &  3.586 &             1.706 & 0.874 &  3.330 \\
    & ht    & 6.502 & 1.679 & 25.176 &             6.716 & 1.751 & 25.756 \\
    & ui    & 2.095 & 0.856 &  5.126 &             2.159 & 0.889 &  5.245 \\
    & ftv   & 1.024 & 0.729 &  1.437 &                 . &     . &      . \\
\hline
\hline
\end{tabular}
\end{table}

The saved file can be seen here

The table can be saved in a file in pandoc markdown format:


matprint b12 using Reg_sum_tbl.md, title(Regression summary table) d(3) style(md) replace

------------------------------------------------------------------------------------------------
                         M1                             M1 minus age, ftv                       
                          b         ll          ul                      b         ll          ul
-----    -------    -------    -------    --------    -------------------    -------    --------
low      age          0.958      0.893       1.026                      .          .           .

         lwt          0.986      0.973       0.999                  0.985      0.972       0.997

         smoke        1.740      0.886       3.417                  1.757      0.898       3.438

         ptl          1.812      0.916       3.586                  1.706      0.874       3.330

         ht           6.502      1.679      25.176                  6.716      1.751      25.756

         ui           2.095      0.856       5.126                  2.159      0.889       5.245

         ftv          1.024      0.729       1.437                      .          .           .
------------------------------------------------------------------------------------------------
Table: Regression summary table

The saved file can be seen here


The do file for this document

Last update: 2017-03-31