# The MIT License
# Copyright (c) 2007 The GGobi Foundation
# http://www.ggobi.org/book/code-license.txt


# Read data
d.olive<-read.csv("olive.csv",row.names=1)

# load libraries
# library(nnet) 
library(rggobi)

indx.tst<-c(1,7,12,15,16,22,27,32,34,35,36,41,50,54,61,68,70,75,
76,80,95,101,102,105,106,110,116,118,119,122,134,137,140,147,148,150,
151,156,165,175,177,182,183,185,186,187,190,192,194,201,202,211,213,217,
218,219,225,227,241,242,246,257,259,263,266,274,280,284,289,291,292,297,
305,310,313,314,323,330,333,338,341,342,347,351,352,356,358,359,369,374,
375,376,386,392,405,406,415,416,418,420,421,423,426,428,435,440,451,458,
460,462,466,468,470,474,476,480,481,482,487,492,493,500,501,509,519,522,
530,532,541,543,545,546,551,559,567,570)
indx.tr<-c(1:572)[-indx.tst]

test.cl <- function(true, pred){
        true <- max.col(true)
        cres <- max.col(pred)
        table(true, cres)
}

d.olive.train<-d.olive[indx.tr,]
d.olive.test<-d.olive[indx.tst,]

d.olive.sth.train<-d.olive.train[d.olive.train[,1]==1,-1]
d.olive.sth.test<-d.olive.test[d.olive.test[,1]==1,-1]

olive.nn<-nnet(as.factor(area)~.,d.olive.sth.train,size=4,linout=T,decay=0.005,
  range=0.06,maxit=1000)
targetr<-class.ind(d.olive.train[,1])
targets<-class.ind(d.olive.test[,1])
test.cl(targetr, predict(olive.nn,d.olive.train[,-1]))
test.cl(targets, predict(olive.nn,d.olive.test[,-1]))
parea<-c(max.col(predict(olive.nn,d.olive.sth.train[,-1])),
  max.col(predict(olive.nn,d.olive.sth.test[,-1])))
d.olive.nn <- cbind(rbind(d.olive.sth.train, 
    d.olive.sth.test), parea)
gg <- ggobi(d.olive.nn)
g <- gg[1]
gcolor <- rep(6,323); gcolor[d.olive.nn[,1]==2] <- 3;
  gcolor[d.olive.nn[,1]==3] <- 2; 
  gcolor[d.olive.nn[,1]==4] <- 9 
glyph_color(g) <- gcolor


