---
title: "NB_SVM"
author: "Matene Zamaninia"
date: "11/21/2021"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

```{r}

library(ggplot2)
library(rpart)   ## FOR Decision Trees
library(rattle)  ## FOR Decision Tree Vis
library(rpart.plot)
library(RColorBrewer)
library(Cairo)
library(network)

##If you install from the source....
#Sys.setenv(NOAWT=TRUE)
## ONCE: install.packages("wordcloud")
library(wordcloud)
library(randomForest)
library(ROSE)
library(tidyverse)
library(partykit)
library(caret)
library(rpart)
library(randomForest)
library(rpart.plot)
library(C50)
library(printr)
library(ROSE)
library(ggplot2)
library(dplyr)
library(class)
library(e1071)
library(corrplot)
library(faraway)
library(pROC)
library(epiDisplay)
library(binaryLogic)
library(OneR)
library(tibble)
library(tm)
#install.packages("tm")
library(stringr)
library(wordcloud)
# ONCE: install.packages("Snowball")
## NOTE Snowball is not yet available for R v 3.5.x
## So I cannot use it  - yet...
##library("Snowball")
##set working directory
## ONCE: install.packages("slam")
library(slam)
library(quanteda)
## ONCE: install.packages("quanteda")
## Note - this includes SnowballC
library(SnowballC)
library(arules)
##ONCE: install.packages('proxy')
library(proxy)
library(cluster)
library(stringi)
library(proxy)
library(Matrix)
library(tidytext) # convert DTM to DF
library(plyr) ## for adply
library(ggplot2)
library(factoextra) # for fviz
library(mclust) # for Mclust EM clustering

library(naivebayes)
#Loading required packages
#install.packages('tidyverse')
library(tidyverse)
#install.packages('ggplot2')
library(ggplot2)
#install.packages('caret')
library(caret)
#install.packages('caretEnsemble')
library(caretEnsemble)
#install.packages('psych')
library(psych)
#install.packages('Amelia')
library(Amelia)
#install.packages('mice')
library(mice)
#install.packages('GGally')
library(GGally)
library(e1071)
```

```{r}

data <- read.csv("Final_cleaning.csv")
df <- as.data.frame(data)

```


```{r}

variables <- c()
variables <- subset(df, EBikeProfileID==1, select = c("Battery","BikeTypeName","EBikeStateID","Latitude", "Longitude", "dmiles", "day_of_week", "binned_hours", "Weekdays_ends"))

df <- variables
colnames(df)

df$Battery.label <- cut(df$Battery, breaks = 2, labels = c("0-50", "50-100"))

```



```{r}

list <- c()
for (i in 1:NROW(df)){
  if (df$day_of_week[i] == "Monday"){
    list[i] = 1}
  else if (df$day_of_week[i] == "Tuesday"){
     list[i] = 2}
  else if (df$day_of_week[i] == "Wednesday"){
     list[i] = 3}
  else if (df$day_of_week[i] == "Thursday"){
     list[i] = 4}
  else if (df$day_of_week[i] == "Friday"){
     list[i] = 5}
  else if (df$day_of_week[i] == "Saturday"){
     list[i] = 6}
  else if (df$day_of_week[i] == "Sunday"){
     list[i] = 7}
}

df$weekday_int <- list

unique(df$Weekdays_ends)

df$Weekdays_ends <- as.factor(df$Weekdays_ends)
str(df)

df$BikeTypeName <- as.factor(df$BikeTypeName)
df$EBikeStateID <- as.factor(df$EBikeStateID)
df$weekday_int <- as.factor(df$weekday_int)

```


```{r}
model_data <- df[,c(2,3,6,8, 9, 10)]




colnames(model_data)
# 
# GoPlot <- function(x) {
#   
#   G <-ggplot(data=model_data, aes(.data[[x]], y="") ) +
#   geom_bar(stat="identity", aes(fill =.data[[x]])) 
#   
#   return(G)
# }


## Use the function in lappy
#lapply(names(model_data), function(x) GoPlot(x))




variables = Battery.label ~ Weekdays_ends+binned_hours+dmiles+EBikeStateID+BikeTypeName
data <- ovun.sample(Battery.label ~., data = model_data, method = 'over',p = 0.5,)$data


model_data <- data

GoPlot <- function(x) {
  
  G <-ggplot(data=model_data, aes(.data[[x]], y="") ) +
  geom_bar(stat="identity", aes(fill =.data[[x]])) 
  
  return(G)
}


## Use the function in lappy
lapply(names(model_data), function(x) GoPlot(x))
```





