Announcement

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

  • Counts for multiple binary variables based on categorical grouping

    Hi, I'm sure this is really basic but would appreciate any help!!

    Using nutrition data, I've generated binary variables whereby participants are either classified as sufficient (0) or deficient (1) - I want to get counts (number of sufficient, number of deficient) for multiple nutrients (vit a, b6, b12, d etc etc) according to one categorical variable with three groups (ethnicity) but I can't figure out a way to do this without repeating tab!

    Thank you,
    Lisa















  • #2
    Hi Lisa,

    You don't give a data example, but it sounds like your data look something like this:

    Code:
    clear
    set obs 1000
    gen ethnicity = runiformint(0, 2)
    gen a = runiformint(0, 1)
    gen b6 = runiformint(0, 1)
    gen b12 = runiformint(0, 1)
    gen d = runiformint(0, 1)
    
    list if _n < 11, clean noobs
    Code:
    . list if _n < 11, clean noobs
    
        ethnic~y   a   b6   b12   d  
               0   1    1     1   1  
               0   1    0     1   0  
               0   1    1     1   1  
               2   1    1     0   1  
               0   1    1     0   1  
               2   0    1     1   1  
               0   1    1     1   1  
               2   1    1     1   1  
               2   1    1     0   0  
               0   1    0     1   1
    Am I understanding that you want to find the number of 1s and 0s for each nutrient by ethnicity? I take it from your last sentence the problem is that you have to write the tab command over and over again (so your idea is to get the count using a cross tabulation) but you don't want to write the same command over and over again? If so, it sounds like you want a for loop.

    Code:
    foreach var in a b6 b12 d {
        tab ethnicity `var'
    }

    Comment


    • #3
      Building on Daniel Schaefer 's very helpful reply:

      I used his code to get a data example with also

      Code:
      set seed 314159

      Check out official commands tab1 and tab2. For example

      Code:
      . tab2 eth a b6 b12 d, firstonly
      
      -> tabulation of ethnicity by a  
      
                 |           a
       ethnicity |         0          1 |     Total
      -----------+----------------------+----------
               0 |       170        172 |       342 
               1 |       167        151 |       318 
               2 |       164        176 |       340 
      -----------+----------------------+----------
           Total |       501        499 |     1,000 
      
      -> tabulation of ethnicity by b6  
      
                 |          b6
       ethnicity |         0          1 |     Total
      -----------+----------------------+----------
               0 |       157        185 |       342 
               1 |       154        164 |       318 
               2 |       164        176 |       340 
      -----------+----------------------+----------
           Total |       475        525 |     1,000 
      
      -> tabulation of ethnicity by b12  
      
                 |          b12
       ethnicity |         0          1 |     Total
      -----------+----------------------+----------
               0 |       171        171 |       342 
               1 |       154        164 |       318 
               2 |       177        163 |       340 
      -----------+----------------------+----------
           Total |       502        498 |     1,000 
      
      -> tabulation of ethnicity by d  
      
                 |           d
       ethnicity |         0          1 |     Total
      -----------+----------------------+----------
               0 |       182        160 |       342 
               1 |       162        156 |       318 
               2 |       159        181 |       340 
      -----------+----------------------+----------
           Total |       503        497 |     1,000
      See also groups from the Stata Journal

      Code:
      . search st0496, entry
      
      Search of official help files, FAQs, Examples, and Stata Journals
      
      SJ-18-1 st0496_1  . . . . . . . . . . . . . . . . . Software update for groups
              (help groups if installed)  . . . . . . . . . . . . . . . .  N. J. Cox
              Q1/18   SJ 18(1):291
              groups exited with an error message if weights were specified;
              this has been corrected
      
      SJ-17-3 st0496  . . . . .  Speaking Stata: Tables as lists: The groups command
              (help groups if installed)  . . . . . . . . . . . . . . . .  N. J. Cox
              Q3/17   SJ 17(3):760--773
              presents command for listing group frequencies and percents and
              cumulations thereof; for various subsetting and ordering by
              frequencies, percents, and so on; for reordering of columns;
              and for saving tabulated data to new datasets
      Code:
       
      . groups eth a b6 b12 d
      
        +-----------------------------------------------+
        | ethnic~y   a   b6   b12   d   Freq.   Percent |
        |-----------------------------------------------|
        |        0   0    0     0   0      17      1.70 |
        |        0   0    0     0   1      21      2.10 |
        |        0   0    0     1   0      24      2.40 |
        |        0   0    0     1   1      17      1.70 |
        |        0   0    1     0   0      33      3.30 |
        |-----------------------------------------------|
        |        0   0    1     0   1      19      1.90 |
        |        0   0    1     1   0      19      1.90 |
        |        0   0    1     1   1      20      2.00 |
        |        0   1    0     0   0      17      1.70 |
        |        0   1    0     0   1      18      1.80 |
        |-----------------------------------------------|
        |        0   1    0     1   0      26      2.60 |
        |        0   1    0     1   1      17      1.70 |
        |        0   1    1     0   0      21      2.10 |
        |        0   1    1     0   1      25      2.50 |
        |        0   1    1     1   0      25      2.50 |
        |-----------------------------------------------|
        |        0   1    1     1   1      23      2.30 |
        |        1   0    0     0   0      25      2.50 |
        |        1   0    0     0   1      17      1.70 |
        |        1   0    0     1   0      21      2.10 |
        |        1   0    0     1   1      16      1.60 |
        |-----------------------------------------------|
        |        1   0    1     0   0      23      2.30 |
        |        1   0    1     0   1      22      2.20 |
        |        1   0    1     1   0      22      2.20 |
        |        1   0    1     1   1      21      2.10 |
        |        1   1    0     0   0      15      1.50 |
        |-----------------------------------------------|
        |        1   1    0     0   1      20      2.00 |
        |        1   1    0     1   0      22      2.20 |
        |        1   1    0     1   1      18      1.80 |
        |        1   1    1     0   0      17      1.70 |
        |        1   1    1     0   1      15      1.50 |
        |-----------------------------------------------|
        |        1   1    1     1   0      17      1.70 |
        |        1   1    1     1   1      27      2.70 |
        |        2   0    0     0   0      17      1.70 |
        |        2   0    0     0   1      25      2.50 |
        |        2   0    0     1   0      18      1.80 |
        |-----------------------------------------------|
        |        2   0    0     1   1      24      2.40 |
        |        2   0    1     0   0      16      1.60 |
        |        2   0    1     0   1      25      2.50 |
        |        2   0    1     1   0      15      1.50 |
        |        2   0    1     1   1      24      2.40 |
        |-----------------------------------------------|
        |        2   1    0     0   0      21      2.10 |
        |        2   1    0     0   1      23      2.30 |
        |        2   1    0     1   0      23      2.30 |
        |        2   1    0     1   1      13      1.30 |
        |        2   1    1     0   0      30      3.00 |
        |-----------------------------------------------|
        |        2   1    1     0   1      20      2.00 |
        |        2   1    1     1   0      19      1.90 |
        |        2   1    1     1   1      27      2.70 |
        +-----------------------------------------------+


      Comment


      • #4
        Thanks Nick Cox for the helpful addition! I was aware of tab1, but I didn't put 2 and 2 together (so to speak) and was unaware of tab2. Please note OP that the foreach loop in #2 is a general way to repeat the same line iteratively, but Stata often has useful built in features that let you avoid loops entirely, tab2 among them.

        Comment

        Working...
        X