library("neuralnet")
library(ISLR)
data <- College
View(data)
Restructring the data so that it satisfies the neuralnet packages
#Scale and analyze the data
max_data <- apply(data[,2:18], 2, max)
min_data <- apply(data[,2:18], 2, min)
data_scaled <- scale(data[,2:18],center = min_data, scale = max_data - min_data)
Private <- as.numeric(College$Private)-1
data_scaled <- cbind(Private,data_scaled)
#Splitting out the training data and the testing data. I split out the data 70% training and 30% testing.
index <- sample(1:nrow(data),round(0.70*nrow(data)))
train_data <- as.data.frame(data_scaled[index,])
test_data <- as.data.frame(data_scaled[-index,])
n <- names(train_data)
f <- as.formula(paste("Private ~", paste(n[!n %in% "Private"], collapse = " + ")))
deep_net <- neuralnet(f,data=train_data,hidden=c(5,3),linear.output=F)
plot(deep_net, rep ='best')
#Analyze the results of our model
predicted_data <- compute(deep_net,test_data[,2:18])
print(head(predicted_data$net.result))
## [,1]
## Albertus Magnus College 1.000000e+00
## Albright College 1.000000e+00
## Alverno College 1.000000e+00
## Andrews University 1.000000e+00
## Arizona State University Main campus 1.184633e-16
## Auburn University-Main Campus 2.809768e-19
predicted_data$net.result <- sapply(predicted_data$net.result,round,digits=0)
#Create table to measure what the model predicted was and was not a private or public college
table(test_data$Private,predicted_data$net.result)
##
## 0 1
## 0 47 6
## 1 3 177