modelr
modelr copied to clipboard
Support rstanarm in add_predictions
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)
}
}