The community has been very helpful as I've tried to figure out collect and table, so I wanted to share two quick tips I wish I had figured out earlier, in hopes that they might help others just starting that journey. Apologies if they are self-evident to all but me...
1. Look at the examples at the end of the TABLES manual, especially Example 6 — Table comparing regression results.
Because they are tucked at the back of the book, I was slow to dig through these in detail. If the rest of the manual is "What does this option do?", these are "How would you use these options?" They offer great worked examples, including "Here is how output looks with option A" followed by "Here is how we can make it look better with option B." I found them super helpful. Kudos to the Stata documentation team.
2. You can call your tags whatever you want.
In the highly contrived example below, I build up a set of 6 regressions--having meaningful names was especially useful when I was making regressions from loops over three levels of variables. I also show how just a few of the options shown in Example 6 can improve how tables look.
1. Look at the examples at the end of the TABLES manual, especially Example 6 — Table comparing regression results.
Because they are tucked at the back of the book, I was slow to dig through these in detail. If the rest of the manual is "What does this option do?", these are "How would you use these options?" They offer great worked examples, including "Here is how output looks with option A" followed by "Here is how we can make it look better with option B." I found them super helpful. Kudos to the Stata documentation team.
2. You can call your tags whatever you want.
In the highly contrived example below, I build up a set of 6 regressions--having meaningful names was especially useful when I was making regressions from loops over three levels of variables. I also show how just a few of the options shown in Example 6 can improve how tables look.
Code:
. collect clear
.
. use https://www.stata-press.com/data/r19/lbw.dta
(Hosmer & Lemeshow data)
.
. forvalues smoke = 0/1 {
2. forvalues race=1/3 {
3.
. collect _r_b _r_p, tags(smoke[`smoke'] race[`race']): ///
> quietly regress bwt age if smoke == `smoke' & race==`race'
4. }
5. }
.
. collect layout (colname#result[_r_b _r_p] result[r2]) (smoke#race)
Collection: default
Rows: colname#result[_r_b _r_p] result[r2]
Columns: smoke#race
Table 1: 7 x 6
--------------------------------------------------------------------------
| Nonsmoker Nonsmoker Nonsmoker Smoker Smoker Smoker
| White Black Other White Black Other
--------------+-----------------------------------------------------------
Age of mother |
Coefficient | 26.49701 -34.90609 -6.752165 -18.02794 -31.22923 1.554007
p-value | 0.143 0.416 0.758 0.316 0.413 0.976
Intercept |
Coefficient | 2739.225 3550.44 2965.239 3238.907 3256.624 2722.202
p-value | 0.000 0.001 0.000 0.000 0.007 0.040
R-squared | .0504162 .0477616 .0018021 .0200793 .0851487 .000096
--------------------------------------------------------------------------
.
. collect style cell result, nformat(%5.3f)
. collect style cell result[_r_p], sformat("(%s)")
. collect style cell smoke[0]#race[3], border(right)
. collect style column, dups(center)
. collect style header result, level(hide)
. collect style header result[r2], level(label)
.
. collect preview
-----------------------------------------------------------------------
| Nonsmoker Smoker
| White Black Other | White Black Other
--------------+----------------------------+---------------------------
Age of mother | 26.497 -34.906 -6.752 | -18.028 -31.229 1.554
| (0.143) (0.416) (0.758) | (0.316) (0.413) (0.976)
Intercept | 2739.225 3550.440 2965.239 | 3238.907 3256.624 2722.202
| (0.000) (0.001) (0.000) | (0.000) (0.007) (0.040)
R-squared | 0.050 0.048 0.002 | 0.020 0.085 0.000
-----------------------------------------------------------------------
