Mel
Mel

Reputation: 750

For loop to run lmer model for each column in a dataframe gives "variable lengths differ" error (R)

I am trying to run a for loop that plugs every column in a dataframe into a lme4::lmer() model syntax and appends the result to a list e.g.

list_results_univariate <- list()

for (i in names(my_dataset))
  
  {
  
  resultmodel <- lmer(y_variable ~ 
         i + i*timevar1 + i*timevar2 + 
         (1|Date) + (1|Location), 
       data= my_dataset)
    
  tidy_resultmodel <- tidy_lmer(resultmodel)  
  
  list_results_univariate[[i]] <- tidy_resultmodel
    
  }    

but the result is:

     Error in model.frame.default(data = my_dataset, drop.unused.levels = TRUE,  : 
  variable lengths differ (found for 'i')

The dataset contains no NAs and no single-level factors as I already removed these. It still returns the same error if I remove timevar1, timevar2, Date and Location from the list of names I iterate over.

How do I get this to run without manually writing the model for each variable?

Upvotes: 1

Views: 1395

Answers (1)

Vincent
Vincent

Reputation: 17725

Your formula includes i directly, which means that lmer expects to find a column called i in your dataset. Your i variable has length 1 (the string column name), but lmer is expecting a variable of length equal to the length of your y_variable, hence the error message.

Inside your loop, you should create a formula that evaluates i to its underlying value, and then use that formula in lmer. For example:

library(lme4)

dat <- data.frame(id = sample(c("a", "b", "c"), 100, replace=TRUE),
                  y = rnorm(100),
                  x = rnorm(100),
                  w = rnorm(100),
                  z = rnorm(100))

# this errors
for (i in c("x", "w", "z")) {
  lmer(y ~ i + (1 | id), data=dat)
}

# this works
models <- list()
for (i in c("x", "w", "z")) {
  f <- formula(paste("y~(1|id)+", i))
  models[[i]] <- lmer(f, data=dat)
}

Upvotes: 3

Related Questions