Announcement

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

  • dtable help re: "super" row headings

    Using the auto dataset as a simple example, I have the following do file:
    Code:
    sysuse auto, clear
    
    local convars price mpg
    local catvars rep78
    
    noi dtable, by(foreign, tests nototals nomissing) ///
        column(by(hide)) ///
        continuous(`convars') nformat(%5.1f mean sd) ///
        factor(`catvars', ///
            statistics(fvfreq fvpercent)        ///
            test(pearson))                ///    
            sformat("N=%s" frequency) ///
    
        collect addtags roweq[Demographics], fortags(var[`convars'])
        foreach b of local catvars {
        collect addtags roweq[`b'], fortags(var[i.`b'])
        }
    exit
    which produces the following table:

    Code:
    . run "/Users/rich/Documents/Stata/testtbl.do"
    note: using test regress across levels of foreign for price and mpg.
    note: using test pearson across levels of foreign for rep78.
    
    ---------------------------------------------------------
                           Domestic        Foreign      Test 
    ---------------------------------------------------------
    N                     N=52 (70.3%)    N=22 (29.7%)       
    Price              6072.4 (3097.1) 6384.7 (2621.9)  0.680
    Mileage (mpg)           19.8 (4.7)      24.8 (6.6) <0.001
    Repair record 1978                                       
      1                       2 (4.2%)        0 (0.0%) <0.001
      2                      8 (16.7%)        0 (0.0%)       
      3                     27 (56.2%)       3 (14.3%)       
      4                      9 (18.8%)       9 (42.9%)       
      5                       2 (4.2%)       9 (42.9%)       
    ---------------------------------------------------------
    I would like to place headings in the middle of the rows - for this (yes, it's silly) example, I would like to place "quantitative variables", preceded by a blank line above the row for "Price" and then I would like to place a blank line and "categorical variables" above "Repair record 1978"

    I would also like to move the results in the "N" row to above the second horizontal line (so the N's and %'s appear under "Domestic" and under "Foreign" but above the horizontal line that is now directly under Domestic and Foreign (the line would still be there but now below the N's and %'s)

  • #2
    To change the column header to include the sample sizes use option option sample(, place(seplabels)).

    To get the super row headers, use collect addtags to tag the items how you want them grouped, then add your group dimension to the layout. Your example added dimension roweq with command collect addtags, but the level you assigned the categorical variable should not be specific to each variable. I'll leave your roweq code, instead I'll add a group dimension with levels convars and catvars then give them the level labels "quantitative variables" and "categorical variables".

    Here is my edited version of the original example, my edits are highlighted in blue.
    Code:
    sysuse auto, clear
    
    local convars price mpg
    local catvars rep78
    
    dtable, by(foreign, tests nototals nomissing) ///
            column(by(hide)) ///
            continuous(`convars') ///
            nformat(%5.1f mean sd) ///
            factor(`catvars', ///
                    statistics(fvfreq fvpercent) ///
                    test(pearson)) ///
            sformat("N=%s" frequency) ///
            sample(, place(seplabels))
    
    collect addtags roweq[Demographics], fortags(var[`convars'])
    foreach b of local catvars {
            collect addtags roweq[`b'], fortags(var[i.`b'])
    }
    
    * add tag for super row headers
    collect addtags group[convars], fortags(var[`convars'])
    foreach catvar of local catvars {
            collect addtags group[catvars], fortags(var[i.`catvar'])
    }
    collect label levels group ///
            convars "quantitative variables" ///
            catvars "categorical variables"
    
    * update the layout
    collect query layout
    collect layout (group#(`s(rows)')) (`s(columns)') (`s(tables)')
    
    noi collect preview
    exit

    Comment


    • #3
      Jeff Pitblado (StataCorp) - thank you for both the code and the explanation!

      Comment


      • #4
        I forgot to add option spacer to collect style row stack.
        Code:
        sysuse auto, clear
        
        local convars price mpg
        local catvars rep78
        
        dtable, by(foreign, tests nototals nomissing) ///
                column(by(hide)) ///
                continuous(`convars') ///
                nformat(%5.1f mean sd) ///
                factor(`catvars', ///
                        statistics(fvfreq fvpercent) ///
                        test(pearson)) ///
                sformat("N=%s" frequency) ///
                sample(, place(seplabels))
        
        collect addtags roweq[Demographics], fortags(var[`convars'])
        foreach b of local catvars {
                collect addtags roweq[`b'], fortags(var[i.`b'])
        }
        
        * add tag for super row headers
        collect addtags group[convars], fortags(var[`convars'])
        foreach catvar of local catvars {
                collect addtags group[catvars], fortags(var[i.`catvar'])
        }
        collect label levels group ///
                convars "quantitative variables" ///
                catvars "categorical variables"
        
        * update the layout
        collect query layout
        collect layout (group#(`s(rows)')) (`s(columns)') (`s(tables)')
        collect style row stack, spacer
        noi collect preview
        exit
        Here is the resulting table.
        Code:
        -------------------------------------------------------------
                                   Domestic        Foreign      Test 
                                 N=52 (70.3%)    N=22 (29.7%)        
        -------------------------------------------------------------
        quantitative variables                                       
          Price                6072.4 (3097.1) 6384.7 (2621.9)  0.680
          Mileage (mpg)             19.8 (4.7)      24.8 (6.6) <0.001
                                                                     
        categorical variables                                        
          Repair record 1978                                         
            1                         2 (4.2%)        0 (0.0%) <0.001
            2                        8 (16.7%)        0 (0.0%)       
            3                       27 (56.2%)       3 (14.3%)       
            4                        9 (18.8%)       9 (42.9%)       
            5                         2 (4.2%)       9 (42.9%)       
        -------------------------------------------------------------

        Comment


        • #5
          If you want a blank row between the super row headers, you just add another tag for the extra row header space.

          Here is that idea in action.
          Code:
          sysuse auto, clear
          
          local convars price mpg
          local catvars rep78
          
          dtable, by(foreign, tests nototals nomissing) ///
                  column(by(hide)) ///
                  continuous(`convars') ///
                  nformat(%5.1f mean sd) ///
                  factor(`catvars', ///
                          statistics(fvfreq fvpercent) ///
                          test(pearson)) ///
                  sformat("N=%s" frequency) ///
                  sample(, place(seplabels))
          
          collect addtags roweq[Demographics], fortags(var[`convars'])
          foreach b of local catvars {
                  collect addtags roweq[`b'], fortags(var[i.`b'])
          }
          
          * add tag for super row headers
          collect addtags extra[space] group[convars], fortags(var[`convars'])
          foreach catvar of local catvars {
                  collect addtags extra[space] group[catvars], fortags(var[i.`catvar'])
          }
          collect label levels group ///
                  convars "quantitative variables" ///
                  catvars "categorical variables"
          * label using no-break space (NBSP) [UNICODE char U+00A0]
          collect label levels extra space " "
          
          * update the layout
          collect query layout
          collect layout (group#extra#(`s(rows)')) (`s(columns)') (`s(tables)')
          collect style row stack, spacer
          noi collect preview
          exit
          Here is the resulting table.
          Code:
          -------------------------------------------------------------
                                     Domestic        Foreign      Test
                                   N=52 (70.3%)    N=22 (29.7%)
          -------------------------------------------------------------
          quantitative variables
             
              Price              6072.4 (3097.1) 6384.7 (2621.9)  0.680
              Mileage (mpg)           19.8 (4.7)      24.8 (6.6) <0.001
          
          categorical variables
             
              Repair record 1978
                1                       2 (4.2%)        0 (0.0%) <0.001
                2                      8 (16.7%)        0 (0.0%)
                3                     27 (56.2%)       3 (14.3%)
                4                      9 (18.8%)       9 (42.9%)
                5                       2 (4.2%)       9 (42.9%)
          -------------------------------------------------------------

          Comment


          • #6
            thanks again, Jeff!

            Comment

            Working...
            X