Self Organising Map R

2.6k Views Asked by At

I have created a SOM map using the Kohonen package in R and would like to identify a specific data point location on the map. i.e the series used is a matrix made of 2 columns and it has increased by one row, how can I flag the location of this last observation on the map itself , or any specific row for that matter? The code I use is as follows:

require(kohonen)
pretty_palette <- c("#1f77b4", '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2')
data_train <- na.omit(cbind(dataseries_1,dataseries_2))
data_train_matrix <- as.matrix(scale(data_train))
som_grid <- somgrid(xdim = 10, ydim=10, topo="hexagonal")

som_model <- som(data_train_matrix, 
             grid=som_grid, 
             rlen=100, 
             alpha=c(0.05,0.01), 
             keep.data=TRUE,
             n.hood="circular")

som_cluster <- cutree(hclust(dist(som_model$codes)), 4)
plot(som_model, type="mapping", bgcol =pretty_palette[som_cluster] , main = "Regimes Map")
add.cluster.boundaries(som_model, som_cluster)

any help appreciated

Thank you

1

There are 1 best solutions below

0
On

I have added two line on the top of the code to represent some data. What I would like is to be able to flag on the map which the script plot the location of::

datapoint_to_flag <- tail(data_train_matrix,1)

By inserting a text on the cell with the value (or nearest) to datapoint_to_flag the code I have written so far create some random series then plot them across 4 clusters, the bit that I do not know how to write is the bit that would localise datapoint_to_flag on the map...if this makes sense.

require(kohonen)
pretty_palette <- c("#1f77b4", '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2')

dataseries1 <- rnorm(1000, mean = 0, sd = 3)
dataseries2 <- rnorm(1000, mean = 0, sd = 1)



data_train <- na.omit(cbind(dataseries1,dataseries2))
data_train_matrix <- as.matrix(scale(data_train))
som_grid <- somgrid(xdim = 20, ydim=20, topo="hexagonal")

som_model <- som(data_train_matrix, 
             grid=som_grid, 
             rlen=1000, 
             alpha=c(0.05,0.01), 
             keep.data=TRUE,
             n.hood="circular")

som_cluster <- cutree(hclust(dist(som_model$codes)), 4)
plot(som_model, type="mapping", bgcol =pretty_palette[som_cluster] , main = "Regimes Map")
add.cluster.boundaries(som_model, som_cluster)

datapoint_to_flag <- tail(data_train_matrix,1)

Thank you

Pierre