Chapter 6 Cluster Analysis

Previously, we have investigated two consecutive words using ngrams, associations of any two words, and associations rules for word relationship. Here, we are interested in what words come together as a group or cluster. In doing so, we need to measure the closeness of words. One way to measure it is to first consider the frequency of each word for all professors. Then we get a matrix of data with words on the columns and frequency on the rows. Then we can calculate the distance of a pair of words. Note that pairwise correlation can be used as a closeness or distance matrix.

We first get the frequency of words for each professor using tidytext. Then we convert the data to a document-term matrix using the cast_dtm function in the tidytext package. After that, we change the document term matrix to a regular matrix using as.matrix.

Note that most of the elements of the matrix are 0. The full matrix would have a total of \(1000\times 8,155\) elements and only 229,773 of them are not 0 and 97% of elements are 0.

If the words are very rarely used, then their association would be low. Therefore, here we focus on frequently used words. Particularly, we remove words that have at lease a sparsity of 0.4, meaning for 40% of professors, the words were not used. After this, there are 57 words left in the data. The sparsity is only 22%.

6.1 Hierarchical cluster analysis

We first cluster the words into groups through hierarchical cluster analysis based on the dissimilarity of the words. One way to measure the dissimilarity is to use the Euclidean distance. Imagine that each word is a point in a 1000-dimensional space formed by the 1,000 professors. The number of times of the word used by the professors is the coordinates in the space. Then for any two words, the distance in the space can be calculated as a measure of dissimilarity. A larger distance indicates more dissimilarity of the two words. Overall, a 57 by 57 distance matrix can be formed. Part of the matrix is shown below.

