Announcement

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

  • automatic coding of "interaction term"

    Hello, I have a rather simple issue that is not statistical.

    I have 3 factor variables in stata, I want to generate 1 single term that is very similar to if I were to use an "interaction" in a regression.
    so example: i have 3 factor variables A B C
    each has 3 categories 1,2,3
    I want to generate a variable called ABC that is essentially like doing i.A#i.B#i.C when I run it in a regression model for example. I want to generate that term while hopefully not having to redo the labels. Using xi:regress .... is not exactly the same so please don't suggest that. I've read the fvvarlist and it did not help.

    There may even be a simple program out there but I am obviously not googling the right words.

    Thank you.

  • #2
    My understanding here is that you want one variable reflecting all 27 combinations of var A B C, presumably coded 1, ..., 27, with value labels for each category.
    I can't think of any way to do this other than writing some code using the extended macro functions to extract the variable and value labels. If you aren't so worried about all that, and you don't require 1, ..., 27 coding, you could start with

    Code:
    gen comb_ABC = 100*A + 10*B + C
    You can't have a single variable that is "essentially like doing i.A#i.B#i.C," since that results in 27 binary variables. Perhaps I misunderstand what you want.

    Comment


    • #3
      You understood what I needed. I am not worried about the value of the codes but I was hoping to have value labels somehow preserved/generated.
      Sadly, it seems like I would have to write the value labels part by hand so that would not be very different from writing all the combinations I have by hand and coding it manually.

      Thanks for your reply.

      Comment


      • #4
        Consider using the egen command.

        Code:
        . use https://www3.nd.edu/~rwilliam/statafiles/ordwarm2, clear
        (77 & 89 General Social Survey)
        
        . egen ycat = group(yr89 male white), label
        
        . tab ycat, nolabel
        
         group(yr89 |
        male white) |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                  1 |         89        3.88        3.88
                  2 |        629       27.43       31.31
                  3 |         72        3.14       34.45
                  4 |        589       25.69       60.14
                  5 |         79        3.45       63.58
                  6 |        430       18.75       82.34
                  7 |         43        1.88       84.21
                  8 |        362       15.79      100.00
        ------------+-----------------------------------
              Total |      2,293      100.00
        
        . tab ycat
        
            group(yr89 male |
                     white) |      Freq.     Percent        Cum.
        --------------------+-----------------------------------
        1977 Women NotWhite |         89        3.88        3.88
           1977 Women White |        629       27.43       31.31
          1977 Men NotWhite |         72        3.14       34.45
             1977 Men White |        589       25.69       60.14
        1989 Women NotWhite |         79        3.45       63.58
           1989 Women White |        430       18.75       82.34
          1989 Men NotWhite |         43        1.88       84.21
             1989 Men White |        362       15.79      100.00
        --------------------+-----------------------------------
                      Total |      2,293      100.00

        -------------------------------------------
        Richard Williams, Notre Dame Dept of Sociology
        StataNow Version: 19.5 MP (2 processor)

        EMAIL: [email protected]
        WWW: https://www3.nd.edu/~rwilliam

        Comment


        • #5
          Thank you Richard, that is exactly what I was looking for.

          Saves me an enormous amount of time!

          Comment


          • #6
            Good catch, Rich. I would never have gotten that from the documentation: "label requests that the integer-coded values of the grouped variable be labeled with the left-hand ends of the grouping intervals." I'd describe this as "...labeled by concatenating the value labels from the component variables."

            Comment


            • #7
              yes, I had to try it to be sure. It works the way I hoped it would work, which is always nice.
              -------------------------------------------
              Richard Williams, Notre Dame Dept of Sociology
              StataNow Version: 19.5 MP (2 processor)

              EMAIL: [email protected]
              WWW: https://www3.nd.edu/~rwilliam

              Comment

              Working...
              X