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
The standard approach for prediction is via a predict() method for the class of the model fit. So, have you checked ?predict.naiveBayes If this does not satisfy your needs, you are on your own. Possibly your best course of action then is to contact the maintainer as the posting guide (linked below) recommends for "non-standard" packages. (?maintainer) Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Feb 27, 2021 at 6:42 AM Jeff Reichman <reichmanj at sbcglobal.net> wrote:> 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 > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Hello, Are you looking for this? newd <- data.frame( Class = '1st', Sex = 'Male', Age = 'Child' ) predict(m, newdata = newd, type = 'raw') # No Yes #[1,] 0.3169345 0.6830655 With the default type = 'class' the result is predict(m, newdata = newd) #[1] Yes #Levels: No Yes Hope this helps, Rui Barradas ?s 14:42 de 27/02/21, Jeff Reichman escreveu:> 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 > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Rui Actually yes. I was able to work this into my shiny app this afternoon. Thank you Jeff -----Original Message----- From: Rui Barradas <ruipbarradas at sapo.pt> Sent: Sunday, February 28, 2021 5:26 AM To: reichmanj at sbcglobal.net; R-help at r-project.org Subject: Re: [R] Making model predictions Hello, Are you looking for this? newd <- data.frame( Class = '1st', Sex = 'Male', Age = 'Child' ) predict(m, newdata = newd, type = 'raw') # No Yes #[1,] 0.3169345 0.6830655 With the default type = 'class' the result is predict(m, newdata = newd) #[1] Yes #Levels: No Yes Hope this helps, Rui Barradas ?s 14:42 de 27/02/21, Jeff Reichman escreveu:> 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 > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >