lme4随机效应结构与挖泥船

时间:2015-07-01 23:56:25

标签: r mixed-models

我在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)

在这个模型结构中,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')

有没有办法配置dredge不选择其中指定了其他固定效果的随机效果?我有x1 ... .x50。

1 个答案:

答案 0 :(得分:3)

由于dredge目前省略了所有(x|g)个表达式,因此无法开箱即用,但您可以制作一个&#34;包装器&#34;周围(glmer取代&#34; |&#34;公式中的术语与其他内容(例如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)

关注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))

现在,

dredge(fm)

操纵固定和随机效果。

相关问题