safe_predict.smooth.spline: Safe predictions from a smooth.spline object

Description Usage Arguments Value Uncertainty in predictions Derivatives of smooths Examples

Description

We strongly recommend using mgcv::gam() instead of smooth.spline(). mgcv provides a feature-rich and frequently updated GAM implementation.

Usage

1
2
3
## S3 method for class 'smooth.spline'
safe_predict(object, new_data, type = "response",
  ...)

Arguments

object

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

new_data

A numeric vector. Can contain NAs, although the presence of any NAs may slow down prediction.

type

What kind of predictions to return. Options are:

  • "response" (default): Standard predictions from smoothing splines.

...

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.

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:

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.

Uncertainty in predictions

If you use mgcv, mgcv::predict.gam() has a se.fit argument that will allow you get standard errors for predictions.

If you insist on using smooth.spline(), your best bet to estimate uncertainty in predictions is bootstrapping. See

1
vignette("bootstrapping", package = "safepredict")

for worked examples and details on how you might do this.

Derivatives of smooths

While stats:::predict.smooth.spline() has a deriv argument we do not support it. If you would like to estimate derivates of smooths we recommend using the gratia package, and in particular gratia::fderiv(). At the time of writing, gratia is not yet on CRAN, but can be installed from Github with devtools::install_github(gavinsimpson/gratia). Additional documentation on gratia is available here.

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
fit <- smooth.spline(mtcars$mpg, mtcars$wt, cv = TRUE)
safe_predict(fit, c(30, NA, 40))

# the following will fail, however
## Not run: 
predict(fit, c(30, NA, 40))

## End(Not run)

# however, we recommend using mgcv instead
library(mgcv)

fit2 <- gam(mpg ~ s(wt), data = mtcars)
predict(fit2, mtcars)  # TODO: update with safe_predict once implemented

# to get estimated derivatives from smooths use gratia,
# which extends mgcv

## Not run: 
# gratia is not on CRAN yet, but you can install it with:
# devtools::install_github("gavinsimpson/gratia")

gratia::fderiv(fit2)

## End(Not run)

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