Announcement

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

  • Combining dummy variables

    Hi all,

    I'm working with dummy variables for a variable MOMRACE for a class I'm taking. MOMRACE can take on 7 values, each corresponding to a different race. Because there are very few data points for 5, 6, and 7, I've been instructed to just combine them into a new dummy variable called "momother" for use in a multiple regression model. However, I can't seem to get the results I want.

    Here's the code I've tried using:

    generate momother = .
    replace momother = 1 if MOMRACE == 5 | MOMRACE == 6 | MOMRACE == 7
    replace momother = 0 if MOMRACE != . & (MOMRACE !=5 | MOMRACE != 6 | MOMRACE != 7)

    I assumed this would make a new dummy that includes categories 5,6, and 7, but when I look at the new variable momother, all of the values are 0.

    Any help would be great, thanks!

  • #2
    6 and 7 qualify for 0 as not equal to 5, 5 qualifies for 0 as not equal to 6 or 7, and so forth. Hence your second statement undoes your first.

    Code:
    generate momother = inlist(MOMRACE, 5, 6, 7) if MOMRACE < . 
    would map missings to missing; 5, 6, 7 to 1; and any other values to 0.

    See for more discussion https://journals.sagepub.com/doi/pdf...36867X19830921

    https://www.stata.com/support/faqs/d...rue-and-false/

    https://journals.sagepub.com/doi/pdf...867X1601600117



    Comment


    • #3
      I will hold up my hand to agree that you can be badly bitten by programming with logical operators, regardless of how long or how much you've done it before.

      Here is a simple worked example with 1, 2, 3, 4 shown as other possible values. If that's wrong, it shouldn't matter, as they are just proxy for other possible values given interest in 5, 6, 7.

      The essential fallacy is that recipe 2 gives the complement of recipe 1, i.e. the result of each is 0 when the other is 1, and vice versa. That is wrong.

      Recipe 3 or recipe 4 give complements.

      However, you don't need both an indicator variable and its complement.

      Code:
      clear
      set obs 7
      gen MOMRACE = _n
      
      gen indicator1 = MOMRACE == 5 | MOMRACE == 6 | MOMRACE == 7
      gen indicator2 = MOMRACE != 5 | MOMRACE != 6 | MOMRACE != 7
      gen indicator3 = !(MOMRACE == 5 | MOMRACE == 6 | MOMRACE == 7)
      gen indicator4 = MOMRACE != 5 & MOMRACE != 6 & MOMRACE != 7
      
      list, sepby(indicator1)
      
           +-----------------------------------------------------+
           | MOMRACE   indica~1   indica~2   indica~3   indica~4 |
           |-----------------------------------------------------|
        1. |       1          0          1          1          1 |
        2. |       2          0          1          1          1 |
        3. |       3          0          1          1          1 |
        4. |       4          0          1          1          1 |
           |-----------------------------------------------------|
        5. |       5          1          1          0          0 |
        6. |       6          1          1          0          0 |
        7. |       7          1          1          0          0 |
           +-----------------------------------------------------+
      Last edited by Nick Cox; 05 Dec 2023, 03:55.

      Comment

      Working...
      X