Announcement

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

  • Tabulations and Tables

    Simple question: tab x y,row nolabel
    tab x z, row nolabel

    What would be the command for a table that combines the above two tab commands?
    So it includes x y z in row without the label

    I tried table x y z, row
    But for some reason that's missing a column ( i have the header for all four columns but it's missing the output of one column)

    Thanks,
    Ghia
    Last edited by Ghia Osseiran; 05 Apr 2016, 03:41.

  • #2
    Not clear to me what your table would look like, but try

    Code:
    ssc inst groups 
    help groups 
    
    webuse nlswork
    groups race collgrad south, sepby(race)
    
      +-------------------------------------------+
      | race   collgrad   south   Freq.   Percent |
      |-------------------------------------------|
      |    1          0       0   10886     38.16 |
      |    1          0       1    5442     19.08 |
      |    1          1       0    2670      9.36 |
      |    1          1       1    1176      4.12 |
      |-------------------------------------------|
      |    2          0       0    2689      9.43 |
      |    2          0       1    4495     15.76 |
      |    2          1       0     331      1.16 |
      |    2          1       1     534      1.87 |
      |-------------------------------------------|
      |    3          0       0     194      0.68 |
      |    3          0       1      27      0.09 |
      |    3          1       0      73      0.26 |
      |    3          1       1       9      0.03 |
      +-------------------------------------------+

    Comment


    • #3
      Thanks this worked but what I'm looking for is more a table with race unigrad (dummy variable so 0 1) and say vocational grad ( 0 1) where I just get the frequency of each per race in this example. So the table would be five columns in total (exactly what you get from the command tab x y, row just with 3 variables instead of 2).

      Comment


      • #4
        Check out tab2, firstonly

        Comment


        • #5
          Hi Ghia: I think some example data might make your question more clear, but here are some thoughts:

          1. You mention: "I tried table x y z, row
          But for some reason that's missing a column ( i have the header for all four columns but it's missing the output of one column)"

          This is due to missing combinations of xy, yz, ... in your table -- where those combinations don't exist, -table- leaves the column blank in the output.


          2. Your original post says you want the output from -tab x y,row nolabel- combined with the output from -tab x z, row nolabel- . Importantly, this produces row percentages in the -tab- output in addition to the frequencies (later you talk about just frequencies in lieu of row percents). If you want to use -table- to produce row percents (though there are better ways to get there, see below), you'd need to manipulate your data so that that the value produced in the -table- are the row percents. One way to do that is to use -contract- like this:

          Code:
          sysuse auto , clear
          keep if mpg<20
          tab mpg rep78,row nolabel
          tab mpg for,row nolabel
          
          **
          table mpg rep78 for
          
          
          preserve
              contract  mpg for rep78,  
              bys mpg: egen denom = total(_freq)
              g rowpct = round((_freq/denom)*100, .01)
          
           table mpg rep78 for , c(mean _freq) //same as prior table cmd
           table mpg rep78 for, c(mean rowpct) //now w/row pcts
          restore

          however, based on your later response to Nick, I'm suspect you might want frequencies only. If so, -table- is already giving you what you need (without any data manipulation like contract).

          **Producing 3-way tabulation options:
          You can produce 3 way tabulations by using something like -contact- or -collapse- and then export your dataset. You can also download -tab3way- from SSC.

          However, I prefer to combine the categories for 2 of the variables, and then produce the tabulate table (or -tabout- from SSC is another good option here) using the new combined variable.

          __
          Here is an example using fake data that mirror what you have described about your dataset:

          Code:
          **create fake data
          clear
          set obs 100
          g unigrade = rbinomial(1, .4)
          g vocational = rbinomial(1, .3)
          g race = int(runiform()*4+1)
          lab def race 1 "Race1" 2 "Race2" ///
              3 "Race3" 4 "Race4", modify
          lab val race race
          lab def yn 0 "No" 1 "Yes", modify
          lab val vocational unigrade yn
          su *
          
          table un vo ra
          
          *use tab
              *combine categories, even if labeled
              g combined = .
              levelsof unigrade, loc(lu)
              levelsof vocation, loc(lv)
                  loc labu `"`:value label unigrade'"'
                  loc labv `"`:value label vocation'"'    
              loc ii = 1
              foreach u in `lu' {
              loc i =0
              foreach v in `lv' {
                loc labb `"`labb'  `ii' `"Uni:`:label `labu' `i''--Voc:`:label `labv' `i''"' "'
                 replace combined = `ii' ///
                      if unigrade ==`u' & vocation ==`v'
                  loc `++i'
                  loc `++ii'
              }
              }
                  di in r `"`labb'"'
              lab def combined `labb', modify
              lab val combined combined
              
              ta  combined race, row
          Produces a new variable 'combined' with these category labels (you can change these to make them more informative

          Code:
          .         lab list
          combined:
                     1 Uni:No--Voc:No
                     2 Uni:Yes--Voc:Yes
                     3 Uni:No--Voc:No
                     4 Uni:Yes--Voc:Yes
          This is how combined is related to (recoded from vocation and unigrade):

          Code:
                 ta combined unigrade
          
                           |       unigrade
                  combined |        No        Yes |     Total
          -----------------+----------------------+----------
            Uni:No--Voc:No |        44          0 |        44
          Uni:Yes--Voc:Yes |        23          0 |        23
            Uni:No--Voc:No |         0         26 |        26
          Uni:Yes--Voc:Yes |         0          7 |         7
          -----------------+----------------------+----------
                     Total |        67         33 |       100
          
          
          .         ta combined vocation
          
                           |      vocational
                  combined |        No        Yes |     Total
          -----------------+----------------------+----------
            Uni:No--Voc:No |        44          0 |        44
          Uni:Yes--Voc:Yes |         0         23 |        23
            Uni:No--Voc:No |        26          0 |        26
          Uni:Yes--Voc:Yes |         0          7 |         7
          -----------------+----------------------+----------
                     Total |        70         30 |       100

          Here's the final output:
          Code:
          .         ta  combined race, row nokey
          
                           |                    race
                  combined |     Race1      Race2      Race3      Race4 |     Total
          -----------------+--------------------------------------------+----------
            Uni:No--Voc:No |        12         11          7         14 |        44
                           |     27.27      25.00      15.91      31.82 |    100.00
          -----------------+--------------------------------------------+----------
          Uni:Yes--Voc:Yes |         7          7          6          3 |        23
                           |     30.43      30.43      26.09      13.04 |    100.00
          -----------------+--------------------------------------------+----------
            Uni:No--Voc:No |         4          7          8          7 |        26
                           |     15.38      26.92      30.77      26.92 |    100.00
          -----------------+--------------------------------------------+----------
          Uni:Yes--Voc:Yes |         2          1          1          3 |         7
                           |     28.57      14.29      14.29      42.86 |    100.00
          -----------------+--------------------------------------------+----------
                     Total |        25         26         22         27 |       100
                           |     25.00      26.00      22.00      27.00 |    100.00
          Note how this final table matches the frequencies and the row percentages produced in the earlier, separate -table- commands I provided. Now these freq/row percentages are all combined in this last table.



          Last edited by eric_a_booth; 05 Apr 2016, 07:59.
          Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

          Comment

          Working...
          X