0

This is a typical example of usage of som function in Kohonen package:

library("kohonen")
Loading required package: class
data("wines")
wines.sc <- scale(wines)
set.seed(7)
wine.som <- som(data = wines.sc, grid = somgrid(5, 4, "hexagonal"))
plot(wine.som, main = "Wine data")

and this is a link to the output: SOM plot

Can anyone explain to me the role of the set.seed command seen in the code. Also, how does the plot command decide the coloring scheme and draw a corresponding legend without being mentioned in the command?

Rohit Gavval
  • 227
  • 1
  • 13
user3351262
  • 11
  • 1
  • 3

3 Answers3

0

I didnt check the R source-code you mentioned, but i can tell you from the original SOM ALGORITHM, that a random initialization of the codebook vectors is required. I guess you know about K-means (if not i recommend to read about it before getting into SOM). The initialization of the SOM and K-Means share the manner in which some representative vectors (also called prototypes) have to be randomly located initially in the input space. For implementing that you need a random generator and therefore you need a seed to initialize it. Additionally, at each training STEP, some versions of the SOM select the input data at random and not sequentially, which also needs a random generator.

Just to make the comment more complete, some authors recommend the use of some other way to initialize the codebook vector which are not random, for example using PCA... but that is another story.

user11924
  • 153
  • 7
0

The random seed thing has already been explained, but I can help you with the colours of the slices and the legend.

The plot function for a SOM object uses a colour palette, which depends on the number of classes/columns you have in your matrix. It creates a range of colours, based on what we could call "major colours". In your example the major colours must be "red", "yellow", "green", "blue" and "purple", and the palette automatically add matching colours to fill in the spaces between these "major colours".

You can choose to use another palette thanks to the argument "palette.name", either by using a pre-defined palette function such as "rainbow()", "topo.colors()", "cm.colors()"... or by creating your own range of major colours, using colorRampPalette().

Here is an example:

If you want the colours to go from pink to yellow, then blue and finally brown in your SOM graph, you could write this :

data("wines")

#You have to define a new palette function, that can create the colour gradient #depending on the number of classes you want to represent

PALETTE.WINES <- colorRampPalette(c("pink", "yellow", "blue", "brown"))

som.wines <- som(scale(wines), grid = somgrid(4, 4, "hexagonal"))

#Finally remember to fill in the argument "palette.name" with your new palette function
plot(som.wines, main = "Wine data", palette.name=PALETTE.WINES)

And here you are! :) Wine SOM with new colour range

If you want to affect a special colour to every single column, you can enter as many major colours as you you want. In the previous example, if you only have four classe, one will be in pink, the second in yellow, the third in blue and the last in brown, with no gradient.

I hope this will be helpful for all the people who still want to change the colours. I highly recommend you to look for more information by looking for the presentation of Earl F. Glynn, on the use of color in R.

You can also see this link to have a bit more details : https://www.r-bloggers.com/color-palettes-in-r/

Éloi
  • 1
  • 1
-3
library(fortunes)
fortune("WTFM")

This is all documented in TFM. Those who WTFM don't want to have to WTFM again on the mailing list. RTFM. -- Barry Rowlingson R-help (October 2003)

?set.seed
Thierry
  • 18,049
  • 5
  • 48
  • 66