Announcement

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

  • Comparing sub-categories of a categorical variable across two groups

    Hello. I am trying to create descriptive statistics across two groups (treatment and control) for a categorical variable (occupation type) that has 5 categories. For each of the categories I would like to test the difference across the two groups. The question I am asking is: In each of the occupation types is there any significant difference in their distribution across the treatment and the control group. The code below gives me a mean of 0 i.e. no difference, which makes sense. What test would be appropriate here?

    ttest occupation if occupation==1, by(treatment)

    Many thanks,
    Karishma

  • #2
    While I'm not sure it really makes sense to do this, here is how you could:

    Code:
    gen one_level = .
    levelsof occupation, local(occupations)
    foreach o of local occupations {
        display "Occupation = `o'"
        replace one_level = (occupation == `o')
        tab one_level treatment, chi2
    }

    Comment


    • #3
      Thank you, Clyde. That worked very well.

      Comment


      • #4
        Hi Clyde! I am working on a similar issue with Karishma. Thank you for your code, which is very helpful for my case. However, I do not quite understand how the codes work. Could you please help me to understand what "foreach o of local occupations" means? what is the "o"? Instead of using these commands, can I create a dummy variable for each of the categories, for example, 1 = Level 1, 0 = all else, and then I will run a chi-square for this dummy variable and the treatment?

        Thank you so much!

        Comment


        • #5
          -foreach- is one of the key commands that everyone needs to learn in order to make effective use of Stata beyond the novice level. It introduces a loop. All of the commands between the braces {...} are executed repeatedly. The o mentioned in that -foreach- command is known as the iterator, or index, of the loop. The first time through the loop it is set to the value of the first item in the list (technically, a local macro) called occupations that was created by the preceding -levelsof- command. The second time through it is set to the second item in that list, and so on, until the list is exhausted. At that point, the loop ends and Stata moves on to the commands (if there are any) that come after the closing brace. While the loop is still running, wherever the value of o is needed, it is referred to by writing `o'.

          There is nothing magical about the use of o here. You can choose anything that is a legal Stata name. My choice of the letter o here was to highlight the fact that it will run through the values of local macro occupations. Particularly if you have multiple loops running inside each other, it's a good idea to give each iterator a name that calls to mind the list over which it iterators, so that when you read the code you can easily see what's what.

          You raise another approach: creating an indicator ("dummy") variable that takes on the levels you describe. You could do that, but occupation, itself, is precisely that variable! So no need to create an exact copy of it under another name. In that case, to get the cross tabulation done for each level of occupation, alternative code would be:
          Code:
          by occupation, sort: tab one_level treatment, chi2
          In fact that is more compact and perhaps more readily understandable than using -foreach- here. However, -by- has a limitation that -foreach- overcomes: -by- will only repeat a single command, whereas -foreach- will allow pretty much arbitrary length and complexity of code between the braces. (It is possible to get -by- to run more than one command at a time, but to do that you must wrap those commands in a -program- and make the program -byable-. I would say that that falls into the advanced level of Stata use that is still in your future.)

          Comment

          Working...
          X