16.8 Applied Examples

The applied examples for this chapter were adapted from a more detailed tutorial on the QuantDev website (https://quantdev.ssri.psu.edu/tutorials/cluster-analysis-example).

Cluster analysis is an exploratory, descriptive, “bottom-up” approach to structure heterogeneity. A key underpinning of cluster analysis is an assumption that a sample is NOT homogeneous. The method is used to examine and describe distinct sub-populations in the sample. The goal is to identify groups of individuals (observations) whose members (a) are similar on group-defining variables, and (b) differ from members of other groups?

16.8.1 Preliminaries

Libraries used in this script. There are two main libraries that we will use cluster (namesake) and fpc (Flexible Procedures for Clustering). Some other functions are in the base package.

#general packages
library(ggplot2)
library(psych)

#cluster packages
library(cluster) #clustering
library(fpc) #flexible procedures for clustering
#library(clusterCrit) #cluster criteria

Our example makes use of one our experience sampling data sets, but treats these data as though they are cross-sectional. Getting the data and doing a bit of data management (new id variable)

#set filepath for data file
filepath <- "https://quantdev.ssri.psu.edu/sites/qdev/files/AMIBbrief_raw_daily1.csv"

#read in the .csv file using the url() function
daily <- read.csv(file=url(filepath),header=TRUE)

#clean-up of variable names so that they are all lowercase
var.names.daily <- tolower(colnames(daily))
colnames(daily)<-var.names.daily

#creating a new "id" variable
#(we had repeated measures nested in people, now they all get different ids)
daily$id <- daily$id*10+daily$day

names(daily)
##  [1] "id"      "day"     "date"    "slphrs"  "weath"   "lteq"    "pss"    
##  [8] "se"      "swls"    "evalday" "posaff"  "negaff"  "temp"    "hum"    
## [15] "wind"    "bar"     "prec"
#reducing down to variable set
daily <- daily[ ,c("id","slphrs","weath","lteq","pss","se","swls","evalday", "posaff","negaff","temp","hum","wind","bar","prec")]

#names of variables
names(daily)
##  [1] "id"      "slphrs"  "weath"   "lteq"    "pss"     "se"      "swls"   
##  [8] "evalday" "posaff"  "negaff"  "temp"    "hum"     "wind"    "bar"    
## [15] "prec"
#looking at data    
head(daily,10)
##      id slphrs weath lteq  pss se swls evalday posaff negaff temp  hum wind
## 1  1010    6.0     1   10 2.50  2  3.8       1    3.9    3.0 28.0 0.79 11.0
## 2  1011    2.0     2   10 2.75  3  4.2       0    3.8    2.3 20.8 0.62  3.6
## 3  1012    9.0     3   10 3.50  4  5.0       1    5.1    1.0 29.1 0.51  1.9
## 4  1013    7.5     2    9 3.00  4  5.0       1    5.6    1.3 30.2 0.58  2.7
## 5  1014    8.0     1   18 2.75  3  4.0       1    4.3    1.1 22.7 0.55  2.4
## 6  1015    8.0     2   19 2.75  3  4.2       1    3.9    1.0 21.4 0.54  0.7
## 7  1016    8.0     3   21 3.50  4  4.6       1    5.1    1.2 31.4 0.49  1.0
## 8  1017    7.0    NA   14 2.75  3  4.6       1    4.8    1.1 45.3 0.52  1.1
## 9  1020    7.0     0   12 3.50  5  5.6       0    6.3    1.4 28.0 0.79 11.0
## 10 1021    6.0     0   20 4.00  5  6.6       0    7.0    1.6 20.8 0.62  3.6
##      bar prec
## 1  29.40 0.20
## 2  30.17 0.00
## 3  30.35 0.02
## 4  30.23 0.00
## 5  30.46 0.00
## 6  30.54 0.00
## 7  30.51 0.00
## 8  30.30 0.00
## 9  29.40 0.20
## 10 30.17 0.00

16.8.2 Preparing Data

Note that cluster analysis does NOT generally work with missing data. Here we simply delete incomplete cases. Other possibilities include imputation, and calculation of distances using most complete subsets.

#removing observations with NA
dailysub <- daily[complete.cases(daily), ] 
describe(dailysub)
##         vars    n    mean      sd  median trimmed     mad     min     max
## id         1 1376 3276.28 1279.88 3271.50 3302.24 1497.43 1010.00 5327.00
## slphrs     2 1376    7.20    1.81    7.00    7.20    1.48    0.00   18.00
## weath      3 1376    2.00    1.29    2.00    2.00    1.48    0.00    4.00
## lteq       4 1376   12.50   10.42    9.00   11.24    8.90    0.00   58.00
## pss        5 1376    2.62    0.68    2.75    2.64    0.74    0.00    4.00
## se         6 1376    3.43    0.99    3.00    3.46    1.48    1.00    5.00
## swls       7 1376    4.11    1.27    4.20    4.15    1.19    1.00    7.00
## evalday    8 1376    0.68    0.46    1.00    0.73    0.00    0.00    1.00
## posaff     9 1376    4.11    1.10    4.20    4.14    1.19    1.00    7.00
## negaff    10 1376    2.45    1.04    2.20    2.34    1.04    1.00    6.90
## temp      11 1376   40.18    7.88   42.00   40.51    8.90   20.80   56.00
## hum       12 1376    0.62    0.20    0.66    0.63    0.21    0.24    0.90
## wind      13 1376    7.36    4.45    7.00    6.81    4.45    0.70   20.00
## bar       14 1376   30.02    0.33   30.00   30.04    0.43   29.32   30.54
## prec      15 1376    0.05    0.09    0.00    0.03    0.00    0.00    0.30
##           range  skew kurtosis    se
## id      4317.00 -0.10    -1.04 34.50
## slphrs    18.00  0.12     1.93  0.05
## weath      4.00 -0.06    -1.06  0.03
## lteq      58.00  1.07     0.95  0.28
## pss        4.00 -0.37     0.17  0.02
## se         4.00 -0.40    -0.12  0.03
## swls       6.00 -0.28    -0.22  0.03
## evalday    1.00 -0.79    -1.37  0.01
## posaff     6.00 -0.24    -0.37  0.03
## negaff     5.90  0.95     0.67  0.03
## temp      35.20 -0.37    -0.24  0.21
## hum        0.66 -0.36    -1.10  0.01
## wind      19.30  0.97     0.86  0.12
## bar        1.22 -0.39    -0.90  0.01
## prec       0.30  1.85     1.98  0.00

16.8.3 Scaling

The unit of distance may be different for different variables. For example, one year of difference in age seems like it should be a larger difference than one dollar difference in income.

Different variables will be “weighted” differently in the distance calculation. To alleviate this, a common approach is to rescale each variable into a standardized, z-score variable (i.e., by subtracting the mean and dividing by the standard deviation).

Thus, all the variables would then have mean = 0, with differences scales in standard deviation units. Note that this scales everything in relation to the observed sample (which has plusses and minuses).

The R function scale() makes it all very easy.

#scaling all the variables
dailyscale <- data.frame(scale(dailysub, center=TRUE, scale=TRUE))
#checking and fixing the id variable (which we did not want standardized)
str(dailyscale$id)
##  num [1:1376] -1.77 -1.77 -1.77 -1.77 -1.77 ...
dailyscale$id <- dailysub$id
str(dailyscale$id)
##  num [1:1376] 1010 1011 1012 1013 1014 ...
describe(dailyscale)
##         vars    n    mean      sd  median trimmed     mad     min     max
## id         1 1376 3276.28 1279.88 3271.50 3302.24 1497.43 1010.00 5327.00
## slphrs     2 1376    0.00    1.00   -0.11    0.00    0.82   -3.98    5.98
## weath      3 1376    0.00    1.00    0.00    0.00    1.15   -1.55    1.54
## lteq       4 1376    0.00    1.00   -0.34   -0.12    0.85   -1.20    4.37
## pss        5 1376    0.00    1.00    0.19    0.03    1.08   -3.83    2.02
## se         6 1376    0.00    1.00   -0.43    0.04    1.49   -2.45    1.58
## swls       7 1376    0.00    1.00    0.07    0.03    0.93   -2.45    2.27
## evalday    8 1376    0.00    1.00    0.68    0.10    0.00   -1.47    0.68
## posaff     9 1376    0.00    1.00    0.08    0.03    1.08   -2.82    2.63
## negaff    10 1376    0.00    1.00   -0.24   -0.11    1.00   -1.40    4.28
## temp      11 1376    0.00    1.00    0.23    0.04    1.13   -2.46    2.01
## hum       12 1376    0.00    1.00    0.20    0.05    1.06   -1.94    1.42
## wind      13 1376    0.00    1.00   -0.08   -0.12    1.00   -1.50    2.84
## bar       14 1376    0.00    1.00   -0.05    0.06    1.29   -2.09    1.56
## prec      15 1376    0.00    1.00   -0.53   -0.26    0.00   -0.53    2.68
##           range  skew kurtosis    se
## id      4317.00 -0.10    -1.04 34.50
## slphrs     9.97  0.12     1.93  0.03
## weath      3.09 -0.06    -1.06  0.03
## lteq       5.57  1.07     0.95  0.03
## pss        5.85 -0.37     0.17  0.03
## se         4.03 -0.40    -0.12  0.03
## swls       4.72 -0.28    -0.22  0.03
## evalday    2.15 -0.79    -1.37  0.03
## posaff     5.45 -0.24    -0.37  0.03
## negaff     5.68  0.95     0.67  0.03
## temp       4.47 -0.37    -0.24  0.03
## hum        3.36 -0.36    -1.10  0.03
## wind       4.33  0.97     0.86  0.03
## bar        3.65 -0.39    -0.90  0.03
## prec       3.21  1.85     1.98  0.03

16.8.4 Plotting

We choose a small subset of variables for easy visualization in a bivariate space. We use lteq, a measure of physical activity (Leisure Time Exercise Questionnaire), and posaff, a measure of positive affect.

ggplot(dailyscale,aes(x=lteq,y=posaff)) +
  geom_point()

This is a good toy data set for class purposes, but keep in mind the original nature of the data which might not be the best for cluster analytic purposes.

16.8.5 Distances

Each individual is conceptualized as a point in a multivariate space. For example, let’s look at the first three individuals.

data1 <- dailyscale[c(1,3,12),c("id","lteq","posaff")]
head(data1,3)
##      id       lteq     posaff
## 1  1010 -0.2399597 -0.1889168
## 3  1012 -0.2399597  0.9016850
## 14 1025 -0.4318716  0.5381510
labels.abc <-c("A","B","C")
ggplot(data1,aes(x=lteq,y=posaff)) +
  geom_polygon(fill="blue",alpha=.6) +
  geom_point(size=3) +
  geom_text(aes(x=lteq-.1,label=labels.abc)) +
  ylim(-1,1) + xlim(-1,1)

Let’s look at the distances. Euclidean Distance is calculated as

\[ EuclideanDistance_{A,B} = \sqrt{(x_{a} - x_{b})^2 + (y_{a} - y_{b})^2} \]

and easily implemented using the dist() function.

dist.abc <- dist(data1[1:3,2:3],method="euclidean",diag=TRUE,upper=FALSE)
dist.abc
##            1         3        14
## 1  0.0000000                    
## 3  1.0906017 0.0000000          
## 14 0.7519693 0.4110804 0.0000000

Might also use a different distance measure, such as Manhattan Distance … The distance between two points in a grid based on a strictly horizontal and/or vertical path (that is, along the grid lines), as opposed to the diagonal or “as the crow flies” distance.

The Manhattan distance is the simple sum of the horizontal and vertical components, whereas the diagonal distance might be computed by applying the Pythagorean theorem.

\[ ManhattanDistance_{A,B} = |x_{a} - x_{b}| + |y_{a} - y_{b}|$ \]

dist.abc2 <- dist(data1[1:3,2:3],method="manhattan",diag=TRUE,upper=FALSE)
dist.abc2
##            1         3        14
## 1  0.0000000                    
## 3  1.0906017 0.0000000          
## 14 0.9189797 0.5554458 0.0000000

The great thing about the distances is that they scale up to distance in many dimensions.

16.8.6 K-Means

Basic clustering in the social sciences often makes use of the K-means procedure.

The k-means algorithm is a traditional and widely used clustering algorithm.

In brief, the algorithm begins by specifying the number of clusters we are interested in. This is the k. Each of the k clusters is identified by the vector of the average (i.e., the mean) value of each of the variables for observations within a cluster. A random clustering is constructed (random set of mean vectors).

The k means are calculated. Then, using the distance measure, we gravitate each observation to its nearest mean. The means are then recalculated and the points re-gravitate. And so on until there is no further change to the means.

Let’s see an example where we chose \(K=4\).

We use the R function kmeans().

#there are random starts involved so we set a seed
set.seed(1234)
#running a cluster analysis
model <- kmeans(dailyscale[,c("lteq","posaff")], centers=4)
model
## K-means clustering with 4 clusters of sizes 377, 578, 240, 181
## 
## Cluster means:
##         lteq     posaff
## 1 -0.6558537 -1.0705106
## 2 -0.4408363  0.6996343
## 3  0.7763740 -0.5678819
## 4  1.7443674  0.7485388
## 
## Clustering vector:
##    1    2    3    4    5    6    7    9   10   11   13   14   15   17   18   19 
##    2    1    2    2    3    3    4    2    4    2    2    2    1    2    2    2 
##   20   21   22   23   27   28   29   30   31   32   37   38   39   40   41   42 
##    1    1    1    4    1    1    1    1    2    2    2    2    2    2    1    2 
##   43   44   47   48   49   51   52   53   54   55   56   57   58   59   60   61 
##    2    1    2    2    2    2    2    2    2    1    3    3    1    1    1    3 
##   62   63   64   66   67   68   70   71   72   73   74   75   76   77   78   79 
##    3    2    4    3    1    3    2    2    2    2    2    2    2    1    2    3 
##   81   84   85   86   87   88   89   90   91   92   93   95   96   97   98   99 
##    2    1    3    1    2    2    2    4    4    4    4    2    2    2    2    4 
##  101  102  103  105  106  107  108  110  111  112  113  114  115  116  117  118 
##    4    3    2    3    4    4    3    2    4    4    4    4    4    2    4    4 
##  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134 
##    4    3    1    1    1    1    1    1    1    2    2    2    2    2    2    2 
##  135  136  137  138  140  141  143  144  145  146  147  149  150  151  152  153 
##    2    4    4    2    3    3    3    4    3    4    4    4    4    4    1    3 
##  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169 
##    2    3    2    3    3    2    2    2    2    2    2    2    2    2    2    2 
##  170  171  172  173  174  175  176  177  178  179  180  181  182  183  184  185 
##    1    2    3    1    1    1    1    1    1    1    1    2    2    2    3    3 
##  186  187  188  189  190  191  192  193  194  196  197  198  199  201  202  203 
##    2    4    4    4    3    3    4    1    1    2    2    2    2    2    4    4 
##  204  205  206  208  209  210  211  212  213  214  215  216  217  218  219  220 
##    2    2    4    4    4    2    2    2    2    2    2    1    2    3    3    1 
##  221  222  223  224  225  226  228  229  230  231  232  233  234  235  236  237 
##    1    3    3    3    3    2    2    2    1    1    2    3    3    2    3    1 
##  238  239  240  241  242  243  244  245  246  247  248  249  250  251  252  253 
##    2    2    1    1    3    3    2    3    3    2    3    2    3    3    3    3 
##  254  255  256  257  258  259  260  261  262  263  264  265  266  267  268  269 
##    3    4    3    3    2    2    2    2    1    2    2    2    2    1    2    1 
##  270  271  272  273  274  275  276  277  278  279  280  281  282  283  285  286 
##    3    3    2    2    2    2    1    1    1    2    1    1    1    3    3    1 
##  287  288  289  290  291  292  293  294  295  296  297  298  299  300  301  302 
##    1    2    2    1    1    1    1    1    1    2    2    2    3    1    2    1 
##  303  304  305  306  307  308  309  312  313  314  315  316  317  318  319  320 
##    3    2    2    2    2    2    1    2    2    2    2    3    3    1    1    2 
##  321  323  324  325  326  327  328  329  330  331  332  333  334  335  343  344 
##    2    2    2    2    2    2    2    4    2    2    2    2    2    2    3    2 
##  345  346  347  348  349  350  352  353  354  355  356  358  359  360  361  364 
##    1    2    1    2    2    2    2    2    2    1    1    1    1    2    4    4 
##  365  366  367  368  369  370  371  372  373  374  376  377  378  379  380  381 
##    4    4    2    2    2    2    3    2    2    3    1    1    1    2    2    1 
##  382  383  384  385  386  387  388  389  390  391  392  393  394  395  396  397 
##    1    2    3    2    3    2    2    3    3    3    3    4    1    1    4    3 
##  398  399  400  401  402  403  404  405  406  407  408  409  410  411  412  413 
##    4    4    4    1    1    4    4    3    4    1    1    1    2    1    1    1 
##  414  415  416  417  418  419  422  423  424  425  426  427  428  429  430  431 
##    1    2    2    2    2    2    1    2    3    3    4    3    3    4    4    2 
##  432  433  434  435  436  437  438  439  441  442  443  444  445  446  447  448 
##    2    2    2    2    3    1    1    3    2    2    3    3    3    3    2    2 
##  449  450  451  452  453  454  455  456  457  458  459  460  461  462  463  464 
##    2    2    2    2    2    2    1    1    1    1    3    3    1    3    2    2 
##  465  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480 
##    1    1    2    2    2    1    2    2    3    3    2    1    1    1    1    1 
##  481  483  485  486  487  488  489  490  491  492  493  494  495  496  497  498 
##    2    1    2    2    2    1    1    1    1    1    1    2    1    2    1    1 
##  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514 
##    1    1    2    2    3    3    3    3    3    2    2    2    2    2    2    2 
##  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530 
##    1    1    4    3    2    2    4    2    4    4    1    1    1    2    3    3 
##  531  533  534  535  536  537  538  539  540  541  542  543  544  545  546  548 
##    3    3    4    4    4    1    1    1    1    1    1    1    4    4    2    4 
##  549  550  551  552  553  554  555  556  557  558  559  560  561  562  563  564 
##    4    2    2    2    4    3    3    4    1    3    4    2    3    1    1    2 
##  565  566  567  568  569  570  571  572  573  574  575  576  577  578  579  580 
##    3    1    1    2    2    2    2    2    2    2    2    1    3    4    4    4 
##  581  582  583  584  585  586  587  588  589  590  591  592  593  594  595  596 
##    3    1    1    1    2    2    1    1    2    1    1    2    1    1    1    2 
##  597  598  599  601  602  603  604  605  606  607  609  610  611  612  613  614 
##    1    1    1    2    1    1    2    1    1    1    2    2    2    2    1    1 
##  615  616  617  618  619  620  621  622  623  624  625  626  628  629  630  631 
##    2    2    2    3    2    2    3    1    1    2    2    2    2    2    2    2 
##  632  633  634  635  636  637  638  639  640  641  642  643  644  645  646  647 
##    1    1    1    3    1    2    1    1    2    3    2    3    1    1    1    1 
##  648  649  650  651  652  653  654  655  656  657  658  659  660  661  662  663 
##    1    2    3    1    1    1    3    4    3    1    1    1    1    1    1    3 
##  664  665  666  667  668  669  670  671  672  673  674  675  676  677  678  679 
##    3    4    4    4    4    4    4    3    4    4    4    2    2    1    4    2 
##  680  681  682  684  685  686  687  688  689  690  691  692  693  694  696  697 
##    2    2    2    1    3    2    2    4    4    2    2    2    4    4    2    1 
##  698  699  700  701  702  703  704  705  706  707  708  709  710  711  712  713 
##    1    2    1    1    1    3    3    3    3    3    3    1    3    1    1    1 
##  714  715  716  717  719  720  721  722  723  724  725  726  727  728  731  735 
##    2    1    1    1    1    1    3    1    1    1    1    3    4    4    4    2 
##  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750  751 
##    4    2    4    4    3    3    3    4    3    3    2    4    3    3    2    2 
##  752  753  754  755  757  758  759  760  761  762  763  764  765  766  767  768 
##    3    3    2    2    1    2    2    1    1    2    2    1    2    2    2    2 
##  769  770  771  772  773  774  775  776  777  778  779  780  781  782  783  784 
##    2    1    2    2    3    1    3    3    3    1    1    1    3    1    1    1 
##  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800 
##    1    1    1    1    2    2    1    1    4    4    4    1    3    2    4    4 
##  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816 
##    4    4    4    1    3    1    2    2    2    2    2    2    2    1    1    1 
##  817  819  820  821  822  823  824  825  826  827  828  829  830  831  832  833 
##    2    1    1    1    1    2    2    2    1    2    1    4    1    1    2    2 
##  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848  849 
##    1    4    1    2    2    2    1    2    2    2    3    3    3    1    3    2 
##  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864  866 
##    2    1    2    1    3    2    2    4    4    2    4    2    4    4    3    4 
##  867  868  869  871  872  873  874  875  876  877  878  879  880  881  882  883 
##    3    4    4    3    4    3    4    1    1    4    1    1    2    1    2    1 
##  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899 
##    1    2    2    1    1    1    2    1    2    2    3    3    2    2    1    2 
##  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915 
##    2    2    2    2    2    1    3    1    2    2    3    3    3    1    3    3 
##  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931 
##    3    3    2    3    2    3    2    1    2    1    2    2    1    2    2    2 
##  932  933  934  935  936  937  938  940  942  943  944  945  946  947  948  949 
##    1    2    4    4    4    4    4    2    2    2    2    2    1    3    2    2 
##  950  951  952  953  956  957  958  959  960  961  962  963  964  965  966  967 
##    3    3    2    1    1    1    2    2    2    3    3    3    3    2    3    3 
##  968  969  970  971  972  973  974  976  977  978  979  980  982  983  984  985 
##    2    1    1    3    4    3    1    2    2    3    3    3    2    2    2    2 
##  986  987  988  989  990  991  992  993  994  995  996  997  998  999 1000 1001 
##    1    3    3    1    3    3    2    2    4    3    3    3    1    2    3    1 
## 1002 1003 1004 1005 1006 1007 1008 1009 1011 1012 1013 1014 1015 1016 1017 1018 
##    3    4    3    4    1    2    2    2    1    1    2    3    3    3    4    4 
## 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 
##    2    2    2    1    3    3    3    4    4    1    2    1    3    1    1    1 
## 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 
##    1    1    1    2    2    2    2    2    2    2    2    4    2    2    2    2 
## 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1064 1065 1066 1067 
##    4    2    4    2    2    2    2    1    1    1    1    3    3    1    3    2 
## 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 
##    2    3    2    4    2    3    3    3    4    2    2    2    2    1    2    1 
## 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 
##    3    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2 
## 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 
##    2    2    1    2    1    1    2    1    2    2    4    2    4    2    2    2 
## 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 
##    2    2    2    3    3    3    1    3    2    2    2    2    2    2    4    4 
## 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 
##    4    2    2    4    2    4    4    3    4    4    2    2    2    3    2    2 
## 1148 1149 1150 1151 1152 1153 1154 1156 1157 1158 1160 1161 1162 1163 1164 1165 
##    2    2    2    2    1    2    2    1    2    1    2    2    2    1    1    1 
## 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 
##    2    4    4    1    3    1    4    2    4    3    3    2    3    3    4    4 
## 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 
##    1    2    2    1    2    2    3    1    3    3    4    2    2    4    2    4 
## 1198 1199 1200 1201 1202 1203 1204 1205 1207 1208 1209 1210 1211 1212 1213 1215 
##    2    2    2    2    2    2    1    1    2    1    2    2    2    2    2    2 
## 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 
##    2    1    2    2    2    2    1    2    1    2    2    1    2    1    4    4 
## 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 
##    4    3    4    4    3    3    2    2    1    2    2    2    1    1    2    2 
## 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 
##    2    3    1    2    4    2    4    4    2    4    4    4    3    4    2    2 
## 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 
##    2    2    3    2    1    3    2    3    4    4    1    2    2    4    2    2 
## 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 
##    2    2    1    1    1    1    2    1    1    1    2    2    2    2    2    2 
## 1296 1297 1298 1299 1300 1301 1302 1304 1305 1306 1307 1308 1309 1310 1311 1312 
##    3    1    3    2    2    2    2    1    1    1    1    2    2    2    2    2 
## 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 
##    2    2    2    2    1    2    2    2    2    1    2    2    2    1    2    1 
## 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 
##    1    1    1    4    1    3    2    3    3    3    3    2    2    2    2    2 
## 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 
##    1    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2 
## 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 
##    2    2    2    2    2    2    2    2    1    2    2    4    4    3    1    4 
## 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 
##    3    3    3    4    1    2    2    4    4    4    4    1    1    1    1    1 
## 1393 1394 1395 1396 1397 1398 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 
##    1    1    1    3    3    3    2    3    3    4    1    1    2    2    1    2 
## 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 
##    3    2    1    2    2    1    1    1    1    2    1    2    1    1    1    1 
## 1426 1427 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 
##    1    2    4    4    4    3    1    3    4    3    1    1    1    1    1    1 
## 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 
##    2    1    1    1    1    1    1    1    3    1    2    2    1    3    2    2 
## 
## Within cluster sum of squares by cluster:
## [1] 201.2980 329.1644 175.7229 180.1350
##  (between_SS / total_SS =  67.8 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"

That is a lot of output! - but pretty easy to walk through and understand. The algorithm even gives a within cluster sum of squares, which is a measure of the explained variance.

Let’s extract the mean vectors and plot for a more intuitive understanding of the results.

#getting centers
model$centers
##         lteq     posaff
## 1 -0.6558537 -1.0705106
## 2 -0.4408363  0.6996343
## 3  0.7763740 -0.5678819
## 4  1.7443674  0.7485388
#plotting clustered data points with k means
ggplot(dailyscale,aes(x=lteq,y=posaff)) +
  geom_point(color=model$cluster, alpha=.6) +#plotting all the points
  #plotting the centroids
  geom_point(aes(x=model$centers[1,1],y=model$centers[1,2]),color=1,size=5,shape=18) +
  geom_point(aes(x=model$centers[2,1],y=model$centers[2,2]),color=2,size=5,shape=18) +
  geom_point(aes(x=model$centers[3,1],y=model$centers[3,2]),color=3,size=5,shape=18) +
  geom_point(aes(x=model$centers[4,1],y=model$centers[4,2]),color=4,size=5,shape=18) 
## Warning in geom_point(aes(x = model$centers[1, 1], y = model$centers[1, : All aesthetics have length 1, but the data has 1376 rows.
## ℹ Did you mean to use `annotate()`?
## Warning in geom_point(aes(x = model$centers[2, 1], y = model$centers[2, : All aesthetics have length 1, but the data has 1376 rows.
## ℹ Did you mean to use `annotate()`?
## Warning in geom_point(aes(x = model$centers[3, 1], y = model$centers[3, : All aesthetics have length 1, but the data has 1376 rows.
## ℹ Did you mean to use `annotate()`?
## Warning in geom_point(aes(x = model$centers[4, 1], y = model$centers[4, : All aesthetics have length 1, but the data has 1376 rows.
## ℹ Did you mean to use `annotate()`?

16.8.6.1 Evaluation of Clustering Quality

Numerous measures are available for evaluating a clustering. Many are stored within the model object returned by kmeans().

A basic concept for evaluating the quality of the clusters is the sum of squares. This is typically a sum of the square of the distances between observations.

model$totss
## [1] 2750
model$withinss
## [1] 201.2980 329.1644 175.7229 180.1350
model$tot.withinss
## [1] 886.3203
model$betweenss
## [1] 1863.68

Evaluation of the sum of squares can help us both evaluate the quality of any given solution, as well as help us choose the number of clusters, k, needed to describe the data.

Evaluation: Within Sum of Squares

The within sum of squares is a measure of how close the observations are within the clusters. For a single cluster this is calculated as the average squared distance of each observation within the cluster from the cluster mean. Then the total within sum of squares is the sum of the within sum of squares over all clusters.

The total within sum of squares generally decreases as the number of clusters increases. As we increase the number of clusters they individually tend to become smaller and the observations closer together within the clusters. As k increases, the changes in the total within sum of squares would be expected to reduce, and so it flattens out. A good value of k might be where the reduction in the total weighted sum of squares begins to flatten.

General rule of thumb: Aim to minimize the total within sum of squares (achieve within-group similarity).

Evaluation: Between Sum of Squares

The between sum or squares is a measure of how far the clusters are from each other.

General rule of thumb: Aim to maximize the between sum of squares (achieve between-group dissimilarity).

A good clustering will have a small within sum of squares and a large between sum of squares.

So, we need to have a range of solutions to see how the within and between sum of squares looks with different k.

#making a empty dataframe
criteria <- data.frame()
#setting range of k                   
nk <- 1:20
#loop for range of clusters
for (k in nk) {
model <- kmeans(dailyscale[,c("lteq","posaff")], k)
criteria <- rbind(criteria,c(k,model$tot.withinss,model$betweenss,model$totss))
}
#renaming columns
names(criteria) <- c("k","tot.withinss","betweenss","totalss")

#scree plot
ggplot(criteria, aes(x=k)) +
  geom_point(aes(y=tot.withinss),color="red") +
  geom_line(aes(y=tot.withinss),color="red") +
  geom_point(aes(y=betweenss),color="blue") +
  geom_line(aes(y=betweenss),color="blue") +
  xlab("k = number of clusters") + ylab("Sum of Squares (within = red, between = blue)")

#looking at criteria
round(criteria,2)
##     k tot.withinss betweenss totalss
## 1   1      2750.00      0.00    2750
## 2   2      1786.49    963.51    2750
## 3   3      1082.70   1667.30    2750
## 4   4       877.93   1872.07    2750
## 5   5       729.55   2020.45    2750
## 6   6       584.64   2165.36    2750
## 7   7       498.26   2251.74    2750
## 8   8       445.17   2304.83    2750
## 9   9       396.60   2353.40    2750
## 10 10       360.53   2389.47    2750
## 11 11       326.35   2423.65    2750
## 12 12       306.13   2443.87    2750
## 13 13       288.19   2461.81    2750
## 14 14       261.78   2488.22    2750
## 15 15       262.64   2487.36    2750
## 16 16       232.66   2517.34    2750
## 17 17       216.25   2533.75    2750
## 18 18       208.83   2541.17    2750
## 19 19       195.18   2554.82    2750
## 20 20       188.38   2561.62    2750

From the scree plot, we might look for 6 clusters (but it is really hard to see any “elbow”).

There are also additional quantitative criteria that can be used to inform selection.

For example, the Calinski-Harabasz criterion, also known as the variance ratio criterion, is the ratio of the between sum of squares (divided by k - 1) to the within sum of squares (divided by n - k).

The relative values can be used to compare clusterings of a single dataset, with higher values being better clusterings. The criterion is said to work best for spherical clusters with compact centers (as with normally distributed data) using k-means with Euclidean distance.

And of course this is a well-trodden area of research so there are many criteria - and packages that calculate them for you - and make automated choices.

#from library(fpc)
# Calinski-Harabasz
model.manyCH <- kmeansruns(dailyscale[,c("lteq","posaff")], krange=c(2:20), criterion="ch",critout = TRUE) 
## 2  clusters  745.4072 
## 3  clusters  1057.359 
## 4  clusters  975.2011 
## 5  clusters  978.5366 
## 6  clusters  1019.07 
## 7  clusters  1033.765 
## 8  clusters  1016.999 
## 9  clusters  1015.787 
## 10  clusters  1010.407 
## 11  clusters  1016.261 
## 12  clusters  1025.676 
## 13  clusters  1016.533 
## 14  clusters  1010.001 
## 15  clusters  1007.922 
## 16  clusters  1010.128 
## 17  clusters  1010.531 
## 18  clusters  1019.647 
## 19  clusters  1025.941 
## 20  clusters  1023.964
model.manyCH
## K-means clustering with 3 clusters of sizes 484, 261, 631
## 
## Cluster means:
##         lteq     posaff
## 1 -0.4338542 -1.0041922
## 2  1.6409244  0.1951617
## 3 -0.3459522  0.6895275
## 
## Clustering vector:
##    1    2    3    4    5    6    7    9   10   11   13   14   15   17   18   19 
##    1    1    3    3    3    2    2    3    3    3    3    3    1    3    3    3 
##   20   21   22   23   27   28   29   30   31   32   37   38   39   40   41   42 
##    1    1    1    3    1    1    1    1    3    3    3    3    3    3    1    3 
##   43   44   47   48   49   51   52   53   54   55   56   57   58   59   60   61 
##    3    1    3    3    3    3    3    3    3    1    2    1    1    1    1    1 
##   62   63   64   66   67   68   70   71   72   73   74   75   76   77   78   79 
##    2    3    3    1    1    3    3    3    3    3    3    3    3    1    3    3 
##   81   84   85   86   87   88   89   90   91   92   93   95   96   97   98   99 
##    3    1    1    1    3    3    3    2    2    2    2    3    3    3    3    2 
##  101  102  103  105  106  107  108  110  111  112  113  114  115  116  117  118 
##    2    2    3    1    2    2    2    3    2    2    2    2    2    3    2    2 
##  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134 
##    2    1    1    1    1    1    1    1    1    3    3    3    3    3    3    3 
##  135  136  137  138  140  141  143  144  145  146  147  149  150  151  152  153 
##    3    2    2    3    2    3    2    2    3    2    2    2    2    2    1    2 
##  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169 
##    3    3    3    2    3    3    3    3    3    3    3    3    3    1    3    3 
##  170  171  172  173  174  175  176  177  178  179  180  181  182  183  184  185 
##    1    3    3    1    1    1    1    1    1    1    1    3    3    3    1    3 
##  186  187  188  189  190  191  192  193  194  196  197  198  199  201  202  203 
##    3    2    2    2    1    2    2    1    1    3    3    3    3    3    2    2 
##  204  205  206  208  209  210  211  212  213  214  215  216  217  218  219  220 
##    3    3    2    2    2    3    3    3    3    3    3    1    3    1    1    1 
##  221  222  223  224  225  226  228  229  230  231  232  233  234  235  236  237 
##    1    2    1    1    1    3    3    3    1    1    3    3    3    3    1    1 
##  238  239  240  241  242  243  244  245  246  247  248  249  250  251  252  253 
##    3    1    1    1    3    3    3    2    2    3    2    3    2    1    2    2 
##  254  255  256  257  258  259  260  261  262  263  264  265  266  267  268  269 
##    1    2    2    2    3    3    3    3    1    3    3    3    3    1    3    1 
##  270  271  272  273  274  275  276  277  278  279  280  281  282  283  285  286 
##    2    3    3    3    3    3    1    1    1    3    1    1    1    1    1    1 
##  287  288  289  290  291  292  293  294  295  296  297  298  299  300  301  302 
##    1    1    3    1    1    1    1    1    1    3    3    3    1    1    3    1 
##  303  304  305  306  307  308  309  312  313  314  315  316  317  318  319  320 
##    3    3    3    3    3    3    1    3    3    3    3    3    2    1    1    3 
##  321  323  324  325  326  327  328  329  330  331  332  333  334  335  343  344 
##    3    3    3    3    3    3    3    3    3    3    3    3    3    3    1    3 
##  345  346  347  348  349  350  352  353  354  355  356  358  359  360  361  364 
##    1    3    1    3    3    3    3    3    3    1    1    1    1    3    3    2 
##  365  366  367  368  369  370  371  372  373  374  376  377  378  379  380  381 
##    2    2    3    3    3    3    3    3    3    3    1    1    1    3    3    1 
##  382  383  384  385  386  387  388  389  390  391  392  393  394  395  396  397 
##    1    3    1    3    2    3    3    1    1    3    2    2    1    1    2    2 
##  398  399  400  401  402  403  404  405  406  407  408  409  410  411  412  413 
##    2    2    2    1    1    2    2    1    2    1    1    1    3    1    1    1 
##  414  415  416  417  418  419  422  423  424  425  426  427  428  429  430  431 
##    1    3    3    3    3    3    1    3    2    3    2    2    3    2    2    3 
##  432  433  434  435  436  437  438  439  441  442  443  444  445  446  447  448 
##    3    3    3    3    3    1    1    2    3    3    1    1    2    2    3    3 
##  449  450  451  452  453  454  455  456  457  458  459  460  461  462  463  464 
##    3    3    3    3    3    3    1    1    1    1    1    1    1    1    3    3 
##  465  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480 
##    1    1    3    3    3    1    3    3    2    3    3    1    1    1    1    1 
##  481  483  485  486  487  488  489  490  491  492  493  494  495  496  497  498 
##    3    1    3    3    3    1    1    1    1    1    1    3    1    3    1    1 
##  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514 
##    1    1    3    3    1    1    1    1    1    3    3    3    3    3    3    3 
##  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530 
##    1    1    3    2    3    3    2    3    2    2    1    1    1    3    2    1 
##  531  533  534  535  536  537  538  539  540  541  542  543  544  545  546  548 
##    2    1    2    2    2    1    1    1    1    1    1    1    3    2    3    2 
##  549  550  551  552  553  554  555  556  557  558  559  560  561  562  563  564 
##    2    3    3    3    2    2    2    2    1    2    2    3    3    1    1    3 
##  565  566  567  568  569  570  571  572  573  574  575  576  577  578  579  580 
##    2    1    1    3    3    3    3    3    3    3    3    1    2    2    2    2 
##  581  582  583  584  585  586  587  588  589  590  591  592  593  594  595  596 
##    1    1    1    1    3    3    1    1    3    1    1    3    1    1    1    3 
##  597  598  599  601  602  603  604  605  606  607  609  610  611  612  613  614 
##    1    1    1    3    1    1    3    1    1    1    3    3    3    3    1    1 
##  615  616  617  618  619  620  621  622  623  624  625  626  628  629  630  631 
##    3    3    3    3    3    3    1    1    1    3    3    3    3    3    3    3 
##  632  633  634  635  636  637  638  639  640  641  642  643  644  645  646  647 
##    1    1    1    1    1    3    1    1    3    3    3    1    1    1    1    1 
##  648  649  650  651  652  653  654  655  656  657  658  659  660  661  662  663 
##    1    3    1    1    1    1    3    2    2    1    1    1    1    1    1    1 
##  664  665  666  667  668  669  670  671  672  673  674  675  676  677  678  679 
##    2    2    2    2    2    2    2    2    2    2    2    3    3    1    2    3 
##  680  681  682  684  685  686  687  688  689  690  691  692  693  694  696  697 
##    3    3    3    1    1    3    3    2    2    3    3    3    2    2    3    1 
##  698  699  700  701  702  703  704  705  706  707  708  709  710  711  712  713 
##    1    3    1    1    1    1    2    2    2    2    1    1    2    1    1    1 
##  714  715  716  717  719  720  721  722  723  724  725  726  727  728  731  735 
##    3    1    1    1    1    1    3    1    1    1    1    1    2    2    2    3 
##  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750  751 
##    2    3    3    2    1    1    1    2    3    3    3    2    1    2    3    3 
##  752  753  754  755  757  758  759  760  761  762  763  764  765  766  767  768 
##    1    1    3    3    1    3    1    1    1    3    3    1    3    3    3    3 
##  769  770  771  772  773  774  775  776  777  778  779  780  781  782  783  784 
##    3    1    3    3    1    1    2    1    2    1    1    1    1    1    1    1 
##  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800 
##    1    1    1    1    3    3    1    1    2    2    2    1    2    3    2    2 
##  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816 
##    2    2    2    1    2    1    3    3    3    3    3    3    3    1    1    1 
##  817  819  820  821  822  823  824  825  826  827  828  829  830  831  832  833 
##    3    1    1    1    1    3    3    3    1    3    1    2    1    1    3    3 
##  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848  849 
##    1    2    1    3    3    3    1    3    3    3    3    1    1    1    2    3 
##  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864  866 
##    3    1    3    1    2    3    3    3    2    3    2    3    2    2    2    2 
##  867  868  869  871  872  873  874  875  876  877  878  879  880  881  882  883 
##    3    2    2    3    2    2    2    1    1    2    1    1    3    1    3    1 
##  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899 
##    1    3    3    1    1    1    3    1    3    3    3    1    3    3    1    3 
##  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915 
##    3    3    3    3    3    1    1    1    3    3    1    3    1    1    2    2 
##  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931 
##    1    2    3    3    3    3    1    1    3    1    3    3    1    3    3    3 
##  932  933  934  935  936  937  938  940  942  943  944  945  946  947  948  949 
##    1    3    2    2    2    2    2    3    3    3    3    3    1    1    1    3 
##  950  951  952  953  956  957  958  959  960  961  962  963  964  965  966  967 
##    2    3    3    1    1    1    3    3    3    1    1    1    1    3    2    2 
##  968  969  970  971  972  973  974  976  977  978  979  980  982  983  984  985 
##    3    1    1    2    2    2    1    3    3    2    1    1    3    3    3    1 
##  986  987  988  989  990  991  992  993  994  995  996  997  998  999 1000 1001 
##    1    2    1    1    3    2    3    3    2    1    2    1    1    3    2    1 
## 1002 1003 1004 1005 1006 1007 1008 1009 1011 1012 1013 1014 1015 1016 1017 1018 
##    2    2    1    2    1    3    3    3    1    1    3    1    1    2    2    2 
## 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 
##    3    3    3    1    2    1    3    2    2    1    3    1    1    1    1    1 
## 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 
##    1    1    1    3    3    3    3    3    3    3    3    3    3    3    3    3 
## 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1064 1065 1066 1067 
##    2    3    2    3    3    3    3    1    1    1    1    3    2    1    1    3 
## 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 
##    3    3    3    2    3    2    1    2    2    3    3    3    3    1    3    1 
## 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 
##    3    3    3    3    3    3    3    3    3    3    3    3    3    3    3    3 
## 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 
##    3    3    1    3    1    1    3    1    3    3    2    3    3    3    3    3 
## 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 
##    3    3    3    3    2    2    1    2    3    1    3    3    3    3    2    2 
## 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 
##    2    3    3    2    3    2    2    2    2    2    3    3    3    3    3    3 
## 1148 1149 1150 1151 1152 1153 1154 1156 1157 1158 1160 1161 1162 1163 1164 1165 
##    3    3    3    3    1    3    3    1    3    1    3    3    3    1    1    1 
## 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 
##    3    2    2    1    1    1    2    3    2    2    1    3    1    3    2    3 
## 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 
##    1    3    3    1    3    3    1    1    1    2    2    1    3    2    3    2 
## 1198 1199 1200 1201 1202 1203 1204 1205 1207 1208 1209 1210 1211 1212 1213 1215 
##    3    3    3    3    3    3    1    1    3    1    3    3    3    3    3    3 
## 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 
##    3    1    3    3    3    3    1    3    1    3    3    1    3    1    2    2 
## 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 
##    2    2    2    2    1    1    3    3    1    3    3    3    1    1    1    3 
## 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 
##    3    3    1    3    2    3    2    3    3    2    2    2    2    2    3    3 
## 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 
##    3    3    1    3    1    3    3    3    2    2    1    3    3    2    3    3 
## 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 
##    3    3    1    1    1    1    3    1    1    1    3    3    3    3    3    3 
## 1296 1297 1298 1299 1300 1301 1302 1304 1305 1306 1307 1308 1309 1310 1311 1312 
##    1    1    1    3    3    3    3    1    1    1    1    3    3    3    3    3 
## 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 
##    3    1    3    3    1    3    3    3    3    1    3    3    3    1    3    1 
## 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 
##    1    1    1    2    1    2    3    1    2    2    2    3    3    3    3    3 
## 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 
##    1    3    3    3    3    3    3    3    3    3    3    3    3    3    3    3 
## 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 
##    3    3    3    3    3    3    3    3    1    3    3    2    2    1    1    2 
## 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 
##    2    2    2    2    1    3    3    2    3    2    3    1    1    1    1    1 
## 1393 1394 1395 1396 1397 1398 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 
##    1    1    1    2    1    2    3    2    1    2    1    1    3    3    1    3 
## 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 
##    1    3    1    3    3    1    1    1    1    3    1    3    1    1    1    1 
## 1426 1427 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 
##    1    3    2    2    2    2    1    2    2    3    1    1    1    1    1    1 
## 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 
##    3    1    1    1    1    1    1    1    2    1    3    3    1    1    3    3 
## 
## Within cluster sum of squares by cluster:
## [1] 334.8595 334.4631 413.2618
##  (between_SS / total_SS =  60.6 %)
## 
## Available components:
## 
##  [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
##  [6] "betweenss"    "size"         "iter"         "ifault"       "crit"        
## [11] "bestk"
#another criteria 
# average silhouette width
model.manyASW <- kmeansruns(dailyscale[,c("lteq","posaff")], krange=c(2:20), criterion="asw",critout = TRUE) 
## 2  clusters  0.3315195 
## 3  clusters  0.3884696 
## 4  clusters  0.3543075 
## 5  clusters  0.3425395 
## 6  clusters  0.3502535 
## 7  clusters  0.3569903 
## 8  clusters  0.3417595 
## 9  clusters  0.347614 
## 10  clusters  0.3521761 
## 11  clusters  0.3606332 
## 12  clusters  0.3620871 
## 13  clusters  0.3577258 
## 14  clusters  0.3577506 
## 15  clusters  0.3558161 
## 16  clusters  0.3615249 
## 17  clusters  0.360369 
## 18  clusters  0.3470803 
## 19  clusters  0.3568075 
## 20  clusters  0.3531087
model.manyASW
## K-means clustering with 3 clusters of sizes 631, 484, 261
## 
## Cluster means:
##         lteq     posaff
## 1 -0.3459522  0.6895275
## 2 -0.4338542 -1.0041922
## 3  1.6409244  0.1951617
## 
## Clustering vector:
##    1    2    3    4    5    6    7    9   10   11   13   14   15   17   18   19 
##    2    2    1    1    1    3    3    1    1    1    1    1    2    1    1    1 
##   20   21   22   23   27   28   29   30   31   32   37   38   39   40   41   42 
##    2    2    2    1    2    2    2    2    1    1    1    1    1    1    2    1 
##   43   44   47   48   49   51   52   53   54   55   56   57   58   59   60   61 
##    1    2    1    1    1    1    1    1    1    2    3    2    2    2    2    2 
##   62   63   64   66   67   68   70   71   72   73   74   75   76   77   78   79 
##    3    1    1    2    2    1    1    1    1    1    1    1    1    2    1    1 
##   81   84   85   86   87   88   89   90   91   92   93   95   96   97   98   99 
##    1    2    2    2    1    1    1    3    3    3    3    1    1    1    1    3 
##  101  102  103  105  106  107  108  110  111  112  113  114  115  116  117  118 
##    3    3    1    2    3    3    3    1    3    3    3    3    3    1    3    3 
##  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134 
##    3    2    2    2    2    2    2    2    2    1    1    1    1    1    1    1 
##  135  136  137  138  140  141  143  144  145  146  147  149  150  151  152  153 
##    1    3    3    1    3    1    3    3    1    3    3    3    3    3    2    3 
##  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169 
##    1    1    1    3    1    1    1    1    1    1    1    1    1    2    1    1 
##  170  171  172  173  174  175  176  177  178  179  180  181  182  183  184  185 
##    2    1    1    2    2    2    2    2    2    2    2    1    1    1    2    1 
##  186  187  188  189  190  191  192  193  194  196  197  198  199  201  202  203 
##    1    3    3    3    2    3    3    2    2    1    1    1    1    1    3    3 
##  204  205  206  208  209  210  211  212  213  214  215  216  217  218  219  220 
##    1    1    3    3    3    1    1    1    1    1    1    2    1    2    2    2 
##  221  222  223  224  225  226  228  229  230  231  232  233  234  235  236  237 
##    2    3    2    2    2    1    1    1    2    2    1    1    1    1    2    2 
##  238  239  240  241  242  243  244  245  246  247  248  249  250  251  252  253 
##    1    2    2    2    1    1    1    3    3    1    3    1    3    2    3    3 
##  254  255  256  257  258  259  260  261  262  263  264  265  266  267  268  269 
##    2    3    3    3    1    1    1    1    2    1    1    1    1    2    1    2 
##  270  271  272  273  274  275  276  277  278  279  280  281  282  283  285  286 
##    3    1    1    1    1    1    2    2    2    1    2    2    2    2    2    2 
##  287  288  289  290  291  292  293  294  295  296  297  298  299  300  301  302 
##    2    2    1    2    2    2    2    2    2    1    1    1    2    2    1    2 
##  303  304  305  306  307  308  309  312  313  314  315  316  317  318  319  320 
##    1    1    1    1    1    1    2    1    1    1    1    1    3    2    2    1 
##  321  323  324  325  326  327  328  329  330  331  332  333  334  335  343  344 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    2    1 
##  345  346  347  348  349  350  352  353  354  355  356  358  359  360  361  364 
##    2    1    2    1    1    1    1    1    1    2    2    2    2    1    1    3 
##  365  366  367  368  369  370  371  372  373  374  376  377  378  379  380  381 
##    3    3    1    1    1    1    1    1    1    1    2    2    2    1    1    2 
##  382  383  384  385  386  387  388  389  390  391  392  393  394  395  396  397 
##    2    1    2    1    3    1    1    2    2    1    3    3    2    2    3    3 
##  398  399  400  401  402  403  404  405  406  407  408  409  410  411  412  413 
##    3    3    3    2    2    3    3    2    3    2    2    2    1    2    2    2 
##  414  415  416  417  418  419  422  423  424  425  426  427  428  429  430  431 
##    2    1    1    1    1    1    2    1    3    1    3    3    1    3    3    1 
##  432  433  434  435  436  437  438  439  441  442  443  444  445  446  447  448 
##    1    1    1    1    1    2    2    3    1    1    2    2    3    3    1    1 
##  449  450  451  452  453  454  455  456  457  458  459  460  461  462  463  464 
##    1    1    1    1    1    1    2    2    2    2    2    2    2    2    1    1 
##  465  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480 
##    2    2    1    1    1    2    1    1    3    1    1    2    2    2    2    2 
##  481  483  485  486  487  488  489  490  491  492  493  494  495  496  497  498 
##    1    2    1    1    1    2    2    2    2    2    2    1    2    1    2    2 
##  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514 
##    2    2    1    1    2    2    2    2    2    1    1    1    1    1    1    1 
##  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530 
##    2    2    1    3    1    1    3    1    3    3    2    2    2    1    3    2 
##  531  533  534  535  536  537  538  539  540  541  542  543  544  545  546  548 
##    3    2    3    3    3    2    2    2    2    2    2    2    1    3    1    3 
##  549  550  551  552  553  554  555  556  557  558  559  560  561  562  563  564 
##    3    1    1    1    3    3    3    3    2    3    3    1    1    2    2    1 
##  565  566  567  568  569  570  571  572  573  574  575  576  577  578  579  580 
##    3    2    2    1    1    1    1    1    1    1    1    2    3    3    3    3 
##  581  582  583  584  585  586  587  588  589  590  591  592  593  594  595  596 
##    2    2    2    2    1    1    2    2    1    2    2    1    2    2    2    1 
##  597  598  599  601  602  603  604  605  606  607  609  610  611  612  613  614 
##    2    2    2    1    2    2    1    2    2    2    1    1    1    1    2    2 
##  615  616  617  618  619  620  621  622  623  624  625  626  628  629  630  631 
##    1    1    1    1    1    1    2    2    2    1    1    1    1    1    1    1 
##  632  633  634  635  636  637  638  639  640  641  642  643  644  645  646  647 
##    2    2    2    2    2    1    2    2    1    1    1    2    2    2    2    2 
##  648  649  650  651  652  653  654  655  656  657  658  659  660  661  662  663 
##    2    1    2    2    2    2    1    3    3    2    2    2    2    2    2    2 
##  664  665  666  667  668  669  670  671  672  673  674  675  676  677  678  679 
##    3    3    3    3    3    3    3    3    3    3    3    1    1    2    3    1 
##  680  681  682  684  685  686  687  688  689  690  691  692  693  694  696  697 
##    1    1    1    2    2    1    1    3    3    1    1    1    3    3    1    2 
##  698  699  700  701  702  703  704  705  706  707  708  709  710  711  712  713 
##    2    1    2    2    2    2    3    3    3    3    2    2    3    2    2    2 
##  714  715  716  717  719  720  721  722  723  724  725  726  727  728  731  735 
##    1    2    2    2    2    2    1    2    2    2    2    2    3    3    3    1 
##  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750  751 
##    3    1    1    3    2    2    2    3    1    1    1    3    2    3    1    1 
##  752  753  754  755  757  758  759  760  761  762  763  764  765  766  767  768 
##    2    2    1    1    2    1    2    2    2    1    1    2    1    1    1    1 
##  769  770  771  772  773  774  775  776  777  778  779  780  781  782  783  784 
##    1    2    1    1    2    2    3    2    3    2    2    2    2    2    2    2 
##  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800 
##    2    2    2    2    1    1    2    2    3    3    3    2    3    1    3    3 
##  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816 
##    3    3    3    2    3    2    1    1    1    1    1    1    1    2    2    2 
##  817  819  820  821  822  823  824  825  826  827  828  829  830  831  832  833 
##    1    2    2    2    2    1    1    1    2    1    2    3    2    2    1    1 
##  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848  849 
##    2    3    2    1    1    1    2    1    1    1    1    2    2    2    3    1 
##  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864  866 
##    1    2    1    2    3    1    1    1    3    1    3    1    3    3    3    3 
##  867  868  869  871  872  873  874  875  876  877  878  879  880  881  882  883 
##    1    3    3    1    3    3    3    2    2    3    2    2    1    2    1    2 
##  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899 
##    2    1    1    2    2    2    1    2    1    1    1    2    1    1    2    1 
##  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915 
##    1    1    1    1    1    2    2    2    1    1    2    1    2    2    3    3 
##  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931 
##    2    3    1    1    1    1    2    2    1    2    1    1    2    1    1    1 
##  932  933  934  935  936  937  938  940  942  943  944  945  946  947  948  949 
##    2    1    3    3    3    3    3    1    1    1    1    1    2    2    2    1 
##  950  951  952  953  956  957  958  959  960  961  962  963  964  965  966  967 
##    3    1    1    2    2    2    1    1    1    2    2    2    2    1    3    3 
##  968  969  970  971  972  973  974  976  977  978  979  980  982  983  984  985 
##    1    2    2    3    3    3    2    1    1    3    2    2    1    1    1    2 
##  986  987  988  989  990  991  992  993  994  995  996  997  998  999 1000 1001 
##    2    3    2    2    1    3    1    1    3    2    3    2    2    1    3    2 
## 1002 1003 1004 1005 1006 1007 1008 1009 1011 1012 1013 1014 1015 1016 1017 1018 
##    3    3    2    3    2    1    1    1    2    2    1    2    2    3    3    3 
## 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 
##    1    1    1    2    3    2    1    3    3    2    1    2    2    2    2    2 
## 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 
##    2    2    2    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1064 1065 1066 1067 
##    3    1    3    1    1    1    1    2    2    2    2    1    3    2    2    1 
## 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 
##    1    1    1    3    1    3    2    3    3    1    1    1    1    2    1    2 
## 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 
##    1    1    2    1    2    2    1    2    1    1    3    1    1    1    1    1 
## 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 
##    1    1    1    1    3    3    2    3    1    2    1    1    1    1    3    3 
## 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 
##    3    1    1    3    1    3    3    3    3    3    1    1    1    1    1    1 
## 1148 1149 1150 1151 1152 1153 1154 1156 1157 1158 1160 1161 1162 1163 1164 1165 
##    1    1    1    1    2    1    1    2    1    2    1    1    1    2    2    2 
## 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 
##    1    3    3    2    2    2    3    1    3    3    2    1    2    1    3    1 
## 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 
##    2    1    1    2    1    1    2    2    2    3    3    2    1    3    1    3 
## 1198 1199 1200 1201 1202 1203 1204 1205 1207 1208 1209 1210 1211 1212 1213 1215 
##    1    1    1    1    1    1    2    2    1    2    1    1    1    1    1    1 
## 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 
##    1    2    1    1    1    1    2    1    2    1    1    2    1    2    3    3 
## 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 
##    3    3    3    3    2    2    1    1    2    1    1    1    2    2    2    1 
## 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 
##    1    1    2    1    3    1    3    1    1    3    3    3    3    3    1    1 
## 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 
##    1    1    2    1    2    1    1    1    3    3    2    1    1    3    1    1 
## 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 
##    1    1    2    2    2    2    1    2    2    2    1    1    1    1    1    1 
## 1296 1297 1298 1299 1300 1301 1302 1304 1305 1306 1307 1308 1309 1310 1311 1312 
##    2    2    2    1    1    1    1    2    2    2    2    1    1    1    1    1 
## 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 
##    1    2    1    1    2    1    1    1    1    2    1    1    1    2    1    2 
## 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 
##    2    2    2    3    2    3    1    2    3    3    3    1    1    1    1    1 
## 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 
##    2    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 
##    1    1    1    1    1    1    1    1    2    1    1    3    3    2    2    3 
## 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 
##    3    3    3    3    2    1    1    3    1    3    1    2    2    2    2    2 
## 1393 1394 1395 1396 1397 1398 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 
##    2    2    2    3    2    3    1    3    2    3    2    2    1    1    2    1 
## 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 
##    2    1    2    1    1    2    2    2    2    1    2    1    2    2    2    2 
## 1426 1427 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 
##    2    1    3    3    3    3    2    3    3    1    2    2    2    2    2    2 
## 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 
##    1    2    2    2    2    2    2    2    3    2    1    1    2    2    1    1 
## 
## Within cluster sum of squares by cluster:
## [1] 413.2618 334.8595 334.4631
##  (between_SS / total_SS =  60.6 %)
## 
## Available components:
## 
##  [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
##  [6] "betweenss"    "size"         "iter"         "ifault"       "crit"        
## [11] "bestk"

Don’t just pick an index that shows your solution but check out the next point that talks about the stability of the solution. There is not “standard reporting” of cluster analysis results in the psychological literature. Different authors report different things - but all are using some metrics to justify the choice of k, and to support why the chosen cluster solution is a good description of the data.

16.8.6.2 Obtaining a Stable Solution

Recall that k-means begins the iterations with a random cluster assignment. Different starting points may lead to different solutions. So, it may be useful to start many times to locate a stable solution. This is automated within the kmeans() function.

#kmeans with nstart = 1
km.res <- kmeans(dailyscale[,c("lteq","posaff")], centers=4, nstart = 1)
km.res$tot.withinss
## [1] 887.3456
#kmeans with nstart = 25
km.res <- kmeans(dailyscale[,c("lteq","posaff")], centers=4, nstart = 25)
km.res$tot.withinss
## [1] 877.9312
#kmeans with nstart = 50
km.res <- kmeans(dailyscale[,c("lteq","posaff")], centers=4, nstart = 50)
km.res$tot.withinss
## [1] 877.9312

The improvement can be seen over the single random start.

Recommended to do 25+ or 50 for stable solutions.

Replication

It may also be informative to repeat the procedure on randomly selected portions of the sample. If the cluster solution replicates in (random) subsets of the data - that would be strong evidence that the typology is pervasive and meaningful.

16.8.6.3 After Clustering

After finding a suitable cluster solution, each individual is placed in a cluster. Formally, we obtain a vector of cluster assignments - a new categorical, grouping variable.

What’s next?

Well, we can both describe the clusters and use this new cluster variable in some other analysis - ANOVAs to test group differences, Chi-square tests, Multinomial regressions … the cluster variable can be used as a predictor, a correlate, an outcome (e.g., check whether those clusters are for example differ across personality variables etc.)

16.8.6.4 Describing Clusters

First we merge the vector of cluster assignments back into the data set.

dailyscale.clus <- cbind(km.res$cluster,dailyscale)
names(dailyscale.clus)[1] <- "cluster"
head(dailyscale.clus[,c(1:4,6)],4)
##   cluster   id     slphrs         weath        pss
## 1       2 1010 -0.6623479 -0.7732500468 -0.1732048
## 2       2 1011 -2.8774184 -0.0005615469  0.1923317
## 3       3 1012  0.9989549  0.7721269531  1.2889411
## 4       3 1013  0.1683035 -0.0005615469  0.5578682

We can describe the different clusters - and potentially name the clusters.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ psych::%+%()         masks ggplot2::%+%()
## ✖ psych::alpha()       masks ggplot2::alpha()
## ✖ dplyr::arrange()     masks plyr::arrange()
## ✖ dplyr::between()     masks data.table::between()
## ✖ dplyr::collapse()    masks nlme::collapse()
## ✖ purrr::compact()     masks plyr::compact()
## ✖ dplyr::count()       masks plyr::count()
## ✖ dplyr::desc()        masks plyr::desc()
## ✖ tidyr::expand()      masks reshape::expand(), Matrix::expand()
## ✖ dplyr::failwith()    masks plyr::failwith()
## ✖ dplyr::filter()      masks stats::filter()
## ✖ dplyr::first()       masks data.table::first()
## ✖ lubridate::hour()    masks data.table::hour()
## ✖ dplyr::id()          masks plyr::id()
## ✖ lubridate::isoweek() masks data.table::isoweek()
## ✖ dplyr::lag()         masks stats::lag()
## ✖ dplyr::last()        masks data.table::last()
## ✖ lubridate::mday()    masks data.table::mday()
## ✖ lubridate::minute()  masks data.table::minute()
## ✖ lubridate::month()   masks data.table::month()
## ✖ dplyr::mutate()      masks plyr::mutate()
## ✖ tidyr::pack()        masks Matrix::pack()
## ✖ lubridate::quarter() masks data.table::quarter()
## ✖ dplyr::rename()      masks reshape::rename(), plyr::rename()
## ✖ lubridate::second()  masks data.table::second()
## ✖ lubridate::stamp()   masks reshape::stamp()
## ✖ dplyr::summarise()   masks plyr::summarise()
## ✖ dplyr::summarize()   masks plyr::summarize()
## ✖ purrr::transpose()   masks data.table::transpose()
## ✖ tidyr::unpack()      masks Matrix::unpack()
## ✖ lubridate::wday()    masks data.table::wday()
## ✖ lubridate::week()    masks data.table::week()
## ✖ lubridate::yday()    masks data.table::yday()
## ✖ lubridate::year()    masks data.table::year()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Gather the data to 'long' format so the clustering variables are all in one column
#gather() has been replaced by pivot_longer()
longdata <- dailyscale.clus %>%
  pivot_longer(c(lteq, posaff), names_to = "variable", values_to = "value")

# Create the summary statistics seperately for cluster and variable (i.e. lteq, posaff)
summary <- longdata %>%
             group_by(cluster, variable) %>%
             summarise(mean = mean(value), se = sd(value) / length(value))
## `summarise()` has grouped output by 'cluster'. You can override using the
## `.groups` argument.
# Plot
ggplot(summary, aes(x = variable, y = mean, fill = variable)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se),                            
                  width = 0.2,
                  position = position_dodge(0.9)) +
  facet_wrap(~cluster) 

