green_chilli
green_chilli

Reputation: 1

R - Subsetting in dredge (MuMin) – only include interaction with b if also an interaction with a

I am wanting to use dredge::MuMIn to explore my data. I only want to include an interaction between I(GISalt^2) and one of the other environmental variables if there is also an interaction between the variable and GISalt.

e.g. I would like to keep:

(GISalt * Forest) AND (I(GISalt^2) * Forest)
mod1 <- glm(MLE2017 ~ MLE200405 + (GISalt * Forest) + Scrub + I(GISalt^2) * Forest)

and

(GISalt * Forest) but NOT I(GISalt^2) * Forest)
mod2 <- glm(MLE2017 ~ MLE200405 + (GISalt * Forest) + Scrub)

and to exclude:

I(GISalt^2) * Forest) but NOT GISalt * Forest) mod3 <- glm(MLE2017 ~ MLE200405 + Scrub + I(GISalt^2) * Forest)

global model containing all variables for dredge()

globmod <- glm(MLE2017 ~ MLE200405 + GISalt * Forest + GISalt * Scrub 
    + GISalt * Meadow + GISalt * RiverLgthm + GISalt * DailySunHrs  + 
    I(GISalt^2) * Forest + I(GISalt^2) * Scrub + I(GISalt^2) * Meadow +
    I(GISalt^2) * RiverLgthm + I(GISalt^2) * DailySunHrs, 
    data = GLMdata, family = x.quasipoisson(link = "log"))

Upvotes: 0

Views: 1380

Answers (1)

Kamil Bartoń
Kamil Bartoń

Reputation: 1562

You need an expression of form dc(I(A^2):B, A:B) or, more explicitly !{I(A^2):B} || {A:B}, i.e. "either no A^2 * B, or A * B" or (note that A*B in an R formula is a shorthand for A+B+A:B, where A:B is the actual interaction term). There is no out-of-box way in dredge to expand B to "any other variable", but you can generate a suitable expression for a list of terms.

If the general expression is !{I(A^2):VARIABLE} || {A:VARIABLE}, you can use substitute to well, substitute the name VARIABLE with an actual variable name.

substitute((!{I(A^2):VARIABLE} || {A:VARIABLE}), list(VARIABLE = as.name("B")))

yields !{I(A^2):B} || {A:B}. Create such expression for each of "the other variables" using && operator. Wrapping all that in a function:

makeRule <- function(...) {
    exprList <- lapply(sys.call()[-1], function(x) substitute((! {I(A^2):VAR} || {A:VAR}), list(VAR = x)))
    rval <- exprList[[1]]
    for(x in exprList[-1]) rval <- call("&&", rval, x)
    rval
}

then:

 subsetExpr <- makeRule(B,C,D,E)

 dredge(model, subset = subsetExpr)

Upvotes: 1

Related Questions