#-*- R -*- ## Script from Fourth Edition of `Modern Applied Statistics with S' # Chapter 12 Classification library(MASS) library(class) library(nnet) # 12.4 Neural networks pltnn <- function(main, ...) { plot(Cushings[,1], Cushings[,2], type="n", xlab="Tetrahydrocortisone", ylab = "Pregnanetriol", main=main, ...) for(il in 1:4) { set <- Cushings$Type==levels(Cushings$Type)[il] text(Cushings[set, 1], Cushings[set, 2], as.character(Cushings$Type[set]), col = 2 + il) } } plt.bndry <- function(size=0, decay=0, ...) { cush.nn <- nnet(cush, tpi, skip=T, softmax=T, size=size, decay=decay, maxit=1000) invisible(b1(predict(cush.nn, cushT), ...)) } b1 <- function(Z, ...) { zp <- Z[,3] - pmax(Z[,2], Z[,1]) contour(xp, yp, matrix(zp, np), add=T, levels=0, labex=0, ...) zp <- Z[,1] - pmax(Z[,3], Z[,2]) contour(xp, yp, matrix(zp, np), add=T, levels=0, labex=0, ...) } data(Cushings) cush <- as.matrix(Cushings[, -3]) tp <- Cushings$Type[1:21, drop = T] xp <- seq(0.6, 4.0, length = 100) np <- length(xp) yp <- seq(-3.25, 2.45, length = 100) cushT <- expand.grid(Tetrahydrocortisone = xp, Pregnanetriol = yp) cush <- cush[1:21,] tpi <- class.ind(tp) # functions pltnn and plt.bndry given in the scripts pltnn("Size = 2") set.seed(1) plt.bndry(size = 2, col = 2)