multinom models

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

library(nnet)

model <- multinom(Species ~ ., data = iris, trace = FALSE)

parsnip

parsnip fitted models are also supported by tidypredict:

library(parsnip)

p_model <- multinom_reg() %>%
  set_engine("nnet") %>%
  fit(Species ~ ., data = iris)
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)

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 "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"