Kmeans Simple Attitude Rstudio


jalankan yang diberi fontBold:

>library(datasets)
> str(attitude)
'data.frame': 30 obs. of  7 variables:
 $ rating    : num  43 63 71 61 81 43 58 71 72 67 ...
 $ complaints: num  51 64 70 63 78 55 67 75 82 61 ...
 $ privileges: num  30 51 68 45 56 49 42 50 72 45 ...
 $ learning  : num  39 54 69 47 66 44 56 55 67 47 ...
 $ raises    : num  61 63 76 54 71 54 66 70 71 62 ...
 $ critical  : num  92 73 86 84 83 49 68 66 83 80 ...
 $ advance   : num  45 47 48 35 47 34 35 41 31 41 ...

> summary(attitude)
     rating        complaints     privileges       learning         raises         critical        advance   
 Min.   :40.00   Min.   :37.0   Min.   :30.00   Min.   :34.00   Min.   :43.00   Min.   :49.00   Min.   :25.00 
 1st Qu.:58.75   1st Qu.:58.5   1st Qu.:45.00   1st Qu.:47.00   1st Qu.:58.25   1st Qu.:69.25   1st Qu.:35.00 
 Median :65.50   Median :65.0   Median :51.50   Median :56.50   Median :63.50   Median :77.50   Median :41.00 
 Mean   :64.63   Mean   :66.6   Mean   :53.13   Mean   :56.37   Mean   :64.63   Mean   :74.77   Mean   :42.93 
 3rd Qu.:71.75   3rd Qu.:77.0   3rd Qu.:62.50   3rd Qu.:66.75   3rd Qu.:71.00   3rd Qu.:80.00   3rd Qu.:47.75 
 Max.   :85.00   Max.   :90.0   Max.   :83.00   Max.   :75.00   Max.   :88.00   Max.   :92.00   Max.   :72.00 
> attitude
   rating complaints privileges learning raises critical advance
1      43         51         30       39     61       92      45
2      63         64         51       54     63       73      47
3      71         70         68       69     76       86      48
4      61         63         45       47     54       84      35
5      81         78         56       66     71       83      47
6      43         55         49       44     54       49      34
7      58         67         42       56     66       68      35
8      71         75         50       55     70       66      41
9      72         82         72       67     71       83      31
10     67         61         45       47     62       80      41
11     64         53         53       58     58       67      34
12     67         60         47       39     59       74      41
13     69         62         57       42     55       63      25
14     68         83         83       45     59       77      35
15     77         77         54       72     79       77      46
16     81         90         50       72     60       54      36
17     74         85         64       69     79       79      63
18     65         60         65       75     55       80      60
19     65         70         46       57     75       85      46
20     50         58         68       54     64       78      52
21     50         40         33       34     43       64      33
22     64         61         52       62     66       80      41
23     53         66         52       50     63       80      37
24     40         37         42       58     50       57      49
25     63         54         42       48     66       75      33
26     66         77         66       63     88       76      72
27     78         75         58       74     80       78      49
28     48         57         44       45     51       83      38
29     85         85         71       71     77       74      55
30     82         82         39       59     64       78      39

> dat = attitude[,c(3,4)] #jika hanya menampilkan semua baris(observasi/obs) kolom 3 dan 4 
> dat
   privileges learning
1          30       39
2          51       54
3          68       69
4          45       47
5          56       66
6          49       44
7          42       56
8          50       55
9          72       67
10         45       47
11         53       58
12         47       39
13         57       42
14         83       45
15         54       72
16         50       72
17         64       69
18         65       75
19         46       57
20         68       54
21         33       34
22         52       62
23         52       50
24         42       58
25         42       48
26         66       63
27         58       74
28         44       45
29         71       71
30         39       59

> dat1 = attitude[1,]
> dat1
  rating complaints privileges learning raises critical advance
1     43         51         30       39     61       92      45
> plot(dat, main = "% of favourable responses to Learning and Privilege", pch =20, cex =2)
#pch=ketebalan pch/titik
#cex=center radius



> set.seed(7)
> km1 = kmeans(dat, 2, nstart=100)  ## Perform K-Means with 2 clusters
>
> km1
K-means clustering with 2 clusters of sizes 13, 17

Cluster means:
  privileges learning
1   63.61538 66.07692
2   45.11765 48.94118

Clustering vector:
 [1] 2 2 1 2 1 2 2 2 1 2 2 2 2 1 1 1 1 1 2 1 2 1 2 2 2 1 1 2 1 2

Within cluster sum of squares by cluster:
[1] 1920.000 1732.706
 (between_SS / total_SS =  56.2 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss" "betweenss"   
[7] "size"         "iter"         "ifault"      

> plot(dat, col =(km1$cluster +1) , main="K-Means result with 2 clusters", pch=20, cex=2)
> C=km1$cluster
> C
 [1] 2 2 1 2 1 2 2 2 1 2 2 2 2 1 1 1 1 1 2 1 2 1 2 2 2 1 1 2 1 2
> dat
   privileges learning
1          30       39
2          51       54
3          68       69
4          45       47
5          56       66
6          49       44
7          42       56
8          50       55
9          72       67
10         45       47
11         53       58
12         47       39
13         57       42
14         83       45
15         54       72
16         50       72
17         64       69
18         65       75
19         46       57
20         68       54
21         33       34
22         52       62
23         52       50
24         42       58
25         42       48
26         66       63
27         58       74
28         44       45
29         71       71
30         39       59



DISPLAY:
mydata <- dat
wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var))
  for (i in 2:15) wss[i] <- sum(kmeans(mydata,centers=i)$withinss)
plot(1:15, wss, type="b", xlab="Number of Clusters",
     ylab="Within groups sum of squares",
     main="Assessing the Optimal Number of Clusters with the Elbow Method",
     pch=20, cex=2)

Kmeans 6 kriteria:

set.seed(7)
km2 = kmeans(dat, 6, nstart=100)

km2:
K-means clustering with 6 clusters of sizes 4, 2, 8, 6, 8, 2

Cluster means:
  privileges learning
1   54.50000   71.000
2   75.50000   49.500
3   47.62500   45.250
4   67.66667   69.000
5   46.87500   57.375
6   31.50000   36.500

Clustering vector:
 [1] 6 5 4 3 1 3 5 5 4 3 5 3 3 2 1 1 4 4 5 2 6 5 3 5 3 4 1 3 4 5

Within cluster sum of squares by cluster:
[1]  71.0000 153.0000 255.3750 133.3333 244.7500  17.0000
 (between_SS / total_SS =  89.5 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss" "betweenss"   
[7] "size"         "iter"         "ifault"      


Oh Mudahnyaaaaaaaaaaaaaaaaaaaaaaa......

Komentar

Postingan populer dari blog ini

KNN on RStudio Studi Kasus

Data Frame

Pengujian NaiveBayes data External