Announcement

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

  • Crosstabs of 2 categorical variables by group

    Hi everyone,
    I am currently working on a large dataset with individual level data from three countries (in one dataset) using Stata 12.
    Let's say I have a country identifier variable, one var that's educational attainment (4 categories plus one don't know" cat), and one that's attitude towards homosexuals (three categories 1=No; 2=yes, 3=don't know).

    I need to create a table as follows:
    for each country, tabulate only those frequencies of attitudes towards homosexuals of category 2(=yes), by educational attainment.

    Based on such a table, I want to be able to easily read, for example, how many percentages of respondents with post-secondary qualifications state 2=yes in the attitudes variable.

    Is there an easy way to do that?

    Thanks,
    Andreas

  • #2
    Code:
    bysort country : tab educational homosexual, col nofreq
    Will give you for each country the percentage of each categories of attitude toward homosexual by educational level.
    You can even recode the attitude variable into a binary (yes/no) to only have two alternatives by education level.


    It would be easier to check whether this is what you want if you post some of your data using dataex (SSC).

    This isn't yet in one table, you could manage to build you own table either by hand (fastidious), either using tabout (SSC). See help tabout for details

    Best,
    Charlie

    Comment


    • #3
      Thanks Charly,
      I am aware of your suggested syntax. That's the way I ussually do. I just thought there might be a more "fancy" solution to my issue, by telling Stata to display exactly and only the information that I need for my table (and finally the respective graph I want to plot).
      I tried
      Code:
      tabout
      on this matter, but I couldn't get the appropriate syntax. Any suggestion on that matter?

      Thanks

      Comment


      • #4
        Here is a similar graph to the one I want to create based on the stata output table:

        Click image for larger version

Name:	samesexrelat.PNG
Views:	1
Size:	39.7 KB
ID:	1364635


        You will notice that the variables are on the left hand side, the percentages display the percentage of respondents who answered with "yes", and the legend displays the three countries.

        Comment


        • #5
          I don't know exactly how to end up with the precise table you want, directly with one Stata line of code, this is why I let you browse the tabout helpfile.
          I was thinking about running the previous command for each country, and use the append otpion in tabout to merge them in one table.
          Maybe other people around here could help you doing this.

          However, why do you want such a complicated table if your real objective is to draw graphs?
          I would know a (long and certainly not the most efficient) way to simply collect each of these proportions into adequate new variables, and then draw the graphs, skipping the table intermediate.

          But perhaps you really want that table...

          Best,
          CharlIE

          Comment


          • #6
            Thanks, Charly!

            Well I will have to make the graph using excel. That is why I need a table in stata in the first place.

            However, why do you want such a complicated table if your real objective is to draw graphs?
            I would know a (long and certainly not the most efficient) way to simply collect each of these proportions into adequate new variables, and then draw the graphs, skipping the table intermediate.
            Would you kindly share the way you do that?

            Thanks
            Last edited by Andreas Head; 17 Nov 2016, 10:14.

            Comment


            • #7
              Actually this was easier than I first thought,
              but this remains Stata graphs.

              Edit : for a generalization to a larger number of countries, I used the distinct command (by Cox & Longton, from SSC).


              Code:
              clear
              input byte attitude float country byte educational
              1 1 5
              2 1 1
              1 1 3
              2 1 2
              3 1 1
              2 1 2
              1 1 2
              2 1 2
              3 1 2
              1 1 3
              3 1 1
              3 1 1
              3 1 1
              1 1 2
              2 1 1
              2 1 2
              2 1 2
              1 1 2
              1 1 1
              1 1 4
              1 1 1
              2 1 1
              3 1 3
              1 1 4
              1 1 3
              3 1 1
              3 1 1
              3 1 1
              1 1 2
              2 1 2
              3 1 1
              2 1 1
              3 1 2
              1 1 3
              3 1 1
              2 1 2
              2 1 2
              2 1 2
              1 1 3
              1 1 2
              3 1 1
              2 1 3
              1 1 3
              1 1 4
              3 1 4
              1 1 4
              2 1 2
              2 1 3
              2 1 2
              2 1 2
              1 1 3
              1 1 2
              3 2 4
              3 2 5
              3 2 5
              3 2 5
              2 2 5
              2 2 4
              3 2 5
              1 2 4
              2 2 3
              2 2 4
              1 2 5
              3 2 5
              1 2 5
              1 2 5
              3 2 3
              1 2 4
              2 2 3
              3 2 5
              2 2 5
              2 2 5
              3 2 5
              3 2 3
              end
              
              
               bysort country edu: gen yes=(att==2)
              
               collapse yes, by(country educ)
              
               
              distinct country
              local ncountry=r(ndistinct)
              forvalues c= 1/`ncountry'{
              graph hbar yes if country==`c',over(educ)
              graph save graph`c' ,replace
              }
              graph combine "graph1" "graph2"
              Last edited by Charlie Joyez; 18 Nov 2016, 01:56.

              Comment


              • #8
                Charlie: Thanks for the mention of distinct, but the Stata Journal website carries the latest version. Either way,

                Code:
                search distinct
                in Stata will call up references, notably to the longer discussion by Gary Longton and myself.

                Comment


                • #9
                  Also, see
                  Code:
                  help graph bar
                  To see how to add labels or colors in your graphs and

                  Code:
                  help graph combine
                  To see how to change the display of the combined graphs.

                  Comment


                  • #10
                    Nick Cox
                    Ok, I take good note of that.
                    Actually I didn't had the latest version of your program then, which is now corrected.
                    I'll take immediately a look at the changes. (glad to see the r(ndistinct) scalar is still here, so my previous code would work).

                    Best,
                    Charlie

                    Comment


                    • #11
                      Thank you, Charly and Nick.

                      I will try your code with -distinct-.

                      Comment


                      • #12
                        Indeed, as I have mentioned in another thread, I don't break previous versions without really good reasons!

                        Here's another approach to the graphics. I use designplot, first posted on SSC but now to be obtained from the Stata Journal website.

                        The original post announcing the program was at

                        http://www.statalist.org/forums/foru...riptive-tables

                        and there have been further mentions since in this forum.

                        In Stata

                        Code:
                        search designplot
                        will bring up a reference to a 2014 paper (accessible with a subscription or a one-off payment) and to the 2015 software update. The software should be installed by first clicking on gr0061_1.

                        SJ-15-2 gr0061_1 . . . . . . . . . . . . . . . Software update for designplot
                        (help designplot if installed) . . . . . . . . . . . . . . N. J. Cox
                        Q2/15 SJ 15(2):605--606
                        bug fixed for Stata 14

                        SJ-14-4 gr0061 Design plots for graphical summary of a response given factors
                        (help designplot if installed) . . . . . . . . . . . . . . N. J. Cox
                        Q4/14 SJ 14(4):975--990
                        produces a graphical summary of a numeric response variable
                        given one or more factors

                        (Incidentally, the Stata Journal team know of the small gremlin whereby an incorect title "The chi-square goodness-of-fit test for count data models" is displayed. That will be fixed as soon as possible.)

                        Code:
                        designplot yes country educational, variablenames t1title(Fraction saying Yes) ysc(r(-0.02 .)) marker(1, ms(O)) yla(0 "0" 0.1(0.1)0.6, format(%02.1f)) ytitle("")
                        Click image for larger version

Name:	designplot3.png
Views:	1
Size:	9.5 KB
ID:	1364744


                        I used Charlie's useful fake data posted in #7. The example here is at best indicative, not definitive. I have a preference here for dot charts over bar charts, especially given that two means are exact zeros, which has to be explained otherwise.

                        The main idea is that you can show results at various levels of aggregation, here for all the data, for two countries, for five educational levels, and for the cross-combinations that occur.




                        Comment

                        Working...
                        X