挖掘子集的交互次数(MuMIn)

时间:2018-02-28 12:55:38

标签: r syntax subset mumin

我尝试在全局模型上使用MuMIn::dredge()给出我的候选模型,给定一定的标准。我已阅读?dredge并了解其中的一些内容,但我仍然对如何纳入我的一个标准提出疑问:

如果我有一个全球模型,例如

y ~ X1 + X2 + X3 + X4 + X5 + X6 + X7 + X1:X2 + X2:X3 + X3:X4 + X4:X6 + X5:X7

(几个主要效果和几个互动) 而且我想说明我只想让dredge一次返回包含一个交互的模型,我该如何以简单的方式对其进行子集化?

此外,如果全局模型还包括参数的二次多项式

Y ~ X1 + X1^2 + X2 + X3 + X4

并且我想指定这两个应该总是在模型中一起存在(主效果X1从不单独没有X1^2)我理解这个的语法是(同意?):

dredge(global.model, subset=(X1^2|!X1))

如果我理解正确,dredge()正在处理相反的问题(如果X1^2在模型中,则X1只会出现在模型中 - 相同的没有主效应的情况下永远不会发生的相互作用)?

dredge()内的二次多项式的语法是怎样的?我是对的,它是这样的:

dredge(global.model, subset=({I(X1^2)}|!X1))

1 个答案:

答案 0 :(得分:1)

不是最优雅的解决方案,但它有效:

library(MuMIn)

# example global model with many interactions:
fm <- lm(y ~ (X1 + X2 + X3 + X4)^2, Cement, na.action = na.fail)

# create a vector of interaction term names:
x <- c(getAllTerms(fm))
x <- x[grep(":", x)] # won't work if any variable name has ":" in it.

# create a subset expression (sum of interactions < N):
ss <- substitute(sum(X) < N, list(N = 3, X = as.call(lapply(c("c", x), as.symbol))))

# the resulting expression is:
sum(c(`X1:X2`, `X1:X3`, `X1:X4`, `X2:X3`, `X2:X4`, `X3:X4`)) < 3

dd <- dredge(fm, subset = ss)

# verify:
max(rowSums(!is.na(dd[, x]))) # --> 2

编辑:更好的互动检测,并包含在功能中:

subsetExprInteractionLimit <- function(model, N = 1) {
    x <- getAllTerms(model)
    x <- c(x)[attr(x, "order")][attr(terms(model), "order") > 1]
    substitute(sum(X) <= N, list(N = N, X = as.call(lapply(c("c", x), as.symbol))))
}

subsetExprInteractionLimit(fm, N = 1) # limit to 1 interaction
相关问题