qda models

Function Works
tidypredict_fit(), tidypredict_sql(), parse_model()
tidypredict_to_column()
tidypredict_test()
tidypredict_interval(), tidypredict_sql_interval()
parsnip

MASS::qda() fits quadratic discriminant analysis models. Unlike MASS::lda(), each class gets its own covariance estimate, so the class scores are quadratic rather than linear in the predictors: one intercept, one coefficient per predictor, and one coefficient per pair of predictors. The posterior probabilities are the softmax of those class scores.

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::qda(Species ~ ., data = iris)

parsnip

parsnip fitted models are also supported by tidypredict:

library(parsnip)
library(discrim)

p_model <- discrim_quad() %>%
  set_engine("MASS") %>%
  fit(Species ~ ., data = iris)
tidypredict_fit(p_model)[["virginica"]]
#> 1/(exp(-113.714590795937 + (Sepal.Length * 44.5528812152965) + 
#>     (Sepal.Width * -7.61595839316372) + (Petal.Length * 33.559537097271) + 
#>     (Petal.Width * -31.2558694597319) + (Sepal.Length * Sepal.Length * 
#>     -9.47171939291025) + (Sepal.Length * Sepal.Width * 12.4048261564855) + 
#>     (Sepal.Length * Petal.Length * 4.50020653857198) + (Sepal.Length * 
#>     Petal.Width * 4.77612732803424) + (Sepal.Width * Sepal.Width * 
#>     -7.78527008659406) + (Sepal.Width * Petal.Length * -1.1110791368753) + 
#>     (Sepal.Width * Petal.Width * 2.1040978276337) + (Petal.Length * 
#>     Petal.Length * -19.3881020632003) + (Petal.Length * Petal.Width * 
#>     17.9350353034303) + (Petal.Width * Petal.Width * -53.0229530714453) - 
#>     (-67.7090772023948 + (Sepal.Length * 7.37247478397159) + 
#>         (Sepal.Width * 13.2452613006359) + (Petal.Length * 6.23406948373775) + 
#>         (Petal.Width * 9.66197608445426) + (Sepal.Length * Sepal.Length * 
#>         -5.26693339970327) + (Sepal.Length * Sepal.Width * 3.47972623582808) + 
#>         (Sepal.Length * Petal.Length * 9.96014594453995) + (Sepal.Length * 
#>         Petal.Width * -1.78815223046388) + (Sepal.Width * Sepal.Width * 
#>         -7.93772112409139) + (Sepal.Width * Petal.Length * -1.10268870214298) + 
#>         (Sepal.Width * Petal.Width * 8.47285053214324) + (Petal.Length * 
#>         Petal.Length * -6.70291026519806) + (Petal.Length * Petal.Width * 
#>         2.8909184691834) + (Petal.Width * Petal.Width * -9.65702517612499))) + 
#>     exp(-68.4372876863761 + (Sepal.Length * 18.0128645045465) + 
#>         (Sepal.Width * 15.9607000492876) + (Petal.Length * 3.26878502392697) + 
#>         (Petal.Width * -14.7125574674066) + (Sepal.Length * Sepal.Length * 
#>         -4.75138188253319) + (Sepal.Length * Sepal.Width * 3.67621661105371) + 
#>         (Sepal.Length * Petal.Length * 8.63171191375034) + (Sepal.Length * 
#>         Petal.Width * -6.45450343914607) + (Sepal.Width * Sepal.Width * 
#>         -9.85548321295217) + (Sepal.Width * Petal.Length * -2.11602239677401) + 
#>         (Sepal.Width * Petal.Width * 19.4803247040045) + (Petal.Length * 
#>         Petal.Length * -9.90187886398409) + (Petal.Length * Petal.Width * 
#>         26.9372270107537) + (Petal.Width * Petal.Width * -43.6223969131695) - 
#>         (-67.7090772023948 + (Sepal.Length * 7.37247478397159) + 
#>             (Sepal.Width * 13.2452613006359) + (Petal.Length * 
#>             6.23406948373775) + (Petal.Width * 9.66197608445426) + 
#>             (Sepal.Length * Sepal.Length * -5.26693339970327) + 
#>             (Sepal.Length * Sepal.Width * 3.47972623582808) + 
#>             (Sepal.Length * Petal.Length * 9.96014594453995) + 
#>             (Sepal.Length * Petal.Width * -1.78815223046388) + 
#>             (Sepal.Width * Sepal.Width * -7.93772112409139) + 
#>             (Sepal.Width * Petal.Length * -1.10268870214298) + 
#>             (Sepal.Width * Petal.Width * 8.47285053214324) + 
#>             (Petal.Length * Petal.Length * -6.70291026519806) + 
#>             (Petal.Length * Petal.Width * 2.8909184691834) + 
#>             (Petal.Width * Petal.Width * -9.65702517612499))) + 
#>     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 "qda"
#>   ..$ version: num 2
#>   ..$ type   : chr "multiclass_regression"
#>   ..$ family : chr "multinomial"
#>  $ classes    : chr [1:3] "setosa" "versicolor" "virginica"
#>  $ class_terms:List of 3
#>   ..$ :List of 15
#>   ..$ :List of 15
#>   ..$ :List of 15
#>  - attr(*, "class")= chr [1:3] "parsed_model" "pm_multiclass_regression" "list"