Loading required libraries

library(readr) library(rsconnect) library(tidyr) library(dplyr) library(ggplot2) library(factoextra)

Loading the dataset

data <- read.csv(“C:/Users/daniv/Downloads/Ice (5).csv”)

Checking the structure of the data

str(data)

Scaling the data

scaled_data <- scale(data)

Doing the Elbow method to find the optimal number of clusters

wss <- numeric(10) for (i in 1:10) { km <- kmeans(scaled_data, centers = i) wss[i] <- km$tot.withinss } plot(1:10, wss, type = “b”, xlab = “Number of Clusters”, ylab = “Within groups sum of squares”)

#From the plot, we can identify the ‘elbow’ point where the decrease in WSS slows down. From that, we can see that the ideal number of clusters is 3. ### Performing k-means clustering with the determined number of clusters k <- 3

Running k-means clustering

kmeans_result <- kmeans(scaled_data, centers = k)

Perform PCA

pca_result <- prcomp(scaled_data)

Get PCA scores

pca_scores <- as.data.frame(pca_result$x[, 1:2]) # considering first two components

Add cluster information to the scores

pca_scores\(cluster <- kmeans_result\)cluster

Visualize the clusters

plot(pca_scores[, 1:2], col = pca_scores$cluster, pch = 19, main = “Clusters based on PCA”, xlab = “PC1”, ylab = “PC2”)

Adding cluster centers to the plot

centers <- as.data.frame(pca_result$rotation[, 1:2] * 3) # multiply by a factor for better visibility text(centers, labels = 1:k, col = 1:k, pos = 3)

Identifying which properties characterize the prototypes

#To identify the properties that characterize the prototypes within each cluster, we can analyze the original data in relation to the cluster assignments. This involves comparing the average values of different properties for each cluster.

Add the cluster assignments to the original dataset

data_with_clusters <- cbind(data, Cluster = kmeans_result$cluster)

Calculate the mean values of properties for each cluster

mean_properties <- aggregate(. ~ Cluster, data = data_with_clusters[, -1], FUN = mean)

View mean property values per cluster

print(mean_properties)

These average values provide a glimpse into the distinctive features of each cluster, shedding light on which attributes hold greater significance within each group.

In the case of Cluster 1, it exhibits elevated average scores in categories such as ‘refreshing’, ‘for_everyday’, ‘fun’, and ‘favorite’, implying a preference for ice cream that embodies these qualities. The ‘expensive’ attribute, on the other hand, appears to be less prominent within this cluster.

In contrast, Cluster 2 demonstrates lower average ratings across most categories compared to the other clusters, suggesting a generally less favorable evaluation of the ice creams under consideration.

Moving on to Cluster 3, it showcases higher average ratings for ‘look_good’, ‘satisfies_hunger’, ‘for_special’, ‘for_adults’, and ‘expensive’, indicating a penchant for ice creams that possess these characteristics, making them visually appealing, satiating, suitable for special occasions or adults, and potentially higher in cost.

By delving into these averages, we can glean insights into the ice cream preferences of each cluster based on the attributes they prioritize or find appealing. The higher average values highlight the attributes that define and are more prevalent within each cluster.