Data Mining Algorithms In R/Clustering/CLARA

An obvious way of clustering larger datasets is to try and extend existing methods so that they can cope with a larger number of objects. The focus is on clustering large numbers of objects rather than a small number of objects in high dimensions. Kaufman and Rousseeuw (1990) suggested the CLARA (Clustering for Large Applications) algorithm for tackling large applications. CLARA extends their k-medoids approach for a large number of objects. It works by clustering a sample from the dataset and then assigns all objects in the dataset to these clusters.

This work is focused on CLARA, a technique for clustering largers datasets.
 * Technique To Be Discussed

Algorithm
Table 1: Summary of symbols and definitions

CLARA (Clustering LARge Applications) relies on the sampling approach to handle large data sets. Instead of finding medoids for the entire data set, CLARA draws a small sample from the data set and applies the PAM algorithm to generate an optimal set of medoids for the sample. The quality of resulting medoids is measured by the average dissimilarity between every object in the entire data set D and the medoid of its cluster, defined as the following cost function:

$$Cost(M,D) = \frac{\sum_{i=1}^n dissimilarity(O_i,rep(M,O_i))}{n}$$

where M is a set of selected medoids, dissimilarity(Oi, Oj) is the dissimilarity between objects Oi and Oj, and rep(M, Oi) returns a medoid in M which is closest to Oi.

To alleviate sampling bias, CLARA repeats the sampling and clustering process a pre-defined number of times and subsequently selects as the final clustering result the set of medoids with the minimal cost. Assume q to be the number of samplings. The CLARA algorithm is detailed in Figure 1.



Figure 1: Clara Algorithm

Since CLARA adopts a sampling approach, the quality of its clustering results depends greatly on the size of the sample. When the sample size is small, CLARA’s efficiency in clustering large data sets comes at the cost of clustering quality.

Implementation
In order to use the CLARA algorithm in R, one must install cluster package. This package includes a function that performs the CLARA process.

Install cluster package install.packages("cluster")

Import Contents library("cluster")

The CLARA function, provided by the cluster package, might be used as follow: clara(x, k, metric = "euclidean", stand = FALSE, samples = 5, sampsize = min(n, 40 + 2 * k), trace = 0, medoids.x = TRUE, keep.data = medoids.x, rngR = FALSE) where the arguments are:


 * x: Data matrix or data frame, each row corresponds to an observation, and each column corresponds to a variable. All variables must be numeric. Missing values (NAs) are allowed.
 * k: Integer, the number of clusters. It is required that 0 < k < n where n is the number of observations (i.e., n = nrow(x)).
 * metric:	Character string specifying the metric to be used for calculating dissimilarities between observations. The currently available options are "euclidean" and "manhattan". Euclidean distances are root sum-of-squares of differences, and manhattan distances are the sum of absolute differences.
 * stand: Logical, indicating if the measurements in x are standardized before calculating the dissimilarities. Measurements are standardized for each variable (column), by subtracting the variable's mean value and dividing by the variable's mean absolute deviation.
 * samples: Integer, number of samples to be drawn from the dataset.
 * sampsize: Integer, number of observations in each sample. sampsize should be higher than the number of clusters (k) and at most the number of observations (n = nrow(x)).
 * trace:	Integer indicating a trace level for diagnostic output during the algorithm.
 * medoids.x: Logical indicating if the medoids should be returned, identically to some rows of the input data x. If FALSE, keep.data must be false as well, and the medoid indices, i.e., row numbers of the medoids will still be returned (i.med component), and the algorithm saves space by needing one copy less of x.
 * keep.data: Logical indicating if the (scaled if stand is true) data should be kept in the result. Setting this to FALSE saves memory (and hence time), but disables clusploting of the result. Use medoids.x = FALSE to save even more memory.
 * rngR: Logical indicating if R's random number generator should be used instead of the primitive clara-builtin one. If true, this also means that each call to clara returns a different result – though only slightly different in good situations.

View
There are actually two ways of viewing the result of a CLARA use. Both of them use the object of class clara returned by the function application.

The first way is to plot the object, creating a chart that represents the data. Thus, if there are N objects divided into K clusters, the chart must contain N points representing the objects, and those points must be colored in K different colors, each one representing a cluster set. For example, given the object clarax, which is a result of the function clara application, all one has to do in order to plot the object is:

plot(clarax)

The second way of viewing the result of a CLARA application is to simply print the components of the object of class clara. For example, given the same object clarax of the previous example, one could print its components using:

print(clarax)

Example

Suppose we have 500 objects and each object have two attributes (or features). Our goal is to group these objects into K=2 groups based on their two features. The function CLARA can be used to define the groups as follow:

x <- rbind(cbind(rnorm(200,0,8), rnorm(200,0,8)), cbind(rnorm(300,50,8), rnorm(300,50,8))) clarax <- clara(x, 2) clarax clarax$clusinfo
 * 1) generate 500 objects, divided into 2 clusters.
 * 1) run clara

print(clarax)
 * 1) print components of clarax

plot(x, col = clarax$cluster) points(clarax$centers, col = 1:2, pch = 8)
 * 1) plot clusters
 * 1) plot centers

