In this tutorial, we will dive into the exploration of the toy dataset by adding clustering analysis. We’ll use the k-means algorithm to group similar observations based on ‘Age’ and ‘BMI’. Let’s dive in!
# Load required libraries
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Generate a toy dataset
set.seed(123)
toy_data <- data.frame(
ID = 1:10,
Age = sample(18:60, 10, replace = TRUE),
Height = rnorm(10, mean = 170, sd = 10),
Weight = rnorm(10, mean = 70, sd = 5)
)
# Print the first few rows of the dataset
head(toy_data)
## ID Age Height Weight
## 1 1 48 182.8055 69.72157
## 2 2 32 152.7273 66.07809
## 3 3 31 186.9018 66.33248
## 4 4 20 175.0381 68.92067
## 5 5 59 195.2834 68.32544
## 6 6 60 175.4910 64.57150
The toy dataset has columns: ID, Age, Height, and Weight.
Let’s add a new column ‘BMI’ to the dataset, calculated as Weight / (Height^2).
# Data manipulation
toy_data <- toy_data %>%
mutate(BMI = Weight / (Height^2))
# Print the updated dataset
head(toy_data)
## ID Age Height Weight BMI
## 1 1 48 182.8055 69.72157 0.002086356
## 2 2 32 152.7273 66.07809 0.002832854
## 3 3 31 186.9018 66.33248 0.001898887
## 4 4 20 175.0381 68.92067 0.002249491
## 5 5 59 195.2834 68.32544 0.001791645
## 6 6 60 175.4910 64.57150 0.002096676
The dataset now includes a new column ‘BMI’, representing the Body Mass Index.
Now, let’s perform k-means clustering on ‘Age’ and ‘BMI’.
# Select columns for clustering
clustering_data <- toy_data[, c("Age", "BMI")]
# Perform k-means clustering with k=2
kmeans_result <- kmeans(clustering_data, centers = 2)
# Add cluster labels to the dataset
toy_data$Cluster <- as.factor(kmeans_result$cluster)
# Print the updated dataset with cluster labels
head(toy_data)
## ID Age Height Weight BMI Cluster
## 1 1 48 182.8055 69.72157 0.002086356 1
## 2 2 32 152.7273 66.07809 0.002832854 2
## 3 3 31 186.9018 66.33248 0.001898887 2
## 4 4 20 175.0381 68.92067 0.002249491 2
## 5 5 59 195.2834 68.32544 0.001791645 1
## 6 6 60 175.4910 64.57150 0.002096676 1
The dataset now includes a new column ‘Cluster’, indicating the cluster assignment for each observation.
Let’s visualize the clusters on a scatter plot.
# Scatter plot with cluster color
plot_clusters <- ggplot(toy_data, aes(x = Age, y = BMI, color = Cluster)) +
geom_point() +
labs(title = "Clustering Analysis: Age vs. BMI",
x = "Age",
y = "BMI",
color = "Cluster") +
theme_minimal()
print(plot_clusters)
The scatter plot shows how the observations are grouped into clusters based on ‘Age’ and ‘BMI’.
In this tutorial, we not only explored and manipulated the toy dataset but also performed clustering analysis using the k-means algorithm. Clustering can reveal hidden patterns in the data and is a valuable technique in data analysis.