R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

#Data yang digunakan

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
pelanggan <- read.csv("https://storage.googleapis.com/dqlab-dataset/customer_segments.txt", sep="\t")
head(pelanggan)
##   Customer_ID      Nama.Pelanggan Jenis.Kelamin Umur      Profesi Tipe.Residen
## 1    CUST-001        Budi Anggara          Pria   58   Wiraswasta       Sector
## 2    CUST-002    Shirley Ratuwati        Wanita   14      Pelajar      Cluster
## 3    CUST-003        Agus Cahyono          Pria   48 Professional      Cluster
## 4    CUST-004    Antonius Winarta          Pria   53 Professional      Cluster
## 5    CUST-005 Ibu Sri Wahyuni, IR        Wanita   41   Wiraswasta      Cluster
## 6    CUST-006     Rosalina Kurnia        Wanita   24 Professional      Cluster
##   NilaiBelanjaSetahun
## 1             9497927
## 2             2722700
## 3             5286429
## 4             5204498
## 5            10615206
## 6             5215541

#Praprosesing Data

pelanggan_matrix <- data.matrix(pelanggan[c("Jenis.Kelamin", "Profesi", "Tipe.Residen")])
pelanggan_matrix
##       Jenis.Kelamin Profesi Tipe.Residen
##  [1,]             1       5            2
##  [2,]             2       3            1
##  [3,]             1       4            1
##  [4,]             1       4            1
##  [5,]             2       5            1
##  [6,]             2       4            1
##  [7,]             1       5            2
##  [8,]             1       4            1
##  [9,]             2       4            2
## [10,]             1       4            1
## [11,]             2       4            2
## [12,]             2       4            2
## [13,]             2       5            1
## [14,]             1       5            1
## [15,]             2       5            1
## [16,]             1       4            1
## [17,]             2       1            1
## [18,]             2       1            1
## [19,]             2       5            1
## [20,]             2       3            2
## [21,]             2       5            1
## [22,]             2       4            1
## [23,]             1       4            1
## [24,]             2       5            1
## [25,]             2       5            2
## [26,]             2       4            1
## [27,]             2       5            1
## [28,]             2       1            1
## [29,]             2       4            1
## [30,]             2       1            2
## [31,]             2       2            1
## [32,]             2       5            2
## [33,]             2       2            1
## [34,]             2       5            2
## [35,]             2       4            2
## [36,]             2       5            1
## [37,]             2       4            2
## [38,]             2       5            2
## [39,]             2       4            1
## [40,]             2       3            2
## [41,]             2       1            1
## [42,]             2       5            1
## [43,]             2       4            1
## [44,]             2       5            1
## [45,]             2       4            1
## [46,]             2       5            2
## [47,]             2       1            1
## [48,]             2       5            2
## [49,]             2       1            2
## [50,]             2       5            2
pelanggan2 <- data.frame(pelanggan, pelanggan_matrix)
pelanggan2
##    Customer_ID        Nama.Pelanggan Jenis.Kelamin Umur          Profesi
## 1     CUST-001          Budi Anggara          Pria   58       Wiraswasta
## 2     CUST-002      Shirley Ratuwati        Wanita   14          Pelajar
## 3     CUST-003          Agus Cahyono          Pria   48     Professional
## 4     CUST-004      Antonius Winarta          Pria   53     Professional
## 5     CUST-005   Ibu Sri Wahyuni, IR        Wanita   41       Wiraswasta
## 6     CUST-006       Rosalina Kurnia        Wanita   24     Professional
## 7     CUST-007         Cahyono, Agus          Pria   64       Wiraswasta
## 8     CUST-008        Danang Santosa          Pria   52     Professional
## 9     CUST-009 Elisabeth Suryadinata        Wanita   29     Professional
## 10    CUST-010        Mario Setiawan          Pria   33     Professional
## 11    CUST-011        Maria Suryawan        Wanita   50     Professional
## 12    CUST-012       Erliana Widjaja        Wanita   49     Professional
## 13    CUST-013          Cahaya Putri        Wanita   64       Wiraswasta
## 14    CUST-014        Mario Setiawan          Pria   60       Wiraswasta
## 15    CUST-015      Shirley Ratuwati        Wanita   20       Wiraswasta
## 16    CUST-016          Bambang Rudi          Pria   35     Professional
## 17    CUST-017             Yuni Sari        Wanita   32 Ibu Rumah Tangga
## 18    CUST-018           Nelly Halim        Wanita   63 Ibu Rumah Tangga
## 19    CUST-019          Mega Pranoto        Wanita   32       Wiraswasta
## 20    CUST-020        Irene Novianto        Wanita   16          Pelajar
## 21    CUST-021      Lestari Fabianto        Wanita   38       Wiraswasta
## 22    CUST-022          Novita Purba        Wanita   52     Professional
## 23    CUST-023       Denny Amiruddin          Pria   34     Professional
## 24    CUST-024         Putri Ginting        Wanita   39       Wiraswasta
## 25    CUST-025        Julia Setiawan        Wanita   29       Wiraswasta
## 26    CUST-026     Christine Winarto        Wanita   55     Professional
## 27    CUST-027         Grace Mulyati        Wanita   35       Wiraswasta
## 28    CUST-028         Adeline Huang        Wanita   40 Ibu Rumah Tangga
## 29    CUST-029          Tia Hartanti        Wanita   56     Professional
## 30    CUST-030        Rosita Saragih        Wanita   46 Ibu Rumah Tangga
## 31    CUST-031         Eviana Handry        Wanita   19        Mahasiswa
## 32    CUST-032       Chintya Winarni        Wanita   47       Wiraswasta
## 33    CUST-033       Cecilia Kusnadi        Wanita   19        Mahasiswa
## 34    CUST-034        Deasy Arisandi        Wanita   21       Wiraswasta
## 35    CUST-035               Ida Ayu        Wanita   39     Professional
## 36    CUST-036        Ni Made Suasti        Wanita   30       Wiraswasta
## 37    CUST-037      Felicia Tandiono        Wanita   25     Professional
## 38    CUST-038          Agatha Salim        Wanita   46       Wiraswasta
## 39    CUST-039          Gina Hidayat        Wanita   20     Professional
## 40    CUST-040        Irene Darmawan        Wanita   14          Pelajar
## 41    CUST-041      Shinta Aritonang        Wanita   24 Ibu Rumah Tangga
## 42    CUST-042          Yuliana Wati        Wanita   26       Wiraswasta
## 43    CUST-043          Yenna Sumadi        Wanita   31     Professional
## 44    CUST-044                  Anna        Wanita   18       Wiraswasta
## 45    CUST-045        Rismawati Juni        Wanita   22     Professional
## 46    CUST-046          Elfira Surya        Wanita   25       Wiraswasta
## 47    CUST-047           Mira Kurnia        Wanita   55 Ibu Rumah Tangga
## 48    CUST-048      Maria Hutagalung        Wanita   45       Wiraswasta
## 49    CUST-049       Josephine Wahab        Wanita   33 Ibu Rumah Tangga
## 50    CUST-050        Lianna Nugraha        Wanita   55       Wiraswasta
##    Tipe.Residen NilaiBelanjaSetahun Jenis.Kelamin.1 Profesi.1 Tipe.Residen.1
## 1        Sector             9497927               1         5              2
## 2       Cluster             2722700               2         3              1
## 3       Cluster             5286429               1         4              1
## 4       Cluster             5204498               1         4              1
## 5       Cluster            10615206               2         5              1
## 6       Cluster             5215541               2         4              1
## 7        Sector             9837260               1         5              2
## 8       Cluster             5223569               1         4              1
## 9        Sector             5993218               2         4              2
## 10      Cluster             5257448               1         4              1
## 11       Sector             5987367               2         4              2
## 12       Sector             5941914               2         4              2
## 13      Cluster             9333168               2         5              1
## 14      Cluster             9471615               1         5              1
## 15      Cluster            10365668               2         5              1
## 16      Cluster             5262521               1         4              1
## 17      Cluster             5677762               2         1              1
## 18      Cluster             5340690               2         1              1
## 19      Cluster            10884508               2         5              1
## 20       Sector             2896845               2         3              2
## 21      Cluster             9222070               2         5              1
## 22      Cluster             5298157               2         4              1
## 23      Cluster             5239290               1         4              1
## 24      Cluster            10259572               2         5              1
## 25       Sector            10721998               2         5              2
## 26      Cluster             5269392               2         4              1
## 27      Cluster             9114159               2         5              1
## 28      Cluster             6631680               2         1              1
## 29      Cluster             5271845               2         4              1
## 30       Sector             5020976               2         1              2
## 31      Cluster             3042773               2         2              1
## 32       Sector            10663179               2         5              2
## 33      Cluster             3047926               2         2              1
## 34       Sector             9759822               2         5              2
## 35       Sector             5962575               2         4              2
## 36      Cluster             9678994               2         5              1
## 37       Sector             5972787               2         4              2
## 38       Sector            10477127               2         5              2
## 39      Cluster             5257775               2         4              1
## 40       Sector             2861855               2         3              2
## 41      Cluster             6820976               2         1              1
## 42      Cluster             9880607               2         5              1
## 43      Cluster             5268410               2         4              1
## 44      Cluster             9339737               2         5              1
## 45      Cluster             5211041               2         4              1
## 46       Sector            10099807               2         5              2
## 47      Cluster             6130724               2         1              1
## 48       Sector            10390732               2         5              2
## 49       Sector             4992585               2         1              2
## 50       Sector            10569316               2         5              2
pelanggan2$NilaiBelanjaSetahun <- pelanggan$NilaiBelanjaSetahun / 1000000
pelanggan2
##    Customer_ID        Nama.Pelanggan Jenis.Kelamin Umur          Profesi
## 1     CUST-001          Budi Anggara          Pria   58       Wiraswasta
## 2     CUST-002      Shirley Ratuwati        Wanita   14          Pelajar
## 3     CUST-003          Agus Cahyono          Pria   48     Professional
## 4     CUST-004      Antonius Winarta          Pria   53     Professional
## 5     CUST-005   Ibu Sri Wahyuni, IR        Wanita   41       Wiraswasta
## 6     CUST-006       Rosalina Kurnia        Wanita   24     Professional
## 7     CUST-007         Cahyono, Agus          Pria   64       Wiraswasta
## 8     CUST-008        Danang Santosa          Pria   52     Professional
## 9     CUST-009 Elisabeth Suryadinata        Wanita   29     Professional
## 10    CUST-010        Mario Setiawan          Pria   33     Professional
## 11    CUST-011        Maria Suryawan        Wanita   50     Professional
## 12    CUST-012       Erliana Widjaja        Wanita   49     Professional
## 13    CUST-013          Cahaya Putri        Wanita   64       Wiraswasta
## 14    CUST-014        Mario Setiawan          Pria   60       Wiraswasta
## 15    CUST-015      Shirley Ratuwati        Wanita   20       Wiraswasta
## 16    CUST-016          Bambang Rudi          Pria   35     Professional
## 17    CUST-017             Yuni Sari        Wanita   32 Ibu Rumah Tangga
## 18    CUST-018           Nelly Halim        Wanita   63 Ibu Rumah Tangga
## 19    CUST-019          Mega Pranoto        Wanita   32       Wiraswasta
## 20    CUST-020        Irene Novianto        Wanita   16          Pelajar
## 21    CUST-021      Lestari Fabianto        Wanita   38       Wiraswasta
## 22    CUST-022          Novita Purba        Wanita   52     Professional
## 23    CUST-023       Denny Amiruddin          Pria   34     Professional
## 24    CUST-024         Putri Ginting        Wanita   39       Wiraswasta
## 25    CUST-025        Julia Setiawan        Wanita   29       Wiraswasta
## 26    CUST-026     Christine Winarto        Wanita   55     Professional
## 27    CUST-027         Grace Mulyati        Wanita   35       Wiraswasta
## 28    CUST-028         Adeline Huang        Wanita   40 Ibu Rumah Tangga
## 29    CUST-029          Tia Hartanti        Wanita   56     Professional
## 30    CUST-030        Rosita Saragih        Wanita   46 Ibu Rumah Tangga
## 31    CUST-031         Eviana Handry        Wanita   19        Mahasiswa
## 32    CUST-032       Chintya Winarni        Wanita   47       Wiraswasta
## 33    CUST-033       Cecilia Kusnadi        Wanita   19        Mahasiswa
## 34    CUST-034        Deasy Arisandi        Wanita   21       Wiraswasta
## 35    CUST-035               Ida Ayu        Wanita   39     Professional
## 36    CUST-036        Ni Made Suasti        Wanita   30       Wiraswasta
## 37    CUST-037      Felicia Tandiono        Wanita   25     Professional
## 38    CUST-038          Agatha Salim        Wanita   46       Wiraswasta
## 39    CUST-039          Gina Hidayat        Wanita   20     Professional
## 40    CUST-040        Irene Darmawan        Wanita   14          Pelajar
## 41    CUST-041      Shinta Aritonang        Wanita   24 Ibu Rumah Tangga
## 42    CUST-042          Yuliana Wati        Wanita   26       Wiraswasta
## 43    CUST-043          Yenna Sumadi        Wanita   31     Professional
## 44    CUST-044                  Anna        Wanita   18       Wiraswasta
## 45    CUST-045        Rismawati Juni        Wanita   22     Professional
## 46    CUST-046          Elfira Surya        Wanita   25       Wiraswasta
## 47    CUST-047           Mira Kurnia        Wanita   55 Ibu Rumah Tangga
## 48    CUST-048      Maria Hutagalung        Wanita   45       Wiraswasta
## 49    CUST-049       Josephine Wahab        Wanita   33 Ibu Rumah Tangga
## 50    CUST-050        Lianna Nugraha        Wanita   55       Wiraswasta
##    Tipe.Residen NilaiBelanjaSetahun Jenis.Kelamin.1 Profesi.1 Tipe.Residen.1
## 1        Sector            9.497927               1         5              2
## 2       Cluster            2.722700               2         3              1
## 3       Cluster            5.286429               1         4              1
## 4       Cluster            5.204498               1         4              1
## 5       Cluster           10.615206               2         5              1
## 6       Cluster            5.215541               2         4              1
## 7        Sector            9.837260               1         5              2
## 8       Cluster            5.223569               1         4              1
## 9        Sector            5.993218               2         4              2
## 10      Cluster            5.257448               1         4              1
## 11       Sector            5.987367               2         4              2
## 12       Sector            5.941914               2         4              2
## 13      Cluster            9.333168               2         5              1
## 14      Cluster            9.471615               1         5              1
## 15      Cluster           10.365668               2         5              1
## 16      Cluster            5.262521               1         4              1
## 17      Cluster            5.677762               2         1              1
## 18      Cluster            5.340690               2         1              1
## 19      Cluster           10.884508               2         5              1
## 20       Sector            2.896845               2         3              2
## 21      Cluster            9.222070               2         5              1
## 22      Cluster            5.298157               2         4              1
## 23      Cluster            5.239290               1         4              1
## 24      Cluster           10.259572               2         5              1
## 25       Sector           10.721998               2         5              2
## 26      Cluster            5.269392               2         4              1
## 27      Cluster            9.114159               2         5              1
## 28      Cluster            6.631680               2         1              1
## 29      Cluster            5.271845               2         4              1
## 30       Sector            5.020976               2         1              2
## 31      Cluster            3.042773               2         2              1
## 32       Sector           10.663179               2         5              2
## 33      Cluster            3.047926               2         2              1
## 34       Sector            9.759822               2         5              2
## 35       Sector            5.962575               2         4              2
## 36      Cluster            9.678994               2         5              1
## 37       Sector            5.972787               2         4              2
## 38       Sector           10.477127               2         5              2
## 39      Cluster            5.257775               2         4              1
## 40       Sector            2.861855               2         3              2
## 41      Cluster            6.820976               2         1              1
## 42      Cluster            9.880607               2         5              1
## 43      Cluster            5.268410               2         4              1
## 44      Cluster            9.339737               2         5              1
## 45      Cluster            5.211041               2         4              1
## 46       Sector           10.099807               2         5              2
## 47      Cluster            6.130724               2         1              1
## 48       Sector           10.390732               2         5              2
## 49       Sector            4.992585               2         1              2
## 50       Sector           10.569316               2         5              2
Profesi <- unique(pelanggan2[c("Profesi","Profesi.1")])
Jenis.Kelamin <- unique(pelanggan2[c("Jenis.Kelamin","Jenis.Kelamin.1")])
Tipe.Residen <- unique(pelanggan2[c("Tipe.Residen","Tipe.Residen.1")])
Profesi
##             Profesi Profesi.1
## 1        Wiraswasta         5
## 2           Pelajar         3
## 3      Professional         4
## 17 Ibu Rumah Tangga         1
## 31        Mahasiswa         2
Jenis.Kelamin
##   Jenis.Kelamin Jenis.Kelamin.1
## 1          Pria               1
## 2        Wanita               2
Tipe.Residen
##   Tipe.Residen Tipe.Residen.1
## 1       Sector              2
## 2      Cluster              1