From this plot we can see the multivariate “profile” of each cluster - and use that to name the clusters. There can be some label switching, depending on random starting values, in terms of the cluster membership identifiers.

There are 4 profiles:

  • Vigorous Exercisers

  • Happy Sedentary

  • Happy Exercisers

  • Unhappy Sedentary

16.8.6.5 Analyzing Clusters

Now that we have clusters = groups, we can analyze them. For example, we can take our 4-cluster solution and see if the clusters differ on another variable.

Let’s see how the cluster groups differ on perceived stress (pss).

fit1 <- aov(pss ~ factor(km.res$cluster), data=dailyscale.clus)
summary(fit1)
##                          Df Sum Sq Mean Sq F value Pr(>F)    
## factor(km.res$cluster)    3    247   82.35   100.2 <2e-16 ***
## Residuals              1372   1128    0.82                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(fit1) 
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = pss ~ factor(km.res$cluster), data = dailyscale.clus)
## 
## $`factor(km.res$cluster)`
##             diff        lwr        upr     p adj
## 2-1 -0.937568465 -1.1029011 -0.7722358 0.0000000
## 3-1 -0.007666659 -0.1785830  0.1632497 0.9994508
## 4-1 -0.493702753 -0.7060229 -0.2813826 0.0000000
## 3-2  0.929901806  0.7681609  1.0916427 0.0000000
## 4-2  0.443865711  0.2388594  0.6488720 0.0000002
## 4-3 -0.486036094 -0.6955715 -0.2765007 0.0000000

