Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding a subheading or blank row using Collect Layout or Table

    Hi folks,
    This seems super basic but searching statalist and trying lots of different options (including collect echo "" to collect...nothing) I can't seem to figure out how to do this and it seems like should be an option. I found another person who did ask this on statalist but their question wasn't answered.

    Query: I'd like to add a blank row OR a subheading between rows in a table.
    I'm using Stata 17 on Mac Ventura.

    I'm using both...
    Code:
    collect layout (cmdset) (result[_r_b][_r_ci])
    and
    Code:
     table (var) (result) (), statistic(fvfrequency var1 var2) statistic(fvpercent var1 var2)
    ...for two different tables, the first using layout is a massive one and I'd love to use some subheadings. I'm not totally desperate for subheadings in the simpler Table one but if there's a trick that someone knows of, please share
    I'm not interested in solutions which use putdocx/ r(table) or any other code, I've invested too much time into collect layout haha....

    Thanks!


  • #2
    It helps when there is example data to play with.
    I'll start with the second example from Karly's post above.
    Code:
    sysuse auto
    table (var) (result), ///
        stat(fvfrequency foreign rep78) ///
        stat(fvpercent foreign rep78)
    which produces
    Code:
    ---------------------------------------------------------------------------
                         |  Factor-variable frequency   Factor-variable percent
    ---------------------+-----------------------------------------------------
    Car origin=Domestic  |                         52                     70.27
    Car origin=Foreign   |                         22                     29.73
    Repair record 1978=1 |                          2                      2.90
    Repair record 1978=2 |                          8                     11.59
    Repair record 1978=3 |                         30                     43.48
    Repair record 1978=4 |                         18                     26.09
    Repair record 1978=5 |                         11                     15.94
    ---------------------------------------------------------------------------
    You can control how the row headers are constructed with command collect style row.
    Use option spacer to put a blank row between levels of the var dimension.
    Code:
    . collect style row stack, spacer
    
    . collect preview
    
    ---------------------------------------------------------------------------
                         |  Factor-variable frequency   Factor-variable percent
    ---------------------+-----------------------------------------------------
    Car origin=Domestic  |                         52                     70.27
    Car origin=Foreign   |                         22                     29.73
                         |                                                     
    Repair record 1978=1 |                          2                      2.90
    Repair record 1978=2 |                          8                     11.59
    Repair record 1978=3 |                         30                     43.48
    Repair record 1978=4 |                         18                     26.09
    Repair record 1978=5 |                         11                     15.94
    ---------------------------------------------------------------------------
    If you prefer to have the factor variable name/label stacked above its values/value-labels, add option nobinder.
    Code:
    . collect style row stack, spacer nobinder
    
    . collect preview
    
    -------------------------------------------------------------------------
                       |  Factor-variable frequency   Factor-variable percent
    -------------------+-----------------------------------------------------
    Car origin         |                                                     
      Domestic         |                         52                     70.27
      Foreign          |                         22                     29.73
                       |                                                     
    Repair record 1978 |                                                     
      1                |                          2                      2.90
      2                |                          8                     11.59
      3                |                         30                     43.48
      4                |                         18                     26.09
      5                |                         11                     15.94
    -------------------------------------------------------------------------


    Karly's first coding example suggests collect was used to consume estimation results.
    Here is a simple example using colname instead of cmdset.
    Code:
    collect clear
    collect : regress mpg turn trunk i.foreign i.rep78
    collect style row stack, spacer nobinder
    collect layout (colname) (result[_r_b _r_ci])
    With options spacer and nobinder, collect will use Stata's coefficient table logic to place spacers between factor terms in the model, but no spacer between unoperated variables.
    Here is the resulting table
    Code:
    -------------------------------------------------------
                          | Coefficient        95% CI      
    ----------------------+--------------------------------
    Turn circle (ft.)     |   -.8502968  -1.19038  -.510214
    Trunk space (cu. ft.) |    -.285687 -.5726855  .0013115
                          |                                
    Car origin            |                                
      Domestic            |           0                    
      Foreign             |    -2.27921 -5.333286  .7748664
                          |                                
    Repair record 1978    |                                
      1                   |           0                    
      2                   |    1.894288 -4.352286  8.140862
      3                   |    .6510895 -5.283189  6.585368
      4                   |    1.108965  -5.05801   7.27594
      5                   |    4.511837 -2.026106  11.04978
                          |                                
    Intercept             |    58.29051  44.33282  72.24819
    -------------------------------------------------------
    If we add another set of estimations results to the collection, we will need to update the layout.
    Assuming we want to stack the estimation results vertically, we can add cmdset to the row specification, and the spacer is placed between levels of the cmdset.
    Code:
    collect : poisson mpg turn trunk i.foreign i.rep78
    collect layout (cmdset#colname) (result[_r_b _r_ci])
    Here is the resulting table
    Code:
    ---------------------------------------------------------
                            | Coefficient        95% CI      
    ------------------------+--------------------------------
    1                       |                                
      Turn circle (ft.)     |   -.8502968  -1.19038  -.510214
      Trunk space (cu. ft.) |    -.285687 -.5726855  .0013115
      Car origin            |                                
        Domestic            |           0                    
        Foreign             |    -2.27921 -5.333286  .7748664
      Repair record 1978    |                                
        1                   |           0                    
        2                   |    1.894288 -4.352286  8.140862
        3                   |    .6510895 -5.283189  6.585368
        4                   |    1.108965  -5.05801   7.27594
        5                   |    4.511837 -2.026106  11.04978
      Intercept             |    58.29051  44.33282  72.24819
                            |                                
    2                       |                                
      Turn circle (ft.)     |   -.0415387 -.0602361 -.0228413
      Trunk space (cu. ft.) |   -.0133932 -.0291939  .0024075
      Car origin            |                                
        Domestic            |           0                    
        Foreign             |   -.0958059 -.2524409   .060829
      Repair record 1978    |                                
        1                   |           0                    
        2                   |    .0756597 -.2736609  .4249804
        3                   |     .007694 -.3235117  .3388998
        4                   |    .0210568   -.32114  .3632535
        5                   |    .1558264 -.1997463  .5113992
      Intercept             |    4.861221  4.092072  5.630369
    ---------------------------------------------------------

    Comment


    • #3
      Wow, thank you so much Jeff Pitblado (StataCorp) I really appreciate that extensive reply. Apologies for not including my own example with web data.

      For your first example, I didn't think of using spacer in that way because I just wanted a space between 2 particular rows, not between each var, but it's a good reminder because I'd really forgotten about spacer!
      For your second example, because I used foreach to run a stack of commands I can't use cmdset in that way, but thankfully I have used tags so now I've just plonked my local tag (myres in this case) in front of cmdset and that has worked a treat
      Code:
       collect layout (myres#cmdset) (result[freq _r_b _r_ci] sex#result[freq _r_b _r_ci])
      and of course recoded the tag to be my header!
      Code:
       collect recode myres (2) = "My Header"
      Many thanks again!!
      Karly

      Comment


      • #4
        Hi all, I hope it's OK if I hop on this thread for a similar question. I'm using table collect in a slightly different way, to get a Table 1 (as described in this wonderful and lifesaving post: https://blog.stata.com/2021/06/24/cu...assic-table-1/). The only challenge I'm currently facing is that I have some variables I want to stack, and some variables I do not want to stack. I've made life more difficult for myself because I have several binary variables where I'd only like to report one level, and not have it stack. Here's an example:

        Code:
        sysuse nlsw88, replace
        table (var) (married), stat(fvfrequency race collgrad south c_city) stat(fvpercent race collgrad south c_city) stat(median age) stat(iqr age)
        
        collect recode result fvfrequency = col1 fvpercent = col2 median = col1 iqr = col2
        
        //I didn't want the 'no' values to be displayed for the binary variables collgrad, south, and c_city,
        //so using specific coding for those variables here:
        collect layout (var[1.race 2.race 3.race 1.collgrad 1.south 1.c_city age]) (married#result[col1 col2])
        
        //now implement formats for columns
        //I do want a decimal point for the median of age, but not for the frequency counts of other values
        collect style cell var[1.race 2.race 3.race 1.collgrad 1.south 1.c_city]#result[col1], nformat(%6.0fc)
        collect style cell var[age]#result[col1], nformat(%6.1fc)
        collect style cell var[1.race 2.race 3.race 1.collgrad 1.south 1.c_city age]#result[col2], nformat(%6.1f)
        collect preview
        
        //now customize column labels
        collect label dim married "Married status", modify
        collect style header result, level(hide)
        collect preview
        
        //now format rows so that they're stacked
        collect style row stack, nobinder spacer
        collect style cell border_block, border(right, pattern(nil))
        collect preview
        You can see that I wind up with a table that has OK formatting..... until I get to the rows where I just wanted one level reported:
        Click image for larger version

Name:	screenshot_stata_table.PNG
Views:	1
Size:	10.6 KB
ID:	1766572



        So, can I:
        1. Prevent only some (but not all) rows from stacking in this way?
        2. Alter the headers of the stacked rows (for example, alter "Race" to "Race and ethnicity")?
        Last edited by Maria Sundaram; 28 Oct 2024, 16:31.

        Comment


        • #5
          Thanks for posting a reproducible example. Add the following to you block of code.

          Code:
          // relabel race -- note that race is part of the var dimension
          collect label levels var race "Race and ethnicity", modify
          
          // hide redundant label for one-level categorical variables (can be combined in a single command)
          collect style header var[1.collgrad], level(hide)
          collect style header var[1.south], level(hide)
          collect style header var[1.c_city], level(hide)
          
          collect preview
          Result

          Code:
          . collect preview
          
          ------------------------------------------------------------------
                                                 Married status             
                                      Single        Married         Total   
          ------------------------------------------------------------------
          Race and ethnicity                                                
            White                   487   60.6   1,150   79.8   1,637   72.9
            Black                   309   38.4     274   19.0     583   26.0
            Other                     8    1.0      18    1.2      26    1.2
                                                                            
          College graduate          188   23.4     344   23.9     532   23.7
                                                                            
          Lives in the south        340   42.3     602   41.7     942   41.9
                                                                            
          Lives in a central city   315   39.2     340   23.6     655   29.2
                                                                            
          Age in current year      39.0    5.0    39.0    5.0    39.0    6.0
          ------------------------------------------------------------------

          Comment


          • #6
            This is wonderful--thank you so much, Leonardo Guizzetti! This worked really well.

            As an aside, in my real data I have a very large number of binary variables. Writing them all out individually would introduce a high possibility for error in misspelling/typos. Using your guidance, I noted that it's also possible to define a macro to call a list of them, and that was also successful in producing the table you showed. (I already have a global macro for that list of variables since it gets called often throughout the data, and I wanted to see if it could be re-used here.)

            In the NLSW88 example, it might look something like this:

            Code:
            //define list of variables that I do not want stacked
            global nostack_vars 1.collgrad 1.south 1.c_city
            
            //hide redundant label for one-level categorical variables
            collect style header var[$nostack_vars], level(hide)
            
            collect preview
            But as you noted, you could also do this:
            Code:
            collect style header var[1.collgrad 1.south 1.c_city], level(hide)
            and that would work well too.

            Thank you so much!!

            Comment

            Working...
            X