Path: blob/master/2019-spring/materials/worksheet_11/worksheet_11.ipynb
2051 views
Worksheet 11 - Clustering
Lecture and Tutorial Learning Goals:
After completing this week's lecture and tutorial work, you will be able to:
Describe a case where clustering would be an appropriate tool, and what insight it would bring from the data.
Explain the k-means clustering algorithm.
Interpret the output of a k-means cluster analysis.
Perform k-means clustering in R using k-means
Visualize the output of k-means clustering in R using a scatter plot facetted across each access
Identify when it is necessary to scale variables before clustering and do this using R
Use the elbow method to choose the number of clusters for k-means
Describe advantages, limitations and assumptions of the kmeans clustering algorithm.
Question 0.1
In which of the following scenarios would clustering methods likely be appropriate?
A. Identifying sub-groups of houses according to their house type, value, and geographical location
B. Predicting whether a given user will click on an ad on a website
C. Segmenting customers based on their preferences to target advertising
D. Both A. and B.
E. Both A. and C.
Assign your answer to an object called answer0.0
.
Question 0.2
Which step is the description of the Kmeans algorithm below is incorrect?
Choose the number of clusters
Randomly assign each of the points to one of the clusters
Calculate the position for the cluster centre (centroid) for each of the clusters (this is the middle of the points in the cluster, as measured by straight-line distance)
Re-assign each of the points to the cluster who's centroid is furthest from that point
Repeat steps 1 - 2 until the cluster centroids don't change very much between iterations
Assign your answer to an object called answer0.1
.
Hoppy craft beer
Craft beer is a strong market in Canada and the US, and is expanding to other countries as well. If you wanted to get into the craft beer brewing market, you might want to better understand the product landscape. One popular craft beer product is hopped craft beer. Breweries create/label many different kinds of hopped craft beer, but how many different kinds of hopped craft beer are there really when you look at the chemical properties instead of the human labels?
We will start to look at the question by looking at a craft beer data set from Kaggle. In this data set, we will use the alcoholic content by volume (abv
column) and the International bittering units (ibu
column) as variables to try to cluster the beers.
Question 1.0
Read in the beers.csv
data set and assign it to an object called beer
. The data set is located within the worksheet_11
folder.
Question 1.1
Let's start by visualizing the variables we are going to use in our cluster analysis as a scatter plot. Name the plot object beer_eda
. Remember to do all the visualization best practices when making this plot, including human readable labels.
Question 1.2
We need to clean up this data set a bit. Specifically, we need to remove the rows where ibu == NA
and select only the columns we are interested in clustering, which are ibu
and abv
. Name the cleaned data clean_beer
.
hint - is.na()
will be useful in this filter
Question 1.3
Given that Kmeans clustering uses a distance function as part of its algorithm, it is important to scale the variables. Let's do that now, and let's do it using the map_df
function (so we can do it to all variables at once - this will be really useful for larger data sets!). Also, although it's not necessary (but it's also not harmful), let's also centre the variables. This is the default to R's scale function.
Name the data frame scaled_beer
.
Question 1.4
From our exploratory data visualization, 2 clusters seems like it might be a reasonable number of clusters. Use the kmeans
function with centers = 2
to perform clustering with this choice of K. Name your model object beer_cluster_k2
. Given that the Kmeans algorithm uses a random start, set the seed to be 1234 so that your result is reproducible.
Question 1.5
Use broom
's augment
function to get the cluster assignment for each point in a tidy data frame. Name that data frame tidy_beer_cluster_k2
.
Question 1.6
Create a scatter plot of ibu
versus abv
(using the data in tidy_beer_cluster_k2
) where the points are labelled by their cluster assignment. Name the plot object tidy_beer_cluster_k2_plot
.
Question 1.7
We do not know, however, that two clusters (K = 2) is the best choice for this data set. What can we do to choose the best K?
A. Perform cross-validation for a variety of possible K's and choose the one with the smallest total within sum of squares
B. Perform cross-validation for a variety of possible K's and choose the one with the greatest drop in total within sum of squares
C. Perform clustering for a variety of possible K's and choose the one with the smallest total within sum of squares
D. Perform clustering for a variety of possible K's and choose the choose the one with the greatest drop in total within sum of squares
Assign your answer to an object called answer1.7
.
Question 1.71
Use broom
's glance
function to get the model-level statistics for the clustering we just performed, including total within sum of squares. Name the data frame beer_cluster_k2_model_stats
.
Question 1.8
Let's now choose the best K for this clustering problem. To do this we need to create a data frame (or a tibble, this time it doesn't matter) with a column named k
, where we vary K from 1 to 10. Name this data frame beer_clustering
.
Question 1.9
Next we use mutate
to create a new column in the beer_clustering
data frame named models
where we use map
to apply the kmeans
function to our scaled_beer
data set for each of the K's.
Question 2.0
Next we use mutate
again to create a new column called model_statistics
in the beer_clustering
data frame where we use map
to apply the glance
function to each of our models (in the models
column) to get the model-level statistics (this is where we can get the value for total within sum of squares that we use to choose K).
Question 2.1
Now we use the unnest
function to expand the data frames in the model_statistics
column so that we can access the values for total within sum of squares as a column. Name the modified data frame beer_clustering_unnested
.
Question 2.2
Now that we have the the values for total within sum of squares for each model in a column (tot.withinss
), we can use it to create a line plot of total within sum of squares versus k (so that we can choose the best K). Total within sum of squares should be on the y-axis and K should be on the x-axis. Create this plot and name the plot object choose_beer_k
.
Question 2.3
From the plot above, which K should we choose? Save your answer (as a whole number) to a variable named answer2.3
.
Question 2.4 (optional - not graded)
In your own words, explain why we chose the K we chose above.
YOUR ANSWER HERE
Question 2.5 (optional - not graded)
What can we conclude from our analysis? How many different types of hoppy craft beer are there in this data set using the two variables we have? Do you think our analysis might change if we added additional variables? Why/why not?
YOUR ANSWER HERE
Question 2.6 (optional - not graded)
Visually verify that 2 clusters is the "best" choice for K for this analysis. Do this by plotting the cluster assignments for the points for each K (each should be its own scatter plot).