We see that clusters differ from each other on pss, except clusters 3 and 4 (or 3 and 2 if there was label switching).

Differences on non-clustering variables provide evidence that, indeed, the cluster solution is providing a meaningful distinction. The typology has value.

In sum, there are variety of ways to justify a cluster solution (e.g., selection of k)

  1. Conceptual arguments
  2. Internal statistical criteria
  3. replication of clusters in random halves
  4. cluster differentiation on external variables

A practical benefit of subgroup-oriented interpretation emerges when considering potential interventions. Multivariate profiles may point toward tailoring diagnostic and intervention efforts to individual needs.

16.8.7 Hierarchical Clustering

The cluster package provides a whole set of options … including both hierarchical and non-hierarchical methods.

Let’s look at a hierarchical method - more explanations can be found here http://www.econ.upf.edu/~michael/stanford/maeb7.pdf .

Prelim: we make distance matrix (not totally necessary, but we do here for conceptual value)

dist.all <- daisy(dailyscale[,c("lteq","posaff")],metric="euclidean",stand=FALSE)
#loking at distances among first 5 persons
as.matrix(dist.all)[1:5,1:5]
##            1          2         3         4         5
## 1 0.00000000 0.09088348 1.0906017 1.5479960 0.8493762
## 2 0.09088348 0.00000000 1.1814852 1.6387144 0.8920640
## 3 1.09060171 1.18148519 0.0000000 0.4644381 1.0573130
## 4 1.54799598 1.63871436 0.4644381 0.0000000 1.4634612
## 5 0.84937623 0.89206403 1.0573130 1.4634612 0.0000000

