STEP 1:
Load necessary libraries
STEP 2:
Create a new dataframe called mammals using the appropriate code to read the mammals_sleep.csv file
# Your code here
Mammal_sleep <- read.csv("C://Users//Gurvinder//Desktop//Data Analytics//Assignments//Predictive Analytics//K-Means//Mammal_Sleep.csv")
head(Mammal_sleep)
## X name genus vore order conservation
## 1 1 Cheetah Acinonyx carni Carnivora lc
## 2 2 Owl monkey Aotus omni Primates <NA>
## 3 3 Mountain beaver Aplodontia herbi Rodentia nt
## 4 4 Greater short-tailed shrew Blarina omni Soricomorpha lc
## 5 5 Cow Bos herbi Artiodactyla domesticated
## 6 6 Three-toed sloth Bradypus herbi Pilosa <NA>
## sleep_total sleep_rem sleep_cycle awake brainwt bodywt
## 1 12.1 NA NA 11.9 NA 50.000
## 2 17.0 1.8 NA 7.0 0.01550 0.480
## 3 14.4 2.4 NA 9.6 NA 1.350
## 4 14.9 2.3 0.1333333 9.1 0.00029 0.019
## 5 4.0 0.7 0.6666667 20.0 0.42300 600.000
## 6 14.4 2.2 0.7666667 9.6 NA 3.850
STEP 3:
Look at your mammals dataframe (str)
Create a subset dataframe that contains only the columns “sleep_total” and “sleep_rem”
Remove any NA’s
Visualize the results using a scatter plot
# Look at your dataframe
# Select the columns for k-means clustering
mammalSleep <- data.frame(sleep_total = Mammal_sleep$sleep_total, sleep_rem = Mammal_sleep$sleep_rem)
# remove any NA's from the two selected variables
mammalSleep <- na.omit(mammalSleep)
# Create a scatterplot of the data before kmeans clustering
ggplot(mammalSleep, aes(x = sleep_total, y = sleep_rem)) +
geom_point() +
labs(x = "Total Sleep", y = "REM Sleep", title = "Initial Scatterplot of Mammal Sleep Data") +
theme(plot.title = element_text(hjust = 0.5))
STEP 4:
Create your kmeans model using the format:
your_model_name <- kmeans(your_dataframe, clusters=k)
Find the optimal value of k using the elbow method. You can do this
several ways including:
Wrap your kmeans model in a for loop and store the values of your_model_name$tot.withinss to create a dataframe that can be used to plot your k-values vs tot.withinss.
Run a variety of kmeans models using a range of k-values. For
each iteration, print out the resulting $tot.withinss values for each
value of k and make your own dataframe to use with ggplot to make a line
plot for the elbow method
k <- c(1,2,3,4,5,6,7,8,9,10)
tot.withinss <- c(value1, value2, …, value10)
results_df <- data.frame(k,tot.withinss)
# Your model here
# Set up the range of k values to test
k_values <- 1:10
tot_withinss <- numeric(length(k_values))
# Run k-means clustering for each value of k
for (i in k_values) {
kmeans_model <- kmeans(mammalSleep, centers = i)
tot_withinss[i] <- kmeans_model$tot.withinss
}
# Create a dataframe to store k-values and their respective tot.withinss
results_df <- data.frame(k = k_values, tot.withinss = tot_withinss)
# Plotting the elbow method to find the optimal value of k
ggplot(results_df, aes(x = k, y = tot.withinss)) +
geom_line() +
geom_point() +
labs(x = "Number of Clusters (k)", y = "Total Withinss", title = "Elbow Method for Optimal k") +
theme(plot.title = element_text(hjust = 0.5))
STEP 5:
Once you have determined the best value of k, run a new model (optimal_model). Produce a new scatter plot using ggplot. You will use sleep_total and sleep_rem as your X and Y variables, and use your_optimal_model$cluster as the color component within your aes function.
# Your ggplot here
# Determine the optimal value of k (for example, let's say it's k=4)
optimal_k <- 4
# Run k-means clustering with the optimal k value
optimal_model <- kmeans(mammalSleep, centers = optimal_k)
# Creating a scatter plot with clustered data
ggplot(mammalSleep, aes(x = sleep_total, y = sleep_rem, color = as.factor(optimal_model$cluster))) +
geom_point() +
labs(x = "Total Sleep", y = "REM Sleep", title = "Clustered Mammal Sleep Data") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_point(data = as.data.frame(optimal_model$centers), aes(x = sleep_total, y = sleep_rem, color = as.factor(1:optimal_k)), pch = 4, size = 5) +
geom_text(data = as.data.frame(optimal_model$centers), aes(label = as.factor(1:optimal_k), x = sleep_total, y = sleep_rem, color = as.factor(1:optimal_k)), hjust = -0.5, vjust = -0.5)
STEP 6: (Optional) Add your centroids to the ggplot produced in STEP 5 and label them according to your_optimal_model$cluster