我正在尝试将 R 包MuMIn
中的dredge
与全局二项式glmer
模型一起使用。我发现我需要用control = glmerControl(optimizer="bobyqa")
来指定收敛的优化器。但是,当我去使用dredge
时,我收到一个错误。如果我减少模型中预测变量的数量,则可以删除bobyqa
规范、获取收敛并使用疏浚。有什么办法可以让dredge
和glmerControl(optimizer="bobyqa")
一起去吗?
test.glob=glmer(exploitpark~X + as.factor(Y) + Z + A + B + (1|ID),
family=binomial(),
glmerControl(optimizer="bobyqa"), data=df)
options(na.action = "na.fail") # prevent fitting models to different datasets
test.Set = dredge(test.glob, beta=c("partial.sd"), extra = c("R^2"))
Fixed term is "(Intercept)"
glm.control(optimizer = c("bobyqa", "bobyqa"), calc.derivs = TRUE, 中的错误: 未使用的参数 (optimizer = c("bobyqa", "bobyqa"), calc.derivs = TRUE, use.last.params = FALSE, restart_edge = FALSE, boundary.tol = 1e-05, tolPwrss = 1e-07, compDev = TRUE, nAGQ0initStep = TRUE, checkControl = list(check.nobs.vs.rankZ = "ignore", check.nobs.vs.nlev = "stop", check.nlev.gtreq.5 = "ignore", check.nlev.gtr.1 = "stop", check.nobs.vs.nRE = "stop", check.rankX = "message+drop.cols", check.scaleX = "warning", check.formula.LHS = "stop", check.response.not.const = "stop"), checkConv= list(check.conv.grad = list( action = "warning", tol = 0.001, relTol = NULL), check.conv.singular = list(action = "message", tol = 1e-04), check.conv.hess = list(action = "warning", tol = 1e-06)), optCtrl = list())
tl;dr可能是MuMIn::dredge()
中的一个错误 - 我仍在挖掘 - 但如果您省略extra="R^2"
规范,它似乎可以正常工作。
可重现的示例
set.seed(101)
dd <- data.frame(x1=rnorm(200),x2=rnorm(200),x3=rnorm(200),
f=factor(rep(1:10,each=20)),
n=50)
library(lme4)
dd$y <- simulate(~x1+x2+x3+(1|f),
family=binomial,
weights=dd$n,
newdata=dd,
newparams=list(beta=c(1,1,1,1),
theta=1))[[1]]
## fit model
m0 <- glmer(y~x1+x2+x3+(1|f),
family=binomial,
weights=n,
data=dd,
na.action="na.fail")
现在尝试 glmer()+dredge(),带和不带优化器规范
library(MuMIn)
d0 <- dredge(m0)
m1 <- update(m0, control=glmerControl(optimizer="bobyqa"))
d1 <- dredge(m1)
这些都有效 - 所以问题一定出在一些可选参数上。测试:
d0B <- dredge(m0, beta=c("partial.sd"), extra = c("R^2")) ## works
d1B <- try(dredge(m1, beta=c("partial.sd"), extra = c("R^2"))) ## fails
哪个额外的论点是罪魁祸首?
d1C <- dredge(m1, beta=c("partial.sd")) ## works
d1D <- try(dredge(m1, extra=c("R^2"))) ## fails
如果你真的,真的想要你的 R^2 值,你可以下载/解压缩源代码到包中,编辑R/r.squaredLR.R
的第 101 行,如下所示(将cl$control
添加到设置为NULL
的元素列表中,然后重新安装包......
===================================================================
--- R/r.squaredLR.R (revision 443)
+++ R/r.squaredLR.R (working copy)
@@ -98,7 +98,7 @@
if(formulaArgName != "formula")
names(cl)[names(cl) == formulaArgName] <- "formula"
cl$formula <- update(as.formula(cl$formula), . ~ 1)
- cl$method <- cl$start <- cl$offset <- contrasts <- NULL
+ cl$method <- cl$start <- cl$offset <- cl$control <- contrasts <- NULL
}
cl <- cl[c(TRUE, names(cl)[-1L] %in% names(call2arg(cl)))]
if(evaluate) eval(cl, envir = envir) else cl
问题出在r.squaredLR
(由extra = "R^2"
暗示),它试图将glm
的空模型与glmer
的参数control = glmerControl(optimizer="bobyqa")
拟合。(我将尝试在即将推出的 MuMIn 版本中实现此错误的解决方案。
在glmer
(或一般的混合模型)的情况下,最好使用r.squaredGLMM
而不是r.squaredLR
.因此,您需要为dredge
提供一个函数,该函数从r.squaredGLMM
的结果中提取 R^2 向量(返回matrix
)。例如:
# (following Ben Bolker's example above))
# Fit a null model with RE (use a non-exported function .nullFitRE or specify it by hand:
nullmodel <- MuMIn:::.nullFitRE(m1)
# the above step is not necessary, but avoids repeated re-fitting of the null model.
dredge(m1, beta="partial.sd", extra =list(R2 = function(x) {
r.squaredGLMM(x, null = nullmodel)["delta", ]
}))