| Function | Works |
|---|---|
tidypredict_fit(), tidypredict_sql(),
parse_model() |
✔ |
tidypredict_to_column() |
✗ |
tidypredict_test() |
✗ |
tidypredict_interval(),
tidypredict_sql_interval() |
✗ |
parsnip |
✔ |
nnet::multinom() fits multinomial log-linear models.
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. The
expressions implement the softmax over the per-class linear predictors,
with the first level of the outcome acting as the reference class.
Since the output is a list, tidypredict_to_column() and
tidypredict_test() are not supported.
tidypredict_ functionsCreate the R formulas, one per class
fit <- tidypredict_fit(model)
names(fit)
#> [1] "setosa" "versicolor" "virginica"
fit[["setosa"]]
#> 1/(1 + exp(18.6903742569434 + (Sepal.Length * -5.45842400699959) +
#> (Sepal.Width * -8.70740085056254) + (Petal.Length * 14.2447701274711) +
#> (Petal.Width * -3.09768387035736) - 0) + exp(-23.8362760290444 +
#> (Sepal.Length * -7.92363397245587) + (Sepal.Width * -15.3707689334044) +
#> (Petal.Length * 23.6597792429877) + (Petal.Width * 15.1353005479622) -
#> 0))Add the predictions to the original table
library(dplyr)
iris %>%
mutate(!!!tidypredict_fit(model)) %>%
glimpse()
#> Rows: 150
#> Columns: 8
#> $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.…
#> $ Sepal.Width <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.…
#> $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.…
#> $ Petal.Width <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.…
#> $ Species <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s…
#> $ setosa <dbl> 1.0000000, 0.9999996, 1.0000000, 0.9999968, 1.0000000, 1.…
#> $ versicolor <dbl> 1.526406e-09, 3.536476e-07, 4.443506e-08, 3.163905e-06, 1…
#> $ virginica <dbl> 2.716417e-36, 2.883729e-32, 6.103424e-34, 7.117010e-31, 1…Confirm that the results match the model’s predict()
results
parsnip fitted models are also supported by
tidypredict:
tidypredict_fit(p_model)[["virginica"]]
#> 1/(exp(0 - (-23.8362760290444 + (Sepal.Length * -7.92363397245587) +
#> (Sepal.Width * -15.3707689334044) + (Petal.Length * 23.6597792429877) +
#> (Petal.Width * 15.1353005479622))) + exp(18.6903742569434 +
#> (Sepal.Length * -5.45842400699959) + (Sepal.Width * -8.70740085056254) +
#> (Petal.Length * 14.2447701274711) + (Petal.Width * -3.09768387035736) -
#> (-23.8362760290444 + (Sepal.Length * -7.92363397245587) +
#> (Sepal.Width * -15.3707689334044) + (Petal.Length * 23.6597792429877) +
#> (Petal.Width * 15.1353005479622))) + 1)Here is an example of the model spec:
pm <- parse_model(model)
str(pm, 2)
#> List of 3
#> $ general :List of 4
#> ..$ model : chr "multinom"
#> ..$ version: num 2
#> ..$ type : chr "multiclass_regression"
#> ..$ family : chr "multinomial"
#> $ classes : chr [1:3] "setosa" "versicolor" "virginica"
#> $ class_terms:List of 3
#> ..$ :List of 1
#> ..$ :List of 5
#> ..$ :List of 5
#> - attr(*, "class")= chr [1:3] "parsed_model" "pm_multiclass_regression" "list"