-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregression + neural net.R
92 lines (53 loc) · 2.56 KB
/
regression + neural net.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
66
67
68
69
70
71
72
73
74
#### Regression
rega=glm(qual~., data=wine.training, family= "binomial")
summary(rega)
rega2=glm(qual~., data=wine.training[-(c(3, 6, 9))], family= "binomial")
summary(rega2)
predict.glm=predict(rega2, wine.test[-c(3, 6, 9)], type="response")
predglm=rep(0, length(wine.test$qual))
predglm[predict.glm>0.5]=1
predglm <- as.factor(predglm); levels(predglm) <- c("poor","good")
conf.mat.glm1 <- confusionMatrix(predglm,wine.test$qual)
accuracy.glm1 <- conf.mat.glm1$overall[1]
accuracy.balanced.glm1 <- conf.mat.glm1$byClass[11]
sensitivity.glm1 <- conf.mat.glm1$byClass[1]
specificity.glm1 <- conf.mat.glm1$byClass[2]
negpredval.glm1 <- conf.mat.glm1$byClass[4]
predglm2=rep(0, length(wine.test$qual))
predglm2[predict.glm>0.7]=1
predglm2 <- as.factor(predglm2); levels(predglm2) <- c("poor","good")
conf.mat.glm2 <- confusionMatrix(predglm2,wine.test$qual)
accuracy.glm2 <- conf.mat.glm2$overall[1]
accuracy.balanced.glm2 <- conf.mat.glm2$byClass[11]
sensitivity.glm2 <- conf.mat.glm2$byClass[1]
specificity.glm2 <- conf.mat.glm2$byClass[2]
negpredval.glm2 <- conf.mat.glm2$byClass[4]
predglm3=rep(0, length(wine.test$qual))
predglm3[predict.glm>0.1]=1
predglm3 <- as.factor(predglm3); levels(predglm3) <- c("poor","good")
conf.mat.glm3 <- confusionMatrix(predglm3,wine.test$qual)
accuracy.glm3 <- conf.mat.glm3$overall[1]
accuracy.balanced.glm3 <- conf.mat.glm3$byClass[11]
sensitivity.glm3 <- conf.mat.glm3$byClass[1]
specificity.glm3 <- conf.mat.glm3$byClass[2]
negpredval.glm3 <- conf.mat.glm3$byClass[4]
wine.glm.important <- summary(rega)$coefficients %>% as.data.frame()
colnames(wine.glm.important) <- c("estimate","error","z","pval")
wine.glm.important <- wine.glm.important %>% rownames_to_column('explanatory') %>% top_n(3,1/pval) %>% select(explanatory,pval)
### Neural net
nnetFit<- train(qual~., data=wine.training, method="nnet", preProcess="range", trace=FALSE, trControl=trainControl(method="cv", sampling="up"))
nnetFit
# Prediction on the training set
wine.nnet.pred<-predict(nnetFit, wine.training[,-12])
#prediction on the testset
wine.nnet.pred.test<-predict(nnetFit, wine.test[,-12])
conf.mat.nnet <- confusionMatrix(wine.nnet.pred.test,as.factor(wine.test$qual))
accuracy.nnet <- conf.mat.nnet$overall[1]
accuracy.balanced.nnet <- conf.mat.nnet$byClass[11]
sensitivity.nnet <- conf.mat.nnet$byClass[1]
specificity.nnet <- conf.mat.nnet$byClass[2]
negpredval.nnet <- conf.mat.nnet$byClass[4]
# Plot of the net
pal <- viridis(8)
plotnet(nnetFit, circle_col= "#277F8EFF", pos_col="#FDE725FF", neg_col= "#440154FF", alpha.val=0.7)
nnetFit$finalModel$entropy