For this week’s practice on clustering, we’ll use two datasets from previous weeks, the wine and biopsy datasets, to try out all the algorithms discussed this week. Just like in the clustering demo for this week, we’ll try to recover a categorical variable from the dataset using clustering.
Download the tables from the meetup website and stick them in the same folder as your practice R Markdown file for easy read-in.
In the wine dataset, Cultivar is a categorical variable that we’ll try to recover. All the information about the dataset is repeated below (from week 7) for reference.
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:
Read in the wine.csv file in the chunk below and do any wrangling you might need to. Save the file read in as an object to use later.
# put answer here
Pick two numeric variables and plot a scatterplot colored by Cultivar. You’ll use this plot for comparison purposes after running kmeans clustering.
# put answer here
Using the code from meetup as a guide, pick the best number of clusters for kmeans.
# put answer here
How many clusters are you going to use? Write your answer here
Now run kmeans()
with the number of clusters you selected and plot the cluster results with whichever numeric variables you want.
# put answer here
Using the same numeric variables as above, plot them colored by kmeans cluster.
# put answer here
Questions to Consider: Did the kmeans recapitulate the cultivar? Did it seem to find an other underlying pattern/structure in the data?
Run a PCA on the wine dataset and add the PCs back to the wine table with augment()
. Don’t forget to remove categorical columns first!
# put answer here
Plot PC1 vs PC2 from your results, colored by cultivar
# put answer here
It doesn’t really discriminate between cultivars does it? Try plotting at least two combinations of other PCs in the chunk below to see if other PCs discriminate between cultivars.
# put answer here
Questions to Consider: Did the PCA create distinct clusters? Do some combinations of PCs create better clusters than others?
Run a tSNE on the wine dataset. Don’t forget to remove categorical columns first and set check_duplicates = FALSE
.
# put answer here
Add the tSNE results vectors back to the original data. Remember you can subset them from the tSNE using the $
operator
# put answer here
Plot your tSNE results colored by cultivar
# put answer here
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:
Read in the biopsy.csv file in the chunk below and do any wrangling you might need to. Save the file read in as an object to use later.
# put answer here
Pick two numeric variables and plot a scatterplot colored by outcome. You’ll use this plot for comparison purposes after running kmeans clustering.
# put answer here
Using the code from meetup as a guide, pick the best number of clusters for kmeans.
# put answer here
How many clusters are you going to use? Write your answer here
Now run kmeans()
with the number of clusters you selected and plot the cluster results with whichever numeric variables you want.
# put answer here
Using the same numeric variables as above, plot them colored by kmeans cluster.
# put answer here
Questions to Consider: How did the kmeans do at separating the set into clusters? Why did it turn out the way it did? Why should you have known from the first plot that kmeans was a bad idea?
Run a PCA on the biopsy dataset.
# put answer here
Plot PC1 vs PC2 from your results, colored by outcome
# put answer here
Questions to Consider: Why did the PCA do better at separating the data than kmeans?
Run a tSNE on the biopsy dataset.
# put answer here
Add the tSNE results vectors back to the original data.
# put answer here
Plot your tSNE results colored by cultivar
# put answer here
Questions to Consider: How does the tSNE compare to the PCA? Which cluster method would you pick and why?