R/stats-smoothspline.R
safe_predict.smooth.spline.RdWe 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", ...)
| object | A |
|---|---|
| new_data | A numeric vector. Can contain |
| type | What kind of predictions to return. Options are:
|
| ... | Unused. |
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.
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.
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.
#> Warning: cross-validation with non-unique 'x' values seems doubtful#> # 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)) # }#>#>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 # }