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

# S3 method for 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:

  • "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.

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

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

fit <- smooth.spline(mtcars$mpg, mtcars$wt, cv = TRUE)
#> Warning: cross-validation with non-unique 'x' values seems doubtful
safe_predict(fit, c(30, NA, 40))
#> # A tibble: 3 x 1 #> .pred #> <dbl> #> 1 1.91 #> 2 NA #> 3 1.06
# the following will fail, however
# NOT RUN { predict(fit, c(30, NA, 40)) # }
# however, we recommend using mgcv instead library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.8-23. For overview type 'help("mgcv-package")'.
fit2 <- gam(mpg ~ s(wt), data = mtcars) predict(fit2, mtcars) # TODO: update with safe_predict once implemented
#> Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive #> 23.03365 21.30454 25.24007 19.21380 #> Hornet Sportabout Valiant Duster 360 Merc 240D #> 17.95427 17.84824 17.28478 19.36025 #> Merc 230 Merc 280 Merc 280C Merc 450SE #> 19.59685 17.95427 17.95427 15.11330 #> Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental #> 16.52446 16.30057 11.46772 11.00221 #> Chrysler Imperial Fiat 128 Honda Civic Toyota Corolla #> 11.21332 26.15766 30.67862 28.98223 #> Toyota Corona Dodge Challenger AMC Javelin Camaro Z28 #> 24.15407 17.53673 17.98094 16.03997 #> Pontiac Firebird Fiat X1-9 Porsche 914-2 Lotus Europa #> 16.01864 28.20817 26.62028 31.46338 #> Ford Pantera L Ferrari Dino Maserati Bora Volvo 142E #> 19.47820 21.99848 17.28478 21.93130
# to get estimated derivatives from smooths use gratia, # which extends mgcv
# NOT RUN { gratia::fderiv(fit2) # TODO: gratia isn't on CRAN yet # }