Code
print(fviz_nbclust(scaled_data, kmeans, method = "wss") +
labs(title = "Elbow Method for Optimal K"))print(fviz_nbclust(scaled_data, kmeans, method = "wss") +
labs(title = "Elbow Method for Optimal K"))km_res <- kmeans(scaled_data, centers = 3, nstart = 25)
customer_data$segment <- as.factor(km_res$cluster)print(fviz_cluster(km_res, data = scaled_data, geom = "point",
ellipse.type = "convex", main = "Customer Segments"))customer_data <- customer_data %>% mutate(propensity_score = (frequency * 0.6) - (recency * 0.4))
ggplot(customer_data, aes(x = propensity_score, y = monetary, color = segment)) + geom_point(size = 3, alpha = 0.7) + theme_minimal() + labs(title = “Unsupervised Segments within Supervised Propensity”, x = “Supervised Propensity Score”, y = “Monetary Value”)