safe_predict.lm: Safe predictions from a linear model

Description Usage Arguments Details Examples

Description

Safe predictions from a linear model

Usage

1
2
3
## S3 method for class 'lm'
safe_predict(object, new_data, type = c("response",
  "conf_int", "pred_int"), ..., std_error = FALSE, level = 0.95)

Arguments

object

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

new_data

TODO

type

What kind of predictions to return. Options are:

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

  • "conf_int": Fitted values plus a confidence interval for the fit.

  • "pred_int": Predictions with accompanying prediction interval.

...

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.

std_error

Logical indicating whether or not calculate standard errors for the fit at each point. Not available for all models, and can be computationally expensive to compute. The standard error is always the standard error for the mean, and never the standard error for predictions. Standard errors are returned in a column called .std_error. Defaults to FALSE.

level

A number strictly between 0 and 1 to use as the confidence level when calculating confidence and prediction intervals. Setting level = 0.90 correspondings to a 90 percent confidence interval. Ignored except when type = "conf_int" or type = "pred_int". Defaults to 0.95.

Details

Do not use on model objects that only subclass lm. This will result in an error.

Examples

1
2
3
4
5
6
7
8
9
fit <- lm(hp ~ ., mtcars)

safe_predict(fit, mtcars)

mt2 <- mtcars
diag(mt2) <- NA

safe_predict(fit, mt2, std_error = TRUE)
safe_predict(fit, mt2, type = "pred_int", level = 0.9)

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