-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_neural.R
65 lines (48 loc) · 1.21 KB
/
test_neural.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
library(neuralnet)
library(NeuralNetTools)
data <-
read.csv(
"~/Downloads/titanic/train.csv",
header = TRUE,
sep = ","
)
data <- na.omit(data)
data[complete.cases(data),]
data[] <- lapply(data, function(x) {
if (is.factor(x))
as.numeric(x)
else
x
})
smp_size <- floor(0.75 * nrow(data))
train_ind <- sample(seq_len(nrow(data)), size = smp_size)
train <- data[train_ind,]
test <- data[-train_ind,]
str(train)
formula <- as.formula("Survived ~ Age + Sex1")
field <- "Survived"
model_nn <-
neuralnet(
formula,
data = train,
hidden = c(2),
linear.output = T,
rep = 2,
stepmax = 1000000,
lifesign = "minimal"
)
plot(model_nn, rep = "best")
d <- subset(test, select = -field)
str(d)
garson(model_nn)
prob <- neuralnet::compute(model_nn, test[, model_nn$model.list$variables])
tt <- test[, model_nn$model.list$variables]
tt$Prediction <- prob$net.result
str(tt)
table(tt)
resultTable <- table(test[, model_nn$model.list$variables], prob$net.result)
resultTable
pred <- ifelse(prob$net.result > 0.5, 1, 0)
confusionMatrix(factor(pred), factor(test$Survived))
pred <- predict(model_nn, test[, model_nn$model.list$variables])
table(test$Survived, apply(pred, 1, which.max))