if (requireNamespace("MASS", quietly = TRUE)) { library(tidypredict) library(dplyr) eval_code <- TRUE } else { eval_code <- FALSE } knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = eval_code )
| Function |Works|
|---------------------------------------------------------------|-----|
|tidypredict_fit(), tidypredict_sql(), parse_model() | ✔ |
|tidypredict_to_column() | ✗ |
|tidypredict_test() | ✗ |
|tidypredict_interval(), tidypredict_sql_interval() | ✗ |
|parsnip | ✔ |
MASS::lda() fits linear discriminant analysis models. Predicting with such a
model projects the predictors onto the discriminant space and compares the
result against each class centroid. That path is linear in the predictors, so it
collapses into one linear predictor per outcome class, and the posterior
probabilities are the softmax of those linear predictors.
Because these models predict one probability per outcome class,
tidypredict_fit() returns a named list of expressions, one for each class,
rather than a single expression. Since the output is a list,
tidypredict_to_column() and tidypredict_test() are not supported.
tidypredict_ functionsNote that MASS is used with :: below rather than attached, because attaching
it would mask dplyr::select().
model <- MASS::lda(Species ~ ., data = iris)
Create the R formulas, one per class
r
fit <- tidypredict_fit(model)
names(fit)
fit[["setosa"]]
Add the predictions to the original table ```r library(dplyr)
iris %>% mutate(!!!tidypredict_fit(model)) %>% glimpse() ```
predict() results
r
probs <- sapply(fit, \(f) rlang::eval_tidy(f, iris))
all.equal(unname(probs), unname(predict(model, iris)$posterior))parsnip fitted models are also supported by tidypredict:
library(parsnip) library(discrim) p_model <- discrim_linear() %>% set_engine("MASS") %>% fit(Species ~ ., data = iris)
tidypredict_fit(p_model)[["virginica"]]
Here is an example of the model spec:
pm <- parse_model(model) str(pm, 2)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.