#SOM example using wines data set
library(kohonen)
## Warning: package 'kohonen' was built under R version 3.5.3
search()
##  [1] ".GlobalEnv"        "package:kohonen"   "package:stats"    
##  [4] "package:graphics"  "package:grDevices" "package:utils"    
##  [7] "package:datasets"  "package:methods"   "Autoloads"        
## [10] "package:base"
ls(2)
##  [1] "add.cluster.boundaries" "check.whatmap"         
##  [3] "classmat2classvec"      "classvec2classmat"     
##  [5] "expandMap"              "getCodes"              
##  [7] "map"                    "object.distances"      
##  [9] "som"                    "somgrid"               
## [11] "supersom"               "tricolor"              
## [13] "unit.distances"         "xyf"
help("som")
## starting httpd help server ... done
help("cutree")
data(wines)
str(wines)
##  num [1:177, 1:13] 13.2 13.2 14.4 13.2 14.2 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:13] "alcohol" "malic acid" "ash" "ash alkalinity" ...
dim(wines)
## [1] 177  13
wineskala<-scale(wines)

grid=somgrid(5,5,"hexagonal")
grid
## $pts
##         x         y
##  [1,] 1.5 0.8660254
##  [2,] 2.5 0.8660254
##  [3,] 3.5 0.8660254
##  [4,] 4.5 0.8660254
##  [5,] 5.5 0.8660254
##  [6,] 1.0 1.7320508
##  [7,] 2.0 1.7320508
##  [8,] 3.0 1.7320508
##  [9,] 4.0 1.7320508
## [10,] 5.0 1.7320508
## [11,] 1.5 2.5980762
## [12,] 2.5 2.5980762
## [13,] 3.5 2.5980762
## [14,] 4.5 2.5980762
## [15,] 5.5 2.5980762
## [16,] 1.0 3.4641016
## [17,] 2.0 3.4641016
## [18,] 3.0 3.4641016
## [19,] 4.0 3.4641016
## [20,] 5.0 3.4641016
## [21,] 1.5 4.3301270
## [22,] 2.5 4.3301270
## [23,] 3.5 4.3301270
## [24,] 4.5 4.3301270
## [25,] 5.5 4.3301270
## 
## $xdim
## [1] 5
## 
## $ydim
## [1] 5
## 
## $topo
## [1] "hexagonal"
## 
## $neighbourhood.fct
## [1] bubble
## Levels: bubble gaussian
## 
## $toroidal
## [1] FALSE
## 
## attr(,"class")
## [1] "somgrid"
som.wines<-som(scale(wines),grid=somgrid(5,5,"hexagonal"))
summary(som.wines)
## SOM of size 5x5 with a hexagonal topology and a bubble neighbourhood function.
## The number of data layers is 1.
## Distance measure(s) used: sumofsquares.
## Training data included: 177 objects.
## Mean distance to the closest unit in the map: 3.507.
str(som.wines)
## List of 13
##  $ data            :List of 1
##   ..$ : num [1:177, 1:13] 0.255 0.206 1.702 0.305 1.491 ...
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : NULL
##   .. .. ..$ : chr [1:13] "alcohol" "malic acid" "ash" "ash alkalinity" ...
##   .. ..- attr(*, "scaled:center")= Named num [1:13] 12.99 2.34 2.37 19.52 99.59 ...
##   .. .. ..- attr(*, "names")= chr [1:13] "alcohol" "malic acid" "ash" "ash alkalinity" ...
##   .. ..- attr(*, "scaled:scale")= Named num [1:13] 0.809 1.119 0.275 3.336 14.174 ...
##   .. .. ..- attr(*, "names")= chr [1:13] "alcohol" "malic acid" "ash" "ash alkalinity" ...
##  $ unit.classif    : num [1:177] 15 24 25 23 25 15 23 20 20 24 ...
##  $ distances       : num [1:177] 3.578 2.901 3.027 1.586 0.697 ...
##  $ grid            :List of 6
##   ..$ pts              : num [1:25, 1:2] 1.5 2.5 3.5 4.5 5.5 1 2 3 4 5 ...
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : NULL
##   .. .. ..$ : chr [1:2] "x" "y"
##   ..$ xdim             : num 5
##   ..$ ydim             : num 5
##   ..$ topo             : chr "hexagonal"
##   ..$ neighbourhood.fct: Factor w/ 2 levels "bubble","gaussian": 1
##   ..$ toroidal         : logi FALSE
##   ..- attr(*, "class")= chr "somgrid"
##  $ codes           :List of 1
##   ..$ : num [1:25, 1:13] -1.0016 0.0419 0.3961 0.1893 0.5157 ...
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : chr [1:25] "V1" "V2" "V3" "V4" ...
##   .. .. ..$ : chr [1:13] "alcohol" "malic acid" "ash" "ash alkalinity" ...
##  $ changes         : num [1:100, 1] 0.0522 0.056 0.0525 0.0546 0.0546 ...
##  $ alpha           : num [1:2] 0.05 0.01
##  $ radius          : Named num [1:2] 3 0
##   ..- attr(*, "names")= chr [1:2] "66.66667%" ""
##  $ user.weights    : num 1
##  $ distance.weights: num 1
##  $ whatmap         : int 1
##  $ maxNA.fraction  : int 0
##  $ dist.fcts       : chr "sumofsquares"
##  - attr(*, "class")= chr "kohonen"
som.wines$unit.classif
##   [1] 15 24 25 23 25 15 23 20 20 24 15 15 25 25 24 24 23 25 19 20 19 15 15
##  [24] 19 23 15 15 23 15 24 24 15 23 23 19 23 15 15 19 20 14 20 14 20 14 20
##  [47] 20 24 25 20 24 25 24 20 24 20 24 25 16  1  2 16 17  1 17 16 16  2 21
##  [70]  9 18  7 22 17 16 16  8 21 13 16 17  6  3 17 16  6  6  6  6  1  1  1
##  [93] 12 17 22 22 16 17 12 16 16  7 11 11  7 11  7 11 18 12 11  1  6  7  6
## [116] 11  7  8 11 12 18 13 13 12 11  7  6  7  8  9  9  9  9  2  4  3  3  3
## [139]  2  2  9  2  3  9  9  3  3  4  5  5  5  5  4  4  3  4  4  5  5  4  2
## [162]  2  9  4  3  5  4  5  5  9  4  4  3  5  4  4  5
plot(som.wines)

