Subsetting in dredge (MuMIn) - must include intera

2019-04-11 08:03发布

问题:

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?

回答1:

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)