Dear Statalist members,
I am using Stata collect to build a custom table that combines results from a mixed model and its margins contrasts for two outcomes. My goal is to have two row groups:
However, any attempt to rename the interaction term using collect label levels colname 1.exposure#1.period ... changes the label globally in both groups, because colname is a single dimension.
I tried these but to no avail.
Because the interaction term 1.exposure#1.period is the same level of colname in both groups, any label applied to it affects both groups. I need a way to tell Stata: when this coefficient appears in the group "Marginal_contrast", use label A; when it appears in group "Other_model_parameters", use label B – while still displaying the group headers as separate row sections.
I am looking for a way to assign group‑specific labels to the same coefficient without flattening the group dimension into the row labels. Is there a way to achieve this with collect (perhaps using collect style with conditions, or a different approach to stacking collections) that preserves the grouping structure?
Thank you for any insights!
Below is my code and data from dataex.
Data
I am using Stata collect to build a custom table that combines results from a mixed model and its margins contrasts for two outcomes. My goal is to have two row groups:
- Marginal contrast (from margins, contrast)
- Other model parameters (from the main estimation, e.g., main effects, constant)
Code:
------------------------------------------------------------------------------------------------------
| Contraceptive self-efficacy Future aspirations score
| Coefficient [95% CI] p-value Coefficient [95% CI] p-value
-----------------------------------------+------------------------------------------------------------
Marginal contrast |
Interaction (Period × Exposure) | 0.581* [0.334, 0.828] 0.000 1.051* [0.312, 1.790] 0.005
Other model parameters |
Interaction (Period × Exposure) | 0.581* [0.334, 0.828] 0.000 1.051* [0.312, 1.790] 0.005
Comparison group change at endline | -0.142 [-0.310, 0.027] 0.099 -0.432 [-0.910, 0.046] 0.077
Intervention vs Comparison at baseline | 0.269* [0.113, 0.426] 0.001 1.102* [0.651, 1.552] 0.000
Comparison group at baseline | 3.478* [3.077, 3.880] 0.000 15.318* [13.984, 16.653] 0.000
------------------------------------------------------------------------------------------------------
I tried these but to no avail.
Code:
collect label levels colname Marginal_contrast#1.exposure#1.period ///
"ATT (Intervention vs counterfactual at endline)", modify
collect label levels colname Other_model_parameters#1.exposure#1.period ///
"Interaction (Period × Exposure)", modify
I am looking for a way to assign group‑specific labels to the same coefficient without flattening the group dimension into the row labels. Is there a way to achieve this with collect (perhaps using collect style with conditions, or a different approach to stacking collections) that preserves the grouping structure?
Thank you for any insights!
Below is my code and data from dataex.
Code:
clear all
use "asrhdata(4DiD).dta", clear
cap drop q104age
gen q104age = .
replace q104age = 1 if q104==15
replace q104age = 2 if q104==16
replace q104age = 3 if q104==17
replace q104age = 4 if q104==18
replace q104age = 5 if q104==19
cap label drop q104age
label define q104age 1 "15" 2 "16" 3 "17" 4 "18" 5 "19"
label val q104age q104age
cap drop q115_merged2
gen q115_merged2 = q115_merged
replace q115_merged = . if q115_merged2==4
label var q104age "Age"
label var q109_merged "Highest education level"
label var q115_merged "Religion"
label var q301cat "Age at marriage (categories)"
label var q303 "Ever given birth"
label var q201cat "Household size (categories)"
label var wealth_quintile "Wealth quintile"
label var state_name "State"
label var cse "Contraceptive self-efficacy"
label var fasp "Future aspirations score"
rename (q104age q109_merged q115_merged q301cat q303 q201cat wealth_quintile state_name) ///
(age edu rel mage birth hhs wea sta)
drop pick
randomtag, count(100) gen(pick)
dataex cse fasp age edu rel mage birth hhs wea sta if pick, count(100)
local outcomes cse fasp
collect clear
foreach outcome of local outcomes {
di _n(2) as result "Processing: `outcome'"
quietly mixed `outcome' i.exposure##i.period i.age i.edu i.rel i.mage i.birth i.hhs i.wea i.sta ///
[pweight=iptw_stabilized] || clustercode:, vce(cluster clustercode)
collect create `outcome'_coll
quietly collect: margins i.exposure#i.period, contrast(nowald)
collect get r(), tags(group["Marginal_contrast"] outcome[`outcome'])
collect get e(), tags(group["Other_model_parameters"] outcome[`outcome'])
}
collect create combined
collect combine all = cse_coll fasp_coll
collect stars _r_p 0.05 "*", attach(_r_b)
collect composite define ci = _r_lb _r_ub, delimiter(", ") trim
collect style cell result[ci], sformat("[%s]")
collect composite define coef_ci_p = _r_b ci _r_p, trim
collect style cell result[coef_ci_p], sformat("%s")
collect layout (group#colname[1.exposure#1.period 1.period 1.exposure _cons]) (outcome#result[coef_ci_p])
collect style cell, nformat(%8.3f)
collect style header result, title(hide)
collect label levels result coef_ci_p "Coefficient [95% CI] p-value", modify
collect label levels colname _cons "Comparison group at baseline", modify
collect label levels colname 1.exposure "Intervention vs Comparison at baseline", modify
collect label levels colname 1.period "Comparison group change at endline", modify
collect label levels colname 1.exposure#1.period "Interaction (Period × Exposure)", modify
collect label list result, all
collect label levels colname Marginal_contrast#1.exposure#1.period ///
"ATT (Intervention vs counterfactual at endline)", modify
collect label levels colname Other_model_parameters#1.exposure#1.period ///
"Interaction (Period × Exposure)", modify
collect label levels group "Marginal_contrast" "Marginal contrast", modify
collect label levels group "Other_model_parameters" "Other model parameters", modify
collect label levels outcome cse "Contraceptive self-efficacy", modify
collect label levels outcome fasp "Future aspirations score", modify
collect dims
collect levelsof group
collect levelsof result
collect title "Article DiD Table - Continuous"
collect preview
collect export ASRH_Results_REVISED.xlsx, sheet("Article_DiD_Continuous" replace) modify cell(A1)
Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input float(cse fasp age edu rel) byte(mage birth hhs) float wea byte sta
4 16 4 2 3 2 1 2 3 2
4.888889 20 5 3 3 2 1 1 5 2
4 16 5 2 2 2 1 1 4 2
2 16 2 . 3 2 2 1 3 2
4.2222223 16 4 3 3 1 1 1 3 2
4.6666665 16 5 1 3 2 1 1 2 2
4 16 3 . 3 2 1 1 3 2
4.111111 19 5 . 3 2 1 1 2 2
4.4444447 19 5 2 3 2 2 1 4 2
3.666667 13 4 2 2 2 1 2 4 2
4 13 5 2 2 2 2 1 4 2
3.5555556 16 2 . 3 2 2 2 2 2
4.111111 11 4 . 3 2 2 1 4 2
4.111111 16 5 2 3 2 1 1 3 1
3.888889 18 4 . 3 2 2 1 5 1
5 20 5 2 3 2 1 1 5 1
5 18 5 2 3 2 1 1 3 1
2.777778 15 5 . 3 2 1 1 2 1
4 16 4 2 3 2 2 1 1 1
4 16 5 2 3 1 1 1 4 1
3.666667 16 5 . 3 2 1 1 2 1
4.777778 19 5 2 3 2 2 1 5 1
5 20 5 2 3 1 1 1 5 1
4 16 3 3 3 2 1 1 5 1
4.888889 19 5 2 3 2 1 1 5 1
4.6666665 18 5 1 3 2 1 1 5 1
2 14 5 . 3 1 1 1 1 1
2 14 1 . 3 1 1 1 1 1
4 16 5 2 3 1 1 1 1 1
4 16 5 2 3 1 1 1 4 1
3.111111 16 4 1 3 1 1 1 2 1
2.2222223 12 2 . 3 2 2 1 1 1
4 16 5 . 3 2 1 1 2 1
3.888889 16 3 . 3 2 2 1 1 1
3 15 4 . 3 2 2 1 1 1
4.111111 18 5 . 2 2 2 1 2 1
4.2222223 14 5 2 2 1 1 1 1 1
4 18 5 2 3 1 1 1 3 1
3.4444444 14 5 2 3 1 1 1 3 1
4 16 5 2 3 2 1 1 4 1
4.6666665 17 1 . 3 2 2 1 2 2
4 16 4 2 3 1 1 1 5 2
3.777778 16 4 2 3 2 2 1 5 2
3.777778 16 4 2 3 2 2 1 5 2
4 16 3 1 3 2 2 1 2 2
3.777778 16 5 2 2 2 2 1 4 2
4.6666665 18 4 2 3 1 1 1 2 1
5 17 5 2 3 2 1 1 2 1
4.5555553 19 3 2 3 1 1 1 5 1
4.777778 18 4 2 3 2 1 1 5 1
4.5555553 18 5 2 3 2 1 1 5 1
4.777778 16 3 2 3 2 1 1 5 1
3.888889 15 1 2 3 1 1 2 2 1
4 16 4 2 2 2 1 1 1 1
4 16 4 2 2 2 2 2 1 1
4 16 5 1 2 2 2 2 1 1
4.777778 19 1 1 3 1 1 1 3 2
4.6666665 20 4 1 3 2 1 1 2 2
3.111111 12 1 3 2 1 1 1 2 2
4 16 5 2 3 2 1 1 4 2
4 16 5 2 3 2 1 1 5 2
4 16 1 2 3 1 1 1 3 2
3.888889 16 5 1 3 1 1 2 3 2
4.5555553 18 4 2 3 2 1 1 4 1
4.5555553 17 4 2 3 2 1 1 3 1
4.4444447 18 5 2 3 2 1 1 4 1
5 20 4 2 3 2 1 1 5 1
4.777778 19 4 1 3 2 1 1 3 1
4.5555553 18 5 . 3 2 1 1 4 1
4.6666665 19 4 3 3 2 1 1 5 1
4.3333335 18 5 2 3 2 1 1 3 1
4.777778 20 5 1 3 2 2 1 4 1
4.4444447 16 4 2 3 2 1 1 3 1
4.4444447 18 5 2 3 1 1 1 5 1
4.5555553 17 5 2 3 1 1 2 5 1
4.5555553 18 5 2 3 2 1 1 5 1
4.777778 19 4 2 3 2 1 1 3 1
4.4444447 18 5 2 3 2 1 1 5 1
4.6666665 18 4 2 3 2 2 1 4 1
2.3333333 12 5 2 3 2 2 1 2 1
2 11 2 . 3 2 2 2 2 1
1.888889 13 4 . 3 2 1 2 1 1
2.666667 14 5 1 3 1 1 1 2 1
2.666667 13 1 2 2 2 2 2 1 1
4.4444447 19 5 . 2 2 1 2 2 1
1.3333334 7 5 2 2 2 1 2 3 1
1.2222222 8 2 . 2 2 1 2 1 1
5 20 5 1 3 2 1 1 4 1
3.777778 14 4 2 3 2 1 2 3 1
3.777778 16 4 2 3 2 1 1 4 2
3.777778 16 5 . 2 2 1 1 4 2
3.777778 16 5 1 3 2 2 1 3 2
3.777778 16 5 4 2 2 2 1 3 2
4.5555553 18 4 1 2 2 1 2 3 1
4.6666665 17 4 . 3 2 1 2 2 1
3.333333 16 3 1 3 2 1 2 1 1
2 8 4 2 3 2 1 1 1 1
4.2222223 14 4 1 3 2 1 1 3 1
4 16 5 1 2 2 1 2 1 1
3.111111 14 5 2 2 2 2 1 1 1
end
label values age q104age
label def q104age 1 "15", modify
label def q104age 2 "16", modify
label def q104age 3 "17", modify
label def q104age 4 "18", modify
label def q104age 5 "19", modify
label values edu q109_merged_lbl
label def q109_merged_lbl 1 "Primary", modify
label def q109_merged_lbl 2 "Secondary", modify
label def q109_merged_lbl 3 "Tertiary", modify
label def q109_merged_lbl 4 "Other", modify
label values rel q115_merged_lbl
label def q115_merged_lbl 2 "Protestant/Other Christian", modify
label def q115_merged_lbl 3 "Islam", modify
label values mage q301cat
label def q301cat 1 "<15", modify
label def q301cat 2 "15-19", modify
label values birth q303
label def q303 1 "Yes", modify
label def q303 2 "No", modify
label values hhs q201cat
label def q201cat 1 "1-5", modify
label def q201cat 2 ">5", modify
label values wea wealth_quintile
label def wealth_quintile 1 "Very poor", modify
label def wealth_quintile 2 "Poor", modify
label def wealth_quintile 3 "Average", modify
label def wealth_quintile 4 "Rich", modify
label def wealth_quintile 5 "Very rich", modify
label values sta state_name
label def state_name 1 "KADUNA", modify
label def state_name 2 "NASARAWA", modify

Comment