Search code examples
rerror-handlingnaivebayes

Why am i getting undefined columns with naiveBayes function?


I am trying to run the naiveBayes function in R and keep getting an error for "undefined columns selected" and cannot figure out why.

File: http://www.mediafire.com/file/1dgqluc1f8gbngc/Train.Example.csv

FA.train <- read.csv("Train.Example.csv")

FA.train$Fatalities <- as.factor(FA.train$Fatalities)
fatalities_nb <- naiveBayes(Fatalities~., data = FA.train)
fatalities_nb

From my knowledge, I am selecting all the columns but it is saying they are undefined.


Solution

  • I believe that you are getting a conflict with the existing column names in your data set. Example: Some of them are not "proper" R names. When you clean then up to proper R names everything seems to be fine. I tried the following and was fine:

    library(tidyverse)
    library(e1071)
    dat <- read_csv("data/Train.Example.csv") %>% 
        janitor::clean_names()
    
    dat$Fatalities <- as.factor(dat$fatalities)
    fatalities_nb <- naiveBayes(Fatalities~., data = dat)
    
    

    And got:

    > summary(fatalities_nb)
              Length Class  Mode     
    apriori    2     table  numeric  
    tables    18     -none- list     
    levels     2     -none- character
    isnumeric 18     -none- logical  
    call       4     -none- call