deschen
deschen

Reputation: 10996

tidymodels how to set priors for a Naive Bayes model

I want to train a model with a Naive Bayes classifier using the tidymodels framework.

Tidymodels uses the discrim packages, which itself uses the klaR package to estimate Naive Bayes models.

Specifying a NB model in the tidymodels framework can be done with e.g.:

naive_Bayes(
  mode = "classification",
  engine = "klaR",
  smoothness = NULL,
  Laplace = NULL)

When checking the original NB function in the klaR package, though, it has an argument:

prior
the prior probabilities of class membership. If unspecified, the class proportions for the training set are used. If present, the probabilities should be specified in the order of the factor levels.

Problem is, I didn't find a way yet to specify these priors in a tidymodel pipeline. Adding the prior parameter just to the naive_Bayes function shown above, doesn't work and throws an error:

Error in naive_Bayes(prior = rep(0.2, 5)) : 
  unused argument (prior = rep(0.2, 5))

Any idea where I can make the parameter settings?

Upvotes: 0

Views: 890

Answers (1)

Julia Silge
Julia Silge

Reputation: 11613

You can set this model parameter as an engine argument:

library(discrim)
#> Loading required package: parsnip

parabolic_prior <- runif(n = 500)

klar_mod <-
    naive_Bayes() %>%
    set_engine("klaR", prior = parabolic_prior) 

translate(klar_mod)
#> Naive Bayes Model Specification (classification)
#> 
#> Engine-Specific Arguments:
#>   prior = parabolic_prior
#> 
#> Computational engine: klaR 
#> 
#> Model fit template:
#> discrim::klar_bayes_wrapper(x = missing_arg(), y = missing_arg(), 
#>     prior = parabolic_prior, usekernel = TRUE)

klar_fit <- fit(klar_mod, class ~ ., data = parabolic)

Created on 2022-01-26 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions