forked from brycemcd/appliedpredictivemodeling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch14.R
More file actions
112 lines (93 loc) · 3.29 KB
/
Copy pathch14.R
File metadata and controls
112 lines (93 loc) · 3.29 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
library("AppliedPredictiveModeling")
library('caret')
library('C50')
library('gbm')
library('ipred')
library('partykit')
library('pROC')
library('randomForest')
library('RWeka')
library('rpart')
library('e1071')
# note - the book's computation section appears to be out of date. Work
# from here instead: https://github.com/cran/AppliedPredictiveModeling/blob/master/inst/chapters/14_Class_Trees.R
# Classification Trees
ctrl <- trainControl(method = "LOGCV",
summaryFunction = twoClassSummary,
classProbs = TRUE,
index = list(TrainSet = pre2008),
savePredictions = TRUE)
set.seed(476)
rpartFit <- train(x = training[, fullSet],
y = training$Class,
method = 'rpart',
tuneLength = 30,
metric = "ROC",
trControl = ctrl)
plot(as.party(rpartFit$finalModel))
rpart2008 <- merge(rpartFit$pred, rpartFit$bestTune)
rpartCM <- confusionMatrix(rpartFit, norm = "none")
rpartCM
rpartRoc <- roc(response = rpartFit$pred$obs,
predictor = rpartFit$pred$successful,
levels = rev(levels(rpartFit$pred$obs)))
rpartRoc
plot(rpartRoc)
rpartFactorFit <- train(x = training[, factorPredictors],
y = training$Class,
method = "rpart",
tuneLength = 30,
metric = "ROC",
trControl = ctrl)
rpartFactorFit
plot(as.party(rpartFactorFit$finalModel))
rpartFactor2008 <- merge(rpartFactorFit$pred, rpartFactorFit$bestTune)
rpartFactorCM <- confusionMatrix(rpartFactorFit, norm = 'none')
rpartFactorCM
rpartFactorRoc <- roc(response = rpartFactorFit$pred$obs,
predictor = rpartFactorFit$pred$successful,
levels = rev(levels(rpartFactorFit$pred$obs)))
plot(rpartFactorRoc)
# omnibus plot
plot(
rpartRoc, type='s', print.thres = c(0.5),
print.thres.pch = 3,
print.thres.pattern = "",
print.thres.cex = 1.2,
col = 'red', legacy.axes = TRUE,
print.thres.col = 'red')
plot(
rpartFactorRoc, type='s', print.thres = c(0.5),
print.thres.pch = 16,
print.thres.pattern = "",
print.thres.cex = 1.2,
add = TRUE,
col = 'black', legacy.axes = TRUE,
print.thres.col = 'red')
legend(.75, .2,
c('Grouped Categories', 'Independent Categories'),
lwd = c(1,1),
col = c('black', 'red'),
pch = c(16, 3))
# woot! Let's do J48
j48FactorFit <- train(x = training[, factorPredictors],
y = training$Class,
method = "J48",
metric = "ROC",
trControl = ctrl)
j48FactorFit
# rule based models
# ugggg --- java hell. Come back to this one
# Bagged Trees
treeBagFit <- train(x = training[, fullSet],
y = training$Class,
nbagg = 50,
method = "treebag",
metric = "ROC",
trControl = ctrl)
treebag2008 <- merge(treeBagFit$pred, treeBagFit$bestTune)
treebagCM <- confusionMatrix(treeBagFit, norm = "none")
treebagCM
treebagRoc <- roc(response = treeBagFit$pred$obs,
predictor = treeBagFit$pred$successful,
levels = rev(levels(treeBagFit$pred$obs)))