Announcement

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

  • dtable and Removing Binary Labels in the Variable Column

    Hi Jeff Pitblado (StataCorp) -- is there a way to tweak the displaying of labels for the dtable command such that a binary variable's labels don't show and only a single value (i.e. "1" or "Yes") is reported on the same line as the variable name? Something like this:

    Code:
    [INDENT=3] [/INDENT]  
    Variable n (%)
    Complex Chronic Condition Flag
    Neurologic Flag
    34 (2.7%)
    Congenital Malformation Flag
    88 (10.2%)
    Malignancy Flag
    150 (12.2%)
    Where the n's and %'s report the percent of cases where each variable = "1" or "Yes".

    Does that make sense?

    Thanks!

  • #2
    You can select/restrict the factor levels shown by dtable by specifying them in the varlist.
    For example,
    Code:
    sysuse auto
    dtable 1.foreign
    which yields table
    Code:
    ---------------------
                 Summary 
    ---------------------
    N                  74
    Car origin           
      Foreign  22 (29.7%)
    ---------------------
    Then you can change the header style for factor variable foreign to hide it's level.
    Code:
    . collect style header foreign, level(hide)
    
    . collect preview
    
    ---------------------
                 Summary 
    ---------------------
    N                  74
    Car origin 22 (29.7%)
    ---------------------

    Comment


    • #3
      Brilliant! I suppose I'll have a lot of variables that this will be the case for. Can I specify something to the effect of: "Do that for all binary variables in the variable column."?

      Comment


      • #4
        There is no such option right now.
        You can specify all the variables in a single call to collect style header though.
        Code:
        . dtable 1.foreign 5.rep78
        
        -----------------------------
                             Summary 
        -----------------------------
        N                          74
        Car origin                   
          Foreign          22 (29.7%)
        Repair record 1978           
          5                11 (15.9%)
        -----------------------------
        
        . collect style header foreign rep78, level(hide)
        
        . collect preview
        
        -----------------------------
                             Summary 
        -----------------------------
        N                          74
        Car origin         22 (29.7%)
        Repair record 1978 11 (15.9%)
        -----------------------------

        Comment


        • #5
          That'll do. Thanks for all the help!

          P.S. You know an awful lot about dtable. Did you develop it?

          Comment


          • #6
            One more thing -- how do I APPLY a collected preview style? Do I have to save it as a new template, then call the dtable command again?

            Comment


            • #7
              I'm not sure what you mean by "collected preview style".

              If you have a style that you saved to disk via collect style save, like I showed in the other thread, then you can apply it to your current dtable collection using collect style use filename, override. In the other thread I also mention specifying it in option style(). Unfortunately, header style edits on factor variables applied using option style() will be overwritten by code in dtable. The only way to apply header style edits to factor variables, currently, is to apply the style edits after the collection is created by dtable.

              Here is an example. First let's define a style edit to hide the levels of factor variables foreign and rep78.
              Code:
              collect clear
              collect style header foreign rep78, level(hide)
              collect style save hide-levels, replace
              Now let's apply this style after our call to dtable.
              Code:
              sysuse auto
              dtable 1.foreign 5.rep78
              collect style use hide-levels, override
              collect preview
              Here is the resulting table
              Code:
              . collect preview
              
              -------------------------------
                                 |    Summary
              -------------------+-----------
              N                  |         74
              Car origin         | 22 (29.7%)
              Repair record 1978 | 11 (15.9%)
              -------------------------------


              For this kind of customization, I would consider writing my own wrap-around program that identifies the 0/1 factor variables, calls dtable, then applies the header style edits.
              Something like
              Code:
              program mydtable
                  version 18
                  syntax [varlist] [if] [in] [, indicators(varlist) *]
                  quietly dtable `varlist' 1.(`indicators') `if' `in', `options'
                  if "`indicators'" != "" {
                      collect style header `indicators', level(hide)
                  }
                  collect preview
              end
              Here is the code in action
              Code:
              . mydtable mpg turn, indicators(foreign rep78)
              
              ---------------------------------
                                     Summary   
              ---------------------------------
              N                              74
              Mileage (mpg)      21.297 (5.786)
              Turn circle (ft.)  39.649 (4.399)
              Car origin             22 (29.7%)
              Repair record 1978       2 (2.9%)
              ---------------------------------

              Comment


              • #8
                Apologies for the confusion. First, all the detail is unbelievably helpful. Thanks!

                With the override function, does it only override the aspect of the old style that the new style modifies? For example, per the previous thread, I've set up my ideal Table 1 style. (It's pretty great, IMHO!), but I don't want any additional style I create solely for the single-line variable label edits to overwrite the rest of the style. Does that make sense?

                I think the wrap-around program idea is really solid, and essentially automates what I'm trying to do. Where I get a bit lost though (and the reason for my previous question about "applying" a collected style) is when I've modified a style using collect style [row/cell/etc], I no longer know how to export the table in the way that I would expect to using dtable's "export()" option. Even in the case of the wrap-around program, I still wouldn't know what to do re: exporting of the table to Word. Any insight on that too?

                Thanks again for all the support,
                Kevin

                Comment


                • #9
                  Use collect export to publish your table into a specific file format. dtable is a convenience command, and its export() option is just a shortcut to call collect export.

                  Comment


                  • #10
                    Regarding option override, it depends on what you started with when you defined your style edits.

                    Let's say we are working with dtable, and make style edits. Then override is not necessary because the style we save is already based on dtable. This is the situation I was proposing in this thread.

                    On the other hand, you could start from an empty style, via
                    Code:
                    collect style clear
                    make edits to that style, and save those edits to a file. If you specify this new file in option style(), you also want to add sub-option override, since you want all the default style choices we already made for dtable, but want your edits to be applied post hoc. If you fail to specify sub-option override with this file in option style(), then dtable would use the empty style choices followed by your edits.

                    The empty style basically means no colors, no borders, numeric format %9.0g, ..., etc. If you are curious about the style choices in the empty style, type the above command to clear the style, then follow it up with one of the following collect query commands.
                    Code:
                    collect query column
                    collect query row
                    collect query table
                    collect query header
                    collect query html
                    collect query putdocx
                    collect query putpdf
                    collect query tex
                    collect query showbase
                    collect query showempty
                    collect query show omit
                    collect query cell
                    collect query _cons
                    collect query stars
                    collect query title
                    collect query notes
                    collect query composite

                    Comment


                    • #11
                      Thanks, Jeff. This answers my question perfectly. Much appreciated!

                      Comment


                      • #12
                        Hi Jeff -- I'm back working on custom tables, and I'm wondering how I would show stars next to significant p-values in dtable? I've tried a bunch of different tacks, and none are taking. Ideas?

                        Comment


                        • #13
                          dtable was not designed with stars in mind, but it is not too hard to replace the reported p-values once you figure out which result levels contain the p-values.

                          Here is an example
                          Code:
                          sysuse auto
                          
                          dtable price mpg head trunk turn i.rep, by(foreign, test)
                          * -dtable- sets result autolevels to its composite results
                          collect query autolevels result
                          * the p-values are in composite _dtable_test: pearson and regress
                          collect query composite _dtable_test
                          * define stars rules using the p-value results
                          collect stars pearson regress .1 "*" .01 "**" .001 "***"
                          * redefine composite _dtable_test to use the new -stars- result
                          collect composite define _dtable_test = stars, replace
                          * replay table, now with stars
                          collect preview
                          Here is the resulting table, with stars
                          Code:
                          --------------------------------------------------------------------------------------------
                                                                              Car origin                              
                                                       Domestic              Foreign                Total         Test
                          --------------------------------------------------------------------------------------------
                          N                                52 (70.3%)            22 (29.7%)           74 (100.0%)     
                          Price                 6,072.423 (3,097.104) 6,384.682 (2,621.915) 6,165.257 (2,949.496)     
                          Mileage (mpg)                19.827 (4.743)        24.773 (6.611)        21.297 (5.786)  ***
                          Headroom (in.)                3.154 (0.916)         2.614 (0.486)         2.993 (0.846)    *
                          Trunk space (cu. ft.)        14.750 (4.306)        11.409 (3.217)        13.757 (4.277)   **
                          Turn circle (ft.)            41.442 (3.968)        35.409 (1.501)        39.649 (4.399)  ***
                          Repair record 1978                                                                          
                            1                                2 (4.2%)              0 (0.0%)              2 (2.9%)  ***
                            2                               8 (16.7%)              0 (0.0%)             8 (11.6%)     
                            3                              27 (56.2%)             3 (14.3%)            30 (43.5%)     
                            4                               9 (18.8%)             9 (42.9%)            18 (26.1%)     
                            5                                2 (4.2%)             9 (42.9%)            11 (15.9%)     
                          --------------------------------------------------------------------------------------------

                          Comment


                          • #14
                            Thanks for the quick reply, Jeff. So, I stumbled upon that as a potential option, but I couldn't piece together how to report both raw p-values AND stars too. Any suggestions on how I could do that, using dtable as the primary command?

                            Comment


                            • #15
                              In that case, instead of redefining composite result _dtable_test, add stars to the result auto levels.

                              Code:
                              dtable price mpg head trunk turn i.rep, by(foreign, test)
                              * -dtable- sets result autolevels to its composite results
                              collect query autolevels result
                              * the p-values are in composite _dtable_test: pearson and regress
                              collect query composite _dtable_test
                              * define stars rules using the p-value results
                              collect stars pearson regress .1 "*" .01 "**" .001 "***"
                              * add stars to the result autolevels
                              collect style autolevels result stars
                              collect style cell result[stars], halign(left)
                              * replay table, now with stars
                              collect preview
                              Here is the resulting table
                              Code:
                              --------------------------------------------------------------------------------------------------
                                                                                     Car origin                                 
                                                           Domestic              Foreign                Total            Test   
                              --------------------------------------------------------------------------------------------------
                              N                                52 (70.3%)            22 (29.7%)           74 (100.0%)           
                              Price                 6,072.423 (3,097.104) 6,384.682 (2,621.915) 6,165.257 (2,949.496)  0.680    
                              Mileage (mpg)                19.827 (4.743)        24.773 (6.611)        21.297 (5.786) <0.001 ***
                              Headroom (in.)                3.154 (0.916)         2.614 (0.486)         2.993 (0.846)  0.011 *  
                              Trunk space (cu. ft.)        14.750 (4.306)        11.409 (3.217)        13.757 (4.277)  0.002 ** 
                              Turn circle (ft.)            41.442 (3.968)        35.409 (1.501)        39.649 (4.399) <0.001 ***
                              Repair record 1978                                                                                
                                1                                2 (4.2%)              0 (0.0%)              2 (2.9%) <0.001 ***
                                2                               8 (16.7%)              0 (0.0%)             8 (11.6%)           
                                3                              27 (56.2%)             3 (14.3%)            30 (43.5%)           
                                4                               9 (18.8%)             9 (42.9%)            18 (26.1%)           
                                5                                2 (4.2%)             9 (42.9%)            11 (15.9%)           
                              --------------------------------------------------------------------------------------------------

                              Comment

                              Working...
                              X