-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCART.R
More file actions
169 lines (127 loc) · 4.41 KB
/
CART.R
File metadata and controls
169 lines (127 loc) · 4.41 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
## Author: Rajesh Jakhotia
## Company Name: K2 Analytics Finishing School Pvt. Ltd
## Email : ar.jakhotia@k2analytics.co.in
## Website : k2analytics.co.in
## Let us first set the working directory path
setwd ("D:/K2Analytics/Datafile/")
getwd()
## Data Import
CTDF.dev <- read.table("DEV_SAMPLE.csv", sep = ",", header = T)
CTDF.holdout <- read.table("HOLDOUT_SAMPLE.csv", sep = ",", header = T)
c(nrow(CTDF.dev), nrow(CTDF.holdout))
str(CTDF.dev)
## installing rpart package for CART
## install.packages("rpart")
## install.packages("rpart.plot")
## loading the library
library(rpart)
library(rpart.plot)
## Target Rate
sum(CTDF.dev$Target)/14000
## setting the control paramter inputs for rpart
r.ctrl = rpart.control(minsplit=100, minbucket = 10, cp = 0, xval = 5)
## calling the rpart function to build the tree
##m1 <- rpart(formula = Target ~ ., data = CTDF.dev[which(CTDF.dev$Holding_Period>10),-1], method = "class", control = r.ctrl)
m1 <- rpart(formula = Target ~ ., data = CTDF.dev[,-1], method = "class", control = r.ctrl)
m1
## install.packages("rattle")
## install.packages("RcolorBrewer")
library(rattle)
library(RColorBrewer)
fancyRpartPlot(m1)
## to find how the tree performs
printcp(m1)
plotcp(m1)
##rattle()
## Pruning Code
ptree<- prune(m1, cp= 0.0021 ,"CP")
printcp(ptree)
fancyRpartPlot(ptree, uniform=TRUE, main="Pruned Classification Tree")
## Let's use rattle to see various model evaluation measures
##rattle()
View(CTDF.dev)
## Scoring syntax
CTDF.dev$predict.class <- predict(m1, CTDF.dev, type="class")
CTDF.dev$predict.score <- predict(m1, CTDF.dev)
View(CTDF.dev)
head(CTDF.dev)
#############################Deciling code ######Pavan#####
## deciling code
decile <- function(x){
deciles <- vector(length=10)
for (i in seq(0.1,1,.1)){
deciles[i*10] <- quantile(x, i, na.rm=T)
}
return (
ifelse(x<deciles[1], 1,
ifelse(x<deciles[2], 2,
ifelse(x<deciles[3], 3,
ifelse(x<deciles[4], 4,
ifelse(x<deciles[5], 5,
ifelse(x<deciles[6], 6,
ifelse(x<deciles[7], 7,
ifelse(x<deciles[8], 8,
ifelse(x<deciles[9], 9, 10
))))))))))
}
class(CTDF.dev$predict.score)
## deciling
CTDF.dev$deciles <- decile(CTDF.dev$predict.score[,2])
View(CTDF.dev)
## Ranking code
##install.packages("data.table")
library(data.table)
tmp_DT = data.table(CTDF.dev)
rank <- tmp_DT[, list(
cnt = length(Target),
cnt_resp = sum(Target),
cnt_non_resp = sum(Target == 0)) ,
by=deciles][order(-deciles)]
rank$rrate <- round(rank$cnt_resp * 100 / rank$cnt,2);
rank$cum_resp <- cumsum(rank$cnt_resp)
rank$cum_non_resp <- cumsum(rank$cnt_non_resp)
rank$cum_perct_resp <- round(rank$cum_resp * 100 / sum(rank$cnt_resp),2);
rank$cum_perct_non_resp <- round(rank$cum_non_resp * 100 / sum(rank$cnt_non_resp),2);
rank$ks <- abs(rank$cum_perct_resp - rank$cum_perct_non_resp);
View(rank)
##install.packages("ROCR")
library(ROCR)
pred <- prediction(CTDF.dev$predict.score[,2], CTDF.dev$Target)
perf <- performance(pred, "tpr", "fpr")
plot(perf)
KS <- max(attr(perf, 'y.values')[[1]]-attr(perf, 'x.values')[[1]])
auc <- performance(pred,"auc");
auc <- as.numeric(auc@y.values)
##install.packages("ineq")
library(ineq)
gini = ineq(CTDF.dev$predict.score[,2], type="Gini")
with(CTDF.dev, table(Target, predict.class))
auc
KS
gini
View(rank)
## Syntax to get the node path
tree.path <- path.rpart(ptree, node = c(26, 27))
nrow(CTDF.holdout)
## Scoring Holdout sample
CTDF.holdout$predict.class <- predict(m1, CTDF.holdout, type="class")
CTDF.holdout$predict.score <- predict(m1, CTDF.holdout)
CTDF.holdout$deciles <- decile(CTDF.holdout$predict.score[,2])
View(CTDF.holdout)
## Ranking code
##install.packages("data.table")
library(data.table)
tmp_DT = data.table(CTDF.holdout)
h_rank <- tmp_DT[, list(
cnt = length(Target),
cnt_resp = sum(Target),
cnt_non_resp = sum(Target == 0)) ,
by=deciles][order(-deciles)]
h_rank$rrate <- round(h_rank$cnt_resp * 100 / h_rank$cnt,2);
h_rank$cum_resp <- cumsum(h_rank$cnt_resp)
h_rank$cum_non_resp <- cumsum(h_rank$cnt_non_resp)
h_rank$cum_perct_resp <- round(h_rank$cum_resp * 100 / sum(h_rank$cnt_resp),2);
h_rank$cum_perct_non_resp <- round(h_rank$cum_non_resp * 100 / sum(h_rank$cnt_non_resp),2);
h_rank$ks <- abs(h_rank$cum_perct_resp - h_rank$cum_perct_non_resp);
View(h_rank)
with(CTDF.holdout, table(Target, predict.class))