som.wines$grid$pts
##         x         y
##  [1,] 1.5 0.8660254
##  [2,] 2.5 0.8660254
##  [3,] 3.5 0.8660254
##  [4,] 4.5 0.8660254
##  [5,] 5.5 0.8660254
##  [6,] 1.0 1.7320508
##  [7,] 2.0 1.7320508
##  [8,] 3.0 1.7320508
##  [9,] 4.0 1.7320508
## [10,] 5.0 1.7320508
## [11,] 1.5 2.5980762
## [12,] 2.5 2.5980762
## [13,] 3.5 2.5980762
## [14,] 4.5 2.5980762
## [15,] 5.5 2.5980762
## [16,] 1.0 3.4641016
## [17,] 2.0 3.4641016
## [18,] 3.0 3.4641016
## [19,] 4.0 3.4641016
## [20,] 5.0 3.4641016
## [21,] 1.5 4.3301270
## [22,] 2.5 4.3301270
## [23,] 3.5 4.3301270
## [24,] 4.5 4.3301270
## [25,] 5.5 4.3301270
som.wines$codes #data code book vector
## [[1]]
##         alcohol  malic acid         ash ash alkalinity   magnesium
## V1  -1.00157078 -0.55562243  0.41339214     0.02299006 -0.42341183
## V2   0.04190582  0.02550588  0.30541177     0.19661484  0.08986299
## V3   0.39611452  2.14437120 -0.01717473     0.54307656 -0.72519841
## V4   0.18931086  0.58084045 -0.04108536     0.37742052 -0.22066427
## V5   0.51566355  0.65373389  0.86072119     1.17976308  0.59099503
## V6  -1.55346525 -0.77783381  0.32148015     1.22742882 -1.12192425
## V7  -0.57858510 -0.45176858 -0.13358797     0.89933508 -0.62229509
## V8  -0.85503371  1.03192514 -0.49762172    -0.13997962 -0.50359346
## V9  -0.29046281  0.44759729 -0.19382654     0.15461269  0.18906035
## V10  0.32683085  0.32090320  0.10427186     0.13401453  0.20716561
## V11 -1.03841922 -0.31952949 -1.18531232     0.13973407 -0.96103721
## V12 -1.35934903  0.79109996 -0.57303607    -0.04659604 -0.50079450
## V13 -0.26659780  1.89281728  0.17345848     0.90980683 -0.19854491
## V14  0.90382935  1.19818651 -0.15188036    -0.34348605  0.22154147
## V15  0.73886262 -0.56394514 -0.08556666    -1.05614308 -0.22611302
## V16 -0.62109416 -0.96731259 -1.92239332    -0.86780742 -0.80373798
## V17 -0.99473109 -0.91740118 -0.15691498    -0.24310251 -0.44026539
## V18 -0.72690664 -0.54563009  1.59010400     1.26386381  0.02135997
## V19  0.75551765  0.47927149  0.59991127    -0.63569638  0.72957321
## V20  1.06274190 -0.49006841 -0.43938104    -1.15423202  0.65669531
## V21 -0.84954952 -1.02347755 -1.59413105    -0.88527362  2.01731698
## V22 -0.64786727 -0.54193135  0.53837753     0.99357712  2.72923140
## V23  0.67326766 -0.34080017  1.58252166     0.12215951  1.16758347
## V24  0.91320647 -0.42193169  0.79844068    -0.24974151  0.52421916
## V25  1.45971609 -0.55807548  0.22790343    -1.31120908  0.46956994
##     tot. phenols  flavonoids non-flav. phenols     proanth   col. int.
## V1    -0.9400140 -0.43936415       1.403295008 -0.61564872 -0.81273220
## V2    -0.5448729 -1.23023067       1.448069047 -1.42035007 -0.02019294
## V3    -1.1597088 -1.37015099       1.368631305 -0.89189478  0.36125989
## V4    -0.9581536 -1.33091980       1.151808110 -0.61838348  1.69078016
## V5    -0.7493124 -0.88968489       0.058251838 -0.15059388  1.90595031
## V6    -0.2448057 -0.19888750       0.744305578 -0.09264421 -1.12432326
## V7    -0.2738831  0.11736391       0.342886464 -0.25017775 -0.91904906
## V8    -0.6975406 -0.55697910       0.451368469 -0.83818288 -0.82792585
## V9    -1.4965649 -1.18047424      -0.304326552 -1.14154603  0.08095311
## V10   -0.5762809 -0.63422641      -0.037715820 -0.49014654  0.69603171
## V11   -0.1126174 -0.02739517      -0.146084327 -0.03309808 -1.12842151
## V12    0.8563408  0.67598083      -0.552537599  1.72259800 -1.00867451
## V13    0.3869437  0.42586741       0.004607682  0.44065097 -0.96821289
## V14    0.5577645  0.58501403      -0.585943668 -0.26526148 -0.11783409
## V15    0.3826992  0.55820564      -0.581268835  0.04219305 -0.25048343
## V16   -0.3657428 -0.17931604      -0.599073658 -0.52354159 -0.73511476
## V17    0.8623497  0.62603976      -1.026463536  0.50129288 -0.52029859
## V18    0.9965917  1.43672898      -0.374120068  0.84003083 -0.48011513
## V19    0.6895183  0.86488105      -1.019763377  0.51455056 -0.08336993
## V20    1.0709497  1.12536111      -0.977848158  0.79229271  0.35427353
## V21   -0.3967921 -0.26546065      -0.865637295  1.24222863 -0.81031003
## V22    0.3541909  0.16391820      -0.981398436  1.10363674 -0.85610711
## V23    0.6809545  0.74906259       0.121361537  0.09421150 -0.16789856
## V24    0.8626260  1.03403338      -0.656405796  0.93112208  0.47421865
## V25    1.6918572  1.62156687      -0.485707157  1.11094938  0.97277085
##        col. hue   OD ratio     proline
## V1   1.01813076 -0.5094731 -0.80114694
## V2  -0.23811536 -0.9758244 -0.46889055
## V3  -0.98400675 -1.2792975 -0.49166722
## V4  -1.48157185 -1.3751346 -0.23516832
## V5  -1.42975789 -1.4767622 -0.47276563
## V6   1.19383740  0.1846570 -0.67418566
## V7  -0.28471349  0.4891775 -1.08203396
## V8  -0.59809238 -0.2894469 -0.78569676
## V9  -1.07332524 -1.2175027 -0.21629667
## V10 -0.77347593 -0.9090949  0.09592919
## V11 -0.09058445  0.6891691 -0.91223551
## V12  0.03668272  0.8191445 -0.92841099
## V13 -0.17682497  0.7227036 -0.92435213
## V14 -0.28306939  0.7187163  0.72886711
## V15  0.52374694  0.8124692  1.27144941
## V16  0.95630863  0.1142933 -0.83567869
## V17  0.52414485  0.3870566 -0.44971048
## V18  0.50574815  1.0155404 -0.64590056
## V19  0.18104446  1.2421036  0.31367282
## V20  0.11033775  1.0290767  0.74444020
## V21  0.90568620  0.1585810 -0.23826560
## V22  0.78248753  0.3096531  0.24043601
## V23  0.73619680  0.6261033  0.76032841
## V24  0.74567202  0.5116907  1.71449830
## V25  0.53976730  0.5479133  2.04703252
peta<-cutree(hclust(dist(som.wines$codes[[1]])),5)
plot(peta)

plot(som.wines,type="codes",bgcol=rainbow(5)[peta])
add.cluster.boundaries(som.wines,peta)