4
$\begingroup$

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?

$\endgroup$
4
$\begingroup$

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.
$\endgroup$
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.