Trae
Trae

Reputation: 119

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.

Upvotes: 1

Views: 287

Answers (1)

MDEWITT
MDEWITT

Reputation: 2368

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 

Upvotes: 2

Related Questions