This post shows the R code for Naive Bayes Classifier by using funtion naiveBayes() in package e1071. And I use the simple example in my post, Naive Bayes Classifier, to show how to use this function.

Details

  • Example Code (e1071::naiveBayes())
    Suppose we have a contingency table like this:

    Q : And, what will be our guess on type if we have a data has X1=“Yes” and X2=“Unsure”?
    A : Our guess is Type B.

    #######################
    #### Generate Data ####
    #######################
    X1=c(rep("yes",10),rep("no",40),rep("yes",70),rep("no",30))
    X2=c(rep("yes",10),rep("no",10),rep("unsure",30),
         rep("yes",40),rep("no",50),rep("unsure",10))
    train=data.frame(X1,X2, Type=c(rep("A",50),rep("B",100)))
    head(train,3)
    ##    X1  X2 Type
    ## 1 yes yes    A
    ## 2 yes yes    A
    ## 3 yes yes    A
    test=data.frame(X1="yes",X2="unsure")
    head(test)
    ##    X1     X2
    ## 1 yes unsure
    ################################
    #### Naive Bayes Classifier ####
    ################################
    if (!require("e1071")) install.packages("e1071")
    library(e1071)
    (m=naiveBayes(Type ~ ., data = train))
    ## 
    ## Naive Bayes Classifier for Discrete Predictors
    ## 
    ## Call:
    ## naiveBayes.default(x = X, y = Y, laplace = laplace)
    ## 
    ## A-priori probabilities:
    ## Y
    ##         A         B 
    ## 0.3333333 0.6666667 
    ## 
    ## Conditional probabilities:
    ##    X1
    ## Y    no yes
    ##   A 0.8 0.2
    ##   B 0.3 0.7
    ## 
    ##    X2
    ## Y    no unsure yes
    ##   A 0.2    0.6 0.2
    ##   B 0.5    0.1 0.4
    (p=predict(m, test))
    ## [1] B
    ## Levels: A B