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.
1 2 3 4 5 6 7
An object of class
A rectangular data object, such as a data frame.
A single character value or
A list of optional arguments to the underlying
predict function that will be used when
Arguments to the underlying model's prediction
function cannot be passed here (see
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.
type = "conf_int" and
type = "pred_int", the options
std_error can be used. The latter is a logical for an
extra column of standard error values (if available).
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
For numeric results with a single outcome, the tibble will have
.pred column and
.pred_Yname for multivariate results.
For hard class predictions, the column is named
type = "prob", the columns are
type = "conf_int" and
type = "pred_int" return tibbles with
.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
.quantile (and perhaps other columns).
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
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 26 27
library(dplyr) lm_model <- linear_reg() %>% set_engine("lm") %>% fit(mpg ~ ., data = mtcars %>% slice(11:32)) pred_cars <- mtcars %>% slice(1:10) %>% 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") )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.