Note that daisy() does include some treatments for missing data. Be careful!

Engage the hierarchical clustering … we use the agnes() (agglomerative nesting, aka hierarchical clustering, Ward’s method, …) function (which also allows for other linkage options)

# Compute Ward clusters 
clusterward.papa <- agnes(dist.all, diss = TRUE, method = "ward")

There are many choices for the linkage method. We have chosen Ward here, as a classic. Again, this is a well-trodden research area, and one can find recommendations of all types. Read widely to find the best for your specific purpose and data.

Then we visualize it!

# Plot
layout(matrix(1))
plot(clusterward.papa, which.plot = 2, main = "Ward clustering of PAPA")

This is a Dendrogram (basically an organized plot of the distance matrix) that indicates how far apart objects are and when they might be merged together. The y-axis indicates the distance between the clusters. Long vertical lines indicate that there is a lot of between-cluster distance. We determine a level at which to “cut the tree”. Generally we are looking for a level above which the lines are long (between-group heterogeneity) and below which the leaves are close (within-group homogeneity).

We see that 4 clusters seems to be a good tradeoff for parsimony.

Lets cut the tree and make cluster assignments.

wardcluster4 <- cutree(clusterward.papa, k = 4)

And look at some statistical criteria

cluster.stats(dist.all, clustering=wardcluster4,
              silhouette = TRUE, sepindex = TRUE)
