问题
I am trying to use dredge
from the R package MuMIn
with a global binomial glmer
model. I find that I need to specify the optimizer with control = glmerControl(optimizer="bobyqa")
for convergence. However, when I go to use dredge
, I get an error. If I reduce the number of predictors in the model, I can remove the bobyqa
specification, get convergence, and use dredge. Any way I can get dredge
to go with 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)"
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())
回答1:
tl;dr probably a bug in MuMIn::dredge()
- I'm still digging - but it seems to work OK if you leave out the extra="R^2"
specification.
reproducible example
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")
now try glmer()+dredge(), with and without optimizer specification
library(MuMIn)
d0 <- dredge(m0)
m1 <- update(m0, control=glmerControl(optimizer="bobyqa"))
d1 <- dredge(m1)
These all work - so the problem must be with some of the optional arguments. Testing that:
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
Which of the extra arguments is the culprit?
d1C <- dredge(m1, beta=c("partial.sd")) ## works
d1D <- try(dredge(m1, extra=c("R^2"))) ## fails
If you really, really want your R^2 values you could download/unpack the source code to the package, edit line 101 of R/r.squaredLR.R
as indicated below (add cl$control
to the list of elements that are set to NULL
, and re-install the package ...
===================================================================
--- 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
回答2:
The issue is in r.squaredLR
(implied by extra = "R^2"
), which tries to fit a glm
null model with glmer
's argument control = glmerControl(optimizer="bobyqa")
. (I will try to implement a solution for this bug in the forthcoming version of MuMIn.)
In case of glmer
(or mixed models in general) it may be better to use r.squaredGLMM
rather than r.squaredLR
. So you would need to provide dredge
with a function that extracts the R^2 vector from the result of r.squaredGLMM
(which returns a matrix
). For example:
# (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", ]
}))
来源:https://stackoverflow.com/questions/53856379/dredge-doesnt-work-when-specifying-glmer-optimizer