[R] Making model predictions
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sun Feb 28 12:25:46 CET 2021
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 using 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.
>
More information about the R-help
mailing list