我已经构建了一个lme4用于模型选择的模型,dredge但是我无法将随机效应与相关的固定效应对齐。我的完整模型的结构如下。
fullModel<-glmer(y ~x1 + x2 + (0+x1|Year) + (0+x1|Country) + (0+x2|Year) + (0+x2|Country) + (1 | Year) +(1|Country), family=binomial('logit'),data = alldata)
Run Code Online (Sandbox Code Playgroud)
在这个模型结构中,模型选择dredge产生了三种固定效应的组合,即 x1、x2 和 x1+x2,但是随机效应结构保持与完整模型相同,这样即使固定效应只有 x1,随机效应将包括(0+x2|Year) + (0+x2|Country). 例如,只有 x1 作为固定效应的模型,在随机效应结构中仍然会有 x2,如下所示。
y ~x1 + (0+x1|Year) + (0+x1|Country) + (0+x2|Year) +(0+x2|Country) + (1 | Year) +(1|Country), family=binomial('logit')
Run Code Online (Sandbox Code Playgroud)
有没有办法配置dredge不选择其中指定了其他固定效果的随机效果?我有大约 x1….x50。
您不能开箱即用,因为dredge当前省略了所有(x|g)表达式,但是您可以在 ( g)周围制作一个“包装器”lmer来替换“|” 公式中的术语与其他内容(例如re(x,g)),因此dredge认为这些是固定效果。例子:
glmerwrap <-
function(formula) {
cl <- origCall <- match.call()
cl[[1L]] <- as.name("glmer") # replace 'lmerwrap' with 'glmer'
# replace "re" with "|" in the formula:
f <- as.formula(do.call("substitute", list(formula, list(re = as.name("|")))))
environment(f) <- environment(formula)
cl$formula <- f
x <- eval.parent(cl) # evaluate modified call
# store original call and formula in the result:
x@call <- origCall
attr(x@frame, "formula") <- formula
x
}
formals(glmerwrap) <- formals(lme4::glmer)
Run Code Online (Sandbox Code Playgroud)
以下example(glmer):
# note the use of re(x,group) instead of (x|group)
(fm <- glmerwrap(cbind(incidence, size - incidence) ~ period +
re(1, herd) + re(1, obs), family = binomial, data = cbpp))
Run Code Online (Sandbox Code Playgroud)
现在,
dredge(fm)
Run Code Online (Sandbox Code Playgroud)
操纵固定和随机效果。