library(readxl)
saham <- read_excel("D:/Matana/Semester 5/ANALISIS MULTIVARIAT/saham saham/rangkuman saham.xlsx")
## New names:
## • `` -> `...1`
saham
## # A tibble: 10 × 40
##    ...1  `45293` `45294` `45295` `45296` `45299` `45300` `45301` `45302` `45303`
##    <chr>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 BBRI    5001.   4935.   5023.   5067.   4957.   5023.   5023.   5067.   5155.
##  2 BMRI    5294.   5273.   5489.   5554.   5532.   5510.   5554.   5575.   5683.
##  3 BBCA    8893.   8823.   8941.   9035.   9035.   9082.   9011.   9035.   9153.
##  4 TLKM    3486.   3459.   3468.   3398.   3398.   3459.   3494.   3486.   3503.
##  5 BBNI    4708.   4686.   4905.   4883.   4883.   4949.   4905.   4905.   4905.
##  6 ASII    4720.   4637.   4720.   4658.   4617.   4617.   4575.   4596.   4637.
##  7 ICBP   10199.  10175.  10582.  10271.  10295.  10559.  10870.  10654.  10702.
##  8 UNVR    3283.   3265.   3255.   3255.   3218.   3172.   3154.   3154.   3209.
##  9 INDF    5961.   5915.   5961.   5961.   5915.   5892.   5869.   5869.   5892.
## 10 KLBF    1551.   1536.   1522.   1512.   1522.   1522.   1512.   1527.   1527.
## # ℹ 30 more variables: `45306` <dbl>, `45307` <dbl>, `45308` <dbl>,
## #   `45309` <dbl>, `45310` <dbl>, `45313` <dbl>, `45314` <dbl>, `45315` <dbl>,
## #   `45316` <dbl>, `45317` <dbl>, `45320` <dbl>, `45321` <dbl>, `45322` <dbl>,
## #   `45323` <dbl>, `45324` <dbl>, `45327` <dbl>, `45328` <dbl>, `45329` <dbl>,
## #   `45334` <dbl>, `45335` <dbl>, `45337` <dbl>, `45338` <dbl>, `45341` <dbl>,
## #   `45342` <dbl>, `45343` <dbl>, `45344` <dbl>, `45345` <dbl>, `45348` <dbl>,
## #   `45349` <dbl>, `45350` <dbl>
data_cluster <- saham[, -1]
set.seed(123)  # biar hasilnya konsisten
k2 <- kmeans(data_cluster, centers = 2)
k2$cluster
##  [1] 2 2 1 2 2 2 1 2 2 2
k2$centers
##      45293    45294    45295    45296    45299    45300    45301    45302
## 1 9546.382 9499.027 9761.511 9653.065 9665.037 9820.309 9940.549 9844.604
## 2 4250.572 4213.362 4293.031 4286.208 4255.359 4268.106 4260.829 4272.328
##      45303    45306    45307    45308    45309     45310    45313    45314
## 1 9927.520 9915.373 9879.636 9927.168 9963.610 10071.703 9999.876 9976.109
## 2 4314.034 4300.500 4296.058 4255.761 4240.851  4235.941 4207.891 4179.102
##      45315    45316    45317    45320    45321     45322     45323     45324
## 1 9952.697 9940.902 9894.075 9964.492 9927.873 10144.059 10119.059 10119.059
## 2 4178.147 4128.371 4124.083 4151.366 4203.391  4214.464  4212.277  4274.974
##       45327     45328     45329     45334     45335     45337     45338
## 1 10012.200 10059.732 10047.232 10202.152 10035.084 10225.742 10177.152
## 2  4256.077  4286.344  4301.856  4322.951  4270.501  4332.375  4352.006
##      45341     45342     45343     45344     45345     45348     45349
## 1 10201.62 10260.421 10200.918 10213.594 10166.062 10154.267 10213.594
## 2  4320.16  4379.467  4385.921  4368.803  4325.058  4305.761  4304.655
##       45350
## 1 10272.568
## 2  4337.937
set.seed(123)
k3 <- kmeans(data_cluster, centers = 3)
k3$cluster
##  [1] 3 3 1 2 3 3 1 2 3 2
k3$centers
##      45293    45294    45295    45296    45299    45300    45301    45302
## 1 9546.382 9499.027 9761.511 9653.065 9665.037 9820.309 9940.549 9844.604
## 2 2773.221 2753.533 2748.576 2722.089 2712.949 2717.919 2720.210 2722.084
## 3 5136.983 5089.259 5219.703 5224.680 5180.804 5198.218 5185.200 5202.475
##      45303    45306    45307    45308    45309     45310    45313    45314
## 1 9927.520 9915.373 9879.636 9927.168 9963.610 10071.703 9999.876 9976.109
## 2 2746.406 2729.567 2724.718 2689.553 2671.739  2642.353 2617.007 2626.768
## 3 5254.610 5243.060 5238.862 5195.486 5182.318  5192.094 5162.422 5110.502
##      45315    45316    45317    45320    45321     45322     45323     45324
## 1 9952.697 9940.902 9894.075 9964.492 9927.873 10144.059 10119.059 10119.059
## 2 2651.521 2635.720 2624.552 2599.053 2611.555  2590.658  2613.955  2630.547
## 3 5094.123 5023.962 5023.802 5082.754 5158.492  5188.748  5171.271  5261.631
##       45327     45328    45329     45334     45335     45337     45338
## 1 10012.200 10059.732 10047.23 10202.152 10035.084 10225.742 10177.152
## 2  2638.479  2653.893  2645.70  2540.434  2494.425  2541.085  2557.685
## 3  5226.636  5265.814  5295.55  5392.462  5336.147  5407.149  5428.598
##       45341     45342     45343     45344     45345     45348     45349
## 1 10201.624 10260.421 10200.918 10213.594 10166.062 10154.267 10213.594
## 2  2543.650  2555.451  2521.945  2503.389  2493.862  2458.526  2435.464
## 3  5386.065  5473.876  5504.308  5488.052  5423.776  5414.102  5426.170
##       45350
## 1 10272.568
## 2  2458.531
## 3  5465.581
print(k2)
## K-means clustering with 2 clusters of sizes 2, 8
## 
## Cluster means:
##      45293    45294    45295    45296    45299    45300    45301    45302
## 1 9546.382 9499.027 9761.511 9653.065 9665.037 9820.309 9940.549 9844.604
## 2 4250.572 4213.362 4293.031 4286.208 4255.359 4268.106 4260.829 4272.328
##      45303    45306    45307    45308    45309     45310    45313    45314
## 1 9927.520 9915.373 9879.636 9927.168 9963.610 10071.703 9999.876 9976.109
## 2 4314.034 4300.500 4296.058 4255.761 4240.851  4235.941 4207.891 4179.102
##      45315    45316    45317    45320    45321     45322     45323     45324
## 1 9952.697 9940.902 9894.075 9964.492 9927.873 10144.059 10119.059 10119.059
## 2 4178.147 4128.371 4124.083 4151.366 4203.391  4214.464  4212.277  4274.974
##       45327     45328     45329     45334     45335     45337     45338
## 1 10012.200 10059.732 10047.232 10202.152 10035.084 10225.742 10177.152
## 2  4256.077  4286.344  4301.856  4322.951  4270.501  4332.375  4352.006
##      45341     45342     45343     45344     45345     45348     45349
## 1 10201.62 10260.421 10200.918 10213.594 10166.062 10154.267 10213.594
## 2  4320.16  4379.467  4385.921  4368.803  4325.058  4305.761  4304.655
##       45350
## 1 10272.568
## 2  4337.937
## 
## Clustering vector:
##  [1] 2 2 1 2 2 2 1 2 2 2
## 
## Within cluster sum of squares by cluster:
## [1]  59110722 662495984
##  (between_SS / total_SS =  74.0 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
print(k3)
## K-means clustering with 3 clusters of sizes 2, 3, 5
## 
## Cluster means:
##      45293    45294    45295    45296    45299    45300    45301    45302
## 1 9546.382 9499.027 9761.511 9653.065 9665.037 9820.309 9940.549 9844.604
## 2 2773.221 2753.533 2748.576 2722.089 2712.949 2717.919 2720.210 2722.084
## 3 5136.983 5089.259 5219.703 5224.680 5180.804 5198.218 5185.200 5202.475
##      45303    45306    45307    45308    45309     45310    45313    45314
## 1 9927.520 9915.373 9879.636 9927.168 9963.610 10071.703 9999.876 9976.109
## 2 2746.406 2729.567 2724.718 2689.553 2671.739  2642.353 2617.007 2626.768
## 3 5254.610 5243.060 5238.862 5195.486 5182.318  5192.094 5162.422 5110.502
##      45315    45316    45317    45320    45321     45322     45323     45324
## 1 9952.697 9940.902 9894.075 9964.492 9927.873 10144.059 10119.059 10119.059
## 2 2651.521 2635.720 2624.552 2599.053 2611.555  2590.658  2613.955  2630.547
## 3 5094.123 5023.962 5023.802 5082.754 5158.492  5188.748  5171.271  5261.631
##       45327     45328    45329     45334     45335     45337     45338
## 1 10012.200 10059.732 10047.23 10202.152 10035.084 10225.742 10177.152
## 2  2638.479  2653.893  2645.70  2540.434  2494.425  2541.085  2557.685
## 3  5226.636  5265.814  5295.55  5392.462  5336.147  5407.149  5428.598
##       45341     45342     45343     45344     45345     45348     45349
## 1 10201.624 10260.421 10200.918 10213.594 10166.062 10154.267 10213.594
## 2  2543.650  2555.451  2521.945  2503.389  2493.862  2458.526  2435.464
## 3  5386.065  5473.876  5504.308  5488.052  5423.776  5414.102  5426.170
##       45350
## 1 10272.568
## 2  2458.531
## 3  5465.581
## 
## Clustering vector:
##  [1] 3 3 1 2 3 3 1 2 3 2
## 
## Within cluster sum of squares by cluster:
## [1] 59110722 86838233 66120124
##  (between_SS / total_SS =  92.3 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
library(cluster)
sil_c3 <- silhouette(k3$cluster, dist(saham))
## Warning in dist(saham): NAs introduced by coercion
avg_sil <- mean(sil_c3[,"sil_width"])
cat("average  silhouette width:", avg_sil)
## average  silhouette width: 0.5895029
library(cluster)
sil_c2 <- silhouette(k2$cluster, dist(saham))
## Warning in dist(saham): NAs introduced by coercion
avg_sil <- mean(sil_c2[,"sil_width"])
cat("average  silhouette width:", avg_sil)
## average  silhouette width: 0.6724336
library(factoextra)
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
fviz_cluster(k2, data = data_cluster)

