-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrm_data_analysis_draft.R
More file actions
371 lines (240 loc) · 11.7 KB
/
crm_data_analysis_draft.R
File metadata and controls
371 lines (240 loc) · 11.7 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#Author: B Ntsiba
#Version 1.0
#Date: Mai 2017
# load required libraries
library(caret)
library(corrplot)
library(plyr)
setwd("C:/Users/User/Dropbox/MSc Introduction to Statistical Learning/")
training <- read.table('train_X.csv', header = TRUE,
sep = '\t', na.strings=T)
testing <- read.table('test_X.csv', header = TRUE,
sep = '\t', na.strings=T)
results <- read.table('train_Y.csv', header = T,
sep = '\t')
results = results[-1,] #delete one row to match the remain dataset
# Set seed
set.seed(123456)
# Remove all the variables(columns names) having high missing values(50%)
train_x <- training[, colMeans(is.na(training)) <= .5]
dim(train_x )
# Remove Zero and Near Zero-Variance Predictors available in the caret package
nzv <- nearZeroVar(train_x)
train_x2<- train_x [, -nzv]
dim(train_x2)
train_x2$churn<- as.factor(results$churn ) #Add a new variable, churn
train_x2$appetency <- as.factor(results$appetency) #Add a new variable, appetency
train_x2$upselling <- as.factor(results$upselling) #Add a new variable, upselling
tail(train_x2)
# Identifying numeric variables within the dataset
numericData <- train_x2[sapply(train_x2, is.numeric)]
# Identifying categorical variables within the dataset
categoricalData <- train_x2[sapply(train_x2, is.factor)]
# Compute the correlation matrix descending
descrCor <- cor(numericData)
# Print correlation matrix and look at max correlation
print(descrCor)
summary(descrCor[upper.tri(descrCor)])
# find attributes that are highly corrected
highlyCorrelated <- findCorrelation(descrCor, cutoff=0.7)
# print indexes of highly correlated attributes
print(highlyCorrelated)
# Check Correlation Plot
corrplot(descrCor)
# Indentifying Variable Names of Highly Correlated Variables
highlyCorCol <- colnames(numericData)[highlyCorrelated]
# Print highly correlated attributes
highlyCorCol
sapply(train_x2, class)
write.table(train_x2, "cleanedtrainingData.csv",row.names=FALSE, sep=",") #save the cleaned dataset
dfEvaluate <- cbind(as.data.frame(sapply(train_x2, as.numeric)),
churn=train_x2$churn)
EvaluateAUC <- function(dfEvaluate) {
require(xgboost)
require(Metrics)
CVs <- 10
cvDivider <- floor(nrow(dfEvaluate) / (CVs+1))
indexCount <- 1
outcomeName <- c('churn')
predictors <- names(dfEvaluate)[!names(dfEvaluate) %in% outcomeName]
lsqErr <- c()
lsqAUC <- c()
for (cv in seq(1:CVs)) {
print(paste('crossval',cv))
dataTestIndex <- c((cv * cvDivider):(cv * cvDivider + cvDivider))
dataTest <- dfEvaluate[dataTestIndex,]
dataTrain <- dfEvaluate[-dataTestIndex,]
bst <- xgboost(data = as.matrix(dataTrain[,predictors]),
label = dataTrain[,outcomeName],
max.depth=6, eta = 1, verbose=0,
nround=5, nthread=4,
objective = "reg:linear")
predictions <- predict(bst, as.matrix(dataTest[,predictors]), outputmargin=TRUE)
err <- rmse(dataTest[,outcomeName], predictions)
auc <- auc(dataTest[,outcomeName],predictions)
lsqErr <- c(lsqErr, err)
lsqAUC <- c(lsqAUC, auc)
gc()
}
print(paste('AVG Error:',mean(lsqErr)))
print(paste('AVG AUC:',mean(lsqAUC)))
}
EvaluateAUC(dfEvaluate)
#///////////////////////using appetency /////////////////////////////
dfEvaluate <- cbind(as.data.frame(sapply(train_x2, as.numeric)),
churn=train_x2$appetency)
EvaluateAUC <- function(dfEvaluate) {
require(xgboost)
require(Metrics)
CVs <- 20
cvDivider <- floor(nrow(dfEvaluate) / (CVs+1))
indexCount <- 1
outcomeName <- c('appetency')
predictors <- names(dfEvaluate)[!names(dfEvaluate) %in% outcomeName]
lsqErr <- c()
lsqAUC <- c()
for (cv in seq(1:CVs)) {
print(paste('crossval',cv))
dataTestIndex <- c((cv * cvDivider):(cv * cvDivider + cvDivider))
dataTest <- dfEvaluate[dataTestIndex,]
dataTrain <- dfEvaluate[-dataTestIndex,]
bst <- xgboost(data = as.matrix(dataTrain[,predictors]),
label = dataTrain[,outcomeName],
max.depth=6, eta = 1, verbose=0,
nround=5, nthread=4,
objective = "reg:linear")
predictions <- predict(bst, as.matrix(dataTest[,predictors]), outputmargin=TRUE)
err <- rmse(dataTest[,outcomeName], predictions)
auc <- auc(dataTest[,outcomeName],predictions)
lsqErr <- c(lsqErr, err)
lsqAUC <- c(lsqAUC, auc)
gc()
}
print(paste('AVG Error:',mean(lsqErr)))
print(paste('AVG AUC:',mean(lsqAUC)))
}
EvaluateAUC(dfEvaluate)
#//////////////////// evaluation using upselling ////////////////////////////////////
dfEvaluate <- cbind(as.data.frame(sapply(train_x2, as.numeric)),
churn=train_x2$upselling)
EvaluateAUC <- function(dfEvaluate) {
require(xgboost)
require(Metrics)
CVs <- 20
cvDivider <- floor(nrow(dfEvaluate) / (CVs+1))
indexCount <- 1
outcomeName <- c('upselling')
predictors <- names(dfEvaluate)[!names(dfEvaluate) %in% outcomeName]
lsqErr <- c()
lsqAUC <- c()
for (cv in seq(1:CVs)) {
print(paste('crossval',cv))
dataTestIndex <- c((cv * cvDivider):(cv * cvDivider + cvDivider))
dataTest <- dfEvaluate[dataTestIndex,]
dataTrain <- dfEvaluate[-dataTestIndex,]
bst <- xgboost(data = as.matrix(dataTrain[,predictors]),
label = dataTrain[,outcomeName],
max.depth=6, eta = 1, verbose=0,
nround=5, nthread=4,
objective = "reg:linear")
predictions <- predict(bst, as.matrix(dataTest[,predictors]), outputmargin=TRUE)
err <- rmse(dataTest[,outcomeName], predictions)
auc <- auc(dataTest[,outcomeName],predictions)
lsqErr <- c(lsqErr, err)
lsqAUC <- c(lsqAUC, auc)
gc()
}
print(paste('AVG Error:',mean(lsqErr)))
print(paste('AVG AUC:',mean(lsqAUC)))
}
EvaluateAUC(dfEvaluate)
library(randomForest)
#//////////////////////////////////////////////////////////////////////////////////////////////
#
# Train Random Forest# /
# /
#/////////////////////////////////////////////////////////////////////////////////////////////
sqtmtry<- round(sqrt(ncol(dfEvaluate) - 1))
rfGrid <- expand.grid(mtry = c(round(sqtmtry / 2), sqtmtry, 2 * sqtmtry))
ctrl <- trainControl(method = "cv", classProbs = TRUE, summaryFunction = twoClassSummary, number = 3)
#//////////////////////////////////////////////////////////////////////////////////////////////
#
# Train Random Forest#
# create new dataset to be used for Random Forest
dfEvaluate1 <- dfEvaluate # new dataset
factor(dfEvaluate1$churn,levels=c(-1, 1),labels=c('no', 'yes')) # discretise churn variable
factor(dfEvaluate1$appetency,levels=c(-1, 1),labels=c('no', 'yes')) # discretise appetency variable
factor(dfEvaluate1$upselling,levels=c(-1, 1),labels=c('no', 'yes')) # discretise upselling variable
set.seed(15689) #set seed
churn.rf <- randomForest(churn ~ ., data = dfEvaluate1,na.action=na.exclude) #train a random forest using churn
table(predict(churn.rf),dfEvaluate1$churn) # cross tabulate the prediction
print(churn.rf) #print the random forest
graphics.off() # graphics parameters
par("mar") # graphics parameters
par(mar=c(1,1,1,1)) # graphics parameters
# Variable Importance for the churn attribute
varImpPlot(diabetes.rf,
sort = T,
n.var=10,
main="Top 10 - Churn Most important Variable")
#Random Forest
set.seed(15689)
optional.mod <- tuneRF(dfEvaluate1[-as.numeric(ncol(dfEvaluate1))],dfEvaluate1$churn,ntreeTry = 150,
stepFactor = 2, improve = 0.05,trace = T, plot = T, doBest = F,na.action=na.exclude)
tun.mtry <- optional.mod[as.numeric(which.min(optional.mod[,"OOBError"])),"mtry"]
tun.rf <- randomForest(churn~.,data=dfEvaluate1, mtry= tun.mtry, ntree=101,
keep.forest=TRUE, proximity=TRUE, importance=TRUE,test=test)
pred.test <- predict(tun.rf, newdata = test) # predict using the testing dataset
confusionMatrix(test$diagnosis,pred.test) # confusion matrix using the testing dataset
#//////////////////////////////////////////////////////////////////////////////////////////////
#
# Appetency Random Forest
# create new dataset to be used for Random Forest
factor(dfEvaluate1$churn,levels=c(-1, 1),labels=c('no', 'yes')) # discretise churn variable
factor(dfEvaluate1$appetency,levels=c(-1, 1),labels=c('no', 'yes')) # discretise appetency variable
factor(dfEvaluate1$upselling,levels=c(-1, 1),labels=c('no', 'yes')) # discretise upselling variable
dfEvaluate2 <- dfEvaluate # new dataset
set.seed(15689) #set seed
appetency.rf <- randomForest(appetency ~ ., data = dfEvaluate2,na.action=na.exclude) #train a random forest using appetency
table(predict(appetency.r),dfEvaluate2$appetency) # cross tabulate the prediction
print(churn.rf) #print the random forest
graphics.off() # graphics parameters
par("mar") # graphics parameters
par(mar=c(1,1,1,1)) # graphics parameters
# Variable Importance for the appetency attribute
varImpPlot(appetency.r,
sort = T,
n.var=10,
main="Top 10 - Appetency Most important Variable")
#//////////////////////////////////////////////////////////////////////////////////////////////
#//////////////////////////////////////////////////////////////////////////////////////////////
#
# Appetency Random Forest
set.seed(15689) #set seed
appetency.rf <- randomForest(churn ~ ., data = dfEvaluate1,na.action=na.exclude) #train a random forest using appetency
table(predict(appetency.rf),dfEvaluate1$appetency) # cross tabulate the prediction
print(appetency.rf) #print the random forest
graphics.off() # graphics parameters
par("mar") # graphics parameters
par(mar=c(1,1,1,1)) # graphics parameters
# Variable Importance for the appetency attribute
varImpPlot(appetency.rf,
sort = T,
n.var=10,
main="Top 10 - Appetency Most important Variable")
#//////////////////////////////////////////////////////////////////////////////////////////////
#//////////////////////////////////////////////////////////////////////////////////////////////
#
# Upselling Random Forest
set.seed(15689) #set seed
upselling.rf <- randomForest(upselling ~ ., data = dfEvaluate1,na.action=na.exclude) #train a random forest using appetency
table(predict(upselling.rf),dfEvaluate1$upselling) # cross tabulate the prediction
print(upselling.rff) #print the random forest
graphics.off() # graphics parameters
par("mar") # graphics parameters
par(mar=c(1,1,1,1)) # graphics parameters
# Variable Importance for the appetency attribute
varImpPlot(upselling.rf,
sort = T,
n.var=10,
main="Top 10 - upselling Most important Variable")