robertevansanders - 14 days ago 4

R Question

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

`NA`

In this example:

`lm(y ~ x * z)`

I want the interaction of

`x`

`z`

`z`

Answer

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:

```
set.seed(0)
y <- rnorm(10)
x <- rnorm(10)
z <- gl(2, 5, labels = letters[1:2])
fit1 <- lm(y ~ x + x:z)
#Coefficients:
#(Intercept) x x:zb
# 0.1989 -0.1627 -0.5456
fit2 <- lm(y ~ x:z)
#Coefficients:
#(Intercept) x:za x:zb
# 0.1989 -0.1627 -0.7082
```

You can check the equivalence

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

Source (Stackoverflow)

Comments