The title may not be very explicit, I will use code to illustrate. My actual data got a four-way interaction, here I simplified it to three-way:
colnames(iris) <- c("y", "a", "b", "c")
m1 <- lm(y ~ a * b * (sin(c) + cos(c)),
na.action = "na.fail", data = iris)
Which is equally:
a + b + sin(c) + cos(c) + a:b + a:sin(c) + a:cos(c) + b:sin(c) + b:cos(c) + a:b:sin(c) + a:b:cos(c)
I want sin(c)
and cos(c)
to present or absent as an entirety in the model selection, so do the a:sin(c)
and a:cos(c)
, b:sin(c)
and b:cos(c)
, a:b:sin(c)
and a:b:cos(c)
.
Currently I can do it by using a complex subsetting logic in dredge()
:
m1_d <- MuMIn::dredge(m1, trace = 2, subset =
(`sin(c)` & `cos(c)` &
`a:sin(c)` & `a:cos(c)` &
`b:sin(c)` & `b:cos(c)` &
`a:b:sin(c)` & `a:b:cos(c)`) |
(`sin(c)` & `cos(c)` &
`a:sin(c)` & `a:cos(c)` &
`b:sin(c)` & `b:cos(c)` &
!`a:b:sin(c)` & !`a:b:cos(c)`) |
(`sin(c)` & `cos(c)` &
`a:sin(c)` & `a:cos(c)` &
!`b:sin(c)` & !`b:cos(c)`) |
(`sin(c)` & `cos(c)` &
!`a:sin(c)` & !`a:cos(c)` &
`b:sin(c)` & `b:cos(c)`) |
(`sin(c)` & `cos(c)` &
!`a:sin(c)` & !`a:cos(c)` &
!`b:sin(c)` & !`b:cos(c)`) |
(!`sin(c)` & !`cos(c)` &
`a:sin(c)` & `a:cos(c)` &
`b:sin(c)` & `b:cos(c)`) |
(!`sin(c)` & !`cos(c)` &
`a:sin(c)` & `a:cos(c)` &
!`b:sin(c)` & !`b:cos(c)`) |
(!`sin(c)` & !`cos(c)` &
!`a:sin(c)` & !`a:cos(c)` &
`b:sin(c)` & `b:cos(c)`) |
(!`sin(c)` & !`cos(c)` &
!`a:sin(c)` & !`a:cos(c)` &
!`b:sin(c)` & !`b:cos(c)`))
But this eventually became a thousand line of code with a four-way interaction.
I checked similar questions using dredge()
, but they were not helpful to my case.
Subsetting in dredge (MuMIn) - must include interaction if main effects are present R - Subsetting in dredge (MuMin) – only include interaction with b if also an interaction with a
I think there should be a more efficient way to do this?