# 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)
## Warning: package 'cluster' was built under R version 4.5.3
avg_sil <- function(k){
  km <- kmeans(df_200, centers = k)
  mean(silhouette(km$cluster, dist(df_200))[,3])
}
avg_sil(3)
## [1] 0.2585276
# 5. Clustering
library(flexclust)
## Warning: package 'flexclust' was built under R version 4.5.3
library(dbscan)
## Warning: package 'dbscan' was built under R version 4.5.3
## 
## Attaching package: 'dbscan'
## The following object is masked from 'package:stats':
## 
##     as.dendrogram
library(meanShiftR)
library(e1071)
## Warning: package 'e1071' was built under R version 4.5.3
## 
## Attaching package: 'e1071'
## The following object is masked from 'package:flexclust':
## 
##     bclust
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)
## Warning in dbscan(df_200, eps = 0.7, MinPts = 5): converting argument MinPts
## (fpc) to minPts (dbscan)!
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")