predict_or_na: Predict at a single observation or return NA

Description Usage Arguments Value Examples

Description

A painful and inefficient hack to bring missing value support to the most desperate cases.

Usage

1
predict_or_na(object, obs)

Arguments

object

Object to calculate predictins from.

obs

A single observation to get a prediction at.

Value

A numeric vector of length one containing a prediction, or NA if prediction is not successful.

Examples

 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)

alexpghayes/safepredict documentation built on May 29, 2019, 11:02 p.m.