3

I'm doing some exploratory work where I use dredge{MuMIn}. In this procedure there are two variables that I want to set to be allowed together ONLY when the interaction between them is present, i.e. they can not be present together only as main effects.

Using sample data: I want to dredge the model fm1 (disregarding that it probably doesn't make sense). If the variables GNP and Population appear together, they must also include the interaction between them.

require(stats); require(graphics)
## give the data set in the form it is used in S-PLUS:
longley.x <- data.matrix(longley[, 1:6])
longley.y <- longley[, "Employed"]
pairs(longley, main = "longley data")
names(longley)
fm1 <- lm(Employed ~GNP*Population*Armed.Forces, data = longley)
summary(fm1)
dredge(fm1, subset=!((GNP:Population) & !(GNP + Population)))
dredge(fm1, subset=!((GNP:Population) && !(GNP + Population)))

dredge(fm1, subset=dc(GNP+Population,GNP:Population))
dredge(fm1, subset=dc(GNP+Population,GNP*Population))

How can I specify in dredge() that it should disregard all models where GNP and Population are present, but not the interaction between them?

merv
  • 67,214
  • 13
  • 180
  • 245
ego_
  • 1,409
  • 6
  • 21
  • 31
  • Sorry, but I don't see the problem you are facing. What is the question? – llrs Mar 07 '14 at 11:14
  • How do I specify in dredge() that it should disregard all models where GNP and Population are present, but not the interaction between them. – ego_ Mar 07 '14 at 11:41

1 Answers1

8

If I understand well, you want to model the two main effects (say, a and b) only together with their interaction (a:b). So how about: subset = !a | (xor(a, b) | 'a:b') (enclose a:b in backticks (`) not straight quotes), e.g:

library(MuMIn)
data(Cement)
fm <- lm(y ~ X1 * X2, Cement, na.action = na.fail)
dredge(fm, subset = !X2 | (xor(X1, X2) | `X1:X2`))

or wrap this condition into a function to have the code more clear:

test <- function(a, b, c) !a | (xor(a, b) | c)
dredge(fm, subset = test(X1, X2, `X1:X2`))

that produces: null, X1, X2, X1*X2 (and excludes X1 + X2)

Kamil Bartoń
  • 1,482
  • 9
  • 10
  • I am facing a similar situation but i want to include all the combinations in the result set. How do i tell dredge? – user13892 Jun 09 '17 at 06:05