1

I am calling the lmer function from the lme4 package. The function works if I hard code the column names. If I refer to it as a variable, though, it throws an error. My ultimate goal is to call a string that includes '+' between each column name.

Here is an example lmer call:

colnames(df)
COL_A, COL_B, COL_D

This works:

output <- lmer(COL_A ~ 1 + COL_B + 1|COL_D, df, REML = FALSE, verbose = TRUE)

This does not:

var_a <- COL_A
var_b <- COL_B
var_c <- COL_D

output <- lmer(var_a ~ 1 + var_b + 1|var_c, df, REML = FALSE, verbose = TRUE)

Error that comes up:

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

If this can be figured out it would be awesome.

My ultimate goal though is to call the function as a string, so something like this:

form_lmer<-"COL_A ~ 1 + COL_B + 1|COL_D"

output <- lmer(form_lmer, df, REML = FALSE, verbose = TRUE)

Has anyone encountered this sort of thing before?

Ben Bolker
  • 192,494
  • 24
  • 350
  • 426
nak5120
  • 3,793
  • 4
  • 28
  • 73

1 Answers1

0

You can do

output<-lmer(paste0(var_a, "~ 1 + ",var_b," + 1|",var_c), df, REML = FALSE, verbose = TRUE)
denis
  • 5,142
  • 1
  • 10
  • 36