简体   繁体   English

如何遍历列以评估连续 lme4 混合模型中的不同固定效应并提取系数和 P 值?

[英]How to loop over columns to evaluate different fixed effects in consecutive lme4 mixed models and extract the coefficients and P values?

I am new to R and am trying to loop a mixed model across 90 columns in a dataset.我是 R 的新手,我正在尝试在数据集中的 90 列中循环混合 model。 My dataset looks like the following one but has 90 predictors instead of 7 that I need to evaluate as fixed effects in consecutive models.我的数据集如下所示,但有 90 个预测变量,而不是 7 个,我需要将其作为连续模型中的固定效应进行评估。 I then need to store the model output (coefficients and P values) to finally construct a figure summarizing the size effects of each predictor.然后我需要存储 model output(系数和 P 值)以最终构建一个总结每个预测变量的大小效应的数字。 I know the discussion of P value estimates from lme4 mixed models.我知道来自 lme4 混合模型的 P 值估计的讨论。

For example:例如:

set.seed(101)

mydata <- tibble(id = rep(1:32, times=25), 
   time = sample(1:800), 
   experiment = rep(1:4, times=200),
   Y = sample(1:800), 
   predictor_1 = runif(800), 
   predictor_2 = rnorm(800), 
   predictor_3 = sample(1:800), 
   predictor_4 = sample(1:800),
   predictor_5 = seq(1:800),
   predictor_6 = sample(1:800),
   predictor_7 = runif(800)) %>% arrange (id, time)

The model to iterate across the N predictors is:遍历 N 个预测变量的 model 是:

library(lme4)
library(lmerTest)  # To obtain new values

mixed.model <- lmer(Y ~ predictor_1 + time + (1|id) + (1|experiment), data = mydata)

summary(mixed.model)

My coding skills are far from being able to set a loop to repeat the model across the N predictors in my dataset and store the coefficients and P values in a dataframe.我的编码技能远不能设置循环以在我的数据集中的 N 个预测变量中重复 model,并将系数和 P 值存储在 dataframe 中。

I have been able to iterate across all the predictors fitting linear models instead of mixed models using lapply.我已经能够迭代所有拟合线性模型的预测变量,而不是使用 lapply 的混合模型。 But I have failed to apply this strategy with mixed models.但我未能将这种策略应用于混合模型。

varlist <- names(mydata)[5:11]

lm_models <- lapply(varlist, function(x) {
  lm(substitute(Y ~ i, list(i = as.name(x))), data = mydata)
})

One option is to update the formula of a restricted model (w/o predictor) in an lapply loop over the predictors.一种选择是在预测变量的lapply循环中update受限 model(无预测变量)的公式。 Then summary ze the resulting list and subset the coef ficient matrix using a Vectorize d function.然后使用Vectorize d function summary结果列表并子集coef矩阵。

library(lmerTest)
mixed.model <- lmer(Y ~ time + (1|id) + (1|experiment), data = mydata)

preds <- grep('pred', names(mydata), value=TRUE)
fits <- lapply(preds, \(x) update(mixed.model, paste('. ~ . + ', x)))

extract_coef_p <- Vectorize(\(x) x |> summary() |> coef() |> {\(.) .[3, c(1, 5)]}())

res <- `rownames<-`(t(extract_coef_p(fits)), preds)
res
#                 Estimate  Pr(>|t|)
# predictor_1 -7.177579138 0.8002737
# predictor_2 -5.010342111 0.5377551
# predictor_3 -0.013030513 0.7126500
# predictor_4 -0.041702039 0.2383835
# predictor_5 -0.001437124 0.9676346
# predictor_6  0.005259293 0.8818644
# predictor_7 31.304496255 0.2511275

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM