### First generate some data as described in Chapter 2 library(MASS) Ident = matrix(c(1,0,0,1),nrow=2) m1 = mvrnorm(10,mu=c(1,0),Sigma = Ident) m2 = mvrnorm(10, mu=c(0,1),Sigma = Ident) x1 = matrix(0,100,2) x2=x1 for(i in 1:100){x1[i,]=mvrnorm(1,mu=m1[sample(1:10,1),],Sigma = Ident/5) x2[i,]=mvrnorm(1,mu=m2[sample(1:10,1),],Sigma = Ident/5)} simmix = rbind(x1,x2) dim(simmix) simmix=cbind(simmix,rep(1:2,each=100)) simmix = data.frame(simmix) plot(simmix[,-3],pch="0", col=c("blue","orange")[simmix[,3]]) ## Now the data is in the data frame simmix ## (200 rows, 3 columns, 3rd column indicates group) library(nnet) ## following the iris example in the help file carefully, this finally worked targets = class.ind(simmix[,3]) # sim.nnet = nnet(simmix[,1:2],targets,size=10,decay=0.02) # now trying to duplicate Figure 11.4 gridlines = expand.grid(xx1 = seq(min(simmix[,1]),max(simmix[,1]),by=.05), xx2 = seq(min(simmix[,2]),max(simmix[,2]),by=.05)) dim(gridlines) # expand.grid is a very handy function fitted = predict(sim.nnet,gridlines) dim(fitted) fitted[1:3,] points(gridlines, pch=".",col=c("blue","orange")[(fitted[,1]<=fitted[,2])+1]) ## this plot is for weight decay =0.02, a similar construction with no weight decay ## imitates Figure 11.4 (top)