lme4:如何指定随机效应之间的 0 相关性(即斜率和截距;在输出中:Corr = 0)

问题描述 投票:0回答:1

作为博士的后续问题。博克的回答

我在分析中遇到了类似的挑战。我的目标是将随机效应之间的相关性设置为 0,特别是随机斜率和截距之间的相关性。

但是,我发现这会导致随机效应之间的相关性为 1.00(我收到边界(奇异)拟合的警告:请参阅 help('isSingular'))。

您能否指出我是否可能忽略了一个方面,或者是否有我应该考虑的替代方法?

非常感谢您的时间和帮助!

我的模拟数据来自:

N <-  500 # sample size
n_clinic <-  50 # number of clusters
MT <- rep(c(0,1), length.out= N) # setting dichotomous predictor: control vs. treat
clinic = rep(1:n_clinic, each = N/n_clinic) # defining cluster ID
ppt_ID <- seq_len(N) # (optional) defining ppt ID
intercept <-  10 # grand mean
MTeffect <- 0 # overall, fixed effect
sigma <-  5 # variability within cluster
sd_int <-  5 # variability in intercepts
sd_MT <-  0 # (no) variability in cluster-specific coefficients
cor <-  0 # (no) association between intercepts and slopes
# var-covar matrix for slopes and intercepts

#set.seed(311)
# var-covar matrix for slopes and intercepts
varmat <-  matrix(c(sd_int^2, cor, cor, sd_MT^2), 2, 2) 
# generate data from var-covar
re <-  mvtnorm::rmvnorm(n_clinic, sigma = varmat)
colnames(re) = c('Intercept', 'MTeffect')
# generate outcome variable
ment_health <-  (intercept + re[clinic, 'Intercept']) + 
          (MTeffect + re[clinic, 'MTeffect'])*MT + rnorm(N, sd = sigma)
# putting everthing together
dat <-  tibble(
    ment_health,
    MT = factor(MT),
    clinic,
    ppt_ID
  )

它故意将斜率的 SD 设置为 0。(我知道这可能看起来很愚蠢,但其想法是从不真正具有变化斜率的总体中抽取随机样本,并且我要明确)。

我厌倦了以下

mod <- lme4::lmer(ment_health ~ MT + (1 + MT || clinic), data = dat)

我期望在summary(mod)的输出中看到Corr为0。但是,我看到 Corr 为 1.00。

我也知道如何使用 glmmTMB 执行此操作(并且它有效),但我需要专门使用 lme4。

任何建议或见解将不胜感激!

r lme4 mlm
1个回答
0
投票

?lmer::lmer

中所述

(由于其实现方式,“||”语法仅适用于包含数字(连续)预测变量的设计矩阵;要拟合具有独立分类效应的模型,请参阅“虚拟”或“lmer_alt”函数afex 的包裹。)

所以这里有三种拟合模型的方法:

将因子变量转换回数字

as.numeric(dat$MT)-1
as.numeric(as.character(dat$MT))
都会让我们回到 0/1 数值变量)

dat$MTnum <- as.numeric(dat$MT)-1
mod2 <- lme4::lmer(ment_health ~ MT + (1 + MTnum || clinic), data = dat)
VarCorr(mod2)

使用
dummy()
函数导出 0/1 变量

mod3 <- lme4::lmer(ment_health ~ MT + (1 + lme4::dummy(MT, "1") || clinic), data = dat)
VarCorr(mod3)

使用
afex::lmer_alt

mod4 <- afex::lmer_alt(ment_health ~ MT + (1 + MT || clinic), data = dat)
VarCorr(mod4)

如果您的因子具有两个以上级别,则最后一个选项将是最方便的(否则您必须为因子的每个非基线级别构造一个虚拟变量)。

© www.soinside.com 2019 - 2024. All rights reserved.