Announcement

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

  • Cross-tabulation of 3 variables with display of percentages

    Dear Stata users,
    Can anyone help me how to have three-way tables with display of percentages, which aweights are allowed with too.

  • #2
    I believe the command - table - can handle 3 variables as well as aweights. I'm not sure about percentage, but you may store the results, then produce these values.
    Best regards,

    Marcos

    Comment


    • #3
      Here's a three-way table.

      Click image for larger version

Name:	threeway.png
Views:	1
Size:	26.5 KB
ID:	1393281


      The code is based on an example in the help of tabplot (Stata Journal). That command allows calculation of percents and also weights. The example here uses frequency weights, but analytic weights are allowed too.

      Code:
        clear
          input str6 sex str8 year str1 policy int freq
          "male" "1" "A" 175
          "male" "1" "B" 116
          "male" "1" "C" 131
          "male" "1" "D" 17
          "male" "2" "A" 160
          "male" "2" "B" 126
          "male" "2" "C" 135
          "male" "2" "D" 21
          "male" "3" "A" 132
          "male" "3" "B" 120
          "male" "3" "C" 154
          "male" "3" "D" 29
          "male" "4" "A" 145
          "male" "4" "B" 95
          "male" "4" "C" 185
          "male" "4" "D" 44
          "male" "Graduate" "A" 118
          "male" "Graduate" "B" 176
          "male" "Graduate" "C" 345
          "male" "Graduate" "D" 141
          "female" "1" "A" 13
          "female" "1" "B" 19
          "female" "1" "C" 40
          "female" "1" "D" 5
          "female" "2" "A" 5
          "female" "2" "B" 9
          "female" "2" "C" 33
          "female" "2" "D" 3
          "female" "3" "A" 22
          "female" "3" "B" 29
          "female" "3" "C" 110
          "female" "3" "D" 6
          "female" "4" "A" 12
          "female" "4" "B" 21
          "female" "4" "C" 58
          "female" "4" "D" 10
          "female" "Graduate" "A" 19
          "female" "Graduate" "B" 27
          "female" "Graduate" "C" 128
          "female" "Graduate" "D" 13
          end
          tabplot policy year [w=freq], by(sex, subtitle(% by sex and year, place(w)) note("")) percent(sex year) ///
      showval bfcolor(none) subtitle(, fcolor(green*0.1))
      Sure, it's a graph too, but you mean you prefer a table to a graph?

      Conversely, there are plenty of tables that tabplot can't do, including those with two or more numbers in each cell.

      Comment


      • #4
        Stealing Nick's example data, here's two methods using -tab3way- from SSC and reshape:
        Code:
         clear
            input str6 sex str8 year str1 policy int freq
            "male" "1" "A" 175
            "male" "1" "B" 116
            "male" "1" "C" 131
            "male" "1" "D" 17
            "male" "2" "A" 160
            "male" "2" "B" 126
            "male" "2" "C" 135
            "male" "2" "D" 21
            "male" "3" "A" 132
            "male" "3" "B" 120
            "male" "3" "C" 154
            "male" "3" "D" 29
            "male" "4" "A" 145
            "male" "4" "B" 95
            "male" "4" "C" 185
            "male" "4" "D" 44
            "male" "Graduate" "A" 118
            "male" "Graduate" "B" 176
            "male" "Graduate" "C" 345
            "male" "Graduate" "D" 141
            "female" "1" "A" 13
            "female" "1" "B" 19
            "female" "1" "C" 40
            "female" "1" "D" 5
            "female" "2" "A" 5
            "female" "2" "B" 9
            "female" "2" "C" 33
            "female" "2" "D" 3
            "female" "3" "A" 22
            "female" "3" "B" 29
            "female" "3" "C" 110
            "female" "3" "D" 6
            "female" "4" "A" 12
            "female" "4" "B" 21
            "female" "4" "C" 58
            "female" "4" "D" 10
            "female" "Graduate" "A" 19
            "female" "Graduate" "B" 27
            "female" "Graduate" "C" 128
            "female" "Graduate" "D" 13
            end
            
        **tab3way
        preserve
        expand freq
        drop freq
        tab3way  year policy sex,  colpct nofreq
        restore
            
            
        
         
        **using reshape
        reshape wide freq, i(policy sex) j(year)    string
            
            **calculate percents
            egen  j1 = rowtotal(freq*)
            foreach j of varlist freq* {
            g p`j' =  string( (`j'/j1)*100, "%3.2f" ) + "%"
            drop `j' //unless you want to keep the freq
            }
            drop j1
            
        **export this table as needed here**
        Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

        Comment


        • #5
          Thank you both,
          by using table command I will have the below result.

          Code:
          table policy sex year
                                      year and    sex                              
                      1                   2                    3                  4                    Graduate --
          policy  female male    female male    female male    female male    female male
                                                          
          A          1        1            1        1         1       1             1        1          1       1
          B          1        1            1        1         1       1             1        1          1       1
          C          1        1            1        1         1       1             1        1          1       1
          D          1        1            1        1         1       1             1        1           1       1
          Simply i want the result to be in percentages instead of frequency, any idea?
          Last edited by Fahim Ahmad; 17 May 2017, 05:46.

          Comment


          • #6
            I dont see an option in the -table- help file for anything concerning producing percents, so my short answer to this is 'no'.

            (Longer answers include (1) the solutions I gave above and (2) using -tabulate- instead of -table- and extracting the elements from the saved / posted matrix results or (3) use something like -logout- from SSC to log the output from -table- command , import it into Stata, and post process it to get the table you want).
            Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

            Comment

            Working...
            X