library(ppclust)
fcm_c3 <- fcm(x=data_cluster, centers = 3)
summary(fcm_c3)
## Summary for 'fcm_c3'
## 
## Number of data objects:  10 
## 
## Number of clusters:  3 
## 
## Crisp clustering vector:
##  [1] 2 2 1 3 2 2 1 3 2 3
## 
## Initial cluster prototypes:
##               45293     45294     45295     45296     45299     45300     45301
## Cluster 1 10199.423 10175.480 10582.500 10271.250 10295.192 10558.558 10869.808
## Cluster 2  5294.356  5272.747  5488.843  5553.672  5532.062  5510.453  5553.672
## Cluster 3  3283.235  3264.738  3255.489  3255.489  3218.495  3172.252  3153.755
##               45302     45303     45306     45307     45308     45309    45310
## Cluster 1 10654.327 10702.211 10654.327 10606.442 10654.327 10797.980 11061.35
## Cluster 2  5575.282  5683.329  5618.500  5640.110  5640.110  5640.110  5640.11
## Cluster 3  3153.755  3209.247  3172.252  3153.755  3070.518  3052.021  2996.53
##               45313     45314     45315     45316     45317     45320     45321
## Cluster 1 10917.692 10893.749 10917.692 10917.692 10965.576 10917.692 10750.097
## Cluster 2  5618.500  5618.500  5532.062  5424.014  5445.624  5596.891  5748.158
## Cluster 3  2885.547  2941.039  2959.536  2922.542  2922.542  2848.553  2894.796
##               45322    45323     45324     45327     45328     45329     45334
## Cluster 1 11276.827 11085.29 11085.289 10989.520 11037.403 10941.635 11157.115
## Cluster 2  5748.158  5661.72  5769.768  5899.425  5921.035  6007.474  6137.131
## Cluster 3  2867.050  2867.05  2931.790  2959.536  3005.779  3024.276  2700.576
##               45335     45337     45338     45341     45342     45343     45344
## Cluster 1 10893.749 11157.115 10965.576 11085.289 11061.346 10989.520 11109.230
## Cluster 2  6072.303  6223.570  6223.570  6180.351  6180.351  6266.789  6137.131
## Cluster 3  2589.594  2709.825  2617.339  2589.594  2552.599  2487.860  2515.605
##               45345     45348     45349     45350
## Cluster 1 11061.346 11061.346 11109.230 11109.230
## Cluster 2  6093.912  6093.912  6115.522  6158.741
## Cluster 3  2487.860  2395.374  2423.120  2497.108
## 
## Final cluster prototypes:
##              45293    45294    45295    45296    45299    45300    45301
## Cluster 1 9566.339 9519.878 9787.667 9671.587 9683.974 9843.205 9970.590
## Cluster 2 5135.156 5090.201 5223.821 5233.476 5189.320 5210.069 5199.652
## Cluster 3 2778.420 2758.235 2753.528 2731.738 2719.628 2716.711 2714.209
##              45302    45303    45306    45307    45308    45309     45310
## Cluster 1 9870.196 9951.728 9938.233 9902.067 9949.524 9989.996 10103.910
## Cluster 2 5216.843 5269.809 5261.350 5258.344 5218.265 5205.531  5216.773
## Cluster 3 2717.226 2744.251 2724.810 2717.729 2677.466 2661.054  2628.613
##               45313     45314    45315    45316    45317    45320    45321
## Cluster 1 10029.388 10005.605 9984.025 9972.655 9929.494 9995.322 9953.793
## Cluster 2  5193.737  5143.613 5128.915 5055.115 5051.861 5126.098 5196.528
## Cluster 3  2591.782  2605.611 2629.451 2610.178 2599.254 2564.943 2583.489
##               45322     45323     45324     45327     45328     45329     45334
## Cluster 1 10181.585 10150.257 10150.325 10043.946 10091.478 10075.837 10232.937
## Cluster 2  5228.433  5211.577  5294.942  5267.038  5303.944  5330.632  5437.450
## Cluster 3  2564.181  2581.640  2608.869  2617.006  2636.362  2630.248  2503.574
##               45335     45337     45338     45341     45342     45343     45344
## Cluster 1 10062.320 10255.578 10201.634 10229.703 10285.342 10225.461 10242.222
## Cluster 2  5383.847  5465.583  5484.717  5442.540  5533.727  5561.578  5539.931
## Cluster 3  2450.717  2499.972  2502.731  2485.868  2493.061  2458.531  2448.565
##               45345     45348     45349     45350
## Cluster 1 10194.689 10183.319 10242.134 10298.834
## Cluster 2  5479.678  5471.078  5480.432  5519.450
## Cluster 3  2434.386  2394.085  2380.658  2408.979
## 
## Distance between the final cluster prototypes
##            Cluster 1  Cluster 2
## Cluster 2  875437594           
## Cluster 3 2154415031  284587088
## 
## Difference between the initial and final cluster prototypes
##               45293     45294     45295     45296     45299     45300     45301
## Cluster 1 -633.0834 -655.6024 -794.8331 -599.6633 -611.2188 -715.3522 -899.2173
## Cluster 2 -159.2007 -182.5460 -265.0219 -320.1959 -342.7419 -300.3837 -354.0201
## Cluster 3 -504.8145 -506.5027 -501.9610 -523.7509 -498.8673 -455.5414 -439.5458
##               45302     45303     45306     45307     45308     45309     45310
## Cluster 1 -784.1309 -750.4829 -716.0941 -704.3758 -704.8031 -807.9849 -957.4356
## Cluster 2 -358.4391 -413.5199 -357.1508 -381.7664 -421.8450 -434.5787 -423.3368
## Cluster 3 -436.5288 -464.9959 -447.4420 -436.0261 -393.0521 -390.9675 -367.9171
##               45313     45314     45315     45316      45317     45320
## Cluster 1 -888.3046 -888.1438 -933.6675 -945.0376 -1036.0825 -922.3703
## Cluster 2 -424.7636 -474.8874 -403.1466 -368.8991  -393.7627 -470.7928
## Cluster 3 -293.7649 -335.4279 -330.0846 -312.3639  -323.2879 -283.6103
##               45321      45322     45323     45324     45327     45328
## Cluster 1 -796.3032 -1095.2426 -935.0324 -934.9642 -945.5739 -945.9254
## Cluster 2 -551.6298  -519.7256 -450.1424 -474.8261 -632.3877 -617.0910
## Cluster 3 -311.3073  -302.8696 -285.4106 -322.9213 -342.5293 -369.4163
##               45329     45334     45335     45337     45338     45341
## Cluster 1 -865.7973 -924.1782 -831.4294 -901.5368 -763.9418 -855.5856
## Cluster 2 -676.8412 -699.6811 -688.4556 -757.9872 -738.8528 -737.8108
## Cluster 3 -394.0275 -197.0025 -138.8763 -209.8530 -114.6085 -103.7257
##                45342      45343      45344      45345       45348     45349
## Cluster 1 -776.00398 -764.05880 -867.00816 -866.65667 -878.026822 -867.0963
## Cluster 2 -646.62321 -705.21075 -597.20020 -614.23414 -622.834087 -635.0897
## Cluster 3  -59.53846  -29.32849  -67.04071  -53.47395   -1.289337  -42.4615
##                45350
## Cluster 1 -810.39634
## Cluster 2 -639.29040
## Cluster 3  -88.12887
## 
## Root Mean Squared Deviations (RMSD): 3769.634 
## Mean Absolute Deviation (MAD): 825627.6 
## 
## Membership degrees matrix (top and bottom 5 rows): 
##     Cluster 1  Cluster 2   Cluster 3
## 1 0.000901234 0.99587737 0.003221395
## 2 0.014327175 0.96057669 0.025096139
## 3 0.931616603 0.05081346 0.017569937
## 4 0.015407699 0.20465004 0.779942265
## 5 0.003577185 0.98077840 0.015644420
## ...
##      Cluster 1  Cluster 2  Cluster 3
## 6  0.020959567 0.77243103 0.20660940
## 7  0.967842877 0.02210660 0.01005053
## 8  0.002189302 0.01901970 0.97879100
## 9  0.025716749 0.93649686 0.03778639
## 10 0.015761947 0.07900602 0.90523204
## 
## Descriptive statistics for the membership degrees by clusters
##           Size       Min        Q1      Mean    Median        Q3       Max
## Cluster 1    2 0.9316166 0.9406732 0.9497297 0.9497297 0.9587863 0.9678429
## Cluster 2    5 0.7724310 0.9364969 0.9292321 0.9605767 0.9807784 0.9958774
## Cluster 3    3 0.7799423 0.8425871 0.8879884 0.9052320 0.9420115 0.9787910
## 
## Dunn's Fuzziness Coefficients:
## dunn_coeff normalized 
##  0.8639274  0.7958911 
## 
## Within cluster sum of squares by cluster:
##        1        2        3 
## 59110722 66120124 86838233 
## (between_SS / total_SS =  92.45%) 
## 
## Available components: 
##  [1] "u"          "v"          "v0"         "d"          "x"         
##  [6] "cluster"    "csize"      "sumsqrs"    "k"          "m"         
## [11] "iter"       "best.start" "func.val"   "comp.time"  "inpargs"   
## [16] "algorithm"  "call"
res.fcm3 <- ppclust2(fcm_c3, "kmeans")
factoextra::fviz_cluster(res.fcm3, data_cluster,
                         ellipse.type = "convex",
                         palette = "jco",
                         repel = TRUE)

sil_c3 <- silhouette(k3$cluster, dist(saham))
## Warning in dist(saham): NAs introduced by coercion
avg_sil <- mean(sil_c3[,"sil_width"])
cat("average  silhouette width:", avg_sil)
## average  silhouette width: 0.5895029
sil_c33 <- silhouette(fcm_c3$cluster, dist(saham))
## Warning in dist(saham): NAs introduced by coercion
avg_sil <- mean(sil_c33[,"sil_width"])
cat("average  silhouette width:", avg_sil)
## average  silhouette width: 0.5895029