Skip to content

Instantly share code, notes, and snippets.

@rischanlab
Created May 20, 2014 02:54
Show Gist options
  • Save rischanlab/9fc3300d4ce865fa3a59 to your computer and use it in GitHub Desktop.
Save rischanlab/9fc3300d4ce865fa3a59 to your computer and use it in GitHub Desktop.
NaiveBayes in R
#author : Rischan Mafrur
#email : [email protected]
#website : rischanlab.github.io, ourmasjid.me
#May 15, 2014
#install requirement packages
install.packages("klaR")
install.packages("caret")
#loading library
library("klaR")
library("caret")
#data initiation
getwd()
setwd("E:\\RESEARCH\\twitter\\data\\csv")
list.files()
data <- read.csv("dataset_train.csv")
names(data) <-c("followers","followings","num_tweets",
"tweet_rate","age","hashtag_ratio",
"mention_ratio","reputation","API_ratio",
"URL_ratio","account")
names(data)
x = data[,-11]
y = data$account
#creating naive bayes model
#k-fold cross validation with k=10
model = train(x,y,'nb',
trControl=trainControl(method='cv',number=10))
#any warning we can see with warning command
warnings()
#print out the model
model
#using model for predicting
#show value
predict(model$finalModel,x)
#show prediction class, in this case real account and campaign account
predict(model$finalModel,x)$class
#generate confusion matrix table
table(predict(model$finalModel,x)$class,y)
#plotting the result
mN <- NaiveBayes(data$account ~ ., data = data)
plot(mN)
mK <- NaiveBayes(data$account ~ ., data = data, usekernel=TRUE)
plot(mK)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment