1. Load data

data <- read.csv(“C:/Users/HP/Downloads/Consumer_Shopping_Trends_2026 (6).csv”)

2. Preprocessing

df <- data[sapply(data, is.numeric)] df_200 <- df[1:200, ]

3. Elbow Method

wss <- sapply(1:10, function(k){ kmeans(df_200, centers = k, nstart = 20)$tot.withinss }) plot(1:10, wss, type=“b”, pch=19, main=“Elbow Method”)

4. Silhouette

library(cluster) avg_sil <- function(k){ km <- kmeans(df_200, centers = k) mean(silhouette(km$cluster, dist(df_200))[,3]) } avg_sil(3)

5. Clustering

library(flexclust) library(dbscan) library(meanShiftR) library(e1071)

km_res <- kmeans(df_200, centers = 3) kmed_res <- kcca(df_200, k = 3, family = kccaFamily(“kmedians”)) db_res <- dbscan(df_200, eps = 0.7, MinPts = 5) ms_res <- meanShift(as.matrix(df_200)) fcm_res <- cmeans(df_200, centers = 3, m = 2)

6. PCA

pca <- prcomp(df_200, scale. = TRUE)

7. Visualisasi

par(mfrow = c(2,3))

plot(pca\(x[,1:2], col = km_res\)cluster, pch = 19, main = “K-Means”) plot(pca\(x[,1:2], col = clusters(kmed_res), pch = 19, main = "K-Medians") plot(pca\)x[,1:2], col = db_res\(cluster + 1, pch = 19, main = "DBSCAN") plot(pca\)x[,1:2], col = ms_res\(assignment, pch = 19, main = "Mean Shift") plot(pca\)x[,1:2], col = fcm_res$cluster, pch = 19, main = “Fuzzy C-Means”)