4

I'm trying to fit a number of linear models as shown below. It is important that all interaction terms are sorted lexicographically. Note that the second model is missing the main effect for x.

x = rnorm(100)
y = rnorm(100)
z = x + y + rnorm(100)
m1 = glm(z ~ x + y + x:y)
m2 = glm(z ~ y + x:y)

The models don't behave as expected with respect to the interaction terms:

m1: 
x:y          -0.1565     0.1151  -1.360   0.1770    

m2:
y:x          -0.2776     0.1416  -1.961   0.0528 . 

I understand that there may be a way to use the interaction() function with the lex.order argument but I can't figure out how or, indeed, whether this is the best way to go. Advice?

rimorob
  • 624
  • 1
  • 5
  • 16
  • If you tell R to fit a model including `x:y`, only the interaction will be included. Use `x*y` if you want to add the main effects as well – adaien Mar 26 '16 at 14:13
  • See my comment above: the main effect is missing on purpose. I have two similar models, one without main effect, and I need to be able to have the interaction terms in the same order. This is not a mistake. – rimorob Mar 26 '16 at 14:27
  • Then why are they not behaving as expected? – adaien Mar 26 '16 at 14:38
  • What is not "as expected" is that the second model is written down as z ~ y + y:x rather than z ~ y + x:y – rimorob Mar 26 '16 at 14:46
  • 1
    It doesn't make any difference, regressing on `y:x` or `x:y` is the same as the variable `y:x` is just the (element-wise) product of `x` and `y` – adaien Mar 26 '16 at 14:51
  • 3
    You will need to change the order of your predictors, as model.frame arranges them in the order it meets them. So `m2 = glm(z ~ x:y + y)` – user20650 Mar 26 '16 at 15:22
  • 1
    But the coefficients fitted will always be the same.. – adaien Mar 26 '16 at 15:47

0 Answers0