leeper / prediction

Tidy, Type-Safe 'prediction()' Methods
https://cran.r-project.org/package=prediction
Other
89 stars 14 forks source link

"at" functionality breaks when model data is a data.table #35

Closed danschrage closed 4 years ago

danschrage commented 5 years ago

When using a data.table as the original parent data source, the "at" argument no longer works, because check_values() relies on selecting variables in a data frame by name (using a vector of strings). To do that with a data.table requires the "with=FALSE" option.

Here's a minimal example that reproduces the error:

library(prediction)
library(data.table)

dt <- data.table(y=1:5, x=1:5)
m <- lm(y~x, data=dt)
prediction(m, at=list(x=2))
# Error in dat[, not_numeric, drop = FALSE] : 
#  incorrect number of dimensions

I've submitted a pull request #34 that fixes this issue by coercing data to be a data.frame at the start of the check_values() function. I don't know if that's the most general solution (this issue could crop up elsewhere with a data.table), but I hate to submit a bug report without code to fix it, and it solves the problem in the case presented above.