[R-sig-ME] lme4 package

Ben Bolker bbo|ker @end|ng |rom gm@||@com
Tue May 31 23:49:52 CEST 2022

   This is not a particularly mixed-model-specific question.  The 
general approach would be something like:

   var_combs <- list(model1 = c("X1", "X7"),
                model2 = c("X2", "X3", "X12"),
                model3 = c("X4", "X8", "X9", "X11"),
         ## etc.)

(I don't know the logic behind your choice of combinations of predictors 
so can't automate it any further)


  results <- list()
  for (m in names(var_combs)) {
      form <- reformulate(c(var_combs[[m]], "(1|group)"), response = "Y")
      fit <- lmer(form, data = ..., ....)
      results[[m]] <- fit

   This assumes that the random-effects component is only variation in 
the intercept across `group`.

There are a huge variety of alternatives -- you could use lapply() or 
purrr::map() instead of a for loop, do something to parallelize the fits 
across cores, etc. etc..

On 2022-05-31 4:38 p.m., mina jahan wrote:
> Hi,
> I want to run linear mixed-effects models based on the different subsets of
> predictor variables (the outcome variable in each model is similar) using
> package lme4. For example, a dataset contains a Y as a longitudinal outcome
> variable and X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, and X12 as
> predictor variables. I want to create a loop to run these models:
> Y~X1+X7
> Y~X2+X3+X12
> Y~X4+X8+X9+X11
> Y~X5+X6+X10
> Please guide me.
> Best regards,
> Mina
> 	[[alternative HTML version deleted]]

Dr. Benjamin Bolker
Professor, Mathematics & Statistics and Biology, McMaster University
Director, School of Computational Science and Engineering
(Acting) Graduate chair, Mathematics & Statistics

More information about the R-sig-mixed-models mailing list