A painful and inefficient hack to bring missing value support to the most desperate cases.
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.
# 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)) # }