[英]dredge subsetting number of interactions (MuMIn)
I try to use MuMIn::dredge()
on a global model to give me my candidate models, given certain criteria.根据某些标准,我尝试在全局模型上使用
MuMIn::dredge()
来为我提供候选模型。 I've read ?dredge
and understood some of it, but I still have some question on how to include one of my criteria:我已经阅读了
?dredge
并理解了其中的一些内容,但我仍然对如何包含我的标准之一有一些疑问:
If I have a global model with eg如果我有一个全局模型,例如
y ~ X1 + X2 + X3 + X4 + X5 + X6 + X7 + X1:X2 + X2:X3 + X3:X4 + X4:X6 + X5:X7
(several main effects and several interaction) and I want to specify that I only want dredge to return models which include one interaction at a time, how do I subset this in an easy way? (几个主要影响和几个交互)并且我想指定我只希望挖泥返回一次包含一个交互的模型,我如何以一种简单的方式进行子集化?
Also, if the global model also includes a second degree polynomial of a parameter另外,如果全局模型还包括一个参数的二次多项式
Y ~ X1 + X1^2 + X2 + X3 + X4
and I want to specify that these two should always exist together in the models (main effects X1
never alone without X1^2
) I understood the syntax for this is (agree?):并且我想指定这两个应该始终一起存在于模型中(没有
X1^2
主效应X1
永远不会单独存在)我理解这种语法是(同意?):
dredge(global.model, subset=(X1^2|!X1))
And if I have understood it correctly, dredge()
is taking care of the other way around (the X1^2
will only occur in the model if X1
is in the model - same for interactions which will never occur without the main effects present)?如果我理解正确的话,
dredge()
会以另一种方式处理(如果X1
在模型中,则X1
X1^2
只会出现在模型中 - 对于在没有主效应的情况下永远不会发生的相互作用也是如此) ?
But how is the syntax for second degree polynomials inside dredge()
?但是
dredge()
二次多项式的语法如何? Am I right that it's something like this:我是否正确,它是这样的:
dredge(global.model, subset=({I(X1^2)}|!X1))
? ?
Not a most elegant solution, but it works:不是最优雅的解决方案,但它有效:
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
Edit: better interaction detection, and wrapped into function:编辑:更好的交互检测,并包装成功能:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.