Announcement

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

  • Margins giving different output if interaction is done by hand?

    Hello everyone,

    I'm trying to understand something about the margins command. I'm using a dataset where my co-author created an interaction by hand and asked me to plot it in Stata. So I did, just like in the following code. I plotted the components of the interaction in margins. But then I tried to replicate the interactive terms by using the "var1##c.var2" pattern. Of course, the model coefficients are the same, but somehow, the margins results are completely different. Am I missing something?


    Code:
    webuse lbw, clear
    
    gen smokeXage = smoke*age
    
    logit low smoke age smokeXage
    margins, at(age=(14(5)45) smoke=(0 1))
    marginsplot, name(byhand, replace)
    
    logit low i.smoke##c.age
    margins, at(age=(14(5)45) smoke=(0 1))
    marginsplot, name(stata, replace)
    
    
    
    graph combine byhand stata

  • #2
    the margins command needs the factor variable notation used in your second example; otherwise it does not know that the variables are related to each other and cannot take advantage of that

    Comment


    • #3
      Yes, of course it is. You've missed the entire point. Only when you use factor-variable notation is Stata able to recognize that there is an interaction present. When you run -
      Code:
      logit low smoke age smokeXage
      smokeXage is just another variable. When -margins- sees it, it has no idea that somewhere earlier in the code you decided to calculate it as the product of smoke and age. So it just treats it as another, unrelated variable. You might as well have coded it:

      Code:
      logit low smoke age Stata_please_read_my_mind
      Added: Crossed with #2 which makes the same point.

      Also Added: Moral of the story--pretty much NEVER compute interactions by hand, use factor-variable notation. (The only reason for qualifying this never is that there are a handful of situations, all rather uncommon, where factor-variable notation is not supported. In that case you have to hand-calculate your interactions, but it also means that you cannot use -margins- afterwards (or can do so only with some pretty difficult workarounds.)
      Last edited by Clyde Schechter; 09 Aug 2017, 14:50.

      Comment


      • #4
        Ahhh I understand. Thank you for the clear explanation!

        Comment


        • #5
          There are some major built-in commands that do not support factor variable notation, e.g., sem, xtabond. The help files usually tell you right after the syntax if factor variable notation is allowed. User-written estimation commands, especially older ones, are also less likely to support factor variables.
          -------------------------------------------
          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