Search code examples
rclassificationnaivebayes

How to make generic the formula parameter of naiveBayes() in R?


When I'm using the function naiveBayes() of the library "e1071" it is required to insert a formula of the type:

myFormula <- myClass~ feature1 + feature2 + feature3

If I want to make it generic (I don't know how many features has the relative dataset) how can I do? I just know the myClass column will be the last one, and I want to keep in considerations all other columns


Solution

  • You can refer to all other columns dynamically with the ..

    myFormula <- cyl ~ .
    naiveBayes(myFormula, data = mtcars)
    
    
    
        Call:
    naiveBayes.default(x = X, y = Y, laplace = laplace)
    
    A-priori probabilities:
    Y
          4       6       8 
    0.34375 0.21875 0.43750 
    
    Conditional probabilities:
       mpg
    Y       [,1]     [,2]
      4 26.66364 4.509828
      6 19.74286 1.453567
      8 15.10000 2.560048
    
       disp
    Y       [,1]     [,2]
      4 105.1364 26.87159
      6 183.3143 41.56246
      8 353.1000 67.77132
    

    If you want the class dymanic, you can use substitute the formula and use eval to evaluate it inside the naiveBayes function call.

    dynamicNB <- function(data, class) {
      myFormula <- substitute(class ~ .)
      naiveBayes(eval(myFormula), data = data)
    }
    
    dynamicNB(class = mpg, data = mtcars)