Announcement

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

  • Coefficient on interaction terms switches sign on alternate model specification. Why?

    I want to run a regression with an interaction term (continuous x dummy). My traditional way of running this regression is reg logerr_w c.matstr_w##i.yr2013 (results attached).

    However, I find that if I manually generate the interaction variables (I would only do this so that I can control the order in which the results are displayed), the sign flips on the coefficient of the interaction. Why does this happen? Note that I understand that I should just run the model the correct way - but I'm very curious about WHY the sign would flip.

    Here is the code for my manual interaction variable construction:

    gen matstr_yr2013_1=0
    replace matstr_yr2013_1=matstr_w if yr2013==1
    gen matstr_yr2013_0=0
    replace matstr_yr2013_0=matstr_w if yr2013==0



    Click image for larger version

Name:	manual interaction.JPG
Views:	1
Size:	40.7 KB
ID:	1760978


    Click image for larger version

Name:	auto interaction.JPG
Views:	1
Size:	40.8 KB
ID:	1760979


  • #2
    However, I find that if I manually generate the interaction variables (I would only do this so that I can control the order in which the results are displayed), the sign flips on the coefficient of the interaction.
    No, that's not what's happening. In fact, in your homebrew model, there is no single variable that represents "the interaction", and nothing that is directly comparable to the coefficient of yr2013#c.mastr_1 in the factor-variable model. In fact, the two models are entirely equivalent; they are algebraic transforms of each other. But they are differently parameterized, and terms that have similar names don't necessarily represent the same thing.

    Specifically, in the homebrew model, the coefficient of matstr_2013yr_1 represents the slope of the outcome:matstr regression line when yr2013 == 1. In the factor variable model, there is no term at all that represents that. Rather, in that model, to get the slope of the outcome:matstr regression line when yr2013 == 1 you have to look at the sum of the coefficient of matstr and the coefficient of 1.yr2013#c.matstr. That sum is -.1885786 + .0941716 = -.094407, which, by no means coincidentally, is the coefficient of matstr_2013yr_1 in the homebrew model. Similarly, if you understand the algebra (I'm not going to work through every combination here) you can demonstrate that every coefficient in either model is either the same as some corresponding coefficient in the other model, or is equal to a sum of some coefficients in the other model.

    The other way you can see that the models are equivalent, run -predict- after each of them: you will get identical results (or perhaps differing by very tiny rounding errors). There's even a hint of it in the output you already have: the R2 and all the sums of squares are exactly the same in both models.

    I suggest you review how interaction models coded in factor-variable notation work. The clearest exposition of this I am aware of is the excellent Richard Williams' https://www3.nd.edu/~rwilliam/stats2/l53.pdf.

    By the way, if your reason for doing the homebrew model is to get the output in that form, with an increment for yr2013 and two separate slopes for matstr corresponding to the two values of yr2013, you can still do that with a different use of factor variable notation:
    Code:
    regress logerr_w i.yr2013 i.yr2013#c.matstr // N.B. #, not ##

    Comment


    • #3
      Wonderful explanation, thank you. I will read the information in the pdf that you linked.

      EDIT: Read the pdf and discovered why I was originally confused. I didn't read the numbers closely enough to see that the two highlighted coefficients in my screenshots were in fact different numbers - because they both started with 0.09 (*head slap*). If I add the coefficients for matstr_w and the interaction term from the factor model, I definitely get the coefficient for the matstr_yr2013_1 in the homebrew model.

      Thank you again for your help!
      Last edited by Sue Cooper; 07 Aug 2024, 18:32. Reason: Learned new info = figured it out

      Comment

      Working...
      X