thothal
thothal

Reputation: 20399

Determine Which Main Effect Belongs to Which Interaction

Assume we have the following model:

set.seed(1)
d <- data.frame(a = gl(4, 1, 64), a4 = sample(4, 64, TRUE), 
                x = rnorm(64), y = rnorm(64))

l <- lm(y ~ a4 + a * x, d)

For the interaction x:a I will get 3 coefficients x:a2, x:a3, x:a4. I want now to determine which coefficients are the corresponding main effects associated to this interactions, which would be x, a2, a3 and a4.

My idea was to use strsplit on the interactions and to retrieve the corresponding main effects:

(atoms <- strsplit(names(coef(l))[7:9], ":"))
# [[1]]
# [1] "a2" "x" 
# [[2]]
# [1] "a3" "x" 
# [[3]]
# [1] "a4" "x"

So far so good. But now I would like to get the value of the corresponding main effect. While this is straight forward for x, a2, a3 (as these are unique names) I struggle to see how I can do that with a4:

lapply(atoms, function(.) coef(l)[.])
# [[1]]
#        a2         x 
# 0.3630732 0.2136751 
# [[2]]
#         a3          x 
# 0.04153299 0.21367510 
# [[3]]
#         a4          x 
# 0.04765737 0.21367510 

The result for a4 is wrong, because it is the main effect associated with the variable a4 and not the dummy coded factor 4 of factor a.

So, the model I was showing is a valid model in R, yet the names of the coefficients are ambiguous. So is there any other way how I can make a correct mapping between the coefficients of an interaction and the corresponding main effects?

Upvotes: 1

Views: 151

Answers (1)

Roland
Roland

Reputation: 132969

You can use the assign component of the lm object:

l$assign
[1] 0 1 2 2 2 3 4 4 4

This maps the coefficients to the expanded formula a4 + a + x + a:x.

See help("model.matrix") for documentation of the assign component.

Edit:

To expand my answer, you can do this:

terms <- labels(terms(l))
coef(l)[l$assign %in% which(terms %in% strsplit("a:x", ":", fixed = TRUE)[[1]])]

#        a2         a3         a4          x 
#0.36307321 0.04153299 0.23383125 0.21367510

Upvotes: 1

Related Questions