# Introduction

K-means clustering takes your unlabeled data and a constant, `k`, and returns `k` number of clusters that are within a specified distance of each other. It automatically groups together things that look alike so you don't have to!

Let's begin with the USArrests data. This data set contains statistics, in arrests per 100,000 residents for assault, murder, and rape in each of the 50 US states in 1973. It also has the percentage of population living in urban areas in each state.

```{r}
data("USArrests")     
head(USArrests, n = 3)
```

Variables are often scaled (i.e. standardized) before measuring the inter-observation dissimilarities. Scaling is recommended when variables are measured in different scales (e.g: kilograms, kilometers, centimeters, etc).

Should I scale? Ask yourself: do I have two features, one where the differences between cases is large and the other small? Am I willing to have the former as almost the only driver of distance? If the first answer is yes and the second answer is no, you will likely need to scale your data. 

See more on scaling and k-means, here: https://pdfs.semanticscholar.org/1d35/2dd5f030589ecfe8910ab1cc0dd320bf600d.pdf

```{r}
# we see all values are numeric
# scale the data set


# for now..

```

Can you tell what actions `scale` performed with default inputs? `scale` calculates the mean and standard deviation of each column vector, then subtracts the mean and divides by the standard deviation for each observation. It's especially useful when your dataset contains variables with widely varying scales (as highlighted above).

Within R it is simple to compute and visualize the distance matrix using the functions `get_dist` and `fviz_dist` from the `factoextra` R package. 

- `get_dist`: for computing a distance matrix between the rows of a data matrix. The default distance computed is the Euclidean; however,
- `get_dist` also supports other distance measures
- `fviz_dist`: for visualizing a distance matrix

```{r}
library(stats)
library(tidyverse)
#install.packages("factoextra")
library(factoextra)

distance <- 
fviz_dist()
```

The graph above helps us begin to illustrate which states have large dissimilarities (red) versus those that appear to be fairly similar (teal).

Below we will first set the seed before running the algorithm. Then, take note of the arguments for kmeans. What are they? 

Why is it important to set the seed? (Hint: imagine you are sharing your Rmd with a classmate!)

We will use kmeans() on our `df` and specify k as equal to 2. We can also change `nstart` to something like 25, if we wanted, since k-means is sensitive to its random starting positions. This means that R will try 25 different random starting assignments and then select the best results corresponding to the one with the lowest within-cluster variation. 

```{r}
# compute k0means with k=4
set.seed(1234)
k2 <- 

#Print the results
print(k2)
```

The printed output tells us our analysis resulted in two groups with cluster sizes for 30 and 20. Plus it gives us a matrix of cluster means, where the rows are the cluster number (in this case 1 to 4) and the columns are the variables in the data.

The output also gives us a clustering vector to tell us the data point that is assigned to each cluster. 

The "within cluster sum of squares by cluster" is a measure of the total variance in your data set that is explained by the clusters. Since k-means minimizes the within group dispersion and maximize the between-group dispersion. In tihs case we can interpret 47.5% as the amount of variance explained by our analysis (so we might want to try more than 2 clusters!)

We can also view our results by using `fviz_cluster.` This provides a nice illustration of the clusters. If there are more than two dimensions (variables) `fviz_cluster` will perform principal component analysis (PCA) and plot the data points according to the first two principal components that explain the majority of the variance.(We will learn more about PCA later on).

Why is this needed? Because visualizing anything above 3D (but often even 2D) is very hard! And usually it is also difficult to interpret for human beings. 

```{r}
fviz_cluster()
```

Wonderfully, you can also just use pairwise scatter plots to illustrate the clusters compared to the original variables.

```{r}
df %>%
  as_tibble() %>%
  
```

# On your own

*Practice*

Re-run the cluster algorithm with k=4. Plot and compare your results to those above. 

```{r}
k3 <- 
k4 <- 
k5 <- 

# plots to compare
p1 <- 
p2 <- 
p3 <- 
p4 <- 

library(gridExtra)

```


