tabtools
Stata社区命令

概述

tabtools是一个绘制出版级别表格的Stata社区命令,包含table1_tc、crosstab、corrtab、regtab、effecttab、comptab、survtab、hrtab、stratetab、diagtab、fittab、tablex和tabtools命令。

依赖

Stata 17 及以上

安装

net install tabtools, from("https://raw.githubusercontent.com/tpcopeland/Stata-Tools/main/tabtools") replace

语法

帮助一览表

帮助命令功能描述
help tabtools套件概览与设置说明
help table1_tc描述性统计(Table 1)
help regtab回归结果表格
help effecttab处理效应与边际效应表
help comptab从框架生成复合表格
help crosstab交叉分类表
help corrtab相关矩阵
help survtab生存分析摘要表
help hrtab风险比(Hazard Ratio)表格
help stratetab分层发生率表
help diagtab诊断准确性评估表
help fittab模型比较表格
help tablex通用表格导出工具

示例

Table 1

webuse nhanes2, clear
table1_tc, by(diabetes) ///
    vars(age contn %5.1f \ female bin \ race cat \ ///
         bmi contn %5.1f \ highbp bin) ///
    excel("table1.xlsx") sheet("Baseline") ///
    title("Table 1. Baseline Characteristics by Diabetes Status") ///
    smd footnote("SMD = standardized mean difference") zebra

Cross-Tabulation

webuse nhanes2, clear
crosstab diabetes highbp, xlsx("crosstab.xlsx") ///
    or colpct exact ///
    title("Cross-tabulation: Diabetes by Hypertension")

Correlation Matrix

sysuse auto, clear
corrtab price mpg weight length, xlsx("correlations.xlsx") ///
    star(0.05 0.01 0.001) lower ///
    title("Correlation Matrix: Vehicle Characteristics")

Regression Results

sysuse auto, clear
collect clear
collect: regress price mpg weight length i.foreign
regtab, xlsx("regression.xlsx") sheet("OLS") ///
    title("Table 2. Predictors of Vehicle Price") ///
    stats(r2) boldp(0.05)

Logistic Regression

webuse nhanes2, clear
collect clear
collect: logit diabetes age female i.race bmi highbp
regtab, xlsx("logistic.xlsx") sheet("Model") ///
    title("Odds Ratios for Diabetes") ///
    noint boldp(0.05)

Treatment Effects

webuse cattaneo2, clear
collect clear
collect: teffects ipw (bweight) (mbsmoke mage medu, logit), ate
effecttab, xlsx("effects.xlsx") sheet("ATE") ///
    title("ATE of Maternal Smoking on Birth Weight") ///
    effect("ATE") tlabels(0 "Non-smoker" 1 "Smoker")

Composite Table

webuse nhanes2, clear
* Model 1: age and sex only
collect clear
collect: logit diabetes age female
regtab, xlsx("models.xlsx") sheet("M1") frame(m1) noint
* Model 2: add comorbidities
collect clear
collect: logit diabetes age female bmi highbp
regtab, xlsx("models.xlsx") sheet("M2") frame(m2) noint
* Combine selected rows
comptab m1 m2, rows(1/2 \ 1/4) ///
    xlsx("models.xlsx") sheet("Composite") ///
    section("Unadjusted" \ "Adjusted") ///
    title("Table 3. Logistic Regression Models") ///
    footnote("OR with 95% CI; bold p < 0.05") boldp(0.05)

Survival Summary

webuse drugtr, clear
stset studytime, failure(died)
survtab, times(5 10 15 20) by(drug) ///
    median rmst(20) ///
    xlsx("survival.xlsx") sheet("KM") ///
    title("Table 4. Survival by Treatment Group") ///
    timeunit("months")

Hazard Ratios

webuse drugtr, clear
gen id = _n
hrtab, exposure(i.drug) model(stcox) ///
    outcome(died) time(studytime) stsetopts(id(id)) ///
    covars(age) ///
    xlsx("hazard_ratios.xlsx") sheet("HR") ///
    title("Table 5. Hazard Ratios by Treatment Group") ///
    pvalue display

Incidence Rates

* After running strate commands and saving results:
stratetab, using(rate_control rate_treated) xlsx("rates.xlsx") ///
    outcomes(2) outlabels("Primary Endpoint \ Secondary Endpoint") ///
    title("Incidence Rates per 1,000 Person-Years") ///
    rateratio ratiodigits(2)

Diagnostic Accuracy

webuse nhanes2, clear
generate byte bmi_high = (bmi >= 30) if !missing(bmi)
diagtab bmi_high diabetes, xlsx("diagnostic.xlsx") ///
    wilson ///
    title("Diagnostic Accuracy: Obesity as Predictor of Diabetes")

Model Comparison

sysuse auto, clear
regress price mpg weight
estimates store m1
regress price mpg weight length
estimates store m2
regress price mpg weight length i.foreign
estimates store m3
fittab m1 m2 m3, xlsx("fit.xlsx") ///
    stats(N aic bic r2) ///
    labels("Base" "Extended" "Full") ///
    title("Model Comparison: Vehicle Price") lrtest(m1)

General Table Export

sysuse auto, clear
table (foreign) (), statistic(mean price mpg weight) ///
    statistic(sd price mpg weight)
tablex using "summary.xlsx", sheet("Summary") ///
    title("Summary Statistics by Origin")

Console Preview

* Omit xlsx() to display results in the console only
sysuse auto, clear
collect clear
collect: regress price mpg weight i.foreign
regtab, boldp(0.05) stats(r2)

作者

姓名:Timothy P Copeland
机构:Department of Clinical Neuroscience, Karolinska Institutet 
邮箱:timothy.copeland@ki.se
网页:https://github.com/tpcopeland/Stata-Tools/tree/main/tabtools

×
重塑学科基石

欢迎有闲之士完善公卫百科

值得:你的点滴投入,大家长期受益

回报:VIP会员特权,大额钢镚奖励