# UNIVERSIDAD NACIONAL DEL ALTIPLANO
# FACULTAD DE INGENIERIA ESTADISTICA E INFORMATICA
# ESTADISTICA BAYESIANA
# NAIVE BAYES

library(e1071)
## Warning: package 'e1071' was built under R version 4.0.5
library(naivebayes) 
## Warning: package 'naivebayes' was built under R version 4.0.5
## naivebayes 0.9.7 loaded
library(caret)  
## Warning: package 'caret' was built under R version 4.0.5
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.0.3
## Loading required package: lattice
## Warning: package 'lattice' was built under R version 4.0.2
library(readxl)
## Warning: package 'readxl' was built under R version 4.0.2
Paciente <- read_excel("E:/ESTADISTICA BAYESIANA/TAREA 06/Paciente.xlsx")
attach(Paciente)
names(Paciente)
## [1] "Escalofrio" "Catarro"    "Cefalea"    "Fiebre"     "Gripe"
#View(Datos)

set.seed(2018)
t.ids <- createDataPartition(Paciente$Gripe, p=0.67, list = F)
mod <- naiveBayes(Gripe ~ ., data = Paciente[t.ids,])
mod
## 
## Naive Bayes Classifier for Discrete Predictors
## 
## Call:
## naiveBayes.default(x = X, y = Y, laplace = laplace)
## 
## A-priori probabilities:
## Y
##        No        Si 
## 0.4285714 0.5714286 
## 
## Conditional probabilities:
##     Escalofrio
## Y           No        Si
##   No 0.6666667 0.3333333
##   Si 0.2500000 0.7500000
## 
##     Catarro
## Y           No        Si
##   No 0.6666667 0.3333333
##   Si 0.2500000 0.7500000
## 
##     Cefalea
## Y        Forte     Media        No
##   No 0.3333333 0.3333333 0.3333333
##   Si 0.5000000 0.2500000 0.2500000
## 
##     Fiebre
## Y           No        Si
##   No 0.6666667 0.3333333
##   Si 0.2500000 0.7500000
pred <- predict(mod, Paciente[t.ids,])
tab <- table(Paciente[t.ids,]$Gripe, pred, dnn = c("Actual","Predicha"))

confusionMatrix(tab)
## Confusion Matrix and Statistics
## 
##       Predicha
## Actual No Si
##     No  2  1
##     Si  0  4
##                                           
##                Accuracy : 0.8571          
##                  95% CI : (0.4213, 0.9964)
##     No Information Rate : 0.7143          
##     P-Value [Acc > NIR] : 0.3605          
##                                           
##                   Kappa : 0.6957          
##                                           
##  Mcnemar's Test P-Value : 1.0000          
##                                           
##             Sensitivity : 1.0000          
##             Specificity : 0.8000          
##          Pos Pred Value : 0.6667          
##          Neg Pred Value : 1.0000          
##              Prevalence : 0.2857          
##          Detection Rate : 0.2857          
##    Detection Prevalence : 0.4286          
##       Balanced Accuracy : 0.9000          
##                                           
##        'Positive' Class : No              
##