#Clustering Data

set.seed(100)
field_yang_digunakan = c("Jenis.Kelamin.1", "Umur", "Profesi.1", "Tipe.Residen.1","NilaiBelanjaSetahun")
segmentasi <- kmeans(x=pelanggan2[field_yang_digunakan], centers=5, nstart=25)
segmentasi
## K-means clustering with 5 clusters of sizes 5, 12, 14, 9, 10
## 
## Cluster means:
##   Jenis.Kelamin.1     Umur Profesi.1 Tipe.Residen.1 NilaiBelanjaSetahun
## 1            1.40 61.80000  4.200000       1.400000            8.696132
## 2            1.75 31.58333  3.916667       1.250000            7.330958
## 3            2.00 20.07143  3.571429       1.357143            5.901089
## 4            2.00 42.33333  4.000000       1.555556            8.804791
## 5            1.70 52.50000  3.800000       1.300000            6.018321
## 
## Clustering vector:
##  [1] 1 3 5 5 4 3 1 5 2 2 5 5 1 1 3 2 2 1 2 3 4 5 2 4 2 5 2 4 5 4 3 4 3 3 4 2 3 4
## [39] 3 3 3 2 2 3 3 3 5 4 2 5
## 
## Within cluster sum of squares by cluster:
## [1]  58.21123 174.85164 316.73367 171.67372 108.49735
##  (between_SS / total_SS =  92.4 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
pelanggan2$cluster <- segmentasi$cluster
str(pelanggan2)
## 'data.frame':    50 obs. of  11 variables:
##  $ Customer_ID        : chr  "CUST-001" "CUST-002" "CUST-003" "CUST-004" ...
##  $ Nama.Pelanggan     : chr  "Budi Anggara" "Shirley Ratuwati" "Agus Cahyono" "Antonius Winarta" ...
##  $ Jenis.Kelamin      : chr  "Pria" "Wanita" "Pria" "Pria" ...
##  $ Umur               : int  58 14 48 53 41 24 64 52 29 33 ...
##  $ Profesi            : chr  "Wiraswasta" "Pelajar" "Professional" "Professional" ...
##  $ Tipe.Residen       : chr  "Sector" "Cluster" "Cluster" "Cluster" ...
##  $ NilaiBelanjaSetahun: num  9.5 2.72 5.29 5.2 10.62 ...
##  $ Jenis.Kelamin.1    : int  1 2 1 1 2 2 1 1 2 1 ...
##  $ Profesi.1          : int  5 3 4 4 5 4 5 4 4 4 ...
##  $ Tipe.Residen.1     : int  2 1 1 1 1 1 2 1 2 1 ...
##  $ cluster            : int  1 3 5 5 4 3 1 5 2 2 ...
which(pelanggan2$cluster == 1)
## [1]  1  7 13 14 18

