aweSOM

The aweSOM package

aweSOM offers a set of tools to explore and analyze datasets with Self-Organizing Maps (SOM, also known as Kohonen maps), a form of artificial neural network originally created by Teuvo Kohonen in the 1980s. The package introduces interactive plots, making analysis of the SOM easier.

aweSOM can be used either through the web-based interface (called by aweSOM()) or through the command-line functions that are detailed here. The interface also produces reproducible R code, which closely resembles the scripts below.

This vignette details some of the most important functions of the aweSOM package, along with the workflow of training a SOM (using the kohonen package), assessing its quality measures and visualizing the SOM along with its superclasses.

Importing data and training a SOM

For the purpose of this example, we will train a 4x4 hexagonal SOM on the famous iris dataset.

After selecting and pre-processing the training data (here by scaling each variable to unit variance), the somInit function is used to initialize the map’s prototypes. By default, this is done using a PCA-based method, but other schemes are available using the method argument.

The training data, initial prototypes and other parameters are then passed to the kohonen::som function for training. The further training arguments used here are the default ones produced by the aweSOM() interface, based on data and grid dimensions.

library(aweSOM)

full.data <- iris
## Select variables
train.data <- full.data[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")]
### Scale training data
train.data <- scale(train.data)

### RNG Seed (for reproducibility)
set.seed(1465)
### Initialization (PCA grid)
init <- somInit(train.data, 4, 4)
## Train SOM
iris.som <- kohonen::som(train.data, grid = kohonen::somgrid(4, 4, "hexagonal"), 
                         rlen = 100, alpha = c(0.05, 0.01), radius = c(2.65,-2.65), 
                         dist.fcts = "sumofsquares", init = init)

Assessing the quality of the map

The somQuality computes several measures to help assess the quality of a SOM.

somQuality(iris.som, train.data)
#> 
#> ## Quality measures:
#>  * Quantization error     :  0.215272 
#>  * (% explained variance) :  94.58 
#>  * Topographic error      :  0.07333333 
#>  * Kaski-Lagus error      :  1.525657 
#>  
#> ## Number of obs. per map cell:
#>  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 
#>  1  8  4  5 16  9 15 12 20  8 17  9 13  0 10  3

Superclasses of SOM

It is common to further cluster the SOM map into superclasses, groups of cells with similar profiles. This is done using classic clustering algorithms on the map’s prototypes.

Two methods are implemented in the web-based interface, PAM (k-medians) and hierarchical clustering. This is how to obtain them in R. In this example, we choose 3 superclasses. We will return to the choice of superclasses below.

superclust_pam <- cluster::pam(iris.som$codes[[1]], 3)
superclasses_pam <- superclust_pam$clustering

superclust_hclust <- hclust(dist(iris.som$codes[[1]]), "complete")
superclasses_hclust <- cutree(superclust_hclust, 3)

Plotting general map information

aweSOMplot creates a variety of different interactive SOM visualizations. Using the type argument to the function, one the following types of plots can be created:

aweSOMplot(som = iris.som, type = "Cloud", data = full.data, 
           variables = c("Species", "Sepal.Length", "Sepal.Width",  
                         "Petal.Length", "Petal.Width"), 
           superclass = superclasses_pam)

Several placement methods are available for the cloud, the one used here ("cellPCA") is a PCA on the training data of each cell, independently.

aweSOMplot(som = iris.som, type = "Hitmap", superclass = superclasses_pam)

aweSOMplot(som = iris.som, type = "UMatrix", superclass = superclasses_pam)

The aweSOMsmoothdist function can further be used to produce a smooth representation of the U-Matrix. Note, however, that the result representation is biased when using hexagonal maps (the smoothing function coerces the grid to a rectangular shape).

aweSOMsmoothdist(iris.som)

Plotting numeric variables on the map

aweSOMplot offers several types of plots for numeric variables :

In all of these plots, by default the means of the chosen variables are displayed within each cell. Other choices of values (medians or prototypes) can be specified using the values parameter. The scales of the plots can also be adapted, using the scales argument. Colors of the variables are controlled by the palvar argument.

aweSOMplot(som = iris.som, type = "Circular", data = full.data, 
           variables = c("Sepal.Length", "Sepal.Width",  
                         "Petal.Length", "Petal.Width"), 
           superclass = superclasses_pam)

On the following barplot, we plot the protoype values instead of the observations means.

aweSOMplot(som = iris.som, type = "Barplot", data = full.data, 
           variables = c("Sepal.Length", "Sepal.Width",  
                         "Petal.Length", "Petal.Width"), 
           superclass = superclasses_pam, 
           values = "prototypes")

On the following box-and-whisker plot, the scales are set to be the same accross variables.

aweSOMplot(som = iris.som, type = "Boxplot", data = full.data, 
           variables = c("Sepal.Length", "Sepal.Width",  
                         "Petal.Length", "Petal.Width"),
           superclass = superclasses_pam, 
           scales = "same")

On the following lines plot, we use the observation medians instead of the means.

aweSOMplot(som = iris.som, type = "Line", data = full.data, 
           variables = c("Sepal.Length", "Sepal.Width",  
                         "Petal.Length", "Petal.Width"), 
           superclass = superclasses_pam, 
           values = "median")

The following radar chart uses the default parameters.

aweSOMplot(som = iris.som, type = "Radar", data = full.data, 
           variables = c("Sepal.Length", "Sepal.Width",  
                         "Petal.Length", "Petal.Width"), 
           superclass = superclasses_pam)

The color plot, or heat map, applies to a single numeric variable. The superclass overlay can be removed by setting the showSC parameter to FALSE.

aweSOMplot(som = iris.som, type = "Color", data = full.data, 
           variables = "Sepal.Length", superclass = superclasses_pam)

Plotting a categorical variable on the map

aweSOMplot can also plot categorical variables, using pie charts or barplots.

In this case, we plot the Species of the iris, a factor with three levels, that was not used during training. The following plots show that, based on the flowers’ measures, the SOM nearly perfectly discriminates between the three species.

aweSOMplot(som = iris.som, type = "CatBarplot", data = full.data, 
           variables = "Species", superclass = superclasses_pam)

aweSOMplot(som = iris.som, type = "Pie", data = full.data, variables = "Species", 
           superclass = superclasses_pam)

By default, the area of each pie is proportional to its cell’s population. This can be changed by setting argument pieEqualSize to TRUE.

Choosing the number of superclasses

aweSOM offers three diagnostics plot for choosing the number of superclasses.

aweSOMscreeplot produces a scree plot, that shows the quality of the clustering (percentage of unexplained variance of the prototypes, lower is better) for varying numbers of superclasses. It supports hierarchical and pam clustering. The rule of thumb is to choose the number of superclasses at the inflection point of this curve.

aweSOMscreeplot(som = iris.som, method = "pam", nclass = 3)

aweSOMsilhouette returns a silhouette plot of the chosen superclasses (hierarchical, pam, or other). The higher the silhouettes, the better.

aweSOMsilhouette(iris.som, superclasses_pam)

For hierarchical clustering, aweSOMdendrogram produces a dendogram, along with the chosen cuts.

aweSOMdendrogram(clust = superclust_hclust, nclass = 3)