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

问题描述 投票:1回答:1

考虑到某些标准,我尝试在全局模型上使用MuMIn::dredge()给我我的候选模型。我已经阅读了?dredge并理解了其中的一些内容,但我仍然对如何包含我的一个标准有一些疑问:

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

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

(几个主要的影响和几个交互)我想指定我只想要挖掘一次返回包含一个交互的模型,我如何以简单的方式对其进行子集化?

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

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))

?

r syntax subset mumin
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.