Title | Understand and manipulate autolevels in collections | |
Author | Chris Cheng, StataCorp |
In the context of collection, autolevels ([TABLES] collect style autolevels) represent the default levels of a dimension that are automatically displayed in a table when the dimension is referenced without specifying detailed levels. Although the result dimension often comes with predefined autolevels, you also have the flexibility to set autolevels for results when using commands like collect: or collect get. Plus, you can set autolevels for any dimension. Let’s explore this concept with an example:
. collect clear . sysuse auto (1978 automobile data) . quietly collect _r_b _r_se: regress price mpg i.foreign . quietly collect _r_b _r_se: regress price mpg turn weight i.foreign
After collecting results (_r_b for coefficients and _r_se for standard errors) from two linear regressions, we can construct a table with predictors in the rows and results nested within. By laying out the results from two models side by side in the columns, we can create a comprehensive estimation table. (Please refer to FAQ: How to change a table’s layout using collect layout? for more information on building table layouts.)
. collect layout (colname#result) (cmdset) Collection: default Rows: colname#result Columns: cmdset Table 1: 18 x 2
1 2 | ||
Mileage (mpg) | ||
Coefficient | -294.1955 -.4660076 | |
Std. error | 55.69172 73.51407 | |
Domestic | ||
Coefficient | 0 0 | |
Std. error | 0 0 | |
Foreign | ||
Coefficient | 1767.292 3221.415 | |
Std. error | 700.158 706.4847 | |
Turn circle (ft.) | ||
Coefficient | -229.2059 | |
Std. error | 114.2423 | |
Weight (lbs.) | ||
Coefficient | 4.284532 | |
Std. error | .7404967 | |
Intercept | ||
Coefficient | 11905.42 1368.197 | |
Std. error | 1158.634 4887.597 | |
We would like to add model-level results, such as sample size and \(R^2\), in the existing table of coefficients. We first attempt to do this by adding the result dimension in the rows, because each regress command (identified by cmdset) provides one set of model-level results.
. collect layout (colname#result result) (cmdset) Collection: default Rows: colname#result result Columns: cmdset Table 1: 18 x 2
1 2 | ||
Mileage (mpg) | ||
Coefficient | -294.1955 -.4660076 | |
Std. error | 55.69172 73.51407 | |
Domestic | ||
Coefficient | 0 0 | |
Std. error | 0 0 | |
Foreign | ||
Coefficient | 1767.292 3221.415 | |
Std. error | 700.158 706.4847 | |
Turn circle (ft.) | ||
Coefficient | -229.2059 | |
Std. error | 114.2423 | |
Weight (lbs.) | ||
Coefficient | 4.284532 | |
Std. error | .7404967 | |
Intercept | ||
Coefficient | 11905.42 1368.197 | |
Std. error | 1158.634 4887.597 | |
We expected the combination between result and cmdset to display the model-level results. However, there are no output changes. Previously, when we referred to this result dimension, in the table layout, _r_b and _r_se were displayed because we “accidentally” set the autolevels when we used collect _r_b _r_se:. To Stata’s eyes, we “deliberately” want these two levels every time this dimension, result, is used in collect layout. The mechanism of collect get: is that all results (coefficient level and model level) are collected. When we supply the keywords, such as _r_b and _r_se, they are automatically interpreted as autolevels.
If you are not sure about what levels are used as the autolevels, use this command:
. collect query autolevels result Automatic dimension levels Collection: default Dimension: result Levels: _r_b _r_se
In the output, we confirmed that the two levels, _r_b and _r_se, are indeed the autolevels. This means that when using result in collect layout, result[_r_b _r_se] is assumed. Stata views
. collect layout (colname#result result) (cmdset)
as
. collect layout (colname#result[_r_b _r_se] result[_r_b _r_se]) (cmdset)
Therefore, the model-level results were not successfully shown. Prior to learning about the configuration of autolevels, we have the option to manually provide the model-level results that should be presented. Let’s list the levels of result by utilizing collect levelsof:
. collect levelsof result Collection: default Dimension: result Levels: F N _r_b _r_ci _r_df _r_lb _r_p _r_se _r_ub _r_z _r_z_abs beta cmd cmdline depvar df_m df_r estat_cmd ll ll_0 marginsok model mss predict properties r2 r2_a rank rmse rss title vce
Here we see the coefficient-level results, such as _r_b and _r_se. Additionally, model-level results like r2 and N are available. If you prefer not to adjust the autolevels, you can include detailed levels directly in the dimension, as illustrated below:
. collect layout (colname#result[_r_b _r_se] result[r2 N]) (cmdset) Collection: default Rows: colname#result[_r_b _r_se] result[r2 N] Columns: cmdset Table 1: 20 x 2
1 2 | ||
Mileage (mpg) | ||
Coefficient | -294.1955 -.4660076 | |
Std. error | 55.69172 73.51407 | |
Domestic | ||
Coefficient | 0 0 | |
Std. error | 0 0 | |
Foreign | ||
Coefficient | 1767.292 3221.415 | |
Std. error | 700.158 706.4847 | |
Turn circle (ft.) | ||
Coefficient | -229.2059 | |
Std. error | 114.2423 | |
Weight (lbs.) | ||
Coefficient | 4.284532 | |
Std. error | .7404967 | |
Intercept | ||
Coefficient | 11905.42 1368.197 | |
Std. error | 1158.634 4887.597 | |
R-squared | .2838475 .5271446 | |
Number of observations | 74 74 | |
If you prefer not to specify the levels of result in collect layout every time (especially when you have many results to display), you can configure the existing autolevels of the result dimension. This can be done by collect style autolevels:
. collect style autolevels result r2 N
This command adds levels r2 and N to the existing autolevels. Thus, no need to include the coefficients (_r_b) and standard errors (_r_se) here, because they’re already autolevels.
If you want to clear and override current autolevels with the ones being specified, use the clear option:
. collect style autolevels result _r_b _r_se r2 N, clear
Now simply specifying the dimension names will yield the desired table:
. collect layout (colname#result result) (cmdset) Collection: default Rows: colname#result result Columns: cmdset Table 1: 20 x 2
1 2 | ||
Mileage (mpg) | ||
Coefficient | -294.1955 -.4660076 | |
Std. error | 55.69172 73.51407 | |
Domestic | ||
Coefficient | 0 0 | |
Std. error | 0 0 | |
Foreign | ||
Coefficient | 1767.292 3221.415 | |
Std. error | 700.158 706.4847 | |
Turn circle (ft.) | ||
Coefficient | -229.2059 | |
Std. error | 114.2423 | |
Weight (lbs.) | ||
Coefficient | 4.284532 | |
Std. error | .7404967 | |
Intercept | ||
Coefficient | 11905.42 1368.197 | |
Std. error | 1158.634 4887.597 | |
R-squared | .2838475 .5271446 | |
Number of observations | 74 74 | |
After you set the autolevels, the underlying command is
. collect layout (colname#result[_r_b _r_se r2 N] result[_r_b _r_se r2 N]) (cmdset)
Please note if a combination of tags does not find any item, such as the combination between colname[mpg]#result[r2] and cmdset[1], it will not error out or affect the table rendering. Only the items identified by the tag combinations (such as colname[mpg]#result[_r_b] and cmdset[1]; result[r2] and cmdset[1]) will be displayed.