Formula Issue when Predicting using LMER

326 Views Asked by At

I get the following error when I try to predict using lmer

> predict(mm1, newdata = TEST)
Error in terms.formula(formula(x, fixed.only = TRUE)) : 
  '.' in formula and no 'data' argument

This is what my formula looks like

> formula(mm1)
log_bid_price ~ . - zip_cbsa_name + (1 | zip_cbsa_name)

I'm able to summarize the model, but I can't pass it to the predict function.

I would like to be able to automatically generate a formula given the columns of the predictor matrix and then pass that to lmer. How would I do that?

1

There are 1 best solutions below

0
alexwhitworth On

You might have more success building formula objects like so:

resp <- "log_bid_price"
reserve.coef <- c("zip_cbsa_name")
RHS <- names(data)[-(which(names(data)  %in% c(resp, reserve.coef))]
f <- paste0(paste(resp, paste(RHS, collapse="+"), sep= "~"), " + (1 | zip_cbsa_name)")
mm1 <- lmer(f, data= data)

eg.

paste0(paste("Y", paste(c("a", "b", "c"), collapse= "+"), sep="~"),  "+ (1 | zip_cbsa_name)")
[1] "Y~a+b+c+ (1 | zip_cbsa_name)"

If you wish to do variable selection as you do model selection, you can iterate on this to produce your RHS object