Making model predictions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Making model predictions

reichmaj
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Making model predictions

Bert Gunter-2
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 <[hidden email]>
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
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Making model predictions

Rui Barradas
In reply to this post by reichmaj
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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Making model predictions

reichmaj
Rui

Actually yes.  I was able to work this into my shiny app this afternoon.

Thank you

Jeff

-----Original Message-----
From: Rui Barradas <[hidden email]>
Sent: Sunday, February 28, 2021 5:26 AM
To: [hidden email]; [hidden email]
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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.