lda models

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_ functions

Note that MASS is used with :: below rather than attached, because attaching it would mask dplyr::select().

model <- MASS::lda(Species ~ ., data = iris)

parsnip

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"]]
#> 1/(exp(-15.477836726795 + (Sepal.Length * 6.31475845867536) + 
#>     (Sepal.Width * 12.1393171806029) + (Petal.Length * -16.9464246511956) + 
#>     (Petal.Width * -20.770054592318) - (-33.5376867395709 + (Sepal.Length * 
#>     -4.7835592704683) + (Sepal.Width * -7.76327370291159) + (Petal.Length * 
#>     12.2507593452831) + (Petal.Width * 17.707469202665))) + exp(-2.02197415376309 + 
#>     (Sepal.Length * -1.53119918820707) + (Sepal.Width * -4.37604347769127) + 
#>     (Petal.Length * 4.69566530591248) + (Petal.Width * 3.06258538965302) - 
#>     (-33.5376867395709 + (Sepal.Length * -4.7835592704683) + 
#>         (Sepal.Width * -7.76327370291159) + (Petal.Length * 12.2507593452831) + 
#>         (Petal.Width * 17.707469202665))) + 1)

Parse model spec

Here is an example of the model spec:

pm <- parse_model(model)
str(pm, 2)
#> List of 3
#>  $ general    :List of 4
#>   ..$ model  : chr "lda"
#>   ..$ version: num 2
#>   ..$ type   : chr "multiclass_regression"
#>   ..$ family : chr "multinomial"
#>  $ classes    : chr [1:3] "setosa" "versicolor" "virginica"
#>  $ class_terms:List of 3
#>   ..$ :List of 5
#>   ..$ :List of 5
#>   ..$ :List of 5
#>  - attr(*, "class")= chr [1:3] "parsed_model" "pm_multiclass_regression" "list"