## $n
## [1] 1376
## 
## $cluster.number
## [1] 4
## 
## $cluster.size
## [1] 437 443 229 267
## 
## $min.cluster.size
## [1] 229
## 
## $noisen
## [1] 0
## 
## $diameter
## [1] 4.710322 3.033785 4.361416 2.844837
## 
## $average.distance
## [1] 1.0705832 0.8725114 1.2911304 0.8755665
## 
## $median.distance
## [1] 0.9638540 0.8580223 1.2044112 0.8146296
## 
## $separation
## [1] 0.09088348 0.09088348 0.09088348 0.09088348
## 
## $average.toother
## [1] 1.778935 1.969837 2.398617 2.164384
## 
## $separation.matrix
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.00000000 0.09088348 0.09088348 0.09088348
## [2,] 0.09088348 0.00000000 0.09595597 0.09088348
## [3,] 0.09088348 0.09595597 0.00000000 1.65318945
## [4,] 0.09088348 0.09088348 1.65318945 0.00000000
## 
## $ave.between.matrix
##          [,1]     [,2]     [,3]     [,4]
## [1,] 0.000000 1.724896 2.024155 1.658275
## [2,] 1.724896 0.000000 2.268755 2.114358
## [3,] 2.024155 2.268755 0.000000 3.226963
## [4,] 1.658275 2.114358 3.226963 0.000000
## 
## $average.between
## [1] 2.036311
## 
## $average.within
## [1] 1.005678
## 
## $n.between
## [1] 691214
## 
## $n.within
## [1] 254786
## 
## $max.diameter
## [1] 4.710322
## 
## $min.separation
## [1] 0.09088348
## 
## $within.cluster.ss
## [1] 948.389
## 
## $clus.avg.silwidths
##         1         2         3         4 
## 0.1784037 0.4533402 0.2833088 0.4524960 
## 
## $avg.silwidth
## [1] 0.3375627
## 
## $g2
## NULL
## 
## $g3
## NULL
## 
## $pearsongamma
## [1] 0.4834923
## 
## $dunn
## [1] 0.01929453
## 
## $dunn2
## [1] 1.284359
## 
## $entropy
## [1] 1.345759
## 
## $wb.ratio
## [1] 0.4938724
## 
## $ch
## [1] 868.775
## 
## $cwidegap
## [1] 0.7249194 0.6166206 0.9088348 0.4544174
## 
## $widestgap
## [1] 0.9088348
## 
## $sindex
## [1] 0.1292593
## 
## $corrected.rand
## NULL
## 
## $vi
## NULL

