---
title: "clustering_assignment"
author: "Matene Zamaninia"
date: "10/9/2021"
output: html_document
---

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


```{r}
library(readxl)
library(dplyr)
library(SnowballC)
library(NbClust)
library(cluster)
library(mclust)
library(amap)
library(lsa)
library(ClusterR)
library(scatterplot3d)
library(akmeans)
library(factoextra)



```

```{r}
df <- read.csv("Final_cleaning.csv")
df <- as.data.frame(df)

str(df)
```


# Here we have the entrie data set. we have no use for some of the columns, but the columns we do need
seem to be in the incorrect data type. ets address both these issue before model building.



#Subsetting the data fram to keep only columns we need to use. 
```{r}
clustering_set<- c()
clustering_set <- subset(df, EBikeProfileID==1, select = c("Battery","BikeTypeName","EBikeStateID","Latitude", "Longitude", "dmiles", "day_of_week", "binned_hours", "Weekdays_ends"))

df <- clustering_set
colnames(df)

#sum(is.na(clustering_set))
# No zeros

```





# Data Reformatting for clustering
```{r}

# 1) Battery. turn into three binned facotrs. 

df$Battery.label <- cut(df$Battery, breaks = 3, labels = c("0-33", "33-66", "66-98"))

#2) Turning the values of Bike Type Name to integers and then ordered factors so as to be able to use them 



list <- c()
for (i in 1:NROW(df)){
  if (df$BikeTypeName[i] == "DUB-General"){
    list[i] = 0}
  else if (df$BikeTypeName[i] == "Workshop"){
     list[i] = 1}
  else{
    list[i] = 2 }
}
df$BikeTypeint <- list

# unique(df$BikeTypeint)

#3) Day of week to integers

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)




```



```{r}
cluster_set <-  subset(df, BikeTypeint<3, select = c("EBikeStateID", "Latitude", "Longitude", "dmiles", "binned_hours", "Weekdays_ends", "BikeTypeint", "weekday_int"))



cluster_set <- as.data.frame(lapply(cluster_set, as.numeric))


label_set <- subset(df, Battery<100, select = c("Battery" , "Battery.label"))

```



# Model Building, we tried it on the entire data set, failed misreably. we will use a sample of the data.
```{r}


sample = cluster_set[sample(nrow(df), 100), ]

small_sample = sample

(m2_Eucl <-dist(small_sample, method = "minkowski", p = 2))
(m1_Man <- dist(small_sample, method = "manhattan", p = 1))

(m4 <- dist(small_sample, method = "minkowski", p = 4))
```
 


# As suspected the dendogram does not eally tell usmuch. but there seems to be 2 main clusters. 
```{r}

hist <- hclust(m2_Eucl, method ="ward.D2")
plot(hist)

hist1 <- hclust(m1_Man, method ="ward.D2")
plot(hist1)

hist4 <- hclust(m4, method ="ward.D2")
plot(hist4)

```

```{r}
str(small_sample)

small_sample$EBikeStateID <- as.ordered(small_sample$EBikeStateID)

small_sample$binned_hours <- as.ordered(small_sample$binned_hours)

small_sample$Weekdays_ends <- as.ordered(small_sample$Weekdays_ends)

small_sample$BikeTypeint <- as.ordered(small_sample$BikeTypeint)

small_sample$weekday_int <- as.ordered(small_sample$weekday_int)
# Euclidean distance 
k <- 3
kmod <- kmeans(small_sample, k)
kmod
kmod$centers



```



# K = 3
```{r}
# Euclidean distance 
k <- 3
kmod <- kmeans(small_sample, k)
kmod
kmod$centers
```

# K = 1
```{r}

k <- 2
kmod <- kmeans(small_sample, k)
kmod
kmod$centers

```

```{r}

k <- 4
kmod <- kmeans(small_sample, k)
#kmod
kmod$centers


```






# elbow suggests 2 clusters

```{r}

fviz_nbclust(as.matrix(small_sample), kmeans, k.max = 5, method = "wss", diss = get_dist(as.matrix(small_sample), method = "minkowski"))


```



# Silhouette method suggests 3

```{r}

fviz_nbclust(small_sample, method = "silhouette", FUN = hcut, k.max = 3)



```













