library(cluster)
library(dbscan)
library(factoextra)
library(tidyverse)
library(patchwork)
library(ggrepel)Activity 4.2 - Kmeans, PAM, and DBSCAN clustering
SUBMISSION INSTRUCTIONS
- Render to html
- Publish your html
- Submit a link to your published solutions
Loading required packages:
Question 1
Reconsider the three data sets below. We will now compare kmeans, PAM, and DBSCAN to cluster these data sets.
three_spheres <- read.csv('Data/cluster_data1.csv')
ring_moon_sphere <- read.csv('Data/cluster_data2.csv')
two_spirals_sphere <- read.csv('Data/cluster_data3.csv')A)
With kmeans and PAM, we can specify that we want 3 clusters. But recall with DBSCAN we select minPts and eps, and the number of clusters is determined accordingly. Use k-nearest-neighbor distance plots to determine candidate epsilon values for each data set if minPts = 4. Add horizontal line(s) to each plot indicating your selected value(s) of \(\epsilon.\)
library(tidyverse)
library(dbscan)
library(patchwork)
three_spheres <- read.csv('Data/cluster_data1.csv')
ring_moon_sphere <- read.csv('Data/cluster_data2.csv')
two_spirals_sphere <- read.csv('Data/cluster_data3.csv')
# Keep only the x, y coordinates
numeric_only <- function(data) {
data %>%
dplyr::select(where(is.numeric)) %>%
dplyr::select(1:2)
}
# k-nearest-neighbor distance plot with candidate eps line(s)
knn_plot <- function(data, eps, title, minPts = 4) {
# minPts counts the point itself, so use k = minPts - 1 neighbors
knn_d <- sort(kNNdist(numeric_only(data), k = minPts - 1))
data.frame(point = seq_along(knn_d), dist = knn_d) %>%
ggplot(aes(x = point, y = dist)) +
geom_line() +
geom_hline(yintercept = eps, linetype = 2, color = 'red') +
labs(title = title,
x = 'Points sorted by distance',
y = paste0(minPts - 1, '-NN distance')) +
theme_classic(base_size = 16)
}
# Candidate eps values: set these at the "elbow" of each plot
eps1 <- 0.5 # three spheres
eps2 <- 0.5 # ring, moon, sphere
eps3 <- 0.5 # two spirals, sphere
knn_plot(three_spheres, eps1, 'Three spheres') +
knn_plot(ring_moon_sphere, eps2, 'Ring, moon, sphere') +
knn_plot(two_spirals_sphere, eps3, 'Two spirals, sphere')B)
Write a function called plot_dbscan_results(df, eps, minPts). This function takes a data frame, epsilon value, and minPts as arguments and does the following:
- Runs DBSCAN on the inputted data frame
df, given theepsandminPtsvalues; - Creates a scatterplot of the data frame with points color-coded by assigned cluster membership. Make sure the title of the plot includes the value of
epsandminPtsused to create the clusters!!
Using this function, and your candidate eps values from A) as a starting point, implement DBSCAN to correctly identify the 3 cluster shapes in each of the three data sets. You will likely need to revise the eps values until you settle on a “correct” solution.
library(tidyverse)
library(dbscan)
library(patchwork)
plot_dbscan_results <- function(df, eps, minPts) {
# Keep only the x, y coordinates
numeric_only <- df %>%
dplyr::select(where(is.numeric)) %>%
dplyr::select(1:2) %>%
setNames(c('x', 'y'))
# Run DBSCAN
db <- dbscan(numeric_only, eps = eps, minPts = minPts)
# Cluster 0 = noise points
numeric_only$cluster <- factor(ifelse(db$cluster == 0, 'Noise', db$cluster))
n_clusters <- length(unique(db$cluster[db$cluster != 0]))
ggplot(data = numeric_only, aes(x = x, y = y, color = cluster)) +
geom_point() +
theme_classic(base_size = 16) +
labs(title = paste0('DBSCAN: eps = ', eps, ', minPts = ', minPts),
subtitle = paste0(n_clusters, ' clusters, ',
sum(db$cluster == 0), ' noise points'),
color = 'Cluster')
}
# Start with the eps values from part A, then adjust until each shows 3 clusters
plot_dbscan_results(three_spheres, eps = eps1, minPts = 4) +
plot_dbscan_results(ring_moon_sphere, eps = eps2, minPts = 4) +
plot_dbscan_results(two_spirals_sphere, eps = eps3, minPts = 4)C)
Compare your DBSCAN solutions to the 3-cluster solutions from k-means and PAM. Use the patchwork package and your function from B) to produce a 3x3 grid of plots: one plot per method/data set combo. Comment on your findings.
library(tidyverse)
library(dbscan)
library(cluster)
library(patchwork)
# Keep only the x, y coordinates
numeric_only <- function(df) {
df %>%
dplyr::select(where(is.numeric)) %>%
dplyr::select(1:2) %>%
setNames(c('x', 'y'))
}
# ---- DBSCAN plot function (from part B) ----
plot_dbscan_results <- function(df, eps, minPts) {
dat <- numeric_only(df)
db <- dbscan(dat, eps = eps, minPts = minPts)
dat$cluster <- factor(ifelse(db$cluster == 0, 'Noise', db$cluster))
n_clusters <- length(unique(db$cluster[db$cluster != 0]))
ggplot(data = dat, aes(x = x, y = y, color = cluster)) +
geom_point() +
guides(color = 'none') +
theme_classic(base_size = 16) +
labs(title = paste0('DBSCAN: eps = ', eps, ', minPts = ', minPts),
subtitle = paste0(n_clusters, ' clusters, ',
sum(db$cluster == 0), ' noise points'))
}
# ---- k-means plot function ----
plot_kmeans_results <- function(df, k = 3) {
dat <- numeric_only(df)
kmeans_clusters <- kmeans(dat, centers = k, iter.max = 20, nstart = 10)
dat$cluster <- factor(kmeans_clusters$cluster)
ggplot(data = dat, aes(x = x, y = y, color = cluster)) +
geom_point() +
guides(color = 'none') +
theme_classic(base_size = 16) +
labs(title = paste0('K-means, k = ', k))
}
# ---- PAM plot function ----
plot_pam_results <- function(df, k = 3) {
dat <- numeric_only(df)
pam_clusters <- pam(dat, k = k, nstart = 10)
dat$cluster <- factor(pam_clusters$clustering)
ggplot(data = dat, aes(x = x, y = y, color = cluster)) +
geom_point() +
guides(color = 'none') +
theme_classic(base_size = 16) +
labs(title = paste0('PAM, k = ', k))
}
# ---- Find an eps that gives exactly 3 clusters (fewest noise points) ----
find_eps <- function(df, eps_grid, minPts = 4) {
dat <- numeric_only(df)
results <- map_dfr(eps_grid, function(e) {
db <- dbscan(dat, eps = e, minPts = minPts)
data.frame(eps = e,
n_clusters = length(unique(db$cluster[db$cluster != 0])),
n_noise = sum(db$cluster == 0))
})
results %>%
filter(n_clusters == 3) %>%
arrange(n_noise) %>%
slice(1) %>%
pull(eps)
}
eps1 <- find_eps(three_spheres, seq(0.05, 0.6, by = 0.01))
eps2 <- find_eps(ring_moon_sphere, seq(0.05, 0.6, by = 0.01))
eps3 <- find_eps(two_spirals_sphere, seq(0.5, 3, by = 0.05))
c(eps1, eps2, eps3) # chosen eps values[1] 0.26 0.31 1.20
set.seed(123)
# Rows = data sets; columns = k-means, PAM, DBSCAN
(plot_kmeans_results(three_spheres) +
plot_pam_results(three_spheres) +
plot_dbscan_results(three_spheres, eps = eps1, minPts = 4)) /
(plot_kmeans_results(ring_moon_sphere) +
plot_pam_results(ring_moon_sphere) +
plot_dbscan_results(ring_moon_sphere, eps = eps2, minPts = 4)) /
(plot_kmeans_results(two_spirals_sphere) +
plot_pam_results(two_spirals_sphere) +
plot_dbscan_results(two_spirals_sphere, eps = eps3, minPts = 4))K-means and PAM separate the three spheres but fail on the ring/moon/sphere and spirals/sphere data, cutting the ring, moon, and spirals into pieces because they can only form spherical clusters around a center. DBSCAN recovers all three shapes in every data set (eps = 0.26, 0.31, and 1.2) because it groups points by density and can follow curved shapes.
Question 2
In this question we will apply cluster analysis to analyze economic development indicators (WDIs) from the World Bank. The data are all 2020 indicators and include:
life_expectancy: average life expectancy at birthgdp: GDP per capita, in 2015 USDco2: CO2 emissions, in metric tons per capitafert_rate: annual births per 1000 womenhealth: percentage of GDP spent on health careimportsandexports: imports and exports as a percentage of GDPinternetandelectricity: percentage of population with access to internet and electricity, respectivelyinfant_mort: infant mortality rate, infant deaths per 1000 live birthsinflation: consumer price inflation, as annual percentageincome: annual per-capita income, in 2020 USD
wdi <- read.csv('Data/wdi_extract_clean.csv')
head(wdi) country life_expectancy gdp co2 fert_rate health internet
1 Afghanistan 61.45400 527.8346 0.180555 5.145 15.533614 17.0485
2 Albania 77.82400 4437.6535 1.607133 1.371 7.503894 72.2377
3 Algeria 73.25700 4363.6853 3.902928 2.940 5.638317 63.4727
4 Angola 63.11600 2433.3764 0.619139 5.371 3.274885 36.6347
5 Argentina 75.87800 11393.0506 3.764393 1.601 10.450306 85.5144
6 Armenia 73.37561 4032.0904 2.334560 1.700 12.240562 76.5077
infant_mort electricity imports inflation exports income
1 55.3 97.7 36.28908 5.601888 10.42082 475.7181
2 8.1 100.0 36.97995 1.620887 22.54076 4322.5497
3 20.4 99.7 24.85456 2.415131 15.53520 2689.8725
4 42.3 47.0 27.62749 22.271539 38.31454 1100.2175
5 8.7 100.0 13.59828 42.015095 16.60541 7241.0303
6 10.2 100.0 39.72382 1.211436 29.76499 3617.0320
Focus on using kmeans for this problem.
A)
My claim: 3-5 clusters appear optimal for this data set. Support or refute my claim using appropriate visualizations.
library(tidyverse)
library(factoextra)
library(patchwork)
wdi <- read.csv('Data/wdi_extract_clean.csv')
head(wdi) country life_expectancy gdp co2 fert_rate health internet
1 Afghanistan 61.45400 527.8346 0.180555 5.145 15.533614 17.0485
2 Albania 77.82400 4437.6535 1.607133 1.371 7.503894 72.2377
3 Algeria 73.25700 4363.6853 3.902928 2.940 5.638317 63.4727
4 Angola 63.11600 2433.3764 0.619139 5.371 3.274885 36.6347
5 Argentina 75.87800 11393.0506 3.764393 1.601 10.450306 85.5144
6 Armenia 73.37561 4032.0904 2.334560 1.700 12.240562 76.5077
infant_mort electricity imports inflation exports income
1 55.3 97.7 36.28908 5.601888 10.42082 475.7181
2 8.1 100.0 36.97995 1.620887 22.54076 4322.5497
3 20.4 99.7 24.85456 2.415131 15.53520 2689.8725
4 42.3 47.0 27.62749 22.271539 38.31454 1100.2175
5 8.7 100.0 13.59828 42.015095 16.60541 7241.0303
6 10.2 100.0 39.72382 1.211436 29.76499 3617.0320
# Use the country name column as rownames, keep numeric indicators only
name_col <- names(wdi)[!sapply(wdi, is.numeric)][1]
wdi_numeric <- wdi %>%
drop_na() %>%
column_to_rownames(name_col) %>%
dplyr::select(where(is.numeric))
# Scale (indicators are on very different scales, e.g. gdp vs. fert_rate)
wdi_scaled <- scale(wdi_numeric)
set.seed(123)
fviz_nbclust(wdi_scaled,
FUNcluster = kmeans,
method = 'wss',
k.max = 10,
nstart = 25) +
labs(title = 'WSS vs k using kmeans') +
fviz_nbclust(wdi_scaled,
FUNcluster = kmeans,
method = 'silhouette',
k.max = 10,
nstart = 25) +
labs(title = 'Avg silhouette vs k using kmeans')B)
Use k-means to identify 4 clusters. Characterize the 4 clusters using a dimension reduction technique. Provide examples of countries that are representative of each cluster. Be thorough.
library(FactoMineR)
set.seed(123)
kmeans4 <- kmeans(wdi_scaled, centers = 4, iter.max = 20, nstart = 25)
# Cluster sizes
table(kmeans4$cluster)
1 2 3 4
75 33 1 35
# PCA biplot colored by cluster
wdi_pca <- PCA(wdi_numeric, graph = FALSE)
fviz_pca(wdi_pca,
habillage = factor(kmeans4$cluster),
repel = TRUE,
labelsize = 3) +
ggtitle('K-means 4-cluster solution')Cluster 1 (75 countries): middle-income, developing economies. This is the largest cluster and sits near the center of the biplot. These countries have moderate income, life expectancy, and internet access, with fertility and infant mortality between the other two main clusters. It includes upper-middle-income countries on the right side of the cluster and lower-middle-income countries closer to the left. Representative examples near its center include Mexico, Colombia, Morocco, Tunisia, Thailand, and Azerbaijan.
Cluster 2 (33 countries): low-income countries with high fertility and infant mortality. These countries sit far to the left, in the direction of the infant mortality and fertility arrows, and opposite internet, electricity, life expectancy, and GDP. Most are in Sub-Saharan Africa, along with Afghanistan and Pakistan. Representative examples are Niger, Chad, Mali, Burkina Faso, Burundi, Ethiopia, and Uganda. A few (Djibouti, Guinea, Lesotho, Mozambique) sit higher on Dim2 because of relatively high trade as a share of GDP.
Cluster 4 (35 countries): high-income, developed economies. These countries sit far to the right, with high GDP, income, life expectancy, internet and electricity access, health spending, and CO2 emissions, and low fertility and infant mortality. Representative examples are Norway, Germany, Australia, Canada, Japan, Denmark, and the United States. Within this cluster, Luxembourg, Singapore, and Ireland stand out at the top of Dim2. They are small, very wealthy countries with extremely high imports and exports relative to GDP, which makes them outliers.
Cluster 3 (1 country): Zimbabwe. Zimbabwe forms a cluster on its own even though it sits among the cluster 2 countries on the biplot. Its separation comes from a variable the first two dimensions barely show: inflation, which was extremely high in Zimbabwe in 2020. Because k-means is sensitive to outliers, it spent one of its four clusters on this single country rather than finding a more meaningful group.C)
C)
Remove Ireland, Singapore, and Luxembourg from the data set. Use k-means to find 4 clusters again, with these three countries removed. How do the cluster definitions change?
# Remove Ireland, Singapore, and Luxembourg
remove <- c('Ireland', 'Singapore', 'Luxembourg')
remove %in% rownames(wdi_numeric) # should all be TRUE[1] TRUE TRUE TRUE
wdi_numeric2 <- wdi_numeric[!(rownames(wdi_numeric) %in% remove), ]
wdi_scaled2 <- scale(wdi_numeric2)
set.seed(123)
kmeans4_new <- kmeans(wdi_scaled2, centers = 4, iter.max = 20, nstart = 25)
# Cluster sizes
table(kmeans4_new$cluster)
1 2 3 4
22 67 21 31
# PCA biplot colored by new clusters
wdi_pca2 <- PCA(wdi_numeric2, graph = FALSE)
fviz_pca(wdi_pca2,
habillage = factor(kmeans4_new$cluster),
repel = TRUE,
labelsize = 3) +
ggtitle('K-means 4-cluster solution, Ireland/Singapore/Luxembourg removed')Cluster 4 (31): low-income countries with high fertility and infant mortality. This is the same as the old cluster 2, now with Zimbabwe included.
Cluster 2 (67): middle-income countries. This is mostly the old middle-income cluster ( Mexico, Colombia, Indonesia, India, Brazil, China, Peru). It lost some of its more trade-oriented members to cluster 1.
Cluster 3 (21): wealthy, less trade-dependent economies. This is the core of the old high-income cluster: very high income, GDP, life expectancy, internet access, and health spending, with relatively low trade as a share of GDP. Examples are Norway, Switzerland, Germany, Denmark, Japan, Australia, Canada, and the United States.
Cluster 1 (22): a new trade-oriented cluster. These are upper-middle- and high-income countries with very high imports and exports relative to GDP. The cluster draws partly from the old high-income cluster (UAE, Belgium, Cyprus, Bahrain, Slovenia, Czechia, Estonia, Slovak Republic, Brunei) and partly from the old middle-income cluster (Hungary, Malaysia, Bulgaria, Viet Nam), plus Djibouti, which moved over from the old low-income cluster.