#Melihat Data pada Cluster ke-N

pelanggan[which(pelanggan2$cluster == 1),]
##    Customer_ID Nama.Pelanggan Jenis.Kelamin Umur          Profesi Tipe.Residen
## 1     CUST-001   Budi Anggara          Pria   58       Wiraswasta       Sector
## 7     CUST-007  Cahyono, Agus          Pria   64       Wiraswasta       Sector
## 13    CUST-013   Cahaya Putri        Wanita   64       Wiraswasta      Cluster
## 14    CUST-014 Mario Setiawan          Pria   60       Wiraswasta      Cluster
## 18    CUST-018    Nelly Halim        Wanita   63 Ibu Rumah Tangga      Cluster
##    NilaiBelanjaSetahun
## 1              9497927
## 7              9837260
## 13             9333168
## 14             9471615
## 18             5340690
pelanggan[which(pelanggan2$cluster == 2),]
##    Customer_ID        Nama.Pelanggan Jenis.Kelamin Umur          Profesi
## 9     CUST-009 Elisabeth Suryadinata        Wanita   29     Professional
## 10    CUST-010        Mario Setiawan          Pria   33     Professional
## 16    CUST-016          Bambang Rudi          Pria   35     Professional
## 17    CUST-017             Yuni Sari        Wanita   32 Ibu Rumah Tangga
## 19    CUST-019          Mega Pranoto        Wanita   32       Wiraswasta
## 23    CUST-023       Denny Amiruddin          Pria   34     Professional
## 25    CUST-025        Julia Setiawan        Wanita   29       Wiraswasta
## 27    CUST-027         Grace Mulyati        Wanita   35       Wiraswasta
## 36    CUST-036        Ni Made Suasti        Wanita   30       Wiraswasta
## 42    CUST-042          Yuliana Wati        Wanita   26       Wiraswasta
## 43    CUST-043          Yenna Sumadi        Wanita   31     Professional
## 49    CUST-049       Josephine Wahab        Wanita   33 Ibu Rumah Tangga
##    Tipe.Residen NilaiBelanjaSetahun
## 9        Sector             5993218
## 10      Cluster             5257448
## 16      Cluster             5262521
## 17      Cluster             5677762
## 19      Cluster            10884508
## 23      Cluster             5239290
## 25       Sector            10721998
## 27      Cluster             9114159
## 36      Cluster             9678994
## 42      Cluster             9880607
## 43      Cluster             5268410
## 49       Sector             4992585
pelanggan[which(pelanggan2$cluster == 3),]
##    Customer_ID   Nama.Pelanggan Jenis.Kelamin Umur          Profesi
## 2     CUST-002 Shirley Ratuwati        Wanita   14          Pelajar
## 6     CUST-006  Rosalina Kurnia        Wanita   24     Professional
## 15    CUST-015 Shirley Ratuwati        Wanita   20       Wiraswasta
## 20    CUST-020   Irene Novianto        Wanita   16          Pelajar
## 31    CUST-031    Eviana Handry        Wanita   19        Mahasiswa
## 33    CUST-033  Cecilia Kusnadi        Wanita   19        Mahasiswa
## 34    CUST-034   Deasy Arisandi        Wanita   21       Wiraswasta
## 37    CUST-037 Felicia Tandiono        Wanita   25     Professional
## 39    CUST-039     Gina Hidayat        Wanita   20     Professional
## 40    CUST-040   Irene Darmawan        Wanita   14          Pelajar
## 41    CUST-041 Shinta Aritonang        Wanita   24 Ibu Rumah Tangga
## 44    CUST-044             Anna        Wanita   18       Wiraswasta
## 45    CUST-045   Rismawati Juni        Wanita   22     Professional
## 46    CUST-046     Elfira Surya        Wanita   25       Wiraswasta
##    Tipe.Residen NilaiBelanjaSetahun
## 2       Cluster             2722700
## 6       Cluster             5215541
## 15      Cluster            10365668
## 20       Sector             2896845
## 31      Cluster             3042773
## 33      Cluster             3047926
## 34       Sector             9759822
## 37       Sector             5972787
## 39      Cluster             5257775
## 40       Sector             2861855
## 41      Cluster             6820976
## 44      Cluster             9339737
## 45      Cluster             5211041
## 46       Sector            10099807
pelanggan[which(pelanggan2$cluster == 4),]
##    Customer_ID      Nama.Pelanggan Jenis.Kelamin Umur          Profesi
## 5     CUST-005 Ibu Sri Wahyuni, IR        Wanita   41       Wiraswasta
## 21    CUST-021    Lestari Fabianto        Wanita   38       Wiraswasta
## 24    CUST-024       Putri Ginting        Wanita   39       Wiraswasta
## 28    CUST-028       Adeline Huang        Wanita   40 Ibu Rumah Tangga
## 30    CUST-030      Rosita Saragih        Wanita   46 Ibu Rumah Tangga
## 32    CUST-032     Chintya Winarni        Wanita   47       Wiraswasta
## 35    CUST-035             Ida Ayu        Wanita   39     Professional
## 38    CUST-038        Agatha Salim        Wanita   46       Wiraswasta
## 48    CUST-048    Maria Hutagalung        Wanita   45       Wiraswasta
##    Tipe.Residen NilaiBelanjaSetahun
## 5       Cluster            10615206
## 21      Cluster             9222070
## 24      Cluster            10259572
## 28      Cluster             6631680
## 30       Sector             5020976
## 32       Sector            10663179
## 35       Sector             5962575
## 38       Sector            10477127
## 48       Sector            10390732
pelanggan[which(pelanggan2$cluster == 5),]
##    Customer_ID    Nama.Pelanggan Jenis.Kelamin Umur          Profesi
## 3     CUST-003      Agus Cahyono          Pria   48     Professional
## 4     CUST-004  Antonius Winarta          Pria   53     Professional
## 8     CUST-008    Danang Santosa          Pria   52     Professional
## 11    CUST-011    Maria Suryawan        Wanita   50     Professional
## 12    CUST-012   Erliana Widjaja        Wanita   49     Professional
## 22    CUST-022      Novita Purba        Wanita   52     Professional
## 26    CUST-026 Christine Winarto        Wanita   55     Professional
## 29    CUST-029      Tia Hartanti        Wanita   56     Professional
## 47    CUST-047       Mira Kurnia        Wanita   55 Ibu Rumah Tangga
## 50    CUST-050    Lianna Nugraha        Wanita   55       Wiraswasta
##    Tipe.Residen NilaiBelanjaSetahun
## 3       Cluster             5286429
## 4       Cluster             5204498
## 8       Cluster             5223569
## 11       Sector             5987367
## 12       Sector             5941914
## 22      Cluster             5298157
## 26      Cluster             5269392
## 29      Cluster             5271845
## 47      Cluster             6130724
## 50       Sector            10569316

