-matprint-
Description
-matprint- prints and styles a matrix in text.
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).
Non standard style codes like pre table (option top), post header (option undertop) and post table (option bottom) can be added.
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.
-matprint- has the concept of styles (for smcl, html, tex/latex, md for markdown and csv) and a simple setting of decimals.
-matprint- can be used to export the content to Excel and saved in named sheets using the option toxl.
-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.
Installation
To install use the command: ssc install matrixtools
Demonstration
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 CIs from 2 regressions
Getting OR estimates and CIs into a matrix
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 = "M2"
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'
Presenting OR estimates and CIs using different styles
Now the results can be presented by matprint:
matprint b12, title(Regression summary table) decimals(3) replace
Regression summary table: ------------------------------------------------------ M1 M2 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 ------------------------------------------------------
Decimals can be set at cell level by specifying a matrix of decimals. Usually a single number giving the same number of cells in all cells, or a row vector specifying the number of decimals for each column.
matprint b12, title(Regression summary table) decimals(J(1,2,(1,2,2))) replace
Regression summary table: ---------------------------------------------- M1 M2 b ll ul b ll ul ---------------------------------------------- low age 1.0 0.89 1.03 lwt 1.0 0.97 1.00 1.0 0.97 1.00 smoke 1.7 0.89 3.42 1.8 0.90 3.44 ptl 1.8 0.92 3.59 1.7 0.87 3.33 ht 6.5 1.68 25.18 6.7 1.75 25.76 ui 2.1 0.86 5.13 2.2 0.89 5.25 ftv 1.0 0.73 1.44 ----------------------------------------------
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 M2 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 (MkDocs overrules code output here and show html as html):
matprint b12 using Reg_sum_tbl.html, title(Regression summary table) d(3) style(html) replace
Regression summary table M1 M2 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 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 \quad & & M1 & & & M2 & & \\ \quad & & b & ll & ul & b & ll & ul \\ \hline low & age & 0.958 & 0.893 & 1.026 & & & \\ \quad & lwt & 0.986 & 0.973 & 0.999 & 0.985 & 0.972 & 0.997 \\ \quad & smoke & 1.740 & 0.886 & 3.417 & 1.757 & 0.898 & 3.438 \\ \quad & ptl & 1.812 & 0.916 & 3.586 & 1.706 & 0.874 & 3.330 \\ \quad & ht & 6.502 & 1.679 & 25.176 & 6.716 & 1.751 & 25.756 \\ \quad & ui & 2.095 & 0.856 & 5.126 & 2.159 & 0.889 & 5.245 \\ \quad & 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.markdown, title(Regression summary table) d(3) style(md) replace
------------------------------------------------------------------------------------ M1: b ll ul M2: 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
Exporting matrix to Excel
The string matrix can also be exported to Excel (and then maybe copied into Word). The formats are set in -matprint-, which gives a nicer look from the beginning.
matprint b12, title(Regression summary table) d(3) toxl(tables, tbl1, replace)
Regression summary table: ------------------------------------------------------ M1 M2 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 saved in "tables.xlsx", in sheet "tbl1"...
The column widths in Excel can be reduced. Below the two first columns have width 10 and the rest have width 8. The default setting for Excel column widths is (25, 25, 15).
matprint b12, title(Regression summary table) d(3) toxl(tables, tbl2, replace, (10,10,8))
Regression summary table: ------------------------------------------------------ M1 M2 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 saved in "tables.xlsx", in sheet "tbl2"...
The saved Excel file can be seen here
Last update: 2022-04-19, Stata version 17