Neural System

profileCarlande
Exercise6Part1.txt

#Neural network method on diabetis data #Run once to install the packaglibrary # install.packages("neuralnet") library("neuralnet") #Read the diabetes data file. Change the file location. setwd("F:/Datasets") diabetes<-read.csv(file="diabetes.csv", head=TRUE, sep=",") #Preview the first 6 rows head(diabetes) #Run the summary command summary(diabetes) #scale the first 8 variables diabetes[1:8]<-scale(diabetes[1:8]) #make sure that the result is reproducible set.seed(12345) #split the data into a training and test set ind <- sample(2, nrow(diabetes), replace = TRUE, prob = c(0.7, 0.3)) train.data <- diabetes[ind == 1, ] test.data <- diabetes[ind == 2, ] #Build the model. If you receive a warning, rerun the command. nn<-neuralnet(formula = class~preg+plas+pres+skin+insu+mass+pedi+age, data = train.data, hidden=2, err.fct="ce", linear.output = FALSE) #names command displays the available neural network properties names(nn) #Run the commands to display the network properties nn$call # the command we ran to generate the model nn$response[1:10] # actual values of the dependent variable for first 10 records nn$covariate [1:12,] # input variables that were used to build the model for first 12 records nn$model.list # list dependent and independent variables in the model nn$net.result[[1]][1:10] # display the first 10 predicted probabilities nn$weights # network weights after the last method iteration nn$startweights # weights on the first method iteration nn$result.matrix # number of trainings steps, the error, and the weights plot(nn) # plot the network #Model evaluation; Round the predicted probabilities mypredict<-compute(nn, nn$covariate)$net.result mypredict<-apply(mypredict, c(1), round) mypredict [1:10] # confusion matrix for the training set table(mypredict, train.data$class, dnn =c("Predicted", "Actual")) mean(mypredict==train.data$class) # confusion matrix for the test set testPred <- compute(nn, test.data[, 0:8])$net.result testPred<-apply(testPred, c(1), round) table(testPred, test.data$class, dnn =c("Predicted", "Actual")) mean(testPred==test.data$class)