16.8.8 Two-step Approach

Often times, researchers are using a two-step approach …

  1. Hierarchical Ward’s method to …
    …evaluate optimal number of clusters
    …produce starting seeds for subsequent step
  2. Non-hierarchical k-means method to …
    …determine final case location in the separate subgroups

The two-step approach circumvents some drawbacks of each procedure
…Ward’s method does not allow revising assigned membership in later steps tends to produce clusters of similar size …k-means method produces optimal clusters only if starting seeds are pre-specified

16.8.8.1 K-Medoids

An alternative hierarchical clustering method … we use the pam() (partitioning around mediods) which is like k-means, but a bit more robust.

# Compute PAM clustering solution for k=4
clusterpam.papa <- pam(dist.all, k=4, diss = TRUE)
clusterpam.papa
## Medoids:
##      ID         
## [1,] "129" "154"
## [2,] "108" "130"
## [3,] "676" "739"
## [4,] "233" "262"
## Clustering vector:
##    1    2    3    4    5    6    7    9   10   11   13   14   15   17   18   19 
##    1    1    2    2    3    3    3    2    2    2    2    2    4    2    1    2 
##   20   21   22   23   27   28   29   30   31   32   37   38   39   40   41   42 
##    1    4    4    2    4    4    4    4    2    1    1    1    2    2    4    2 
##   43   44   47   48   49   51   52   53   54   55   56   57   58   59   60   61 
##    2    1    1    1    1    1    2    1    1    4    3    4    4    4    4    4 
##   62   63   64   66   67   68   70   71   72   73   74   75   76   77   78   79 
##    3    1    2    3    4    1    2    2    2    1    2    2    1    1    2    1 
##   81   84   85   86   87   88   89   90   91   92   93   95   96   97   98   99 
##    1    1    4    1    1    2    2    3    3    3    3    2    2    2    2    3 
##  101  102  103  105  106  107  108  110  111  112  113  114  115  116  117  118 
##    3    3    2    4    3    3    3    2    3    3    3    3    3    3    3    3 
##  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134 
##    3    4    1    4    4    4    4    4    1    2    2    2    2    1    2    2 
##  135  136  137  138  140  141  143  144  145  146  147  149  150  151  152  153 
##    2    3    3    2    3    1    3    3    1    3    3    3    3    3    4    3 
##  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169 
##    1    1    1    3    3    2    2    2    2    2    2    2    1    1    2    2 
##  170  171  172  173  174  175  176  177  178  179  180  181  182  183  184  185 
##    1    1    1    4    1    4    4    4    4    1    1    1    2    2    4    1 
##  186  187  188  189  190  191  192  193  194  196  197  198  199  201  202  203 
##    1    3    3    3    1    3    3    4    4    2    1    1    1    1    3    3 
##  204  205  206  208  209  210  211  212  213  214  215  216  217  218  219  220 
##    2    2    3    3    3    1    2    2    1    1    1    1    2    4    4    4 
##  221  222  223  224  225  226  228  229  230  231  232  233  234  235  236  237 
##    4    3    4    4    4    2    2    2    4    4    1    3    1    1    1    4 
##  238  239  240  241  242  243  244  245  246  247  248  249  250  251  252  253 
##    1    1    1    4    1    1    2    3    3    1    3    2    3    1    3    3 
##  254  255  256  257  258  259  260  261  262  263  264  265  266  267  268  269 
##    4    3    3    3    2    1    2    2    4    1    1    2    1    1    1    1 
##  270  271  272  273  274  275  276  277  278  279  280  281  282  283  285  286 
##    3    1    1    1    1    2    1    1    1    1    1    1    4    1    4    4 
##  287  288  289  290  291  292  293  294  295  296  297  298  299  300  301  302 
##    4    1    2    1    1    1    4    1    4    2    2    2    1    1    1    4 
##  303  304  305  306  307  308  309  312  313  314  315  316  317  318  319  320 
##    1    2    2    2    1    2    1    1    2    2    1    1    3    4    4    2 
##  321  323  324  325  326  327  328  329  330  331  332  333  334  335  343  344 
##    2    2    2    2    2    2    2    2    2    2    2    2    2    2    4    2 
##  345  346  347  348  349  350  352  353  354  355  356  358  359  360  361  364 
##    1    1    1    1    1    2    1    2    1    1    1    1    1    2    2    3 
##  365  366  367  368  369  370  371  372  373  374  376  377  378  379  380  381 
##    3    3    2    1    1    1    1    1    1    1    4    4    4    2    2    4 
##  382  383  384  385  386  387  388  389  390  391  392  393  394  395  396  397 
##    4    1    4    1    3    1    1    1    1    1    3    3    4    4    3    3 
##  398  399  400  401  402  403  404  405  406  407  408  409  410  411  412  413 
##    3    3    3    1    4    3    3    1    3    4    4    1    1    4    4    4 
##  414  415  416  417  418  419  422  423  424  425  426  427  428  429  430  431 
##    4    2    1    2    2    2    1    2    3    1    3    3    3    3    3    2 
##  432  433  434  435  436  437  438  439  441  442  443  444  445  446  447  448 
##    2    2    1    1    1    1    1    3    2    1    1    4    3    3    2    2 
##  449  450  451  452  453  454  455  456  457  458  459  460  461  462  463  464 
##    2    2    2    1    2    2    4    4    4    4    4    4    4    4    1    2 
##  465  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480 
##    1    1    1    1    1    4    2    1    3    3    1    1    1    4    4    4 
##  481  483  485  486  487  488  489  490  491  492  493  494  495  496  497  498 
##    2    4    2    1    2    4    4    1    4    4    4    2    1    2    1    1 
##  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514 
##    4    4    1    3    1    4    4    4    4    1    2    2    2    2    2    2 
##  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530 
##    4    1    2    3    2    2    3    2    3    3    4    1    1    2    3    1 
##  531  533  534  535  536  537  538  539  540  541  542  543  544  545  546  548 
##    3    4    3    3    3    4    4    4    4    4    4    4    2    3    2    3 
##  549  550  551  552  553  554  555  556  557  558  559  560  561  562  563  564 
##    3    2    2    1    3    3    3    3    4    3    3    2    3    4    1    2 
##  565  566  567  568  569  570  571  572  573  574  575  576  577  578  579  580 
##    3    4    1    2    2    2    2    2    2    2    2    4    3    3    3    3 
##  581  582  583  584  585  586  587  588  589  590  591  592  593  594  595  596 
##    4    4    4    1    2    1    1    1    1    1    1    2    1    4    4    1 
##  597  598  599  601  602  603  604  605  606  607  609  610  611  612  613  614 
##    1    4    4    1    1    4    1    4    4    4    2    1    1    2    1    4 
##  615  616  617  618  619  620  621  622  623  624  625  626  628  629  630  631 
##    1    2    1    1    2    2    1    4    1    2    2    1    2    2    1    1 
##  632  633  634  635  636  637  638  639  640  641  642  643  644  645  646  647 
##    4    1    4    4    1    2    4    4    1    3    1    4    4    4    4    4 
##  648  649  650  651  652  653  654  655  656  657  658  659  660  661  662  663 
##    4    1    1    4    4    4    1    3    3    4    4    4    4    4    4    4 
##  664  665  666  667  668  669  670  671  672  673  674  675  676  677  678  679 
##    3    3    3    3    3    3    3    3    3    3    3    2    1    4    3    2 
##  680  681  682  684  685  686  687  688  689  690  691  692  693  694  696  697 
##    2    2    1    4    1    2    2    3    3    2    2    2    3    3    2    4 
##  698  699  700  701  702  703  704  705  706  707  708  709  710  711  712  713 
##    4    2    4    4    4    3    3    3    3    3    1    4    3    4    1    4 
##  714  715  716  717  719  720  721  722  723  724  725  726  727  728  731  735 
##    1    1    1    1    4    1    1    4    4    4    4    4    3    3    3    1 
##  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750  751 
##    3    1    2    3    4    4    1    3    3    3    2    3    1    3    2    2 
##  752  753  754  755  757  758  759  760  761  762  763  764  765  766  767  768 
##    1    4    3    1    4    1    1    4    4    1    2    1    2    2    1    1 
##  769  770  771  772  773  774  775  776  777  778  779  780  781  782  783  784 
##    1    1    2    1    1    1    3    1    3    4    4    4    4    4    4    4 
##  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800 
##    4    1    4    4    1    1    4    4    3    3    3    4    3    2    3    3 
##  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816 
##    3    3    3    4    3    4    2    2    2    2    1    1    1    4    4    4 
##  817  819  820  821  822  823  824  825  826  827  828  829  830  831  832  833 
##    1    4    4    1    4    2    1    1    4    2    1    3    4    4    2    2 
##  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848  849 
##    4    3    1    2    1    2    1    1    1    2    3    4    1    1    3    1 
##  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864  866 
##    1    4    1    1    3    2    2    2    3    2    3    2    3    3    3    3 
##  867  868  869  871  872  873  874  875  876  877  878  879  880  881  882  883 
##    3    3    3    3    3    3    3    4    1    3    1    4    1    1    1    1 
##  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899 
##    4    2    1    4    4    4    1    4    1    1    1    1    1    1    1    1 
##  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915 
##    1    1    1    1    2    1    1    1    1    3    4    1    4    4    3    3 
##  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931 
##    4    3    2    1    2    1    1    4    1    4    2    2    1    1    1    1 
##  932  933  934  935  936  937  938  940  942  943  944  945  946  947  948  949 
##    1    2    3    3    3    3    3    2    2    2    1    2    4    4    1    1 
##  950  951  952  953  956  957  958  959  960  961  962  963  964  965  966  967 
##    3    1    1    1    4    1    2    1    2    1    1    1    1    1    3    3 
##  968  969  970  971  972  973  974  976  977  978  979  980  982  983  984  985 
##    2    4    4    3    3    3    4    2    1    3    4    4    2    2    2    1 
##  986  987  988  989  990  991  992  993  994  995  996  997  998  999 1000 1001 
##    4    3    4    1    1    3    1    2    3    1    3    1    4    2    3    4 
## 1002 1003 1004 1005 1006 1007 1008 1009 1011 1012 1013 1014 1015 1016 1017 1018 
##    3    3    1    3    1    1    1    1    4    4    1    4    1    3    3    3 
## 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 
##    2    2    2    4    3    4    1    3    3    4    1    1    1    4    4    1 
## 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 
##    4    4    4    2    2    2    2    2    2    2    2    2    2    2    2    2 
## 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1064 1065 1066 1067 
##    3    2    3    2    2    1    1    1    1    1    1    1    3    4    1    2 
## 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 
##    2    1    2    3    2    3    4    3    3    2    2    2    2    1    1    1 
## 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 
##    1    2    2    2    2    2    2    1    2    2    2    2    2    2    2    2 
## 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 
##    2    2    4    2    1    4    1    1    1    2    3    2    2    2    2    2 
## 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 
##    2    2    1    1    3    3    1    3    2    1    1    1    2    2    3    3 
## 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 
##    3    2    2    3    2    3    3    3    3    3    1    2    2    1    1    2 
## 1148 1149 1150 1151 1152 1153 1154 1156 1157 1158 1160 1161 1162 1163 1164 1165 
##    2    2    2    2    4    1    2    4    2    4    2    1    1    1    4    4 
## 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 
##    2    3    3    4    4    1    3    2    3    3    4    1    1    3    3    2 
## 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 
##    4    2    2    4    1    1    1    4    1    3    3    1    2    3    3    3 
## 1198 1199 1200 1201 1202 1203 1204 1205 1207 1208 1209 1210 1211 1212 1213 1215 
##    1    2    2    2    1    2    4    1    2    4    2    1    2    2    2    1 
## 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 
##    1    1    2    2    1    2    4    1    1    2    2    1    1    4    3    3 
## 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 
##    3    3    3    3    4    4    2    2    1    2    2    1    4    4    1    1 
## 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 
##    2    3    4    2    3    1    3    2    2    3    3    3    3    3    2    1 
## 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 
##    2    2    1    1    4    1    2    1    3    3    4    1    2    3    2    2 
## 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 
##    1    2    1    4    1    1    1    1    4    4    1    1    2    2    2    2 
## 1296 1297 1298 1299 1300 1301 1302 1304 1305 1306 1307 1308 1309 1310 1311 1312 
##    4    4    4    2    1    2    2    1    4    4    4    2    1    1    1    2 
## 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 
##    1    1    2    1    4    2    1    2    1    4    1    2    1    4    1    4 
## 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 
##    4    4    1    3    4    3    2    1    3    3    4    2    2    2    2    2 
## 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 
##    1    1    2    2    2    2    2    2    2    2    1    2    2    2    2    2 
## 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 
##    1    2    2    2    2    2    2    2    4    2    2    3    3    1    4    3 
## 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 
##    3    3    3    3    1    2    2    3    2    3    2    1    4    4    4    4 
## 1393 1394 1395 1396 1397 1398 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 
##    4    4    4    3    4    3    2    3    4    3    4    1    1    2    4    1 
## 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 
##    1    1    1    2    2    4    1    1    4    1    1    2    1    4    4    4 
## 1426 1427 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 
##    4    1    3    3    3    3    4    3    3    1    1    4    4    4    1    4 
## 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 
##    1    1    4    4    4    4    4    1    3    1    2    2    4    4    1    1 
## Objective function:
##     build      swap 
## 0.7176465 0.7049830 
## 
## Available components:
## [1] "medoids"    "id.med"     "clustering" "objective"  "isolation" 
## [6] "clusinfo"   "silinfo"    "diss"       "call"
#Checking length
pamcluster <- clusterpam.papa$clustering
length(pamcluster)
## [1] 1376
#binding to originaldata
dailyscale.pam <- cbind(dailyscale,pamcluster)
#plotting clustered data points 
ggplot(dailyscale.pam,aes(x=lteq,y=posaff)) +
  geom_point(alpha=.6, color=factor(pamcluster))

