Thanks to Kit Baum, a new package, tabtools, is now available on SSC. It requires Stata 17 or later.
tabtools is a suite of 16 commands for getting the tables of an applied paper out of Stata: Table 1, regression and treatment-effect tables, survival summaries, incidence rates, and composites of all of these. Each command renders the table in the Results window and can write the same table to a formatted Excel sheet (xlsx()), a GitHub-flavored Markdown file (markdown()), a CSV file (csv()), or a Stata frame (frame()), in one call. Kit mentioned he'd be interested in LaTeX exports, so that's a "coming soon" feature that will land on the GitHub version first, so stay tuned for that.
The commands:
- Descriptive: table1_tc (Table 1 by group, with tests, standardized mean differences, optional weights, and small-cell suppression), desctab (the same engine called directly), crosstab (two-way tables with tests, and odds ratios, risk ratios, risk differences and trend tests for 2x2 and ordered tables)
- Models: regtab (the active collect of a regression: regress, logit, stcox, poisson, mixed, mlogit and so on, including several models side by side), effecttab (teffects and margins), outtab (binary outcomes by exposure, with events/N (%) and one ratio column per model), tabcell (one formatted cell for tables you build by hand)
- Survival and rates: survtab (Kaplan-Meier estimates at chosen times, medians, RMST), ratetab (events, person-time and rates with exact, Poisson or cluster-robust intervals), stratetab (rates and rate ratios from strate, output() files)
- Assembly: comptab and hrcomptab (selected rows from several model tables, or model rows next to a rate table), puttab (style any dataset, frame or matrix as a sheet), stacktab (combine blocks from sheets already written)
- Utility: tabtools (session and permanent formatting defaults), tabtools_tips (worked recipes)
A Table 1:
Regression models side by side. regtab reads the results collected by collect:, labels the estimate column from the model (OR after logit, HR after stcox, IRR after poisson, Coef. after regress), and puts several models in one table:
Survival and incidence rates:
studytime is in months, so pyscale(12) converts person-time to years; ratetab does not guess the unit and prints a note when it is not told.
Leave out xlsx() and the table is only displayed, which is a quick way to preview it. markdown("report.md") writes the same table as Markdown, and mdappend adds further tables to the same file, which is useful for reports and for sharing results by e-mail or on GitHub.
Some other points:
1. Small-cell suppression. smallcells(#) in table1_tc, desctab and crosstab suppresses small counts, together with the complementary cells that would let them be recovered by subtraction, before anything is written, so every output receives the same suppressed table.
2. Confidence levels are checked rather than assumed: an explicit level() that conflicts with the level stored in a collection or a saved strate file is an error, not a relabelled interval.
3. Shared formatting options (font(), fontsize(), borderstyle(), zebra, boldp(), title(), footnote() and others) work the same way across commands, and tabtools set stores your preferences for the session, or permanently with the permanent option:
4. Results are returned in r(), including the rendered table and, for most commands, a methods sentence in r(methods) describing the tests or estimators used.
5. regtab and effecttab can also write a graph-ready frame with eplotframe(), which my eplot package (also on SSC) turns into a forest plot.
desctab, the engine behind table1_tc, is a fork of Mark Chatfield's table1_mc (version 3.5), extended with Excel and Markdown export, explicit formatting, variable-type detection, weighting, standardized mean differences, small-cell suppression and the methods sentence. My thanks to Mark for table1_mc, which I used for years.
To install:
The source, a README with worked examples of every command, and an issue tracker are at https://github.com/tpcopeland/Stata-.../main/tabtools. Bug reports and suggestions are welcome, here or on GitHub.
tabtools is a suite of 16 commands for getting the tables of an applied paper out of Stata: Table 1, regression and treatment-effect tables, survival summaries, incidence rates, and composites of all of these. Each command renders the table in the Results window and can write the same table to a formatted Excel sheet (xlsx()), a GitHub-flavored Markdown file (markdown()), a CSV file (csv()), or a Stata frame (frame()), in one call. Kit mentioned he'd be interested in LaTeX exports, so that's a "coming soon" feature that will land on the GitHub version first, so stay tuned for that.
The commands:
- Descriptive: table1_tc (Table 1 by group, with tests, standardized mean differences, optional weights, and small-cell suppression), desctab (the same engine called directly), crosstab (two-way tables with tests, and odds ratios, risk ratios, risk differences and trend tests for 2x2 and ordered tables)
- Models: regtab (the active collect of a regression: regress, logit, stcox, poisson, mixed, mlogit and so on, including several models side by side), effecttab (teffects and margins), outtab (binary outcomes by exposure, with events/N (%) and one ratio column per model), tabcell (one formatted cell for tables you build by hand)
- Survival and rates: survtab (Kaplan-Meier estimates at chosen times, medians, RMST), ratetab (events, person-time and rates with exact, Poisson or cluster-robust intervals), stratetab (rates and rate ratios from strate, output() files)
- Assembly: comptab and hrcomptab (selected rows from several model tables, or model rows next to a rate table), puttab (style any dataset, frame or matrix as a sheet), stacktab (combine blocks from sheets already written)
- Utility: tabtools (session and permanent formatting defaults), tabtools_tips (worked recipes)
A Table 1:
Code:
sysuse auto, clear
table1_tc, by(foreign) vars(price contn %9.0fc \ mpg contn %4.1f \ weight contn %9.0fc \ rep78 cat) smd test xlsx("tables.xlsx") sheet("Table 1") title("Table 1. Characteristics by origin")
Code:
webuse nhanes2, clear
collect clear
collect: logit diabetes age female bmi
collect: logit diabetes age female bmi i.race highbp
regtab, xlsx("tables.xlsx") sheet("Table 2") models("Model 1 \ Model 2") noint boldp(0.05) title("Table 2. Odds ratios for diabetes")
Code:
webuse drugtr, clear
stset studytime, failure(died)
survtab, times(5 10 20) by(drug) median rmst(20) timeunit(months) xlsx("tables.xlsx") sheet("Table 3")
ratetab drug, ci(poisson) per(100) pyscale(12)
Leave out xlsx() and the table is only displayed, which is a quick way to preview it. markdown("report.md") writes the same table as Markdown, and mdappend adds further tables to the same file, which is useful for reports and for sharing results by e-mail or on GitHub.
Some other points:
1. Small-cell suppression. smallcells(#) in table1_tc, desctab and crosstab suppresses small counts, together with the complementary cells that would let them be recovered by subtraction, before anything is written, so every output receives the same suppressed table.
2. Confidence levels are checked rather than assumed: an explicit level() that conflicts with the level stored in a collection or a saved strate file is an error, not a relabelled interval.
3. Shared formatting options (font(), fontsize(), borderstyle(), zebra, boldp(), title(), footnote() and others) work the same way across commands, and tabtools set stores your preferences for the session, or permanently with the permanent option:
Code:
tabtools set font Calibri tabtools set fontsize 10 tabtools get
5. regtab and effecttab can also write a graph-ready frame with eplotframe(), which my eplot package (also on SSC) turns into a forest plot.
desctab, the engine behind table1_tc, is a fork of Mark Chatfield's table1_mc (version 3.5), extended with Excel and Markdown export, explicit formatting, variable-type detection, weighting, standardized mean differences, small-cell suppression and the methods sentence. My thanks to Mark for table1_mc, which I used for years.
To install:
Code:
ssc install tabtools help tabtools help tabtools_tips

Comment