指定 glmer 优化器时疏浚不起作用

dredge doesn't work when specifying glmer optimizer

我正在尝试将 R 包 MuMIn 中的 dredge 与全局二项式 glmer 模型一起使用。我发现我需要使用 control = glmerControl(optimizer="bobyqa") 指定优化器以实现收敛。但是,当我去使用 dredge 时,出现错误。如果我减少模型中预测变量的数量,我可以删除 bobyqa 规范,收敛并使用疏浚。有什么方法可以让 dredgeglmerControl(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)"

Error in glm.control(optimizer = c("bobyqa", "bobyqa"), calc.derivs = TRUE, : unused arguments (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 值,您可以 download/unpack 包的源代码,编辑 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" 暗示),它试图用 glmer 的参数 control = glmerControl(optimizer="bobyqa") 拟合 glm 空模型. (我将尝试在即将发布的 MuMIn 版本中针对此错误实施解决方案。)

如果是 glmer(或一般的混合模型),使用 r.squaredGLMM 可能比 r.squaredLR 更好。因此,您需要为 dredge 提供一个函数,该函数从 r.squaredGLMM(returns 和 matrix)的结果中提取 R^2 向量。例如:

# (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", ]
}))