Description Usage Arguments Value Uncertainty in predictions Derivatives of smooths Examples
We strongly recommend using mgcv::gam()
instead of smooth.spline()
.
mgcv
provides a featurerich and frequently updated GAM implementation.
1 2 3  ## S3 method for class '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
1  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.
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)

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.