Let’s run the autosearch and see what comes out …

pamauto <- pamk(dist.all,krange=2:10,criterion="asw", usepam=TRUE,
                scaling=FALSE, alpha=0.001, diss=TRUE,
                critout=FALSE, ns=10, seed=NULL)
pamauto
## $pamobject
## Medoids:
##      ID         
## [1,] "598" "653"
## [2,] "63"  "78" 
## [3,] "738" "802"
## Clustering vector:
##    1    2    3    4    5    6    7    9   10   11   13   14   15   17   18   19 
##    1    1    2    2    3    3    3    2    2    2    2    2    1    2    2    2 
##   20   21   22   23   27   28   29   30   31   32   37   38   39   40   41   42 
##    1    1    1    2    1    1    1    1    2    2    2    2    2    2    1    2 
##   43   44   47   48   49   51   52   53   54   55   56   57   58   59   60   61 
##    2    1    2    2    2    2    2    2    2    1    3    1    1    1    1    1 
##   62   63   64   66   67   68   70   71   72   73   74   75   76   77   78   79 
##    3    2    3    3    1    3    2    2    2    2    2    2    2    1    2    3 
##   81   84   85   86   87   88   89   90   91   92   93   95   96   97   98   99 
##    2    1    1    1    2    2    2    3    3    3    3    2    2    2    2    3 
##  101  102  103  105  106  107  108  110  111  112  113  114  115  116  117  118 
##    3    3    2    1    3    3    3    2    3    3    3    3    3    3    3    3 
##  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134 
##    3    1    1    1    1    1    1    1    1    2    2    2    2    2    2    2 
##  135  136  137  138  140  141  143  144  145  146  147  149  150  151  152  153 
##    2    3    3    2    3    3    3    3    3    3    3    3    3    3    1    3 
##  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169 
##    2    2    2    3    3    2    2    2    2    2    2    2    2    1    2    2 
##  170  171  172  173  174  175  176  177  178  179  180  181  182  183  184  185 
##    1    2    3    1    1    1    1    1    1    1    1    2    3    3    1    3 
##  186  187  188  189  190  191  192  193  194  196  197  198  199  201  202  203 
##    2    3    3    3    1    3    3    1    1    2    2    2    2    3    3    3 
##  204  205  206  208  209  210  211  212  213  214  215  216  217  218  219  220 
##    2    2    3    3    3    2    2    2    2    2    2    1    2    1    1    1 
##  221  222  223  224  225  226  228  229  230  231  232  233  234  235  236  237 
##    1    3    1    1    3    2    2    2    1    1    2    3    2    2    1    1 
##  238  239  240  241  242  243  244  245  246  247  248  249  250  251  252  253 
##    2    1    1    1    3    2    2    3    3    2    3    2    3    1    3    3 
##  254  255  256  257  258  259  260  261  262  263  264  265  266  267  268  269 
##    1    3    3    3    2    2    2    2    1    2    2    2    2    1    2    1 
##  270  271  272  273  274  275  276  277  278  279  280  281  282  283  285  286 
##    3    2    2    2    2    2    1    1    1    2    1    1    1    1    1    1 
##  287  288  289  290  291  292  293  294  295  296  297  298  299  300  301  302 
##    1    1    2    1    1    1    1    1    1    2    2    2    1    1    2    1 
##  303  304  305  306  307  308  309  312  313  314  315  316  317  318  319  320 
##    3    2    2    2    2    2    1    2    2    2    2    3    3    1    1    2 
##  321  323  324  325  326  327  328  329  330  331  332  333  334  335  343  344 
##    2    2    2    2    2    2    2    3    2    2    2    2    2    2    1    2 
##  345  346  347  348  349  350  352  353  354  355  356  358  359  360  361  364 
##    1    2    1    2    2    2    2    2    2    1    1    1    1    2    3    3 
##  365  366  367  368  369  370  371  372  373  374  376  377  378  379  380  381 
##    3    3    2    2    2    2    3    2    2    3    1    1    1    2    2    1 
##  382  383  384  385  386  387  388  389  390  391  392  393  394  395  396  397 
##    1    2    3    2    3    2    2    1    1    3    3    3    1    1    3    3 
##  398  399  400  401  402  403  404  405  406  407  408  409  410  411  412  413 
##    3    3    3    1    1    3    3    1    3    1    1    1    2    1    1    1 
##  414  415  416  417  418  419  422  423  424  425  426  427  428  429  430  431 
##    1    2    2    2    2    2    1    2    3    3    3    3    3    3    3    2 
##  432  433  434  435  436  437  438  439  441  442  443  444  445  446  447  448 
##    2    2    2    2    3    1    1    3    2    2    3    1    3    3    2    2 
##  449  450  451  452  453  454  455  456  457  458  459  460  461  462  463  464 
##    2    2    2    2    2    2    1    1    1    1    3    1    1    1    2    2 
##  465  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480 
##    1    1    2    2    2    1    2    2    3    3    2    1    1    1    1    1 
##  481  483  485  486  487  488  489  490  491  492  493  494  495  496  497  498 
##    2    1    2    2    2    1    1    1    1    1    1    2    1    2    1    1 
##  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514 
##    1    1    2    3    1    1    1    1    1    2    2    2    2    2    2    2 
##  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530 
##    1    1    3    3    2    2    3    2    3    3    1    1    1    2    3    3 
##  531  533  534  535  536  537  538  539  540  541  542  543  544  545  546  548 
##    3    1    3    3    3    1    1    1    1    1    1    1    2    3    2    3 
##  549  550  551  552  553  554  555  556  557  558  559  560  561  562  563  564 
##    3    3    3    2    3    3    3    3    1    3    3    2    3    1    1    2 
##  565  566  567  568  569  570  571  572  573  574  575  576  577  578  579  580 
##    3    1    1    2    2    2    2    2    2    2    2    1    3    3    3    3 
##  581  582  583  584  585  586  587  588  589  590  591  592  593  594  595  596 
##    1    1    1    1    2    2    1    1    2    1    1    2    1    1    1    2 
##  597  598  599  601  602  603  604  605  606  607  609  610  611  612  613  614 
##    1    1    1    2    1    1    2    1    1    1    2    2    2    2    1    1 
##  615  616  617  618  619  620  621  622  623  624  625  626  628  629  630  631 
##    2    2    2    2    2    2    1    1    1    2    2    2    2    2    2    2 
##  632  633  634  635  636  637  638  639  640  641  642  643  644  645  646  647 
##    1    1    1    1    1    2    1    1    2    3    2    1    1    1    1    1 
##  648  649  650  651  652  653  654  655  656  657  658  659  660  661  662  663 
##    1    2    1    1    1    1    3    3    3    1    1    1    1    1    1    1 
##  664  665  666  667  668  669  670  671  672  673  674  675  676  677  678  679 
##    3    3    3    3    3    3    3    3    3    3    3    2    2    1    3    2 
##  680  681  682  684  685  686  687  688  689  690  691  692  693  694  696  697 
##    2    2    2    1    1    2    2    3    3    2    2    2    3    3    2    1 
##  698  699  700  701  702  703  704  705  706  707  708  709  710  711  712  713 
##    1    2    1    1    1    3    3    3    3    3    1    1    3    1    1    1 
##  714  715  716  717  719  720  721  722  723  724  725  726  727  728  731  735 
##    2    1    1    1    1    1    3    1    1    1    1    1    3    3    3    2 
##  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750  751 
##    3    2    3    3    1    1    3    3    3    3    2    3    1    3    2    2 
##  752  753  754  755  757  758  759  760  761  762  763  764  765  766  767  768 
##    1    1    3    2    1    2    1    1    1    2    2    1    2    2    2    2 
##  769  770  771  772  773  774  775  776  777  778  779  780  781  782  783  784 
##    2    1    2    2    1    1    3    1    3    1    1    1    1    1    1    1 
##  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800 
##    1    1    1    1    2    2    1    1    3    3    3    1    3    2    3    3 
##  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816 
##    3    3    3    1    3    1    2    2    2    2    2    2    2    1    1    1 
##  817  819  820  821  822  823  824  825  826  827  828  829  830  831  832  833 
##    2    1    1    1    1    2    2    2    1    2    1    3    1    1    2    2 
##  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848  849 
##    1    3    1    3    2    2    1    2    2    2    3    1    1    1    3    2 
##  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864  866 
##    2    1    2    1    3    2    2    3    3    2    3    2    3    3    3    3 
##  867  868  869  871  872  873  874  875  876  877  878  879  880  881  882  883 
##    3    3    3    3    3    3    3    1    1    3    1    1    2    1    2    1 
##  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899 
##    1    2    2    1    1    1    2    1    2    2    2    3    2    2    1    2 
##  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915 
##    2    2    2    2    2    1    1    1    2    3    1    3    1    1    3    3 
##  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931 
##    1    3    2    3    3    3    1    1    2    1    2    2    1    2    2    2 
##  932  933  934  935  936  937  938  940  942  943  944  945  946  947  948  949 
##    1    2    3    3    3    3    3    2    2    2    2    2    1    1    1    2 
##  950  951  952  953  956  957  958  959  960  961  962  963  964  965  966  967 
##    3    3    2    1    1    1    2    2    2    1    1    1    1    2    3    3 
##  968  969  970  971  972  973  974  976  977  978  979  980  982  983  984  985 
##    2    1    1    3    3    3    1    2    2    3    1    1    2    2    2    1 
##  986  987  988  989  990  991  992  993  994  995  996  997  998  999 1000 1001 
##    1    3    1    1    3    3    2    2    3    3    3    1    1    3    3    1 
## 1002 1003 1004 1005 1006 1007 1008 1009 1011 1012 1013 1014 1015 1016 1017 1018 
##    3    3    3    3    1    2    2    2    1    1    2    1    3    3    3    3 
## 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 
##    2    2    2    1    3    1    3    3    3    1    2    1    1    1    1    1 
## 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 
##    1    1    1    2    2    2    2    2    2    2    2    3    2    2    2    2 
## 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1064 1065 1066 1067 
##    3    2    3    2    2    2    2    1    1    1    1    3    3    1    3    3 
## 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 
##    2    3    2    3    2    3    1    3    3    3    2    2    2    1    2    1 
## 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 
##    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2 
## 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 
##    2    2    1    2    1    1    2    1    2    2    3    2    3    2    2    2 
## 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 
##    2    2    2    3    3    3    1    3    2    1    2    2    3    2    3    3 
## 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 
##    3    2    2    3    2    3    3    3    3    3    2    2    2    3    2    2 
## 1148 1149 1150 1151 1152 1153 1154 1156 1157 1158 1160 1161 1162 1163 1164 1165 
##    2    2    2    2    1    2    2    1    2    1    2    2    2    1    1    1 
## 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 
##    2    3    3    1    1    1    3    2    3    3    1    2    1    3    3    3 
## 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 
##    1    2    2    1    2    3    3    1    1    3    3    1    3    3    3    3 
## 1198 1199 1200 1201 1202 1203 1204 1205 1207 1208 1209 1210 1211 1212 1213 1215 
##    2    2    2    2    2    2    1    1    2    1    2    2    2    2    2    2 
## 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 
##    2    1    2    2    2    2    1    2    1    2    2    1    2    1    3    3 
## 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 
##    3    3    3    3    1    1    2    2    1    2    2    2    1    1    1    2 
## 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 
##    2    3    1    2    3    2    3    3    2    3    3    3    3    3    2    2 
## 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 
##    2    2    3    2    1    2    2    3    3    3    1    2    3    3    2    2 
## 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 
##    2    2    1    1    1    1    2    1    1    1    2    2    2    2    2    2 
## 1296 1297 1298 1299 1300 1301 1302 1304 1305 1306 1307 1308 1309 1310 1311 1312 
##    1    1    1    2    2    2    2    1    1    1    1    2    2    2    2    2 
## 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 
##    2    1    2    2    1    2    2    2    2    1    2    2    2    1    2    1 
## 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 
##    1    1    1    3    1    3    2    3    3    3    3    2    2    2    2    2 
## 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 
##    1    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2 
## 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 
##    2    2    2    2    2    2    2    2    1    2    2    3    3    1    1    3 
## 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 
##    3    3    3    3    1    2    2    3    3    3    3    1    1    1    1    1 
## 1393 1394 1395 1396 1397 1398 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 
##    1    1    1    3    1    3    2    3    1    3    1    1    2    2    1    2 
## 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 
##    3    2    1    2    2    1    1    1    1    2    1    2    1    1    1    1 
## 1426 1427 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 
##    1    2    3    3    3    3    1    3    3    2    1    1    1    1    1    1 
## 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 
##    2    1    1    1    1    1    1    1    3    1    2    2    1    1    2    2 
## Objective function:
##     build      swap 
## 0.8498193 0.7869947 
## 
## Available components:
## [1] "medoids"    "id.med"     "clustering" "objective"  "isolation" 
## [6] "clusinfo"   "silinfo"    "diss"       "call"      
## 
## $nc
## [1] 3
## 
## $crit
##  [1] 0.0000000 0.3345128 0.3808428 0.3165995 0.3264280 0.3362667 0.3489111
##  [8] 0.3348566 0.3340303 0.3330956

Here, the suggestion is for k = 3. Only three clusters.

#Obtain medoids
pamauto$pamobject$id.med
## [1] 598  63 738
#binding new cluster assignment to originaldata
dailyscale.pam$pamnew <- pamauto$pamobject$clustering

#plotting clustered data points with the medoids
ggplot(dailyscale.pam,aes(x=lteq,y=posaff)) +
  geom_point(alpha=.6, color=factor(dailyscale.pam$pamnew)) +
  geom_point(data=dailyscale.pam[598,],aes(x=lteq,y=posaff),color=2,size=5,shape=18) +
  geom_point(data=dailyscale.pam[63,],aes(x=lteq,y=posaff),color=1,size=5,shape=18) +
  geom_point(data=dailyscale.pam[738,],aes(x=lteq,y=posaff),color=4,size=5,shape=18)

Now we have a zone to play in.

16.8.9 Final Thoughts

Please remember the usual caution. Our intention here has been simple exposure. When using these methods for a paper or project, do the research necessary to engage the method precisely and with good form.