if (requireNamespace("sda", 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 | ✔ |
sda::sda() fits shrinkage discriminant analysis models, including the diagonal
variant (diagonal = TRUE). Predicting with such a model is a softmax over one
linear predictor per outcome class, so 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.
Only the features that survive shrinkage appear in the fitted model, and the generated expressions reference just those.
tidypredict_ functionsmodel <- sda::sda(as.matrix(iris[1:4]), iris$Species, verbose = FALSE)
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))
posterior <- predict(model, as.matrix(iris[1:4]), verbose = FALSE)
all.equal(unname(probs), unname(posterior$posterior))sda() rounds its posterior probabilities with zapsmall(), so expect agreement
to about seven decimal places rather than exactly.
parsnip fitted models are also supported by tidypredict:
library(parsnip) library(discrim) p_model <- discrim_linear() %>% set_engine("sda") %>% fit(Species ~ ., data = iris)
tidypredict_fit(p_model)[["virginica"]]
sda() is fit from a numeric matrix, so a model fit directly can only refer to
the matrix columns it was given. Categorical predictors therefore work through
the parsnip interface, which keeps the formula around and lets the dummy
columns be written in terms of the original factors:
cars <- transform(mtcars, cyl = factor(cyl), gear = factor(gear)) c_model <- discrim_linear() %>% set_engine("sda") %>% fit(cyl ~ mpg + gear + disp, data = cars) tidypredict_fit(c_model)[["8"]]
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.