robertevansanders robertevansanders - 3 months ago 22
R Question

How to use formula in R to exclude main effect but retain interaction

I do not want main effect because it is collinear with a finer factor fixed effect, so it is annoying to have these


In this example:

lm(y ~ x * z)

I want the interaction of
(numeric) and
(factor), but not the main effect of


From your question, x is numeric. Assuming you have z as a factor already, the specification you want is:

y ~ x + x:z

Since x is numeric, it is equivalent to do

y ~ x:z

The only difference here is parametrization (see examples below).

But if x is a factor, too, these two specifications are different which you can read Why do I get NA coefficients and how does lm drop reference level for interaction.

Consider a small example:

y <- rnorm(10)
x <- rnorm(10)
z <- gl(2, 5, labels = letters[1:2])

fit1 <- lm(y ~ x + x:z)
#(Intercept)            x         x:zb  
#     0.1989      -0.1627      -0.5456  

fit2 <- lm(y ~ x:z)
#(Intercept)         x:za         x:zb  
#     0.1989      -0.1627      -0.7082 

You can check the equivalence

all.equal(fit1$fitted, fit2$fitted)
# [1] TRUE