Apply a model to create different types of predictions.
predict()
can be used for all types of models and used the
"type" argument for more specificity.
# S3 method for model_fit
predict(object, new_data, type = NULL, opts = list(), ...)# S3 method for model_fit
predict_raw(object, new_data, opts = list(), ...)
predict_raw(object, ...)
An object of class model_fit
A rectangular data object, such as a data frame.
A single character value or NULL
. Possible values
are "numeric", "class", "prob", "conf_int", "pred_int", "quantile",
or "raw". When NULL
, predict()
will choose an appropriate value
based on the model's mode.
A list of optional arguments to the underlying
predict function that will be used when type = "raw"
. The
list should not include options for the model object or the
new data being predicted.
Arguments to the underlying model's prediction
function cannot be passed here (see opts
). There are some
parsnip
related options that can be passed, depending on the
value of type
. Possible arguments are:
level
: for type
s of "conf_int" and "pred_int" this
is the parameter for the tail area of the intervals
(e.g. confidence level for confidence intervals).
Default value is 0.95.
std_error
: add the standard error of fit or prediction (on
the scale of the linear predictors) for type
s of "conf_int"
and "pred_int". Default value is FALSE
.
quantile
: the quantile(s) for quantile regression
(not implemented yet)
time
: the time(s) for hazard probability estimates
(not implemented yet)
With the exception of type = "raw"
, the results of
predict.model_fit()
will be a tibble as many rows in the output
as there are rows in new_data
and the column names will be
predictable.
For numeric results with a single outcome, the tibble will have
a .pred
column and .pred_Yname
for multivariate results.
For hard class predictions, the column is named .pred_class
and, when type = "prob"
, the columns are .pred_classlevel
.
type = "conf_int"
and type = "pred_int"
return tibbles with
columns .pred_lower
and .pred_upper
with an attribute for
the confidence level. In the case where intervals can be
produces for class probabilities (or other non-scalar outputs),
the columns will be named .pred_lower_classlevel
and so on.
Quantile predictions return a tibble with a column .pred
, which is
a list-column. Each list element contains a tibble with columns
.pred
and .quantile
(and perhaps other columns).
Using type = "raw"
with predict.model_fit()
will return
the unadulterated results of the prediction function.
In the case of Spark-based models, since table columns cannot contain dots, the same convention is used except 1) no dots appear in names and 2) vectors are never returned but type-specific prediction functions.
When the model fit failed and the error was captured, the
predict()
function will return the same structure as above but
filled with missing values. This does not currently work for
multivariate models.
If "type" is not supplied to predict()
, then a choice
is made (type = "numeric"
for regression models and
type = "class"
for classification).
predict()
is designed to provide a tidy result (see "Value"
section below) in a tibble output format.
When using type = "conf_int"
and type = "pred_int"
, the options
level
and std_error
can be used. The latter is a logical for an
extra column of standard error values (if available).
# NOT RUN {
library(dplyr)
lm_model <-
linear_reg() %>%
set_engine("lm") %>%
fit(mpg ~ ., data = mtcars %>% dplyr::slice(11:32))
pred_cars <-
mtcars %>%
dplyr::slice(1:10) %>%
dplyr::select(-mpg)
predict(lm_model, pred_cars)
predict(
lm_model,
pred_cars,
type = "conf_int",
level = 0.90
)
predict(
lm_model,
pred_cars,
type = "raw",
opts = list(type = "terms")
)
# }
Run the code above in your browser using DataLab