safe_predict.nls: Safe predictions from an nls object

Description Usage Arguments Value Determining uncertainty in predictions Examples

Description

Safe predictions from an nls object

Usage

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

Arguments

object

An nls object returned from a call to stats::nls().

new_data

Required. A data frame containing predictors.

type

What kind of predictions to return. Options are:

  • "response" (default): Standard predictions from non-linear regression.

...

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.

Determining uncertainty in predictions

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:

  1. Bootstrapping (recommended)

  2. The Delta Method

Bootstrapping: We recommend using the rsample package for bootstrapping, in particular rsample::bootstraps(). See

1
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().

Examples

1
2
3
4
5
fit <- nls(demand ~ SSasympOrig(Time, A, lrc), data = BOD)
safe_predict(fit, BOD)

fit2 <- nls(mpg ~ k / wt + b, mtcars, start = list(k = 1, b = 0))
safe_predict(fit2, mtcars)

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