Let's say there is a 4-way interaction with a 2x2x2 factorial design plus a continuous variable. Factors have standard contrast coding ( contr.treatment). Here is an example:
set.seed(1)
cat1 <- as.factor(sample(letters[1:2], 1000, replace = TRUE))
cat2 <- as.factor(sample(letters[3:4], 1000, replace = TRUE))
cat3 <- as.factor(sample(letters[5:6], 1000, replace = TRUE))
cont1 <- rnorm(1000)
resp <- rnorm(1000)
df <- data.frame(cat1, cat2, cat3, cont1, resp)
mod <- lm(resp ~ cont1 * cat1 * cat2 * cat3, data = df)
Looking at the output coef(mod), we get something like:
(Intercept) cont1 cat1b
0.019822407 0.011990238 0.207604677
cat2d cat3f cont1:cat1b
-0.010132897 0.105397591 -0.001153867
cont1:cat2d cat1b:cat2d cont1:cat3f
0.023358901 -0.194991402 0.060960695
cat1b:cat3f cat2d:cat3f cont1:cat1b:cat2d
-0.240624582 -0.117278931 -0.069880751
cont1:cat1b:cat3f cont1:cat2d:cat3f cat1b:cat2d:cat3f
-0.120446848 -0.141688864 0.136945262
cont1:cat1b:cat2d:cat3f
0.201792298
And in order to get an estimated intercept for cat1b(for example), we add our implicit member (Intercept)and cat1b, i.e. coef(mod)[1] + coef(mod)[3]. To get the slope change for the same category, we will use coef(mod)[2] + coef(mod)[6], a la this post is r-bloggers . It’s tedious enough to write everything, and it methods(class="lm")doesn’t look like it has any functions that make it right out of the gate.
- ?