predict.nnet(object, newdata, type=c("raw","class"), ...)
nnet as returned by
nnet.
type="raw", the matrix of values returned by the trained network;
type="class", the corresponding class (which is probably only
useful if the net was generated by
nnet.formula).
This function is a method for the generic function for class nnet. It can be invoked by calling for an object x of the appropriate class, or directly by calling regardless of the class of the object.
# use half the iris data
ir <- rbind(iris[,,1],iris[,,2],iris[,,3])
targets <- class.ind( c(rep("s", 50), rep("c", 50), rep("v", 50)) )
samp <- c(sample(1:50,25), sample(51:100,25), sample(101:150,25))
ir1 <- nnet(ir[samp,], targets[samp,],size=2, rang=0.1,
decay=5e-4, maxit=200)
test.cl <- function(true, pred){
true <- max.col(true)
cres <- max.col(pred)
table(true, cres)
}
test.cl(targets[-samp,], predict(ir1, ir[-samp,]))
# or
ird <- data.frame(rbind(iris[,,1], iris[,,2], iris[,,3]),
species=c(rep("s",50), rep("c", 50), rep("v", 50)))
ir.nn2 <- nnet(species ~ ., data=ird, subset=samp, size=2, rang=0.1,
decay=5e-4, maxit=200)
table(ird$species[-samp], predict(ir.nn2, ird[-samp,], type="class"))