```{r}
(Size <- (as.integer(nrow(model_data)/4)))  ## Test will be 1/4 of the data
(SAMPLE <- sample(nrow(model_datadata), Size, replace = FALSE))

(DF_Test_data<-data[SAMPLE, ])
(DF_Train_data<-data[-SAMPLE, ])
##
## REMOVE the labels and KEEP THEM
##   
## 

##################################### REMOVE AND SAVE LABELS...
## Copy the Labels
(DF_Test_data_Labels <- DF_Test_data$Battery.label)
## Remove the labels
DF_Test_data_NL<-DF_Test_data[ , -which(names(DF_Test_data) %in% c("Battery.label"))]
(DF_Test_data_NL[1:5, 1:5])
## Check size
(ncol(DF_Test_data_NL))
#(DF_Test_Student_NL)
## Train...--------------------------------
## Copy the Labels
(DF_Train_data_Labels <- DF_Train_data$Battery.label)
## Remove the labels
DF_Train_data_NL<-DF_Train_data[ , -which(names(DF_Train_data) %in% c("Battery.label"))]
(DF_Train_data_NL[1:5, 1:5])
## Check size
(ncol(DF_Train_data_NL))
#(DF_Train_Student_NL)
```
    



```{r}


NB_e1071_2<-naiveBayes(DF_Train_data_NL, 
                       DF_Train_data_Labels, 
                       laplace = 1)

NB_e1071_Pred <- predict(NB_e1071_2, DF_Test_data_NL)
#NB_e1071_2 
cf<-(table(NB_e1071_Pred,DF_Test_data_Labels))
cf <- as.data.frame(cf)



(confusionMatrix(NB_e1071_Pred,DF_Test_data_Labels))


par(mfrow=c(2,1))
barplot(table(NB_e1071_Pred ), main = 'Barplot of predicted labels (NB1)', col = brewer.pal(6, name = 'Blues'))
barplot(table(DF_Train_data_Labels), main = "Barplot of correct labels", col = brewer.pal(6, name = 'Blues'))







ggplot(data = cf, mapping = aes(NB_e1071_Pred, DF_Test_data_Labels, fill = Freq))+
  geom_tile()+
  ggtitle('Heatmap of Naive Bayes model 1')+
  xlab('Prediction')+
  ylab('Correct Label')
```



```{r}
# update.packages("klaR")
# install.packages("rlang")
library(klaR)
library(caret)
x <- DF_Train_data_NL
y <- DF_Train_data_Labels
# model = train(x,y,'nb')
# #,trControl=trainControl(method='cv',number=10))
# model$results
# 
# Predict <- predict(model,DF_Test_data_NL )
# CF <- table(Predict,DF_Test_data_Labels)
# 
# cf <- as.data.frame(CF)
#Plot Variable performance
X <- varImp(model)
plot(X)

```


# SVM, cleaning
```{r}
str(model_data)

model_data$BikeTypeName <- as.numeric(model_data$BikeTypeName)
model_data$EBikeStateID <- as.numeric(model_data$EBikeStateID)
model_data$dmiles <- as.numeric(model_data$dmiles)
model_data$binned_hours <- as.numeric(model_data$binned_hours)
model_data$Weekdays_ends <- as.numeric(model_data$Weekdays_ends)


model_data2 <- model_data
```

```{r}


(Size <- (as.integer(nrow(model_data2)/4)))  ## Test will be 1/4 of the data
(SAMPLE <- sample(nrow(model_data2), Size, replace = FALSE))

(DF_Test_data2<-model_data2[SAMPLE, ])
(DF_Train_data2<-model_data2[-SAMPLE, ])
##
## REMOVE the labels and KEEP THEM
##   
## 

##################################### REMOVE AND SAVE LABELS...
## Copy the Labels
(DF_Test_data_Labels2 <- DF_Test_data2$Battery.label)
## Remove the labels
DF_Test_data_NL2<-DF_Test_data2[ , -which(names(DF_Test_data2) %in% c("Battery.label"))]
(DF_Test_data_NL2[1:5, 1:5])
## Check size
(ncol(DF_Test_data_NL2))
#(DF_Test_Student_NL)
## Train...--------------------------------
## Copy the Labels
(DF_Train_data_Labels2 <- DF_Train_data2$Battery.label)
## Remove the labels
DF_Train_data_NL2<-DF_Train_data2[ , -which(names(DF_Train_data) %in% c("Battery.label"))]
(DF_Train_data_NL2[1:5, 1:5])
## Check size
(ncol(DF_Train_data_NL2))




```





```{r}

SVM_fit_P <- svm(Battery.label~., data=DF_Train_data2, 
               kernel="linear", cost=.1, 
               scale=FALSE)
print(SVM_fit_P)

pred_P <- predict(SVM_fit_P, DF_Test_data2, type="class")



```


```{r}

#print(Linear_SVM)
str(model_data)
plot(SVM_fit_P, model_data2, dmiles~  Weekdays_ends , slice = list( BikeTypeName= 0,EBikeStateID = 0,binned_hours=0), main = 'Linear SVM')

colnames(model_data)


```






