#Analisis Hasil Cluster Means

segmentasi$centers
##   Jenis.Kelamin.1     Umur Profesi.1 Tipe.Residen.1 NilaiBelanjaSetahun
## 1            1.40 61.80000  4.200000       1.400000            8.696132
## 2            1.75 31.58333  3.916667       1.250000            7.330958
## 3            2.00 20.07143  3.571429       1.357143            5.901089
## 4            2.00 42.33333  4.000000       1.555556            8.804791
## 5            1.70 52.50000  3.800000       1.300000            6.018321
segmentasi$size
## [1]  5 12 14  9 10

#Analisis Hasil Sum of Squares

segmentasi$withinss
## [1]  58.21123 174.85164 316.73367 171.67372 108.49735

#Menentukan Jumlah Cluster Terbaik

sse <- sapply(1:10, function(param_k) {kmeans(pelanggan2[field_yang_digunakan], param_k, nstart=25)$tot.withinss})
sse
##  [1] 10990.9740  3016.5612  1550.8725  1064.4187   829.9676   625.1462
##  [7]   508.1568   431.6977   374.1095   317.9424
library(ggplot2)
jumlah_cluster_max <- 10
ssdata = data.frame(cluster=c(1:jumlah_cluster_max),sse)
ggplot(ssdata, aes(x=cluster,y=sse)) +
 geom_line(color="red") + geom_point() +
 ylab("Within Cluster Sum of Squares") + xlab("Jumlah Cluster") +
 geom_text(aes(label=format(round(sse, 2), nsmall = 2)),hjust=-0.2, vjust=-0.5) +
 scale_x_discrete(limits=c(1:jumlah_cluster_max))
## Warning in scale_x_discrete(limits = c(1:jumlah_cluster_max)): Continuous limits supplied to discrete scale.
## ℹ Did you mean `limits = factor(...)` or `scale_*_continuous()`?

#Menamakan Segmen

segmentasi$centers
##   Jenis.Kelamin.1     Umur Profesi.1 Tipe.Residen.1 NilaiBelanjaSetahun
## 1            1.40 61.80000  4.200000       1.400000            8.696132
## 2            1.75 31.58333  3.916667       1.250000            7.330958
## 3            2.00 20.07143  3.571429       1.357143            5.901089
## 4            2.00 42.33333  4.000000       1.555556            8.804791
## 5            1.70 52.50000  3.800000       1.300000            6.018321