[R] Making model predictions

Jeff Reichman re|chm@nj @end|ng |rom @bcg|ob@|@net
Sat Feb 27 15:42:19 CET 2021


R User Forum 

Is there a better way than grabbing individual cell values from a model
output to make predictions. For example the output from the following Naïve
Bayes model 

library(e1071)

## Example of using a contingency table:
data(Titanic)
m <- naiveBayes(Survived ~ ., data = Titanic)
m

will produce the following results:

Call:
naiveBayes.formula(formula = Survived ~ ., data = Titanic)

A-priori probabilities:
Survived
      No      Yes 
0.676965 0.323035 

Conditional probabilities:
        Class
Survived        1st        2nd        3rd       Crew
     No  0.08187919 0.11208054 0.35436242 0.45167785
     Yes 0.28551336 0.16596343 0.25035162 0.29817159

        Sex
Survived       Male     Female
     No  0.91543624 0.08456376
     Yes 0.51617440 0.48382560

        Age
Survived      Child      Adult
     No  0.03489933 0.96510067
     Yes 0.08016878 0.91983122

Say I want to calculate the probability of P(survival = No | Class = 1st,
Sex = Male, and Age= Child).

While I  can set an object (e.g. myObj <- m$tables$Class[1,1])  to the
respective cell and perform the calculation, there must be a better way, as
I continue to learn R.

Jeff



More information about the R-help mailing list