Safe predictions from a loess object

# S3 method for loess
safe_predict(object, new_data, type = c("response",
  "conf_int", "pred_int"), ..., std_error = FALSE, level = 0.95)

Arguments

object

A loess object returned from a call to stats::loess().

new_data

TODO

type

What kind of predictions to return. Options are:

  • "response" (default): Standard predictions from LOESS regression.

  • "conf_int": Fitted values plus a confidence interval for the fit.

  • "pred_int": Predictions with accompanying prediction interval.

...

Unused. safe_predict() checks that all arguments in ... are evaluated via the ellipsis package. The idea is to prevent silent errors when arguments are mispelled. This feature is experimental and feedback is welcome.

std_error

Logical indicating whether or not calculate standard errors for the fit at each point. Not available for all models, and can be computationally expensive to compute. The standard error is always the standard error for the mean, and never the standard error for predictions. Standard errors are returned in a column called .std_error. Defaults to FALSE.

level

A number strictly between 0 and 1 to use as the confidence level when calculating confidence and prediction intervals. Setting level = 0.90 correspondings to a 90 percent confidence interval. Ignored except when type = "conf_int" or type = "pred_int". Defaults to 0.95.

Value

A tibble::tibble() with one row for each row of new_data. Predictions for observations with missing data will be NA. Returned tibble has different columns depending on type:

  • "response":

    • univariate outcome: .pred (numeric)

    • multivariate outcomes: .pred_{outcome name} (numeric) for each outcome

  • "class": .pred_class (factor)

  • "prob": .pred_{level} columns (numerics between 0 and 1)

  • "link": .pred (numeric)

  • "conf_int": .pred, .pred_lower, .pred_upper (all numeric)

  • "pred_int": .pred, .pred_lower, .pred_upper (all numeric)

If you request standard errors with std_error = TRUE, an additional column .std_error.

For interval predictions, the tibble has additional attributes level and interval. The level is the same as the level argument and is between 0 and 1. interval is either "confidence" or "prediction". Some models may also set a method attribute to detail the method used to calculate the intervals.

Examples

fit <- loess(mpg ~ wt, mtcars) safe_predict(fit, mtcars)
#> # A tibble: 32 x 1 #> .pred #> <dbl> #> 1 22.2 #> 2 21.1 #> 3 24.6 #> 4 19.5 #> 5 17.9 #> 6 17.8 #> 7 17.2 #> 8 19.6 #> 9 19.8 #> 10 17.9 #> # ... with 22 more rows
safe_predict(fit, mtcars, std_error = TRUE)
#> # A tibble: 32 x 2 #> .pred .std_error #> <dbl> <dbl> #> 1 22.2 1.01 #> 2 21.1 1.07 #> 3 24.6 1.03 #> 4 19.5 0.861 #> 5 17.9 0.755 #> 6 17.8 0.747 #> 7 17.2 0.721 #> 8 19.6 0.866 #> 9 19.8 0.868 #> 10 17.9 0.755 #> # ... with 22 more rows
# the default behavior of loess() is return NA for values # outside the range of the training data: safe_predict(fit, data.frame(wt = 1:10))
#> # A tibble: 10 x 1 #> .pred #> <dbl> #> 1 NA #> 2 27.9 #> 3 20.5 #> 4 15.3 #> 5 12.4 #> 6 NA #> 7 NA #> 8 NA #> 9 NA #> 10 NA
# to enable extrapolation, use: fit2 <- loess( mpg ~ wt, mtcars, control = loess.control(surface = "direct") ) safe_predict(fit2, data.frame(wt = 1:10), type = "pred_int", level = 0.9)
#> # A tibble: 10 x 3 #> .pred .pred_lower .pred_upper #> <dbl> <dbl> <dbl> #> 1 35.0 45.0 25.0 #> 2 27.9 32.8 23.0 #> 3 21.2 26.2 16.3 #> 4 15.1 20.1 10.1 #> 5 12.3 17.4 7.22 #> 6 11.4 19.8 3.00 #> 7 11.5 33.6 -10.6 #> 8 12.6 58.8 -33.7 #> 9 14.6 95.8 -66.7 #> 10 17.5 145. -110.