Safe predictions from an nls object
# S3 method for nls safe_predict(object, new_data, type = "response", ...)
| object | An |
|---|---|
| new_data | Required. A data frame containing predictors. |
| 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.
Note that stats::predict.nls() has an se.fit argument, but it is
currently ignored. There is no build-in capability in base R to determine
the uncertainty in the predictions from nls.
In practice, there are two options to get these:
Bootstrapping (recommended)
The Delta Method
Bootstrapping: We recommend using the rsample package for
bootstrapping, in particular rsample::bootstraps(). See
vignette("bootstrapping", package = "safepredict")
for worked examples.
Delta Method: Some people seem to be happy to use the delta method,
and others claim it is numerically unstable. Two options include
car::deltaMethod() and emdbook::deltavar().
#> # A tibble: 6 x 1 #> .pred #> <dbl> #> 1 7.89 #> 2 12.5 #> 3 15.3 #> 4 16.9 #> 5 17.8 #> 6 18.7#> # A tibble: 32 x 1 #> .pred #> <dbl> #> 1 21.9 #> 2 20.3 #> 3 24.1 #> 4 18.6 #> 5 17.7 #> 6 17.6 #> 7 17.2 #> 8 18.8 #> 9 18.9 #> 10 17.7 #> # ... with 22 more rows