tidyverse / modelr

Helper functions for modelling
https://modelr.tidyverse.org
GNU General Public License v3.0
401 stars 66 forks source link

Support rstanarm in add_predictions #113

Closed EdwardJRoss closed 10 months ago

EdwardJRoss commented 3 years ago

I expect when calling add_predictions on a dataframe with an rstanarm model, that it will add a single new column, with default name pred, to the input dataframe containing the predictions of applying the model. Instead it raises an error.

Running:

model <- rstanarm::stan_glm(x ~ 1 , data=data.frame(x=c(1,2)), refresh=0)
modelr::add_predictions(data.frame(x=1), model)

Result:

Error in `[[<-.data.frame`(`*tmp*`, var, value = c(`1` = 1.5023766891339, : replacement has 2 rows, data has 1
Traceback:

1. modelr::add_predictions(data.frame(x = 1), model)
2. `[[<-`(`*tmp*`, var, value = c(`1` = 1.5023766891339, `2` = 1.5023766891339
 . ))
3. `[[<-.data.frame`(`*tmp*`, var, value = c(`1` = 1.5023766891339, 
 . `2` = 1.5023766891339))
4. stop(sprintf(ngettext(N, "replacement has %d row, data has %d", 
 .     "replacement has %d rows, data has %d"), N, nrows), domain = NA)

The reason is predict.stanreg ignores all positional arguments after the first, and requires the newdata argument to be named. However add_predictions passes the data by position.

A workaround would be to change the predict2 function in predictions.R to explicitly pass the data as argument newdata. But I'm not sure if this causes issues with other models.

predict2 <- function(model, data, type = NULL) {
  if (is.null(type)) {
    stats::predict(model, newdata = data)
  } else {
    stats::predict(model, newdata = data, type = type)
  }
}
hadley commented 10 months ago

modelr is now superseded, which means that we'll only perform critical bug fixes needed to keep it on CRAN. Thanks for contributing this idea and my apologies that it took so long to inform you that this package is no longer under development.