library(MASS)
library(caret)
install.packages("neuralnet")
library(neuralnet)
library(vcd)
data(shuttle)
head(shuttle)
# stability error sign wind magn vis use
# 1 xstab LX pp head Light no auto
# 2 xstab LX pp head Medium no auto
# 3 xstab LX pp head Strong no auto
# 4 xstab LX pp tail Light no auto
# 5 xstab LX pp tail Medium no auto
# 6 xstab LX pp tail Strong no auto
table(shuttle$use)
# auto noauto
# 145 111
#分类变量需要处理成哑变量
dummies <- dummyVars(use~.,data = shuttle)
dummies
shuttle.2 <- as.data.frame(predict(dummies,newdata = shuttle))
head(shuttle.2,3)
# stability.stab stability.xstab error.LX error.MM error.SS error.XL sign.nn sign.pp wind.head wind.tail magn.Light magn.Medium magn.Out magn.Strong vis.no vis.yes
# 1 0 1 1 0 0 0 0 1 1 0 1 0 0 0 1 0
# 2 0 1 1 0 0 0 0 1 1 0 0 1 0 0 1 0
# 3 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0
shuttle.2$use <- ifelse(shuttle$use=="auto",1,0)
table(shuttle.2$use)
# 0 1
# 111 145
#划分数据集
set.seed(123)
trainIndex <- createDataPartition(shuttle.2$use,p=0.7,list = FALSE,times = 1)
shuttleTrain <- shuttle.2[trainIndex,]
table(shuttleTrain$use)
# 0 1
# 73 107
shuttleTest <- shuttle.2[-trainIndex,]
table(shuttleTest$use)
# 0 1
# 38 38
构建神经网络模型
#构建神经网络模型
n <- names(shuttleTrain)
form <- as.formula(paste("use~",paste(n[!n %in% "use"],collapse = "+")))
form
# use ~ stability.stab + stability.xstab + error.LX + error.MM +
# error.SS + error.XL + sign.nn + sign.pp + wind.head + wind.tail +
# magn.Light + magn.Medium + magn.Out + magn.Strong + vis.no +
# vis.yes
fit <- neuralnet(form,data = shuttleTrain,err.fct = "ce",linear.output = FALSE)
fit$result.matrix
# [,1]
# error 0.034172183
# reached.threshold 0.009881012
# steps 196.000000000
# Intercept.to.1layhid1 0.927004568
# stability.stab.to.1layhid1 2.843133961
# stability.xstab.to.1layhid1 -5.640394634
# error.LX.to.1layhid1 -2.914143760
# error.MM.to.1layhid1 -0.757744212
# error.SS.to.1layhid1 4.217875578
# error.XL.to.1layhid1 -4.067079927
# sign.nn.to.1layhid1 -1.048810408
# sign.pp.to.1layhid1 1.772158507
# wind.head.to.1layhid1 -0.638870226
# wind.tail.to.1layhid1 1.722314124
# magn.Light.to.1layhid1 0.787091479
# magn.Medium.to.1layhid1 1.015102440
# magn.Out.to.1layhid1 -7.201957019
# magn.Strong.to.1layhid1 0.380298367
# vis.no.to.1layhid1 19.168715515
# vis.yes.to.1layhid1 -5.836542535
# Intercept.to.use -9.659001340
# 1layhid1.to.use 20.013238671
plot(fit)
评估模型效果
shuttleTrain$prob <- fit$net.result[[1]]
plot(density(shuttleTrain$prob),main = "Train Predict")
shuttleTrain$prob <- fit$net.result[[1]]
plot(density(shuttleTrain$prob),main = "Train Predict")
#ROC曲线
library(scorecard)
perf_eva(pred = fit$net.result[[1]],label = shuttleTrain$use)
res2 <- compute(fit,shuttleTest)
predTest <- res2$net.result
perf_eva(pred = predTest,label = shuttleTest$use)