【问题标题】:dredge subsetting number of interactions (MuMIn)疏浚子集交互次数 (MuMIn)
【发布时间】:2018-02-28 12:55:38
【问题描述】:

我尝试在全局模型上使用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))

?

【问题讨论】:

  • 更新:我想关于第一个问题有可能,做一长行的子集=!(X1:X2&&X2:x3)&&!(X1:X2&&X3:X4)&&等等- 但是在疏浚中是否存在这种子集的捷径,可以更容易地限制同时存在的交互数量?

标签: r syntax subset mumin


【解决方案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

【讨论】:

  • 谢谢!正如你所说,这不是最优雅的解决方案,但这在我的数据集中也适用,并且比我自己提出的解决方案更优雅。
  • 再想一想,这在尝试同时在子集中实现其他标准时不起作用。例如。如果我希望 X1 和 X2 始终一起出现,而不是单独出现 - 同时我只希望存在一个交互。
  • 你可以使用类似substitute({other_criteria} &amp; (XXX), list(XXX= subsetExprInteractionLimit(...)))的东西。
猜你喜欢
  • 2014-04-10
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 2023-02-12
  • 1970-01-01
  • 2018-06-18
  • 2016-06-12
  • 1970-01-01
相关资源
最近更新 更多