# Nick Zinns wrapper around the neural net function # ---------------------------------------------------------------- testnnet<-function(x, y, out = 0.5, ...) { if(is.vector(x) == T){x <- as.matrix(x,ncol=1)} if(nrow(x) != length(y)) stop("x and y must be equal length") if(out >= 1 || out <= 0) stop("Out must be a decimal value between 0 and 1") training <- sample(length(y), floor(abs(length(y) * (1 - out)))) validation <- seq(1, length(y))[ -training] ## Run nnet fit <- nnet(x[training, ], y[training], ...) ##make out of sample prediction if(ncol(x) == 1){ outsample.pred <- ifelse((predict.nnet(fit, matrix(x[validation, ],ncol=1)) > 0.5), 1, 0) } if(ncol(x) != 1){ outsample.pred <- ifelse((predict.nnet(fit, x[validation, ]) > 0.5), 1, 0) } ##assemble comparison validate.data <- as.matrix(y[validation]) ##print the results print(fit) print(paste(length(training), "observations in training set")) print(paste(length(validate.data), "observations in validation set")) output.table <- table(validate.data, outsample.pred) cat("\nOut of sample prediction table:\n") print(output.table) ##return the nnet object invisible(fit) }