A Euclidean distance is a straight-line distance between two points in a Euclidean space. For example, consider a 3-dimensional space shown in the figure below (the figure was adapted from Wikipedia: https://en.wikipedia.org/wiki/Euclidean_distance). For two points \(p\) and \(q\) with the coordinates \((p_1, p_2, p_3)\) and \((q_1, q_2, q_3)\), respectively. The Euclidean distance between them is

\[ d(p,q) = \sqrt{(p_1-q_1)^2 + (p_2-q_2)^2 + (p_3-q_3)^2}. \]

With the distance matrix, we can then cluster the words. The R function hclust provides several methods for cluster analysis. The different methods generally follow the same procedure. Initially, each word is assumed to its own cluster. Then the algorithm proceeds iteratively, at each stage joining the two most similar clusters and continuing until there is just a single cluster. At each stage distances between clusters are recomputed by the Lance–Williams algorithm according to the particular clustering method being used.

The default method used by hclust is called complete-linkage clustering, also known as farthest neighbor clustering. At the beginning of the clustering process, each element is in a cluster of its own. Then the two clusters separated by the shortest distance are combined. For two clusters, the distance of them is the maximum distance among any pair of elements from the two clusters.
Mathematically, the distance between clusters \(X\) and \(Y\) is defined as \[ D(X,Y)= \max_{x\in X, y\in Y} d(x,y) \]

where \(d(x,y)\) is the distance between an element \(x\) in the cluster \(X\) and an element \(y\) in the cluster \(Y\).

The following code conducts the cluster analysis based on the distance matrix for the words.

It is often easier to visualize the clusters through a plot. Such a plot is called a “dendrogram”, which has a tree structure. For example, the following code draws the cluster dendrogram and then adds a rectangular box around each cluster. From it, we can easily see which words belong to which groups. For example, if we decide to keep 6 clusters, we can see that (1) “lecture”, “easy”, and “test” form their own clusters; (2) “great”, “good”, “help”, “hard”, and “work” belong to one cluster; (3) “study”, “exam”, and “note” belong to one cluster; and (4) the rest of words belong to one cluster. We will discuss how to choose the optimal number of clusters later.

6.2 k-means

k-means clustering is a popular method for clustering. It aims to partition the observations into k clusters in which each observation belongs to the cluster with the nearest mean.

The idea of the k-means is straightforward. Suppose we want to cluster the data in the figure below. Our task is to group the data into two clusters.

In the first step, we can randomly initialize two points (the blue and red circles), called the cluster centroids. Then, we can cluster each data into the closest cluster centroid.

After that, we can then calculate the new cluster centroids based on the elements in them.

Then, we can re-assign each element to the closest centroid again.

The above procedure can be repeated so the cluster centroid will not move any further.

6.2.1 k-means in R

The R function kmeans can be used for k-means clustering. To do it, we need to specify the number of clusters. For example, a k-means clustering with two clusters is conducted using the following code. Note that the function kmeans needs the input of raw data other than a distance matrix.

From the output, we can see that the two clusters have 50 and 7 words, respectively. The cluster membership can be identified using the clustering vector. The means of each cluster are also provided.

kmeans.data <- as.matrix(t(prof.subset))
kfit <- kmeans(kmeans.data, 2)

kfit
## K-means clustering with 2 clusters of sizes 7, 50
## 
## Cluster means:
##          1        2        3    4        5        6        7        8        9
## 1 13.42857 1.857143 49.14286 7.00 28.42857 2.571429 2.857143 1.428571 8.857143
## 2  7.06000 0.920000 17.90000 3.04 10.16000 1.060000 1.320000 0.760000 3.160000
##         10       11       12       13       14       15       16       17
## 1 32.28571 5.571429 4.571429 6.571429 15.28571 2.714286 11.42857 11.85714
## 2 11.58000 2.780000 1.440000 3.400000  4.40000 1.180000  3.70000  5.78000
##         18       19       20       21       22       23       24       25
## 1 6.571429 6.857143 2.285714 3.142857 4.857143 15.28571 6.714286 7.428571
## 2 5.640000 2.700000 0.880000 1.580000 2.560000  5.38000 2.740000 2.100000
##         26       27   28   29       30       31       32       33       34
## 1 15.14286 3.714286 11.0 3.00 3.428571 7.428571 7.571429 5.714286 7.714286
## 2  6.92000 1.660000  4.1 0.84 0.960000 2.300000 3.120000 2.000000 2.640000
##         35       36       37       38        39       40       41       42
## 1 15.57143 4.714286 14.85714 2.857143 0.4285714 13.57143 5.714286 10.14286
## 2  5.82000 1.520000  6.30000 1.140000 0.1400000  4.00000 1.880000  3.58000
##         43   44       45       46       47       48       49    50       51
## 1 1.857143 7.00 3.714286 4.714286 6.142857 3.571429 5.428571 20.00 5.714286
## 2 0.700000 2.56 1.540000 2.900000 2.680000 1.920000 2.720000  6.52 3.240000
##         52       53       54       55       56       57       58       59
## 1 11.57143 3.285714 5.571429 5.142857 16.71429 9.142857 6.285714 19.71429
## 2  3.70000 1.500000 1.540000 1.580000  5.48000 3.180000 2.240000  7.64000
##         60       61       62       63       64       65       66   67       68
## 1 8.857143 11.57143 8.857143 7.571429 7.285714 2.428571 19.28571 8.00 8.285714
## 2 5.220000  2.62000 2.620000 2.360000 2.000000 2.040000  6.86000 3.36 4.020000
##      69       70       71       72       73       74       75   76       77
## 1 17.00 8.571429 1.142857 8.142857 10.28571 14.42857 9.857143 10.0 16.28571
## 2  6.02 4.000000 0.660000 2.040000  2.90000  6.04000 4.780000  3.8  5.02000
##         78       79       80       81       82       83   84       85    86
## 1 17.71429 20.42857 27.42857 4.857143 29.14286 4.285714 19.0 3.714286 15.00
## 2  5.72000  7.24000 10.22000 1.180000  8.16000 2.140000  5.2 1.540000  5.06
##         87       88       89       90       91       92   93       94       95
## 1 2.857143 2.285714 8.285714 22.28571 2.285714 15.57143 2.00 3.428571 22.85714
## 2 1.040000 1.020000 2.440000  7.22000 0.760000  7.08000 1.56 0.760000  7.62000
##    96       97       98       99      100      101      102  103      104
## 1 3.0 7.142857 7.714286 11.42857 3.428571 18.14286 9.285714 6.00 5.571429
## 2 1.2 2.920000 2.860000  3.42000 1.460000  5.94000 3.760000 1.52 1.780000
##        105      106      107      108 109      110      111      112      113
## 1 37.28571 4.857143 13.14286 4.428571 3.0 5.714286 4.142857 3.857143 2.428571
## 2 12.76000 3.860000  4.46000 0.800000 1.1 2.380000 1.960000 1.780000 0.500000
##         114      115      116      117      118      119  120      121      122
## 1 0.5714286 6.428571 9.285714 3.714286 2.428571 23.42857 14.0 22.28571 14.71429
## 2 0.8800000 3.480000 3.440000 1.580000 1.680000  9.08000  4.2 10.28000  5.06000
##        123  124      125      126      127  128      129 130      131 132
## 1 5.428571 3.00 8.285714 15.85714 12.28571 8.00 9.571429 3.0 6.571429 7.0
## 2 1.880000 0.88 4.180000  5.40000  5.62000 3.34 4.620000 1.1 1.400000 2.2
##        133  134      135      136      137      138      139      140      141
## 1 61.28571 2.00 3.142857 2.857143 4.285714 9.142857 12.57143 18.28571 4.714286
## 2 22.60000 0.88 1.580000 1.420000 1.940000 5.800000  4.92000  8.20000 2.000000
##        142      143      144      145      146      147      148      149
## 1 1.714286 29.28571 2.285714 11.14286 18.42857 1.571429 9.285714 2.571429
## 2 1.860000  8.16000 0.820000  3.36000  4.98000 1.100000 2.420000 1.260000
##         150  151      152 153      154      155      156      157  158  159 160
## 1 0.1428571 5.00 2.285714 4.0 5.714286 4.142857 2.571429 5.142857 19.0 3.00 6.0
## 2 0.6200000 1.78 0.880000 1.2 2.500000 1.860000 1.140000 2.220000  8.1 1.76 2.4
##        161      162      163      164   165      166       167      168
## 1 5.142857 27.28571 1.428571 5.285714 24.00 5.714286 0.7142857 7.142857
## 2 1.840000 10.14000 0.500000 2.720000  6.32 1.320000 0.5200000 3.760000
##        169      170      171      172      173      174  175      176      177
## 1 9.857143 25.42857 7.571429 25.28571 5.285714 3.285714 7.00 7.857143 6.142857
## 2 5.640000  8.56000 2.420000  6.86000 1.580000 1.260000 3.82 3.900000 2.580000
##        178      179  180      181      182      183      184      185      186
## 1 11.85714 2.857143 2.00 6.571429 7.285714 3.285714 8.285714 9.714286 5.142857
## 2  2.48000 1.260000 2.08 2.340000 3.760000 1.740000 2.680000 3.520000 1.280000
##        187      188      189      190      191      192      193      194
## 1 3.714286 3.714286 5.571429 11.28571 9.142857 3.571429 3.428571 32.57143
## 2 1.460000 1.380000 1.520000  3.46000 3.060000 1.500000 1.360000  8.84000
##        195      196      197      198      199      200      201      202
## 1 3.857143 6.857143 4.714286 9.857143 3.714286 2.142857 10.28571 8.714286
## 2 1.220000 3.260000 2.720000 4.220000 1.340000 1.460000  4.46000 4.180000
##        203      204      205  206      207      208  209      210      211
## 1 4.571429 15.57143 2.714286 8.00 2.714286 13.14286 7.00 20.85714 8.857143
## 2 2.120000  7.04000 1.560000 2.38 1.300000  8.22000 2.32  5.42000 5.740000
##        212   213      214      215      216      217      218      219      220
## 1 4.857143 16.00 6.857143 4.428571 1.571429 13.85714 2.857143 4.142857 6.428571
## 2 2.320000  3.84 2.360000 2.680000 1.400000  3.76000 1.480000 1.440000 4.340000
##        221      222      223      224      225      226      227 228      229
## 1 3.857143 4.428571 5.428571 5.857143 16.85714 7.285714 2.571429 3.0 5.142857
## 2 2.360000 1.540000 1.160000 2.480000  4.94000 4.200000 1.660000 1.5 2.160000
##        230      231      232      233  234      235      236      237      238
## 1 5.428571 6.142857 4.285714 9.571429 9.00 13.71429 4.428571 3.857143 14.57143
## 2 1.160000 3.080000 2.040000 2.460000 4.82  6.46000 1.760000 1.480000  3.92000
##    239      240      241      242      243      244      245      246  247
## 1 8.00 10.14286 4.571429 5.142857 2.428571 2.714286 4.571429 4.714286 4.00
## 2 2.08  4.00000 1.860000 1.900000 1.340000 1.140000 2.520000 2.100000 1.26
##        248      249      250      251      252      253      254      255   256
## 1 3.285714 2.142857 7.857143 2.285714 4.714286 3.857143 5.571429 5.571429 20.00
## 2 2.160000 0.840000 4.000000 1.860000 2.240000 1.220000 2.640000 2.220000  7.46
##   257      258      259      260      261       262      263   264      265
## 1 7.0 6.571429 8.714286 5.714286 5.571429 0.8571429 7.857143 17.00 13.42857
## 2 2.3 2.400000 3.700000 3.400000 2.700000 0.6000000 2.600000  4.64  6.40000
##        266      267      268      269      270      271      272      273
## 1 4.285714 1.857143 12.71429 2.285714 7.857143 4.285714 12.57143 3.714286
## 2 2.160000 1.060000  4.20000 1.040000 3.540000 2.200000  5.62000 1.120000
##        274      275      276      277      278      279  280      281      282
## 1 7.428571 8.857143 4.857143 5.571429 17.71429 3.857143 3.00 4.714286 4.714286
## 2 2.380000 4.160000 2.880000 2.200000  6.96000 1.900000 1.12 1.820000 1.260000
##        283      284      285      286   287      288      289      290  291
## 1 18.28571 5.571429 12.85714 6.571429 17.00 8.142857 5.285714 18.57143 4.00
## 2  6.42000 1.540000  4.74000 2.440000  6.36 3.220000 1.600000  6.16000 1.06
##        292      293      294      295  296      297      298      299      300
## 1 8.857143 21.57143 6.428571 12.85714 6.00 29.28571 5.571429 7.285714 5.142857
## 2 2.240000  9.62000 3.220000  4.30000 1.88  9.50000 3.460000 2.520000 1.820000
##        301      302      303      304      305      306      307   308  309
## 1 5.571429 2.285714 5.857143 17.28571 4.714286 7.428571 5.428571 12.00 4.00
## 2 2.140000 1.380000 3.460000  5.80000 2.300000 4.020000 2.180000  2.74 1.38
##     310      311      312 313      314  315      316      317      318      319
## 1 10.00 12.71429 8.571429 4.0 7.714286 7.00 6.142857 20.14286 26.71429 5.857143
## 2  3.84  4.86000 2.480000 0.8 5.800000 3.52 2.140000  7.78000  7.20000 2.380000
##        320      321      322      323  324 325      326      327      328
## 1 7.285714 1.142857 8.285714 10.71429 10.0 8.0 4.857143 5.142857 1.571429
## 2 3.260000 0.260000 4.100000  3.92000  4.3 2.1 1.840000 1.600000 1.220000
##        329      330      331      332      333      334      335      336
## 1 4.857143 4.714286 2.714286 5.571429 4.571429 4.857143 13.42857 15.14286
## 2 3.320000 1.620000 1.140000 2.280000 1.720000 1.080000  3.88000  7.16000
##        337      338      339      340      341      342      343      344
## 1 4.142857 10.71429 14.57143 7.571429 7.857143 8.142857 3.571429 2.857143
## 2 2.580000  3.72000  6.78000 2.780000 2.740000 2.680000 1.200000 1.160000
##        345      346      347      348  349      350      351      352      353
## 1 4.285714 17.71429 2.714286 2.714286 8.00 8.571429 18.42857 8.428571 4.857143
## 2 1.480000  7.54000 0.860000 1.420000 2.42 2.000000  6.44000 3.200000 1.760000
##        354      355      356      357  358      359  360      361      362  363
## 1 6.714286 6.571429 4.285714 3.428571 3.00 5.428571 5.00 1.714286 7.714286 7.00
## 2 2.300000 3.100000 1.260000 3.860000 0.64 1.580000 2.66 0.840000 2.560000 3.12
##        364      365      366      367      368      369      370      371
## 1 12.85714 12.71429 9.142857 4.857143 6.571429 4.428571 4.714286 11.14286
## 2  4.86000  6.66000 2.940000 1.360000 2.180000 2.000000 2.120000  3.88000
##        372      373      374      375      376  377      378  379      380
## 1 9.714286 9.857143 11.71429 9.142857 4.142857 3.00 5.285714 6.00 5.428571
## 2 4.340000 3.600000  4.52000 3.240000 1.280000 1.68 2.220000 4.08 2.920000
##     381       382      383      384      385      386      387      388
## 1 10.00 0.8571429 14.57143 9.571429 5.142857 7.571429 2.857143 2.142857
## 2  4.42 0.9600000  4.98000 3.420000 2.180000 2.500000 1.060000 0.880000
##        389      390      391      392      393      394   395      396   397
## 1 2.142857 10.14286 3.857143 8.714286 2.285714 11.71429 11.00 3.428571 13.00
## 2 1.260000  3.20000 2.160000 2.140000 1.060000  3.70000  3.06 1.380000  5.02
##        398      399      400      401      402      403      404      405  406
## 1 6.571429 2.142857 6.428571 4.142857 6.142857 2.428571 5.571429 6.285714 9.00
## 2 2.000000 0.760000 2.620000 1.020000 3.080000 1.660000 2.480000 1.300000 3.86
##        407      408      409      410      411      412      413  414  415
## 1 9.285714 7.142857 13.14286 13.57143 9.714286 2.571429 9.571429 5.00 6.00
## 2 3.720000 4.800000  4.74000  5.20000 2.420000 2.040000 4.180000 1.16 1.88
##        416      417      418      419      420      421      422      423
## 1 4.428571 3.285714 2.428571 9.142857 1.428571 4.857143 14.85714 6.428571
## 2 2.220000 1.500000 2.280000 6.100000 1.100000 2.320000  7.70000 2.260000
##        424      425      426      427      428      429      430  431      432
## 1 2.142857 1.857143 4.142857 16.14286 7.285714 11.71429 7.142857 3.00 6.142857
## 2 1.580000 1.420000 2.020000  6.12000 2.160000  4.82000 2.540000 1.74 3.620000
##        433      434      435      436      437      438      439      440  441
## 1 10.42857 1.857143 15.14286 7.714286 2.285714 4.714286 4.142857 3.714286 3.00
## 2  4.72000 1.160000  6.04000 2.420000 0.620000 2.100000 2.220000 1.320000 1.28
##        442      443      444      445      446      447  448      449      450
## 1 7.714286 3.857143 6.714286 3.428571 14.42857 2.857143 5.00 5.714286 10.14286
## 2 4.240000 1.120000 2.440000 1.520000  5.96000 1.160000 2.62 1.640000  3.66000
##    451      452      453      454      455      456      457      458      459
## 1 7.00 5.428571 3.714286 2.571429 15.14286 12.28571 6.142857 16.28571 10.14286
## 2 2.54 3.440000 2.380000 0.920000  3.60000  4.54000 2.560000  5.02000  3.36000
##        460      461      462      463      464      465      466      467
## 1 5.857143 3.714286 3.857143 9.428571 12.14286 11.57143 2.571429 4.857143
## 2 2.260000 2.140000 2.600000 5.100000  5.34000  3.86000 1.700000 1.660000
##        468      469      470      471      472      473      474      475 476
## 1 5.142857 6.714286 8.428571 9.285714 4.571429 4.571429 2.285714 7.285714 5.0
## 2 2.300000 1.760000 3.060000 3.440000 1.580000 2.260000 1.020000 3.220000 2.1
##        477      478      479      480      481      482      483      484
## 1 5.571429 5.857143 2.714286 5.285714 3.285714 5.285714 5.571429 5.428571
## 2 2.300000 3.340000 1.680000 3.380000 1.140000 1.600000 1.620000 3.380000
##        485      486      487      488      489      490      491  492      493
## 1 5.428571 5.571429 3.285714 5.285714 6.285714 1.142857 5.571429 4.00 4.714286
## 2 1.340000 1.480000 2.880000 1.880000 2.540000 0.860000 1.400000 1.44 1.220000
##        494      495      496   497      498      499      500  501      502
## 1 17.71429 3.857143 3.428571 18.00 1.714286 7.428571 10.14286 7.00 13.28571
## 2  6.54000 2.100000 2.080000  5.72 0.860000 1.740000  4.02000 1.86  3.52000
##        503      504  505      506      507      508      509      510      511
## 1 8.285714 3.714286 3.00 17.85714 8.428571 3.428571 5.857143 7.714286 4.714286
## 2 2.920000 2.420000 1.32  4.20000 1.740000 2.020000 1.820000 2.580000 1.300000
##        512      513      514 515      516      517      518      519      520
## 1 6.285714 4.571429 7.714286 9.0 3.428571 5.857143 4.857143 6.571429 3.571429
## 2 4.560000 1.740000 3.100000 2.5 1.260000 1.500000 2.180000 1.380000 1.560000
##        521      522  523  524      525      526      527      528      529
## 1 7.142857 3.857143 3.00 9.00 6.428571 15.28571 5.285714 2.714286 7.857143
## 2 3.140000 1.240000 0.96 4.84 1.540000  8.22000 2.120000 1.300000 3.000000
##        530      531      532      533      534      535      536      537
## 1 14.28571 9.857143 4.428571 9.142857 9.571429 14.57143 8.857143 6.857143
## 2  5.06000 3.920000 1.660000 3.540000 5.740000  3.98000 2.140000 1.880000
##        538  539      540      541      542      543      544      545      546
## 1 2.857143 8.00 4.428571 9.857143 3.142857 18.71429 5.142857 5.285714 1.428571
## 2 1.520000 3.56 2.260000 2.720000 1.540000  5.82000 1.620000 1.780000 0.900000
##        547      548      549      550 551      552      553      554  555
## 1 7.714286 4.714286 6.285714 3.857143 8.0 11.28571 15.14286 8.857143 3.00
## 2 2.900000 1.400000 4.040000 3.000000 3.3  4.46000  4.76000 2.580000 1.66
##        556      557      558      559   560      561      562      563      564
## 1 5.857143 2.714286 10.42857 9.428571 10.00 4.285714 30.71429 6.714286 29.85714
## 2 1.760000 0.840000  2.80000 4.320000  3.62 2.160000 13.70000 2.580000  9.02000
##        565      566      567      568      569      570  571      572      573
## 1 4.285714 6.285714 3.285714 12.57143 8.428571 3.142857 5.00 12.14286 6.285714
## 2 1.560000 2.000000 1.500000  3.52000 1.960000 2.160000 3.24  4.68000 2.660000
##        574      575      576      577 578      579      580      581      582
## 1 5.285714 7.142857 5.285714 14.57143 2.0 3.428571 4.714286 7.428571 23.14286
## 2 2.120000 2.360000 3.080000  6.44000 1.5 2.100000 1.720000 2.600000  9.08000
##        583      584      585      586  587  588   589      590      591
## 1 13.28571 6.714286 9.428571 9.714286 4.00 8.00 11.00 5.285714 2.285714
## 2  3.00000 1.960000 2.660000 3.920000 2.06 2.84  4.52 1.860000 1.200000
##        592      593  594      595  596      597      598      599      600
## 1 6.571429 10.42857 3.00 9.285714 6.00 3.857143 6.285714 2.571429 5.571429
## 2 2.820000  4.94000 1.28 7.760000 2.62 1.900000 2.320000 1.200000 1.900000
##        601      602      603      604      605 606   607      608      609
## 1 1.428571 3.428571 10.71429 11.42857 3.428571 2.0 13.00 6.714286 15.85714
## 2 1.020000 1.680000  4.56000  3.92000 1.000000 0.7  3.46 3.400000  6.42000
##        610      611      612      613      614      615      616  617      618
## 1 3.857143 7.142857 5.714286 8.571429 6.142857 9.285714 6.571429 7.00 12.85714
## 2 1.600000 2.100000 1.880000 2.400000 2.560000 3.760000 2.960000 2.56  3.52000
##         619  620      621      622      623      624      625      626      627
## 1 0.8571429 3.00 1.571429 2.571429 6.285714 3.571429 18.85714 4.571429 19.28571
## 2 1.3000000 1.56 0.960000 1.400000 2.660000 2.640000  4.56000 1.100000  7.24000
##        628      629      630      631      632      633      634      635
## 1 5.857143 3.285714 5.571429 8.428571 2.428571 3.285714 17.57143 2.857143
## 2 1.780000 1.000000 2.060000 5.260000 2.300000 2.020000  4.74000 1.700000
##        636      637      638      639      640      641      642      643
## 1 4.285714 3.714286 3.142857 3.714286 16.42857 47.85714 6.142857 12.85714
## 2 1.720000 2.160000 2.100000 1.380000  6.00000 17.74000 2.880000  4.32000
##        644      645      646  647      648      649      650      651      652
## 1 11.85714 8.142857 5.714286 2.00 3.142857 4.571429 4.428571 7.428571 5.142857
## 2  5.54000 3.040000 2.880000 1.92 1.780000 1.940000 2.380000 2.300000 2.240000
##        653      654      655      656      657      658      659      660
## 1 4.142857 5.714286 5.142857 2.857143 9.428571 50.28571 15.14286 2.714286
## 2 1.900000 2.500000 2.100000 1.020000 3.880000 13.98000  3.74000 2.780000
##        661      662      663      664      665  666      667      668      669
## 1 4.714286 10.28571 9.285714 7.285714 9.428571 3.00 4.285714 3.142857 16.57143
## 2 1.720000  2.88000 3.840000 2.240000 2.200000 1.48 2.200000 1.880000  6.44000
##        670      671   672      673      674      675      676      677      678
## 1 6.571429 3.714286 17.00 10.14286 11.85714 23.14286 15.57143 10.14286 6.428571
## 2 2.740000 2.000000  6.46  3.10000  3.68000  8.16000  4.32000  7.22000 2.900000
##        679      680      681      682      683      684      685      686
## 1 5.714286 4.857143 7.285714 6.571429 3.142857 9.857143 13.42857 4.428571
## 2 2.260000 2.120000 2.340000 2.240000 2.000000 4.200000  6.78000 1.640000
##        687  688      689      690      691      692      693      694      695
## 1 2.714286 2.00 4.285714 13.14286 6.285714 12.71429 3.714286 2.428571 16.85714
## 2 1.560000 1.74 2.480000  4.64000 3.560000  5.66000 1.760000 2.080000  4.82000
##        696      697      698      699  700      701      702      703      704
## 1 5.857143 7.857143 8.142857 5.142857 6.00 6.285714 8.857143 3.285714 18.85714
## 2 2.400000 2.080000 3.920000 2.100000 2.22 2.080000 3.260000 2.220000  5.68000
##        705      706      707 708      709      710      711      712      713
## 1 3.857143 3.142857 9.142857 3.0 7.857143 6.428571 13.28571 15.85714 4.714286
## 2 1.980000 1.780000 2.640000 1.6 5.220000 1.940000  4.64000  5.30000 1.340000
##        714      715      716      717      718      719      720      721  722
## 1 28.42857 3.285714 4.714286 3.142857 7.714286 2.857143 5.285714 46.14286 5.00
## 2  7.16000 1.820000 1.280000 1.160000 3.500000 1.240000 2.020000 15.84000 2.34
##        723      724      725  726      727      728      729      730      731
## 1 4.714286 12.57143 3.142857 8.00 9.285714 5.714286 24.71429 4.571429 12.28571
## 2 1.980000  3.54000 2.160000 3.82 2.840000 2.320000  5.98000 1.920000  2.90000
##        732      733      734      735      736      737      738      739
## 1 3.285714 4.714286 8.428571 4.571429 11.14286 8.857143 7.285714 4.571429
## 2 1.600000 1.960000 3.960000 1.640000  3.90000 3.980000 2.340000 1.520000
##        740  741      742      743      744      745      746      747  748
## 1 21.14286 6.00 2.428571 6.142857 8.285714 4.571429 15.28571 21.85714 5.00
## 2  8.48000 2.26 1.480000 2.320000 3.500000 1.500000  6.22000  5.84000 2.12
##        749      750      751      752      753      754  755      756      757
## 1 7.857143 9.571429 5.142857 1.714286 5.285714 3.714286 2.00 4.142857 6.714286
## 2 2.240000 3.520000 2.560000 1.120000 2.320000 1.880000 1.38 1.100000 2.400000
##        758 759      760      761 762      763      764      765      766 767
## 1 4.571429 1.0 3.714286 5.142857 9.0 3.142857 8.857143 6.285714 4.857143 5.0
## 2 1.460000 0.7 1.520000 2.100000 3.6 1.220000 3.080000 3.360000 2.240000 1.8
##        768      769      770      771      772      773      774      775   776
## 1 17.85714 12.42857 6.714286 3.571429 4.285714 4.714286 18.85714 9.571429 12.00
## 2  7.30000  3.64000 1.600000 2.120000 1.280000 2.280000  6.62000 3.300000  3.82
##        777      778      779  780      781      782      783      784 785  786
## 1 13.57143 3.714286 8.428571 9.00 4.142857 33.85714 4.285714 2.285714 4.0 5.00
## 2  5.46000 2.040000 3.680000 4.54 1.380000 14.00000 1.600000 1.180000 1.6 2.06
##        787      788      789  790      791      792      793      794      795
## 1 8.714286 7.142857 4.714286 6.00 6.857143 4.285714 28.28571 19.14286 4.285714
## 2 2.920000 1.840000 1.560000 1.62 2.400000 1.400000 11.74000  5.24000 1.840000
##        796      797  798      799      800      801      802      803 804
## 1 5.571429 11.42857 4.00 10.85714 7.571429 8.285714 6.142857 12.85714 5.0
## 2 2.640000  4.64000 2.42  4.20000 2.640000 3.040000 3.200000  5.84000 1.8
##        805  806      807      808      809      810      811      812      813
## 1 5.857143 4.00 3.857143 12.57143 9.857143 8.285714 6.857143 8.571429 3.714286
## 2 2.680000 1.74 1.500000  4.32000 3.600000 3.060000 2.900000 1.740000 1.420000
##        814      815      816      817      818   819      820      821      822
## 1 10.57143 17.85714 3.857143 4.857143 3.428571 14.00 3.571429 3.714286 2.285714
## 2  2.96000  6.50000 1.420000 1.600000 1.980000  4.92 2.100000 1.400000 1.920000
##        823  824  825      826      827      828      829      830      831
## 1 12.28571 6.00 4.00 21.85714 4.142857 9.714286 2.428571 6.714286 4.285714
## 2  3.28000 2.62 1.38  7.74000 1.280000 4.520000 1.340000 1.800000 2.560000
##        832      833      834      835      836      837      838      839
## 1 5.857143 5.714286 16.28571 3.857143 7.285714 8.285714 4.571429 4.142857
## 2 2.100000 2.320000  7.92000 1.620000 2.700000 2.140000 1.960000 2.200000
##        840      841 842      843      844      845      846      847      848
## 1 7.142857 2.142857 9.0 4.142857 9.285714 6.428571 2.714286 4.142857 6.428571
## 2 2.440000 2.020000 3.6 2.260000 3.880000 2.100000 1.440000 2.280000 2.200000
##        849  850      851      852      853      854      855      856      857
## 1 2.571429 6.00 5.714286 7.714286 5.571429 2.571429 3.285714 5.714286 30.14286
## 2 2.260000 2.44 2.580000 1.980000 2.380000 1.820000 2.300000 2.860000 12.38000
##        858      859      860      861      862      863      864      865
## 1 11.57143 4.714286 8.428571 6.571429 6.571429 13.57143 6.857143 2.571429
## 2  4.82000 1.940000 3.500000 2.440000 2.780000  4.90000 1.760000 1.540000
##        866      867      868      869      870      871      872      873
## 1 18.42857 8.142857 6.571429 4.285714 3.428571 4.428571 5.857143 8.428571
## 2  5.94000 3.060000 3.000000 2.360000 1.740000 2.000000 1.940000 2.880000
##        874      875      876      877      878      879      880      881
## 1 6.428571 6.714286 10.57143 4.142857 18.28571 7.714286 16.28571 1.428571
## 2 2.200000 2.540000  2.82000 1.900000  6.52000 2.700000  8.02000 1.460000
##        882      883      884      885  886      887      888      889      890
## 1 4.142857 4.428571 4.571429 8.285714 3.00 5.142857 6.714286 5.857143 8.285714
## 2 1.740000 1.460000 1.900000 3.940000 2.18 2.040000 2.700000 2.100000 2.980000
##        891      892  893      894      895      896      897      898      899
## 1 3.857143 9.571429 9.00 1.428571 4.714286 6.571429 4.857143 13.14286 6.571429
## 2 1.880000 3.520000 3.68 1.120000 1.800000 2.140000 2.300000  3.86000 2.700000
##        900      901      902      903      904      905      906      907
## 1 3.714286 11.42857 13.14286 4.571429 29.71429 5.142857 4.714286 6.857143
## 2 1.520000  2.02000  4.14000 1.980000  8.60000 2.040000 2.480000 2.560000
##        908      909      910      911      912      913      914      915
## 1 2.142857 2.285714 14.57143 5.142857 12.14286 4.142857 3.142857 4.142857
## 2 0.920000 2.600000  4.08000 2.560000  3.98000 2.720000 1.560000 1.700000
##        916  917  918      919      920      921      922      923  924  925
## 1 11.71429 10.0 8.00 4.285714 4.714286 2.285714 25.85714 5.857143 6.00 6.00
## 2  4.54000  3.9 3.26 1.480000 2.700000 1.440000 12.64000 1.720000 1.98 2.02
##    926      927      928      929      930      931      932      933      934
## 1 8.00 4.285714 5.285714 3.285714 12.28571 6.714286 7.428571 3.571429 2.142857
## 2 2.48 1.520000 1.680000 1.600000  4.94000 3.860000 2.680000 2.500000 1.940000
##        935      936      937      938      939      940      941      942  943
## 1 3.857143 8.142857 9.857143 9.714286 7.857143 13.85714 10.28571 3.714286 8.00
## 2 1.800000 2.480000 3.000000 3.580000 2.640000  6.28000  3.92000 1.820000 2.38
##        944      945      946      947  948      949      950  951      952
## 1 10.42857 2.714286 7.571429 10.57143 7.00 9.857143 4.285714 6.00 8.142857
## 2  3.04000 1.180000 2.800000  3.74000 1.98 3.820000 2.040000 1.92 2.120000
##        953      954      955      956      957      958      959      960
## 1 5.714286 4.714286 6.142857 28.71429 7.142857 2.285714 17.42857 167.1429
## 2 2.360000 1.940000 2.240000 10.20000 2.220000 1.580000  5.54000  45.7800
##        961      962   963      964      965      966      967  968      969
## 1 4.571429 6.714286 17.00 5.714286 6.714286 6.428571 3.428571 7.00 4.428571
## 2 2.100000 2.280000  5.52 3.280000 1.900000 2.340000 1.920000 3.22 2.120000
##        970      971      972      973  974      975  976      977      978
## 1 14.71429 4.142857 8.857143 32.14286 3.00 2.571429 3.00 4.571429 11.85714
## 2  3.70000 2.280000 2.860000 16.52000 2.18 1.880000 2.02 2.020000  3.80000
##        979      980      981  982      983      984      985      986      987
## 1 5.285714 4.428571 7.571429 8.00 6.142857 3.142857 8.285714 7.571429 5.428571
## 2 2.480000 1.600000 1.520000 2.42 2.200000 1.780000 2.180000 2.120000 2.340000
##        988      989      990      991      992      993      994   995      996
## 1 4.857143 5.142857 4.571429 5.428571 3.142857 5.285714 6.857143 10.00 3.571429
## 2 1.600000 2.860000 2.560000 2.220000 1.920000 2.780000 2.200000  3.12 2.260000
##        997      998      999     1000
## 1 7.571429 8.571429 7.285714 8.142857
## 2 2.400000 3.100000 1.780000 2.600000
## 
## Clustering vector:
##      actual      answer  assignment     awesome         bad        best 
##           2           2           2           2           2           2 
##      better      boring        care       clear    definite   difficult 
##           2           2           2           2           2           2 
##       don't        easy        exam      expect     explain     extreme 
##           2           1           2           2           2           2 
##       final        fine         fun        good       grade     grading 
##           2           2           2           1           2           2 
##       great        hard        help     helpful interesting        know 
##           1           1           1           2           2           2 
##       learn     lecture        like      little        love    material 
##           2           1           2           2           2           2 
##        most        nice        note        pass      pretty    question 
##           2           2           2           2           2           2 
##   recommend    semester       study     subject        talk        test 
##           2           2           2           2           2           1 
##        time  understand        well     willing        work        book 
##           2           2           2           2           2           2 
##       enjoy       funny        read 
##           2           2           2 
## 
## Within cluster sum of squares by cluster:
## [1] 357551.1 696885.9
##  (between_SS / total_SS =  24.9 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"

The output also includes the information on individual (withinss) and total within-cluster (tot.withinss) sum of squares (SS). The individual SS is calculated as the sum of squares within each cluster. The total is the sum of them. In addition, the total sum of squares (totss) and between-cluster sum of squares (betweenss) can also be obtained from the output. For this example, the ratio of between SS and total SS is 24.9%. This means that 24.9% variation in the data can be explained by the two clusters.

6.2.2 Determine the number of clusters

Many methods can be used to determine the best number of clusters. Here we use the “eblow” method. For each given number of clusters, we can calculate how much variance in the data can be explained by the clustering. Typically, this will increase with the number of clusters. However, the increase would slow down at a certain point and that’s where we choose the number of clusters.

For example, the following code obtains the explained variances for 1 to 7 clusters.

From the plot, after 3 clusters, the increase in the explained variance becomes slower - there is an elbow here. Therefore, we might use 3 clusters here.

6.3 k-medoids

k-medoids is another clustering method. Both k-means and k-medoids attempt to minimize the distance between points within a cluster. In contrast to k-means, k-medoids chooses datapoints in the data as centers (called medoids). Another difference is the use of the distance. k-medoids typically use the Manhattan distance to define the dissimilarity. For example, for the example used in k-means, the distance is defined as below for k-medoids.

\[ d(p,q) = |p_1-q_1| + |p_2-q_2| + |p_3-q_3|. \]

A common implementation of k-medoid clustering is the Partitioning Around Medoids (PAM) algorithm. In R, the function pam of the R package cluster can be used. Different from the kmeans function, the pam function can take both a data matrix or a dissimilarity matrix.

For example,

kmedoid.res <- pam(kmeans.data, 2, metric = "manhantan")
kmedoid.res
## Medoids:
##      ID  1 2  3 4  5 6 7 8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## most 37  4 2 15 1 12 3 0 2  8 15  2  0  4  2  2  1  9  5  3  2  0  2  4  3  1
## good 22 15 3 30 2 24 4 3 3 14 18  7  9 11 18  4 10 20  8 13  7  3  5 14 11  8
##      26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
## most  9  3  3  0  1  0  2  0  3  4  0  5  2  0  6  1  1  0  2  3  0  1  2  2  0
## good 12  6 11  4  4 13  6 12  9  6  5 15  2  1 15  4 16  2 15  7  5  5  3  7  9
##      51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
## most  6  1  0  0  2  8  0  1  7  8  3  2  2  1  0  5  0  2  1  2  5  0  0  7  3
## good  8  7  3  0  5 15  4  4 24 18  9 10  9  5  5 20  5  8 17 13  2  7 12 13 17
##      76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
## most  4  7  8  4  6  0  2  1  2  0  1  0  1  1 11  0  7  1  1 10  1  1  4  4
## good  8  7 18 16 41  4 30  4 22  6  9  0  3  4  7  6 34  6  0  8  2  4  7  7
##      100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
## most   2   7   0   0   0  10   3   4   0   2   4   4   2   0   1   4   4   1
## good   3   7  18   6  11  25   8  16   2   4   1   6   3   2   1  10   7   4
##      118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
## most   2   7   4  11   2   1   2   7   2   1   2   1   2   1   2  20   1   0
## good   1  13  12  27  12   4   2  13  14  12   5   7   0   5   8  47   0   2
##      136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
## most   1   1   1   5   3   2   2   5   0   4   3   1   2   2   1   3   0   1
## good   2   6  16   5  34   4   1  23   2   3  13   2   7   1   0   1   1   5
##      154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
## most   4   4   0   1   4   4   1   1   8   0   5   5   0   2   3   7   7   2
## good   3   7   2   5  10   2   6   7  29   4   4  26   2   1  13   6  12   4
##      172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
## most   6   2   1   3   2   1   1   0   6   1   2   2   0   3   1   1   2   2
## good   9   1   2   9  12   8  11   1   2   4  11   0   7   7   3   3   5   4
##      190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
## most   2   5   5   1   6   0   1   1   4   3   1   3   6   3   5   0   2   0
## good  12   8   7   1  19   1  11   1  11   5   0   5   4   7  13   4   4   1
##      208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
## most   6   3   3   4   2   4   3   6   3   1   4   3   1   4   1   1   3   4
## good  22   7  17  13   2   9   4   3   3   9   0   1  17   1   1   5   5  12
##      226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
## most   2   3   1   1   0   3   4   1   3   5   2   0   4   2   3   4   0   1
## good  11   1   5   9   2   6   4  13   8   9   6   3  10   9   7   1   1   3
##      244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
## most   0   4   2   0   2   0   7   0   4   1   3   1   3   0   2   5   6   3
## good   6   6   7   7   4   2  10   2   6   4  11  11  19   4   4  11   9   5
##      262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
## most   1   1   4   6   2   0   5   0   3   0   6   1   1   6   2   3   2   2
## good   2   9  11  11   4   3  15   3  10   4  11   6  13   4   5   7  10   5
##      280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
## most   1   1   1  12   0   5   3   3   4   0   5   1   3   7   5   2   0  12
## good   5   2   3  13   3  16   5   7  10   0  17   2   9  23   7  13   5  29
##      298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
## most   3   1   0   1   1   4   6   1   2   2   2   2   6   1   0   1   4   2
## good   5   5   4   2   4   5  15   3  11   9   7   8  12  10   8   6   8   4
##      316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
## most   1  19   2   2   0   0   4   2   6   2   1   3   0   4   1   1   1   1
## good  11  21  20   3   8   0  17  15   6   5   5   4   3   4   5   3   4   5
##      334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
## most   0   2  10   3   3   7   1   0   2   2   3   2   7   0   3   2   2   5
## good   9   6  12   6  11  14   2   5   8   5   0   4  32   3   4   9   8  16
##      352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
## most   8   3   3   2   4   5   0   2   4   1   1   3   5   7   4   1   0   0
## good   5   5   7   8   4   3   4   6   4   1   6   5  18  13   9   9   5   5
##      370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
## most   4   2   1   1   4   4   0   1   1   2   2   3   0   5   3   6   5   0
## good   7   8   6  13  18   7   1   2   7   4   4  12   1  19   6   3   9   1
##      388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
## most   1   0   3   3   5   3   5   1   1   1   1   1   3   2   1   2   0   3
## good   1   3  17  11   2   5   7  13   7  15   4   5   3   3   4   2   3   0
##      406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
## most   6   4   3   4   2   1   2   5   6   2   0   2   3   6   2   2   3   3
## good   9  10  14  18  10   8   4  12   5   3   4   3   2  12   1   6  20   3
##      424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
## most   4   2   3   3   2   8   2   0   2   5   0   2   3   0   1   3   1   0
## good   1   3   2  19   6  13   5  10   5  12   3  16   4   3   2   9   6   3
##      442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
## most   2   1   1   1   3   0   0   0   4   3   5   0   0   3   2   0   8   4
## good   7   5  11   4  15   2   4  10  11   3   4   3   7  10  12   8  26  12
##      460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
## most   3   0   4   4   1   1   1   3   3   1   1   1   1   5   0   1   3   2
## good   3   6   9   9  23   5   4   6  10  10   8  15   7   4   5   8   2   7
##      478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
## most   6   0   3   0   4   2   4   0   0   2   1   1   3   0   1   0   2   3
## good   6   5   4   3   4   5   2   7   4   5   7  10   0   3   5   2  26   3
##      496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
## most   3   2   2   1   4   1   3   5   1   1   4   2   0   1   4   0   3   2
## good   6  12   7   8   4   3   8  13   4   3  19   0   4   4   4   6  14   3
##      514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
## most   4   1   1   1   1   1   2   3   2   1   2   1   5   4   2   4   4   3
## good   4   8   4   2   5   9   3   7   2   2   4   6  18   2   3   8  14  12
##      532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
## most   1   1   8   2   2   0   2   3   0   0   6   8   0   1   2   1   1   2
## good   6   2  13   5   8   5   4   5   5   6   4  21   8   2   4   2   3   9
##      550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
## most   0   0   6   3   2   1   0   0   1   2   2   2  13   1  12   0   0   0
## good  11   9   8  13   8   6   3   3   8  11   9   6  35   8  23   7   4   4
##      568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
## most   1   1   2   3   7   3   2   1   3   1   1   6   1   1   8   2   0   0
## good   9  13   1  10   9   2   0   7   3  10   2   3   0   7  19  11   5   4
##      586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
## most   2   1   3   5   2   0   0   3   3  15   2   2   1   0   1   4   3   1
## good   3   3   7   6   7   2   6   9   1  15   7   2   8   1   0   1   2   9
##      604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
## most   2   1   0   0   2   6   1   1   1   0   1   2   2   3   7   1   2   2
## good   7   3   0   5  13  15   7  11   6   7   4   8   6   7  10   1   3   3
##      622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
## most   0   2   3   5   0   5   0   0   0   3   0   1   3   2   0   3   4   1
## good   4   5   4   9   5  10   2   6   7   4   0   5   8   2   8   1   3   4
##      640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
## most   5  12   4   3   3  11   3   3   3   0   1   0   3   0   7   1   2   2
## good  17  38   6   7  11   1   7   3   2   2   1   3   7   4   3   2   2  11
##      658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
## most  10   3   3   1   1   3   0   1   3  11   2   4   2   0   8   1   2   4
## good  28  10   3   4  11   9   7   8   3   0   3  12   4   0  14   8   4  16
##      676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
## most   3   8   2   2   0   4   3   3   3   8   0   4   2   7   3   3   4   0
## good  18   7   4   5   0   5   3   4  11   9   4   3   4   4  13   2  22   3
##      694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
## most   3   1   2   0   4   1   2   1   1   1   4   2   0   1   1   3   4   4
## good   4   9   6   4  14   4   5   7   5   5   8   3   0   8   8   6   9  10
##      712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
## most  10   0   6   0   0   1   9   0   1  11   5   1   1   1   2   1   2   2
## good  16   1  16   4   2   2   3   4   5  25   1   2   6   1  13  11   2  21
##      730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
## most   0   2   2   1   3   0   3   5   2   0   5   1   0   1   1   1   8   2
## good   5   7   6   3  10   4   6  10  10   3  20   5   2   8   2   3  10   9
##      748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
## most   1   2   1   3   0   5   2   1   0   2   3   0   0   3   3   1   1   2
## good   2   9   8   2   2  10   3   4   3   4   2   1   5   6   8   2   9   6
##      766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
## most   0   1   6   3   1   4   0   5   6   8   0   2   1   1   3   2   6   2
## good   3   2  15  11   2   7   4   4  21   4  14   7   1   3  12   4  23   1
##      784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
## most   0   0   1   2   1   0   2   2   2   4   2   1   2   4   3   2   3   0
## good   3   1   2   7   5   7   8   8   1  13   6   1   9   8   2  13   5   9
##      802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
## most   1   5   1   1   1   2   2   2   3   1   1   2   3   5   1   0   5   6
## good   9  11   1   7   3   3  10   5   7   4   1   3   5  14   7   7   2   6
##      820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
## most   4   2   0   1   3   1   4   2   4   1   1   2   2   0  13   2   3   3
## good   5   7   3   1   1   6  11   4   9   1   3   7   2   4  20   4   3   7
##      838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
## most   1   1   2   2   2   1   2   1   0   1   1   4   1   4   1   2   0   1
## good   1   3   4   4   7   6   7   7   5   3   5   3   7   4   2   2   0   6
##      856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
## most   1   7   6   1   2   0   2   4   1   0  11   1   0   0   2   4   5   1
## good   4  34   5   0   7   8   0   3   3   2  16   4   5   6   1   3  11   5
##      874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
## most   3   1   0   3   6   7   2   1   1   1   2   9   0   2   0   0   2   1
## good   7   6   2   1  10   5   9   1   1   2   7   8   7   3   4   5   2   3
##      892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
## most   0   3   2   3   1   0   2   1   0   4   5   3   1   6   1   2   1   1
## good  14   5   0   4   5   4   5   5   4   5   4   5  25   3   2   9   0   1
##      910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
## most   2   1   5   2   2   1   4   6   4   0   2   4   8   0   2   0   1   1
## good   7   1   3   1   4   2   6  10   3   2   7   4  21   4   2   9   7   2
##      928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
## most   0   0   4   3   2   3   3   1   1   2   4   1   5   3   2   4   3   0
## good   5   3  11   3   5   6   3   4   2   7   5   3  10   7   3   7   4   3
##      946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
## most   1   0   2   8   3   3   0   1   1   2   9   2   0   3  44   3   1   7
## good   6  13   4   8   6   7   4   2   4   5  14   7   1  20 144   0  12   9
##      964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
## most   1   2   0   2   1   1   2   2   3   9   4   5   4   0   1   1   0   2
## good   4   2   8   3  11   2   8   2   7  35   1   1   0   3  19   3   1   4
##      982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
## most   1   1   4   2   2   1   1   3   5   1   2   2   1   0   2   0   1   0
## good   2   5   5   2   1   8   4   5   2   4   2   4   9   2   3   4   4   5
##      1000
## most    4
## good    9
## Clustering vector:
##      actual      answer  assignment     awesome         bad        best 
##           1           1           1           1           1           1 
##      better      boring        care       clear    definite   difficult 
##           1           1           1           1           1           1 
##       don't        easy        exam      expect     explain     extreme 
##           1           2           1           1           1           1 
##       final        fine         fun        good       grade     grading 
##           1           1           1           2           1           1 
##       great        hard        help     helpful interesting        know 
##           2           2           2           1           1           1 
##       learn     lecture        like      little        love    material 
##           1           2           1           1           1           1 
##        most        nice        note        pass      pretty    question 
##           1           1           1           1           1           1 
##   recommend    semester       study     subject        talk        test 
##           1           1           1           1           1           2 
##        time  understand        well     willing        work        book 
##           1           1           1           1           1           1 
##       enjoy       funny        read 
##           1           1           1 
## Objective function:
##    build     swap 
## 138.8718 138.8718 
## 
## Available components:
##  [1] "medoids"    "id.med"     "clustering" "objective"  "isolation" 
##  [6] "clusinfo"   "silinfo"    "diss"       "call"       "data"

6.3.1 Visualization

Many times, it is useful to visualize the results. When there are more than 2 clustering variables such as in our case with 1,000 clustering variables, principal component analysis (PCA) is often conducted to find 2 principal components and the visualize the data in the two-dimensional space. However, PCA cannot directly be conducted when the sample size is small. In this case, we use the dissimilarity/distance matrix for analysis instead.

The R function clusplot from the package cluster is used. Note that kmedoid.res$diss is the distance matrix from the previous k-medoids analysis.