Chapter 2: STATA Code Sample dataset codebook: treat = Binary indicator of treatment versus control group x1-x5 = continuous confounders associated with Treat cont_out = Continuous outcome of interest bin_out = Binary outcome of interest Estimating the propensity score in STATA with logistic regression STATA> logistic treat x1 x2 x3 x4 x5 STATA> predict pscore MATCHING USING PSMATCH2 PACKAGE // Install psmatch2.ado file STATA> findit psmatch2 // Sort individuals randomly before matching // Set random seed prior to psmatch2 to ensure replication STATA> set seed 1234 STATA> generate sort_id = uniform() STATA> sort sort_id K:1 matching, with and without replacement // 1:1 matching with replacement, estimate PS with logistic regression STATA> psmatch2 treat x1 x2 x3 x4 x5, logit // 2:1 matching without replacement STATA> psmatch2 treat x1 x2 x3 x4 x5, logit noreplace n(2) // 2:1 matching with replacement and caliper, PS previously estimated STATA> psmatch2 treat, pscore(pscore) n(2) cal(0.20) // Mahalanobis matching with caliper STATA> psmatch2 treat, mahal(x1 x2 x3 x4 x5) cal(0.10) Radius matching STATA> psmatch2 treat x1 x2 x3 x4 x5, logit radius caliper(0.10) Kernel matching // Kernel matching, PS estimated with logistic regression STATA> psmatch2 treat x1 x2 x3 x4 x5, kernel logit // Perform kernel matching, bandwidth=0.10 STATA> psmatch2 treat x1 x2 x3 x4 x5, kernel logit bwidth(0.10) // Estimate ATT for outcome variable(s) STATA> psmatch2 treat x1 x2 x3 x4 x5, kernel outcome(cont_out) Effect estimates // ATT (default estimand) for both outcome variables STATA> psmatch2 treat x1 x2 x3 x4 x5, outcome(cont_out bin_out) logit // Regression approach: Equivalent to ATT estimates from psmatch2 // First generate weights from psmatch2 STATA> psmatch2 treat x1 x2 x3 x4 x5, logit STATA> regress cont_out treat [iweight=_weight] if _weight!=. // Regression including covariates STATA> regress cont_out treat x1 x2 x3 x4 x5 [iweight=_weight] if _weight!=. Balance diagnostics // Balance table and plot STATA> pstest x1 x2 x3 x4 x5, both graph MATCHING USING TEFFECTS (STATA 13) Nearest neighbor matching // 1:1 Nearest Neighbor Matching with replacement, estimate ATT STATA> teffects psmatch (cont_out)(treat x1 x2 x3 x4 x5), nn(1) // 2:1 Nearest Neighbor Matching with replacement, estimate ATT STATA> teffects psmatch (cont_out)(treat x1 x2 x3 x4 x5), nn(2) effect atet effect atet Mahalanobis matching // 1:1 Mahalanobis matching, ATT effect STATA> teffects nnmatch (cont_out x1 x2 x3 x4 x5) (treat), atet MATCHING USING CEM PACKAGE Coarsened exact matching /* install cem package STATA> findit cem /* CEM with automatic binning STATA> cem x1 x2 x3 x4 x5, treatment(treat) /* CEM with user-specified cutpoints for x3 STATA> cem x1 x2 x3 (0 1.5 4 7 9 14) x4 x5, treatment(treat) /* Estimate treatment effects using weights since variable-ratio STATA> regress cont_out treat x1 x2 x3 x4 x5 [iweight=cem_weights] /* Restrict so that all strata contain the same number of treated and controls; no weights necessary in final analysis STATA> cem x1 x2 x3 x4 x5, treatment(treat) k2k /* Estimate treatment effects; no weighting STATA> regress cont_out treat x1 x2 x3 x4 x5 PROPENSITY SCORE WEIGHTING, PARAMETRIC PS ESTIMATION // Estimate the propensity score with logistic regression STATA> logistic treat x1 x2 x3 x4 x5 STATA> predict pscore // Calculate ATE propensity score weights (IPTW) STATA> gen w_ate = treat/pscore + (1-treat)/(1-pscore) // Use ATE weights as probability weights in final analysis STATA> svyset [pw=w_ate] STATA> svy: regress cont_out treat x1 x2 x3 x4 x5 // Calculate ATT propensity score weights STATA> gen w_att = treat + (1-treat)*(pscore/(1-pscore)) // Use ATT weights as probability weights in final analysis STATA> svyset [pw=w_att] STATA> svy: regress cont_out treat x1 x2 x3 x4 x5 SUBCLASSIFICATION Creating 5 propensity score subclasses // After generating propensity score, can create quintiles STATA> xtile pscore_5 = pscore, nq(5) Estimating subclass-specific and overall effect estimates // Binary outcome: Mantel-Haenszel stratified analysis STATA> cc bin_out treat, by(pscore_5) bd // Continuous outcome: Van Elteren test (Stratified Wilcoxon rank sum) /* install Van Elteren package STATA> findit vanelteren STATA> vanelteren cont_out, by(treat) strata(pscore_5)
© Copyright 2026 Paperzz