Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • New package on SSC: tabtools - publication-ready tables to Excel, Markdown, CSV and frames

    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:
    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")
    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:
    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")
    Survival and incidence rates:
    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)
    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:
    Code:
    tabtools set font Calibri
    tabtools set fontsize 10
    tabtools get
    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:
    Code:
    ssc install tabtools
    help tabtools
    help tabtools_tips
    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.

  • #2
    For those of you who prefer LaTeX, the GitHub version now has added LaTeX support. I'm not one to use LaTeX, but Kit mentioned tabtools would be more attractive to him if it was supported, so I figured, "why not." Holding off on SSC release for a bit so real users can test it out a bit first, but tabtools v.1.1.0 is now live on GitHub.
    Code:
    capture ado uninstall tabtools
    net install tabtools, from("https://raw.githubusercontent.com/tpcopeland/Stata-Tools/main/tabtools") replace

    Comment


    • #3
      This is such a useful package! Thanks for putting it out there.

      Comment


      • #4
        Originally posted by Erik Reinbergs View Post
        This is such a useful package! Thanks for putting it out there.
        Thanks! Please let me know if there's anything that would be broadly useful to users you'd like to see added--this goes for anyone reading this.

        Comment


        • #5
          This is a very useful package. table1_tc is so powerful. Thank you.
          I got an error message when I used regtab in Stata17: No active collect table found.
          It works very well in Stata 19. Can you check if there is an error in the ado?
          Attached Files

          Comment


          • #6
            Check whether -collect- had the relevant functionality in your version 17; the command's functionality has been increased over time/versions. Look at -help whatsnew-. Perhaps also https://www.stata.com/support/faqs/r...able-commands/

            Comment


            • #7
              Originally posted by Zumin Shi View Post
              This is a very useful package. table1_tc is so powerful. Thank you.
              I got an error message when I used regtab in Stata17: No active collect table found.
              It works very well in Stata 19. Can you check if there is an error in the ado?
              I wonder if it could be your particular installation is bugged? The Stata installation on my work's secure server refused to draw histograms until it was updated from 19 to 19.5 and the same exact histogram command on same sysuse datasets worked on multiple other Stata installations on different computers.

              My personal copy is Stata 17 MP, and as far as I can tell you shouldn't be having any problems. See the screenshot, where I reinstalled (after uninstalling, which was not pictured), then used a webuse dataset with something very similar. Run -update all- and see if your Stata version is fully up to date. The example I'm showing in the screenshot is working on Mac OS, Linux, and Windows Stata 17 MP installations at the "21 May 2024" current update level for Stata (you'll see which yours is at when you run -update all-
              Click image for larger version

Name:	Screenshot 2026-11-24 at 12.00.06 PM.png
Views:	1
Size:	368.0 KB
ID:	1788291

              Comment


              • #8
                Originally posted by Tim Copeland View Post

                I wonder if it could be your particular installation is bugged? The Stata installation on my work's secure server refused to draw histograms until it was updated from 19 to 19.5 and the same exact histogram command on same sysuse datasets worked on multiple other Stata installations on different computers.

                My personal copy is Stata 17 MP, and as far as I can tell you shouldn't be having any problems. See the screenshot, where I reinstalled (after uninstalling, which was not pictured), then used a webuse dataset with something very similar. Run -update all- and see if your Stata version is fully up to date. The example I'm showing in the screenshot is working on Mac OS, Linux, and Windows Stata 17 MP installations at the "21 May 2024" current update level for Stata (you'll see which yours is at when you run -update all-
                [ATTACH=CONFIG]n1788291[/ATTACH]
                Thank you for your suggestion. As I worked in a virtual machine using Stata 17 with no Internet access, I have requested the admin team to install Stata 19. There is no such error anymore.

                Comment

                Working...
                X