# The MIT License # Copyright (c) 2007 The GGobi Foundation # http://www.ggobi.org/book/code-license.txt # Read data, and standardize before running SOM d.music<-read.csv("music-sub.csv",row.names=1) d.music.std<-cbind(d.music[,c(1,2)],apply(d.music[,-c(1,2)],2,f.std.data)) # Utility functions f.std.data<-function(x) { return((x-mean(x,na.rm=T))/sd(x,na.rm=T)) } # Load libraries library(rggobi) library(som) # Run SOM music.som<-som(d.music.std[,-c(1:2)],6,6,neigh="bubble",rlen=100) music.som<-som(d.music.std[,-c(1:2)],6,6,neigh="bubble",rlen=1000) # Set up data for ggobi - data needs to have any labels in the first few columns f.ggobi.som<-function(x,x.som) { xmx<-jitter(x.som$visual$x,factor=2) xmy<-jitter(x.som$visual$y,factor=2) ncols<-ncol(x) x.ggobi<-cbind(x,xmx,xmy) dimnames(x.ggobi)[[2]][ncols+1]<-"Map 1" dimnames(x.ggobi)[[2]][ncols+2]<-"Map 2" x.grid<-cbind(x.som$code,x.som$code.sum[,1:2]) dimnames(x.grid)[[2]]<-dimnames(x.ggobi)[[2]] x.clust<-rbind(x.ggobi,x.grid) } f.ggobi.som.net<-function(x.som) { x.net<-NULL for (i in 1:x.som$xdim) { for (j in 1:x.som$ydim) { if (j