################################################
#Colors3 - RGB

rm(list=ls(all=TRUE))
library("som")
library("kohonen")

setwd("C:/Users/John/Desktop/CSE 8331 Tutorial/R")
c <- read.table(file="colors3.txt", header = TRUE)
c.sc <- scale(c)
c.matrix <- as.matrix(c.sc)

set.seed(10)
c.som <- som(c.sc, somgrid(10, 20, "hexagonal"))

# Code plot summary
#plot(c.som, type = "codes")


# Properties plots . . .
par(mfrow=c(1,3))
for(i in c(1, 2, 3)) {
  dev.new();plot(c.som, "property", property = c.som$codes[,i], main = colnames(c)[i])
}



################################################
#Colors8 - RGB

rm(list=ls(all=TRUE))
library("som")
library("kohonen")

setwd("C:/Users/John/Desktop/CSE 8331 Tutorial/R")
c <- read.table(file="colors7.txt", header = TRUE)
c.sc <- scale(c)
c.matrix <- as.matrix(c.sc)

set.seed(10)
c.som <- som(c.sc, somgrid(20, 40, "hexagonal"))

# Code plot summary
plot(c.som, type = "codes")

##plot(c.som, type = "codes")
for(i in c(1, 2, 3)) {
  dev.new();plot(c.som, "property", property = c.som$codes[,i], main = colnames(c)[i])
}

#U-Matrix Plot
c.som <- som(c.sc, somgrid(10, 20, "hexagonal"))
dev.new();plot(c.som, type = "dist.neighbours", palette.name = rainbow, ncolors = 8, main = "U-Matrix")

c.som <- som(c.sc, somgrid(20, 40, "hexagonal"))
dev.new();plot(c.som, type = "dist.neighbours", palette.name = rainbow, ncolors = 8, main = "U-Matrix")

c.som <- som(c.sc, somgrid(40, 80, "hexagonal"))
dev.new();plot(c.som, type = "dist.neighbours", palette.name = rainbow, ncolors = 8, main = "U-Matrix")


################################################
#### CDMA Data
################################################

rm(list=ls(all=TRUE))
library("som")
library("kohonen")

setwd("C:/Users/John/Desktop/CSE 8331 Tutorial/R")
c <- read.table(file="rawCDMAData_Norm.txt", header = TRUE)
c.sc <- scale(c)
c.matrix <- as.matrix(c.sc)

set.seed(10)
c.som <- som(c.sc, somgrid(20, 40, "hexagonal"))

# Code plot summary
#plot(c.som, type = "codes")


# Properties plots . . .
#dev.set(dev.4)
#par(mfrow=c(1,3))
for(i in c(1, 2, 3, 4, 5, 6, 7)) {
  dev.new();
  plot(c.som, "property", property = c.som$codes[,i], main = colnames(c)[i])
}



#U-Matrix Plot
dev.new();
c.som <- som(c.sc, somgrid(10, 20, "hexagonal"))
plot(c.som, type = "dist.neighbours", palette.name = rainbow, ncolors = 8, main = "U-Matrix")

dev.new();
c.som <- som(c.sc, somgrid(20, 40, "hexagonal"))
plot(c.som, type = "dist.neighbours", palette.name = rainbow, ncolors = 8, main = "U-Matrix")

dev.new();
c.som <- som(c.sc, somgrid(40, 80, "hexagonal"))
plot(c.som, type = "dist.neighbours", palette.name = rainbow, ncolors = 8, main = "U-Matrix")

#
######################################################



Several different types of plots are supported: 

"changes" shows the mean distance to the closest codebook vector during training.

"codes" shows the codebook vectors. 

"counts" shows the number of objects mapped to the individual units. Empty units are depicted in gray. 

"dist.neighbours" shows the sum of the distances to all immediate neighbours. This kind of visualisation 
                  is also known as a U-matrix plot. Units near a class boundary can be expected to have 
                  higher average distances to their neighbours. Only available for the "som" and "supersom" 
                  maps, for the moment. 

"mapping" shows where objects are mapped. It needs the "classif" argument, and a "labels" or "pchs" argument. 

"property" properties of each unit can be calculated and shown in colour code. It can be used to visualise 
           the similarity of one particular object to all units in the map, to show the mean similarity of 
           all units and the objects mapped to them, etcetera. The parameter property contains the numerical 
           values. See examples below. 

"quality" shows the mean distance of objects mapped to a unit to the codebook vector of that unit. The 
          smaller the distances, the better the objects are represented by the codebook vectors.







