# How to specify a non MLE prior for a naiveBayes model in R?

I am just learning R, and am aware that the package e1071 has a naiveBayes method that takes in predictor and class membership, and estimates the class prior using the relative frequency (ML estimate).

I would like to see the impact of having a sample that is not balanced across classes (let's say male, female are represented in 3:7 ratio in the available data), when I know that for the population I am interested in, the prior of male, female would be much closer to 0.5:0.5 than 0.3:0.7. The skewed representation is due to practical problems in sampling/surveying.

I've tried doing a down-sampling of the female subset to make the training set reflect the 1:1 ratio. Is there a way of using naiveBayes with a pre-specified prior that is not reflected in the relative frequency in the data? I've looked through the documentation on e1071::naiveBayes and there does not seem to be any way of specifying my own prior.

Is there another package that does that?

-

If you are looking for an implementation of the naive Bayes algorithm allowing for the use of prior, you should probably take a look at the klaR package, and its NaiveBayes() function which has

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.

-
 Thanks @chi ! It worked great. – oalah Aug 12 '12 at 0:04

This is an ugly, ugly solution, but if you're locked into e1071, you can pass the 'raw' option to predict, which will give you the probabilities of each class.

You could then "correct" these by dividing out the "empirical" prior (i.e. the one from your sample) and multiplying them by the true population prior.

-