Announcement

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

  • dtable difference between dimensions var and colname plus sample frequency not displayed when combining collections

    I thought I wouldn't have any further questions about dtable and collect for the moment but have two more. I'm sorry to be taking up so much time and space on Statalist of late. I hope other Stata users are get some benefit from my posts.

    Here's the code and the output along the way.
    Code:
    *_______________________________________________________________________________
    *Creating the example dataset.
    
    *Reading in the dataset.
    use https://www.stata-press.com/data/r18/exercise.dta,clear
    keep if inlist(day, 0, 4, 6, 8)
    
    *Replacing the variable program with group_allocation, where 1=intervention and 2=control.
    gen group_allocation=program
    lab var group_allocation "Group allocation"
    lab def grplbl 1 Intervention 2 Control
    lab val group_allocation grplbl
    drop program
    
    gen blinding=2
    replace blinding=3 if _n/3==int(_n/3)
    bysort day: replace blinding=1 if _n/4==int(_n/4) & day!=0
    
    lab def blindlbl 1 "Change or lack of change from baseline" ///
                     2  "Participant informed assessor" ///
                     3  "Guess"
    lab val blinding blindlbl
    
    
    *_______________________________________________________________________________
    *Creating the table.
    
    *Clearing any collections that may be in Stata's memory.
    collect clear
    
    *Creating a separate descriptive table for each level of time.
    *For time=1, including the sample frequency in the column header.    
    dtable i.blinding if day==0, ///
        by(group_allocation,nototal) ///
        sample(, statistic(frequency) place(seplabels)) ///
        sformat("n=%s" frequency) ///
        title(Table 5: Blinding of assessor) ///
        titlestyles(font(Calibri, size(12) bold)) ///
        name(Day0)
    
    forval i=4(2)8 {
        dtable i.blinding if day==`i', ///
            by(group_allocation,nototal) ///
            nosample name(Day`i')
    }
    
    
    *Combining the above collections.
    collect combine comb = Day0 Day4 Day6 Day8
    collect query composite _dtable_stats
    
    OUTPUT:
    Composite definition
    Collection: comb
     Composite: _dtable_stats
      Elements: frequency
                fvfrequency
                fvpercent
     Delimiter: " "
          Trim: on
      Override: off
    The sample frequency is in _dtable_stats. The following command also confirms this.
    Code:
    . collect label list result
    
      Collection: comb
       Dimension: result
           Label: Result
    Level labels:
       frequency  Frequency
     fvfrequency  Factor-variable frequency
       fvpercent  Factor-variable percent
    Now for the rest of the code and the table produced.
    Code:
    *Setting the cell percentages to integers.
    collect style cell result[fvpercent], nformat(%3.0f)
    
    *Adding a border to separate each level of time
    collect style cell colname[3.blinding], border( bottom, width(1))
    
    *Creating the final table.
    collect layout (collection#colname) (group_allocation#result)
    
    OUTPUT:
    
    Table 5: Blinding of assessor
    ----------------------------------------------------------------
                                                  Group allocation  
                                               Intervention  Control
    ----------------------------------------------------------------
    Day0                                                            
      blinding                                                      
        Participant informed assessor              11 (69%) 14 (67%)
        Guess                                       5 (31%)  7 (33%)
    ----------------------------------------------------------------
    Day4                                                            
      blinding                                                      
        Change or lack of change from baseline      4 (25%)  5 (24%)
        Participant informed assessor               8 (50%) 12 (57%)
        Guess                                       4 (25%)  4 (19%)
    ----------------------------------------------------------------
    Day6                                                            
      blinding                                                      
        Change or lack of change from baseline      2 (12%)  7 (33%)
        Participant informed assessor               9 (56%) 10 (48%)
        Guess                                       5 (31%)  4 (19%)
    ----------------------------------------------------------------
    Day8                                                            
      blinding                                                      
        Change or lack of change from baseline      4 (25%)  5 (24%)
        Participant informed assessor               8 (50%) 10 (48%)
        Guess                                       4 (25%)  6 (29%)
    ----------------------------------------------------------------
    Question 1: How do I display the sample frequency in the header for the Intervention and Control groups?

    Question 2: What is the difference between the dimensions colname and var? When I use var instead of colname for the row component, results are shown for only 2 levels of my variable blinding. There genuinely are only 2 levels for Day=0 but there are 3 levels for the other days.

    Code:
    . collect layout (collection#var) (group_allocation#result)
    
    Collection: comb
          Rows: collection#var
       Columns: group_allocation#result
       Table 1: 16 x 2
    
    Table 5: Blinding of assessor
    -------------------------------------------------------
                                         Group allocation  
                                      Intervention  Control
    -------------------------------------------------------
    Day0                                                   
      blinding                                             
        Participant informed assessor     11 (69%) 14 (67%)
        Guess                              5 (31%)  7 (33%)
    Day4                                                   
      blinding                                             
        Participant informed assessor      8 (50%) 12 (57%)
        Guess                              4 (25%)  4 (19%)
    Day6                                                   
      blinding                                             
        Participant informed assessor      9 (56%) 10 (48%)
        Guess                              5 (31%)  4 (19%)
    Day8                                                   
      blinding                                             
        Participant informed assessor      8 (50%) 10 (48%)
        Guess                              4 (25%)  6 (29%)
    -------------------------------------------------------
    Kind regards,
    Suzanna

  • #2
    2. The auto levels style for var is set in collection Day0, and takes precedence in collection comb. This is why there is a missing level. The auto levels style for colname is not set by dtable. Clear the auto levels style for var to get the same behavior as with colname.
    Code:
    collect style autolevels var, clear
    1. If you look at the layout specification in collection Day0, which was constructed using sample() sub-option place(seplabels), you will see that the column specification includes dimension _dtable_sample_dim which is how dtable gets the sample sizes into the column heading. To get this column heading in your combined table, you need to add _dtable_sample_dim tags in the other collections before you combine them, then add _dtable_sample_dim to your column specification in your layout.

    To accomplish this, I changed the loop creating the other collections to
    Code:
    forval i=4(2)8 {
        dtable i.blinding if day==`i', ///
            by(group_allocation,nototal) ///
            nosample name(Day`i')
        collect addtags _dtable_sample_dim[1], fortags(group_allocation[1])
        collect addtags _dtable_sample_dim[2], fortags(group_allocation[2])
    }
    and I changed the layout to
    Code:
    collect layout (collection#colname) (group_allocation#result#_dtable_sample_dim)
    or
    Code:
    collect style autolevels var, clear
    collect layout (collection#var) (group_allocation#result#_dtable_sample_dim)

    Comment


    • #3
      Thank-you Jeff. That works beautifully.

      Cheers,
      Suzanna

      Comment

      Working...
      X