## R code for Chapter 11 in The Elements of Statistical Learning (ESL) ## Load R package for the book library("ElemStatLearn") ## load the training data data(vowel.train) str(vowel.train) # 'data.frame': 528 obs. of 11 variables: # $ y : int 1 2 3 4 5 6 7 8 9 10 ... # $ x.1 : num -3.64 -3.33 -2.12 -2.29 -2.6 ... # $ x.2 : num 0.418 0.496 0.894 1.809 1.938 ... ## use neural network method ## need package "nnet" library("nnet") y <- class.ind(vowel.train$y) # Generates class indicator from the given factor "xclass" X <- vowel.train[,2:11] ## fit neural network model based on Cross-Validation size.nn <- 10 # number of units in the hidden layer decay.nn <- 0.001 # decay.nn.opt[18] = 0.0009765625 rang.nn <- 0.3 # Initial random weights on [-rang, rang] maxit.nn <- 5000 # maximum number of iterations set.seed(123) fit.nn <- nnet(X, y, size=size.nn, rang=rang.nn, decay=decay.nn, maxit=maxit.nn) ## check training error rate train.label.nn <- apply(predict(fit.nn, X), 1, which.max) sum(train.label.nn!=vowel.train$y) # 39 sum(train.label.nn!=vowel.train$y)/dim(y)[1] #0.07386364 ## check testing error rate data(vowel.test) Xt=vowel.test[,2:11] tclass=vowel.test[,1] test.label.nn <- apply(predict(fit.nn, Xt), 1, which.max) sum(test.label.nn!=tclass) # 275 sum(test.label.nn!=tclass)/length(tclass) # 0.5952381