```{r setup, include=FALSE}
# load libraries
library(tidyverse)
library(conflicted)

# additional libraries for today
library(viridis)
library(magrittr)
library(broom)
library(pheatmap)

# resolve package conflicts
filter <- dplyr::filter
select <- dplyr::select
knitr::opts_chunk$set(echo = TRUE)
```

## Assignment 2021-07-14: Clustering

Please email me your Rmarkdown document at <kkeith@coriell.org> with your **LAST NAME** in the file name by 9AM Friday 7/15

---

We'll practice clustering with 3 different datasets

### wine

The wine dataset contains the results of a chemical analysis of wines grown in a specific area of Italy. Three types of wine are represented in the 178 samples, with the results of 13 chemical analyses recorded for each sample. The Type variable has been transformed into a categoric variable. The tidy wine dataset contains the following columns:

- **Sample** = the unique sample ID for each row
- **Cultivar** = the number factor indicating the grape cultivar the wine was made from
- **Alcohol** = the alcohol concentration in the wine sample (g/L)
- **MalicAcid** = the malic acid concentration in the wine sample (g/L)
- **Ash** = the ash concentration in the wine sample (g/L)
- **Magnesium** = the magnesium concentration in the wine sample (g/L)
- **TotalPhenol** = the total amount of all phenol compounds in the wine sample (g/L)
- **Flavanoids** = the concentration of all flavanoids in the wine sample (g/L)
- **NonflavPhenols** = the concentration of all non-flavanoid phenols in the wine sample (g/L)
- **Color** = wine color (spectrophotometric measure?)

In the wine dataset, Cultivar is a **categorical** variable (even though it's coded using numbers) that we'll try to discriminate using clustering.

---

Read in the wine dataset in the chunk below.

```{r}

```

#### heatmp

1. Plot a heatmap of the `wine` dataset. Annotate it with the cultivar of the sample.

```{r}

```

2. Does the heatmap do a good job of discriminating the cultivar? Why or why not?

**WRITE YOUR ANSWER HERE**

#### PCA

3. Plot a PCA of the wine dataset. Start by plotting PC1 and PC2.

```{r}

```

4. Does the PCA do a good job of discriminating the cultivar? Why or why not?

**WRITE YOUR ANSWER HERE**

5. Does any combination of the first 3 PCs do a good job of discriminating the cultivar? Can any cultivar be distinguished from the others? Why or why not? Plot the PCs in the chunk below and explain your reasoning.

**WRITE YOUR ANSWER HERE**

```{r}

```

#### kmeans

6. Run kmeans clustering with 3 clusters on the wine dataset and plot the results

```{r}

```

7. Does kmeans do a good job of discriminating the cultivars? Why or why not?

**WRITE YOUR ANSWER HERE**

<br>

### biopsy

In the biopsy dataset, outcome is the categorical variable that we'll try to recover. All the information about the dataset is below for reference.

The biopsy dataset contains the results of breast tumor biopsy results from 699 patients from the University of Wisconsin, Madison. Tumor biopsy attributes were measured on a scale of 1-10 and the diagnosis is given in the outcome column. The tidy biopsy dataset contains the following columns:

- **sample_id** = numeric sample ID
- **outcome** = is the biopsy cancerous or not? character, either 'benign' or 'malignant'
- **clump_thickness** = biopsy thickness on a scale from 1-10
- **uniform_cell_size** = uniformity of cell size on a scale from 1-10
- **marg_adhesion** = marginal adhesion on a scale from 1-10
- **epithelial_cell_size** = epithelial cell size on a scale from 1-10
- **bare_nuclei** = proportion of cells that are mainly nucleus on a scale from 1-10
- **bland_chromatin** = texture of chromatin on a scale from 1-10
- **normal_nucleoli** = proportion of cells with normal nucleoli on a scale from 1-10
- **mitoses** = proportion of mitoses on a scale from 1-10

---

Use the chunk below to read in the biopsy table

```{r}

```

#### PCA

8. Run a PCA on the biopsy dataset and plot the first two PCs. How does the PCA analysis discriminate the outcomes?

**WRITE YOUR ANSWER HERE**

```{r}

```

#### kmeans

9. Run kmeans clustering with two clusters on the biopsy dataset. How does kmeans clustering do to discriminate the data?

**WRITE YOUR ANSWER HERE**

```{r}

```

#### heatmap

10. Plot a heatmp using the biopsy dataset. Annotate the heatmap with the outcome. How does it do discriminating the outcomes?

**WRITE YOUR ANSWER HERE**

```{r}

```

---

11. In your opinion, which of the three methods discriminated the outcome the best?

I would say PCA because it's the most visually compelling visualization of the difference in outcomes, but they all do a good job 

<br>

### nycflights13

For this last section, we'll use the `flights` table from the `nycflights13` package. There's no variable of interest here; instead we'll do an unbiased clustering analysis in a (relatively) large dataset to see if we can find anything interesting.

```{r}
# install.packages('nycflights13')
library(nycflights13)
```

Look at the table. If you need to know what the columns are, look at the documentation (Remember `?flights`)

```{r}
flights
```

#### kmeans

12. Pick the best number of clusters to use for kmeans clustering and explain why you picked that number below.

**How many clusters will you use?**: 
**EXPLAIN HERE WHY YOU PICKED THAT NUMBER**: 

```{r}

```

13. Run kmeans clustering with your chosen number of clusters, then visualize the kmeans with some plot.

```{r}

```

#### PCA

14. Calculate a PCA and plot PCs one and two fpr the flights dataset.

```{r}

```

15. Now color the PCA plot by all the categorical variables with the flights dataset. Do any of them go with the patterns of variation identified by the PCA?

**WRITE YOUR ANSWER HERE**

```{r}

```




