-
Notifications
You must be signed in to change notification settings - Fork 3
/
Phase 2.Rmd
510 lines (453 loc) · 16.3 KB
/
Phase 2.Rmd
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
---
title: "Phase II"
author: "Shreya and Francis"
date: "November 11, 2017"
output: html_document
---
Content of the RMarkdown
- Hierarchical clustering analysis
- All Data: Login deposited vs not deposited
- All Data: Messages vs Login - deposited
- All Data: Messages vs Login - not deposited
```{r}
install.packages("readxl")
install.packages("ggplot2")
install.packages("dplyr")
install.packages("tidyr")
```
# Francis
```{r}
install.packages("dplyr")
install.packages("tidyr")
library("dplyr")
library("tidyr")
library("rpart")
```
read data
```{r}
A1<-dplyr::select(ApplicationBase,identity,EverDeposited,Scholarship_app,permanent_country)
A2<--dplyr::select(PersonGRE,identity,Total)
A3<-dplyr::select(PersonSchools,identity,gpa,earned_degree)
A4<-dplyr::select(ApplicationDecisions,identity,decision_name)
A5<-dplyr::full_join(A1,A2,by="identity", all=TRUE)
A6<-dplyr::full_join(A3,A4,by="identity", all=TRUE???
DF<-dplyr::full_join(A6,A5,by="identity", all=TRUE???
A5<-na.omit(A5)
```
```{r}
A5$earned_degree<-ifelse(A5$earned_degree=="Y",1,0)
View(A5)
A5$EverDeposited<-ifelse(A5$EverDeposited=="Y",1,0)
View(A5)
```
```{r}
set.seed(1)
sub<-sample(1:nrow(A5),round(nrow(A5)*2/3))
length(sub)
data_train<-A5[sub,]
data_test<-A5[-sub,]
dim(data_train)
dim(data_test)
table(data_train$data1)
table(data_test$data2)
tree.both<-rpart(as.factor(data_test$EverDeposited)~data_train$earned_degree .,data=data_train,method='class',minsplit=20,minbucket=150,cp=0.00017)
summary(tree.both)
tree.both$variable.importance
printcp(tree.both)
plotcp(tree.both,lwd=2)
```
#plot tree
```{r}
fit <- rpart(e ~ a + b+ c, method="class", data=DATA)
printcp(fit) # display the results
plotcp(fit) # visualize cross-validation results
summary(fit) # detailed summary of splits
plot(fit, uniform=TRUE, main="Tree")
text(fit, use.n=TRUE, all=TRUE, cex=.8)
post(fit)
#cluster analysis(ggplot,ggplot2)
```{r}
y1=lm(data1~data2,data=A)
plot(A$data1,B$data2)
abline(y1)
```
```{r}
ggplot(data, aes(x=a, y=b, colour=c)) + geom_point()#color
ggplot(data, aes(x=a, y=b, shape=c)) + geom_point()#shape
```
```{r}
ggplot(data, aes(x=a, y=b))+ stat_bin2d()
ggplot(data, aes(x=a, y=b))+ geom_point(alpha=.1)
ggplot(data, aes(x=a, y=b)+ geom_point(alpha=.01)
ggplot(data, aes(x=a, y=b)+ stat_bin2d()
```
#K means clustering analysis
-> Read data
```{r}
a1<-dplyr::select()
a2<-read_excel("xlsx")
a3<-dplyr::full_join(a1,a3,by="m", all=TRUE)
```
-> clean
```{r}
a3<-na.omit(a3)
a3$ear<-ifelse(a5$ear=="Y",1,0)
View(a5)
a3$Ever<-ifelse(a5$ear=="Y",1,0)
```
-> plots
```{r}
kmeans.cluster <- kmeans(data, centers=3)
kmeans.cluster$withinss
table(kmeans.cluster$cluster, A$data)
require(factoextra)
fviz_cluster(kmeans.cluster,
data = data,
geom = c("point","text"),
frame.type = "norm")
```
```{r}
fviz_nbclust(data,
FUNcluster = kmeans,
method = "wss",
k.max = 12 # max number of clusters to consider
) +
labs(title="Elbow Method for K-Means") +
geom_vline(xintercept = 3,
linetype = 2)
```
#K-Medoid clustering analysis
```{r}
require(cluster)
kmedoid.cluster <- pam(data, k=3) require(factoextra)
kmedoid.cluster$objective
table(kmedoid.cluster$clustering, A$data)
require(factoextra)
fviz_cluster(kmedoid.cluster,
data = data,
geom = c("point"),
frame.type = "norm")
```
```{r}
fviz_nbclust(data,
FUNcluster = pam,
method = "wss",
k.max = 12
) +
labs(title="Elbow Method for K-Medoid") +
geom_vline(xintercept = 3,
linetype = 2)
```
```{r}
require(factoextra)
fviz_nbclust(data,
FUNcluster = kmeans, # K-Means
method = "silhouette", # Avg. Silhouette
k.max = 12 # max number of clusters
) +
labs(title="Avg.Silhouette Method for K-Means")
```
# Shreya
Login and Message Clustering on time to differentiate between deposited and not deposited
All Data
1. Login deposited vs not deposited
2. Messages vs Login - deposited
3. Messages vs Login - not deposited
Year wise analysis based on term start to see if there are differences
Content of the RMarkdown
1. Read data
2. Message data
3. Login data
4. App base data
5. Final Data Visualizations
```{r}
install.packages("readxl")
install.packages("psych")
install.packages("ggplot2")
install.packages("dplyr")
install.packages("tidyr")
```
Read Data
```{r}
library(readxl)
appbase <- read_excel("C:/Users/Shreya/Documents/GITHUB/Capstone/ApplicationBase.xlsx")
```
```{r}
library(readxl)
login <- read_excel("C:/Users/Shreya/Documents/GITHUB/Capstone/PersonLogins.xlsx")
```
```{r}
library(readxl)
message <- read_excel("C:/Users/Shreya/Documents/GITHUB/Capstone/PersonMessages.xlsx")
```
Message Data
```{r}
table(message$opened_or_url)
#table(message$identity, message$opened_or_url)
```
```{r}
message$opened_or_url_1 <- ifelse(message$opened_or_url == "NULL", "0", 1)
message$opened_or_url_1 <-as.numeric(message$opened_or_url_1)
```
```{r}
library(dplyr)
library(tidyr)
message$count <- 1
message2 <- message %>% group_by(identity) %>% summarize(sum(opened_or_url_1))
message3 <- message %>% group_by(identity) %>% summarize(sum(count))
```
```{r}
message2 <- setNames(message2, c("identity", "sum_open_or_url_message"))
message3 <- setNames(message3, c("identity", "sum_num_of_message"))
```
```{r}
library(dplyr)
message_main <- dplyr::full_join(message3, message2, by="identity")
```
Login Data
```{r}
library(dplyr)
library(tidyr)
login$count <- 1
login1 <- login %>% group_by(identity) %>% summarize(sum(count))
login1 <- setNames(login1, c("identity", "sum_login_count"))
```
Application Base Data
```{r}
appbase$sex_1 <- ifelse(appbase$sex == "NULL", "Not_Stated", appbase$sex)
appbase$PrevApply_1 <- ifelse(appbase$PrevApply == "0", "No", "Yes")
appbase$Scholarship_app_1 <- ifelse(appbase$Scholarship_app == "0", "No", "Yes")
```
```{r}
range(appbase$age_at_app)
```
```{r}
appbase$age_at_app_new <- ifelse(appbase$age_at_app <=16, NA, appbase$age_at_app)
appbase$age_at_app_new <- as.numeric(appbase$age_at_app_new)
```
```{r}
appbase$credits_req <- appbase$`Credits Req`
appbase$credits_req_new <-
ifelse(appbase$credits_req ==32, "32",
ifelse(appbase$credits_req ==33, "33-36",
ifelse(appbase$credits_req ==34, "33-36",
ifelse(appbase$credits_req == 36, "33-36",
ifelse(appbase$credits_req == 38, "38-45",
ifelse(appbase$credits_req == 40, "38-45",
ifelse(appbase$credits_req == 41, "38-45",
ifelse(appbase$credits_req == 42, "38-45",
ifelse(appbase$credits_req == 43, "38-45",
ifelse(appbase$credits_req == 44, "38-45",
ifelse(appbase$credits_req == 45, "38-45",
ifelse(appbase$credits_req == 47, "47-60",
ifelse(appbase$credits_req == 48, "47-60",
ifelse(appbase$credits_req == 52, "47-60",
ifelse(appbase$credits_req == 57, "47-60",
ifelse(appbase$credits_req == 60, "47-60",
ifelse(appbase$credits_req == 62, "62-75",
ifelse(appbase$credits_req == 69, "62-75",
ifelse(appbase$credits_req == 74, "62-75",
ifelse(appbase$credits_req == 75, "62-75",
ifelse(appbase$credits_req == 86, "86-95",
ifelse(appbase$credits_req == 90, "86-95",
ifelse(appbase$credits_req == 95, "86-95",
appbase$credits_req)))))))))))))))))))))))
table(appbase$credits_req_new)
```
Merging Data
```{r}
Data <- dplyr::full_join(appbase, login1, by="identity", all=TRUE)
Data <- dplyr::full_join(Data, message_main, by="identity", all=TRUE)
```
Login Data more manipulation
spliting date and time
```{r}
login2 <- login
login2$datelogin <- as.Date(login2$issued)
login2$timelogin <- format(login2$issued,"%H:%M:%S")
Data1 <- dplyr::full_join(login2, Data, by="identity", all=TRUE)
```
Difference in login date and term start date
```{r}
Data1$diff_in_days<- difftime(Data1$datelogin, Data1$`Term Start`, units = c("days"))
Data1$diff_in_days <- as.numeric(Data1$diff_in_days)
hist(Data1$diff_in_days)
```
Final Data Visualizations - Clustering of Logins and Messages over Time
#1
```{r}
library(ggplot2)
ggplot(data=Data1, aes(diff_in_days)) +
geom_histogram(breaks=seq(-1200,900, by = 10),
aes(fill=..count..)) +
scale_fill_gradient("Count", low = "darkorange", high = "mediumblue")+
labs(title="Difference in days from term start date - All Applicants", x="Difference in days from term start date", y="Number of logins on that day")+
theme(plot.title = element_text(face = "bold", size=15))
```
#2
```{r}
library(ggplot2)
ggplot(data=Data1, aes(diff_in_days)) +
geom_histogram(breaks=seq(-365, 10, by = 10),
aes(fill=..count..)) +
scale_fill_gradient("Count", low = "darkorange", high = "mediumblue")+
labs(title="Difference in days from term start date - All Applicants", x="Difference in days from term start date", y="Number of logins on that day")+
theme(plot.title = element_text(face = "bold", size=15))
```
#3
```{r}
Everdeposit_Yes <- Data1[Data1$EverDeposited == "Y",]
Everdeposit_Yes$diff_in_days<- difftime(Everdeposit_Yes$datelogin, Everdeposit_Yes$`Term Start`, units = c("days"))
library(ggplot2)
ggplot(data=Everdeposit_Yes, aes(diff_in_days)) +
geom_histogram(breaks=seq(-365, 10, by = 10),
aes(fill=..count..)) +
scale_fill_gradient("Count", low = "yellow", high = "forestgreen")+
labs(title="Difference in days from term start date - Applicants who Deposit", x="Difference in days from term start date", y="Number of logins on that day")+
theme(plot.title = element_text(face = "bold", size=15))
```
#4
```{r}
Everdeposit_No <- Data1[Data1$EverDeposited == "N",]
Everdeposit_No$diff_in_days<- difftime(Everdeposit_No$datelogin, Everdeposit_No$`Term Start`, units = c("days"))
library(ggplot2)
ggplot(data=Everdeposit_No, aes(diff_in_days)) +
geom_histogram(breaks=seq(-365, 10, by = 10),
aes(fill=..count..)) +
scale_fill_gradient("Count", low = "yellow2", high = "red")+
labs(title="Difference in days from term start date - Applicant who did not Deposit", x="Difference in days from term start date", y="Number of logins on that day")+
theme(plot.title = element_text(face = "bold", size=15))
```
Data Manipulation
```{r}
Data5 <- Data1
Data5$diff_in_days<- difftime(Data5$datelogin, Data5$`Term Start`, units = c("days"))
Data5$count <- 1
library(dplyr)
library(tidyr)
Data5 <- Data5 %>% group_by(diff_in_days) %>% summarize(sum(count))
Data5 <- setNames(Data5, c("diff_in_days", "Sum_logins_all"))
#summary(Everdeposit_No)
Everdeposit_No$diff_in_days<- difftime(Everdeposit_No$datelogin, Everdeposit_No$`Term Start`, units = c("days"))
Everdeposit_No$count <- 1
library(dplyr)
library(tidyr)
Everdeposit_No_2 <- Everdeposit_No %>% group_by(diff_in_days) %>% summarize(sum(count))
Everdeposit_No_2 <- setNames(Everdeposit_No_2, c("diff_in_days", "Sum_logins_not_deposit"))
#summary(Everdeposit_Yes)
Everdeposit_Yes$diff_in_days<- difftime(Everdeposit_Yes$datelogin, Everdeposit_Yes$`Term Start`, units = c("days"))
Everdeposit_Yes$count <- 1
library(dplyr)
library(tidyr)
Everdeposit_Yes_2 <- Everdeposit_Yes %>% group_by(diff_in_days) %>% summarize(sum(count))
Everdeposit_Yes_2 <- setNames(Everdeposit_Yes_2, c("diff_in_days", "Sum_logins_did_deposit"))
Data_login <- dplyr::full_join(Data5, Everdeposit_No_2, by="diff_in_days", all=TRUE)
Data_login <- dplyr::full_join(Data_login, Everdeposit_Yes_2, by="diff_in_days", all=TRUE)
```
#5
```{r}
ggplot(Data_login, aes(x = diff_in_days)) +
geom_line(aes(y = Sum_logins_all, colour = "All")) +
geom_line(aes(y = Sum_logins_not_deposit, colour = "Not Deposit"))+
geom_line(aes(y = Sum_logins_did_deposit, colour = "Did Deposit"))+
labs(x = 'Difference in days from term start date'
, y = 'Number of logins on that day'
, title = "Clustering Logins over Time") +
theme(plot.title = element_text(size = 20)
,axis.title = element_text(size = 15, color = '#555555')
,axis.title.y = element_text(size = 15, color = '#555555')
) + scale_color_manual(values=c("royalblue1", "mediumseagreen", "lightcoral"))
```
#6
```{r}
ggplot(Data_login, aes(x = diff_in_days)) +
geom_line(aes(y = Sum_logins_all, colour = "All")) +
geom_line(aes(y = Sum_logins_not_deposit, colour = "Not Deposit"))+
geom_line(aes(y = Sum_logins_did_deposit, colour = "Did Deposit"))+
labs(x = 'Difference in days from term start date'
, y = 'Number of logins on that day'
, title = "Clustering Logins over Time") +
theme(plot.title = element_text(size = 20)
,axis.title = element_text(size = 15, color = '#555555')
,axis.title.y = element_text(size = 15, color = '#555555')
) + coord_cartesian(ylim = c(-20,1500), xlim = c(-400, 10)) + scale_color_manual(values=c("royalblue1", "mediumseagreen", "lightcoral"))
```
Data Manipulation for further graphs
```{r}
Data_login$Sum_logins_not_deposit_1 <- scale(Data_login$Sum_logins_not_deposit)
Data_login$Sum_logins_all_1 <- scale(Data_login$Sum_logins_all)
Data_login$Sum_logins_did_deposit_1 <- scale(Data_login$Sum_logins_did_deposit)
```
#7
```{r}
ggplot(Data_login, aes(x = diff_in_days)) +
geom_line(aes(y = Sum_logins_all_1, colour = "All")) +
geom_line(aes(y = Sum_logins_not_deposit_1, colour = "Not Deposit"))+
geom_line(aes(y = Sum_logins_did_deposit_1, colour = "Did Deposit"))+
labs(x = 'Difference in days from term start date'
, y = 'Number of logins on that day'
, title = "Clustering Logins over Time") +
theme(plot.title = element_text(size = 20)
,axis.title = element_text(size = 15, color = '#555555')
,axis.title.y = element_text(size = 15, color = '#555555')
) + coord_cartesian(xlim = c(-400, 10)) + scale_color_manual(values=c("royalblue1", "mediumseagreen", "lightcoral"))
```
#8
```{r}
ggplot(Data_login, aes(x = diff_in_days)) +
geom_line(aes(y = Sum_logins_not_deposit_1, colour = "Not Deposit"))+
geom_line(aes(y = Sum_logins_did_deposit_1, colour = "Did Deposit"))+
labs(x = 'Difference in days from term start date'
, y = 'Number of logins on that day'
, title = "Clustering Logins over Time") +
theme(plot.title = element_text(size = 20)
,axis.title = element_text(size = 15, color = '#555555')
,axis.title.y = element_text(size = 15, color = '#555555')
) + coord_cartesian(xlim = c(-164, 10)) + scale_color_manual(values=c("mediumseagreen", "lightcoral")) + scale_x_continuous(breaks = scales::pretty_breaks(n = 10))
```
Data Manipulation
```{r}
message5 <- message
message5$delivered_date <- as.Date(message5$delivered)
data_message <- dplyr::full_join(message5, message2, by="identity", all=TRUE)
data_message <- dplyr::full_join(data_message, message3, by="identity", all=TRUE)
data6 <- dplyr::full_join(data_message, appbase, by="identity", all=TRUE)
data6$diff_in_days_msg <- difftime(data6$delivered_date, data6$`Term Start`, units = c("days"))
data6$count <- 1
library(dplyr)
library(tidyr)
data7 <- data6 %>% group_by(diff_in_days_msg) %>% summarize(sum(sum_num_of_message))
data7 <- setNames(data7, c("diff_in_days", "number_msg_sent"))
```
```{r}
data9 <- data8[c(1,2,6)]
data9$number_msg_sent <- scale(data9$number_msg_sent)
data10 <- data8[c(1,2,8)]
data10$number_msg_sent <- scale(data10$number_msg_sent)
```
9
```{r}
ggplot(data9, aes(x = diff_in_days)) +
geom_line(aes(y = number_msg_sent, colour = "msg_notdeposit")) +
geom_line(aes(y = Sum_logins_not_deposit_1, colour = "login_notdeposit"))+
labs(x = 'Difference in days from term start date'
, y = 'Number of logins on that day'
, title = "Messages delivered from term start") +
theme(plot.title = element_text(size = 20)
,axis.title = element_text(size = 15, color = '#555555')
,axis.title.y = element_text(size = 15, color = '#555555')
) + coord_cartesian(xlim = c(-200, 10))
```
10
```{r}
ggplot(data10, aes(x = diff_in_days)) +
geom_line(aes(y = number_msg_sent, colour = "msg_diddeposit")) +
geom_line(aes(y = Sum_logins_not_deposit_1, colour = "login_diddeposit"))+
labs(x = 'Difference in days from term start date'
, y = 'Number of logins on that day'
, title = "Messages delivered from term start") +
theme(plot.title = element_text(size = 20)
,axis.title = element_text(size = 15, color = '#555555')
,axis.title.y = element_text(size = 15, color = '#555555')
) + coord_cartesian(xlim = c(-200, 10))
```