Result of printing components of clarax:

Call:   clara(x = x, k = 2) Medoids: [,1]      [,2] [1,]  1.091033 -0.5367556 [2,] 51.044099 51.0638017 Objective function:      9.946085 Clustering vector:      int [1:500] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... Cluster sizes:          200 300 Best sample: [1]  6  45  51  56  67  75  85  90  94  97 110 111 160 170 176 181 201 219 249 [20] 260 264 275 296 304 317 319 337 340 361 362 369 370 374 379 397 398 411 420 [39] 422 424 436 448 465 489

Available components: [1] "sample"    "medoids"    "i.med"      "clustering" "objective" [6] "clusinfo"  "diss"       "call"       "silinfo"    "data"

Result of plotting "clarax"

Figure 2: Result of plotting clarax

Case study
In this section, we illustrate a case study using CLARA.

Scenario
This data set contains statistics, in arrests per 100,000 residents for assault, murder, and rape in each of the 50 US states in 1973. Also given is the percent of the population living in urban areas.

Input Data
A data frame with 50 observations on 4 variables.


 * [,1] Murder  numeric Murder arrests (per 100,000)
 * [,2] Assault numeric Assault arrests (per 100,000)
 * [,3] UrbanPop numeric Percent urban population
 * [,4] Rape    numeric Rape arrests (per 100,000)

Table 2: USArrests Database

Execution
The function "clara" was used as follows:

x <- USArrests
 * 1) import data

clarax <- clara(x[1:4], 3)
 * 1) run CLARA

print(clarax)
 * 1) print components of clarax

plot(x, col = clarax$cluster) points(clarax$centers, col = 1:2, pch = 8)
 * 1) plot clusters
 * 1) plot centers

(USArrests) points(254,11.1, pch=16) text(254,11.11, labels ='New York') lines(Assault, (.63168 + (.04191 * Assault)))
 * 1) plot(Assault, Murder)

Output
The result of printing the components of the class returned by the function application is shown below:

Call:   clara(x = x[1:4], k = 3) Medoids: Murder Assault UrbanPop Rape Michigan  12.1     255       74 35.1 Missouri   9.0     178       70 28.2 Nebraska   4.3     102       62 16.5 Objective function:     29.31019 Clustering vector:      Named int [1:50] 1 1 1 2 1 2 3 1 1 2 3 3 1 3 3 3 3 1 ... - attr(*, "names")= chr [1:50] "Alabama" "Alaska" "Arizona" "Arkansas" "California" "Colorado" "Connecticut" ... Cluster sizes:          16 14 20 Best sample: [1] Alabama       Alaska         Arizona        Arkansas       California [6] Colorado      Delaware       Florida        Georgia        Idaho [11] Illinois      Indiana        Iowa           Kansas         Kentucky [16] Louisiana     Maine          Maryland       Massachusetts  Michigan [21] Minnesota     Mississippi    Missouri       Montana        Nebraska [26] Nevada        New Hampshire  New York       North Carolina North Dakota [31] Ohio          Oklahoma       Oregon         Pennsylvania   Rhode Island [36] South Carolina South Dakota  Tennessee      Texas          Utah [41] Vermont       Virginia       Washington     West Virginia  Wisconsin [46] Wyoming

Available components: [1] "sample"    "medoids"    "i.med"      "clustering" "objective" [6] "clusinfo"  "diss"       "call"       "silinfo"    "data"

The result of plotting the class returned by the function application it is shown below:

Figure 3: Results of the example

Analysis
The implementation of CLARA generated three clusters, relatively homogeneous, consisting of 16, 14 and 20 countries. Analyzing the cluster means, we can relate each group with each of the three classes of states:


 * The cluster formed by Alabama, Alaska, Arizona, California, Delaware, Florida, Illinois, Louisiana, Maryland, Michigan, Mississippi, Nevada, New Mexico, New York, North Carolina, South Carolina has the highest Murder, Assault and Rape arests (per 100,00) and, not least, the largest population.


 * The cluster formed by Arkansas, Colorado, Georgia, Massachusetts, Missouri, New Jersey, Oklahoma, Oregon, Rhode Island, Tennessee, Texas, Virginia, Washington, Wyoming has the intermediate Murder, Assault and Rape arests (per 100,00) and, not least, the largest population.


 * The cluster formed by Connecticut, Hawaii, Idaho, Indiana, Iowa, Kansas, Kentucky, Maine, Minnesota, Montana, Nebraska, New Hampshire, North Dakota, Ohio, Pennsylvania, South Dakota, Utah, Vermont, West Virginia, Wisconsin has the lowest Murder, Assault and Rape arests (per 100,00) and, not least, the largest population.

Analyzing, based on [3], the states of the two extreme clusters (1,3) it was possible to verify that there is a reason for each country to be in these groups. California, although has a good Human Development Index and Median Personal Earnings rate, has the 3rd biggest Unemployment Rate in the USA, the 2nd is Michigan and the 1st is Nevada, two other states that are also in the cluster one. Connecticut has the highest Human Development Index and is on the cluster three. Wyoming has the best percentage of people with High School Diploma, and is on the cluster two. Others reasons can be verified checking this work together with [3].