#Using ISLR dataset
#Response is Purchase, yes or no they purchased an insurance policy
library(ISLR)
str(Caravan)
summary(Caravan$Purchase)

#Cleaning Data
any(is.na(Caravan))

#Standardize Variables
#Because the KNN classifier predicts the class of a given test observation by 
#identifying the observations that are nearest to it, the scale of the variables 
#matters. Any variables that are on a large scale will have a much larger effect 
#on the distance between the observations, and hence on the KNN classifier, than 
#variables that are on a small scale. For example, let's check out the variance of two features:
var(Caravan[,1])
var(Caravan[,2])

# save the Purchase column in a separate variable
purchase <- Caravan[,86]

# Standarize the dataset using "scale()" R function
standardized.Caravan <- scale(Caravan[,-86])

#Check variance again
var(standardized.Caravan[,1])
var(standardized.Caravan[,2])

#Test and Train Data
# First 100 rows for test set
test.index <- 1:1000
test.data <- standardized.Caravan[test.index,]
test.purchase <- purchase[test.index]

# Rest of data for training
train.data <- standardized.Caravan[-test.index,]
train.purchase <- purchase[-test.index]

#Using KNN
library(class)
set.seed(101)
predicted.purchase <- knn(train.data,test.data,train.purchase,k=1)
head(predicted.purchase)
#misclassification error rate
mean(test.purchase != predicted.purchase)

#Choosing K Value
predicted.purchase <- knn(train.data,test.data,train.purchase,k=3)
mean(test.purchase != predicted.purchase)
#Misclassification error went down
predicted.purchase <- knn(train.data,test.data,train.purchase,k=5)
mean(test.purchase != predicted.purchase)

#iterate to find best k value using elbow method
predicted.purchase = NULL
error.rate = NULL

for(i in 1:20){
    set.seed(101)
    predicted.purchase = knn(train.data,test.data,train.purchase,k=i)
    error.rate[i] = mean(test.purchase != predicted.purchase)
}

print(error.rate)

#Elbow Method
library(ggplot2)
k.values <- 1:20
error.df <- data.frame(error.rate,k.values)
error.df
ggplot(error.df,aes(x=k.values,y=error.rate)) + geom_point()+ geom_line(lty="dotted",color='red')
#can see which k value to use now