Description Usage Arguments Value Examples
A painful and inefficient hack to bring missing value support to the most desperate cases.
1 | predict_or_na(object, obs)
|
object |
Object to calculate predictins from. |
obs |
A single observation to get a prediction at. |
A numeric vector of length one containing a prediction, or NA
if
prediction is not successful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | ## Not run:
# doesn't support prediction at missing values
fit <- smooth.spline(mtcars$mpg, mtcars$hwy, cv = TRUE)
has_missing <- c(30, NA, 40)
# this fails
predict(fit, has_missing)
# and this is the hacky solution
vapply(has_missing, function(x) predict_or_na(fit, x)$y, numeric(1))
# this also works on data frames
fit2 <- lm(mpg ~ ., mtcars)
# add in some missing values
mt2 <- mtcars
diag(mt2) <- NA
apply(mt2, 1, predict_or_na, object = fit2)
purrr::map_dbl(mt2, ~predict_or_na(fit2, .x))
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.