-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.Rmd
1246 lines (1000 loc) · 45.3 KB
/
analysis.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Final Report"
author: "Howard Baek"
date: "8/10/2018"
output:
html_document:
toc: yes
toc_float: yes
code_folding: hide
theme: spacelab
highlight: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(cache=002,
cache.rebuild = F,
message = F, warning = F,
autodep=TRUE,
error = TRUE, comment = "")
```
# Introduction
MOOCs (Massive Open Online Courses) are educational resources that teach anyone with an internet connection a wide variety of topics, from Statistics to Modern Poetry. In 2013, the President’s Council of Advisors on Science and Technology stated that although “many questions and challenges remain” about MOOCs, this innovation has the potential to increase access to high-quality higher education at low cost. Evidently, MOOC is a transformative technology that allows better access to education than ever before.
However, a big problem in MOOC literature is the extremely high dropout rate from these courses. A widely known rate is 90% and this rate ranges from 65% to 97%. In this research project, I will be looking at factors leading to dropouts by using K-Means clustering to group the students into dropouts and non-dropouts.
```{r, warning=FALSE, message=FALSE}
library(tidyverse)
library(kableExtra)
library(plotly)
library(cluster)
library(magrittr)
library(viridis)
library(ggthemes)
```
```{r, cache=TRUE, cache.lazy = FALSE, warning = FALSE, message = FALSE}
source("starter_code_analysis.R")
```
# EDA (Exploratory Data Analysis)
### Glimpse of Datasets (Scroll Right to view Full Dataset)
```{r}
event_xtract %>%
head() %>%
kable() %>%
kable_styling()
```
```{r}
weekly_effort %>%
head() %>%
kable() %>%
kable_styling()
```
```{r}
activity_grade %>%
head() %>%
kable() %>%
kable_styling()
```
```{r}
video_int %>%
head() %>%
kable() %>%
kable_styling()
```
### How many unique students are there in the course?
```{r}
paste(length(reduce(list(event_xtract$anon_screen_name,
activity_grade$anon_screen_name,
weekly_effort$anon_screen_name), intersect)), "Students")
# Save students for later usage:
students <- reduce(list(event_xtract$anon_screen_name,
activity_grade$anon_screen_name,
weekly_effort$anon_screen_name), intersect)
```
* We found the number of students who are common in all four datasets.
* This ensures maximum number of features to use for k-means clustering.
### What are the total number of students who dropped the course?
##### Definition of dropout student
(Borrowed from Halawa et al "Dropout prediction in MOOCs using learner activity features")
* Absent from the course for a period exceeding more than one month
OR
* Viewed fewer than 50 percent of the videos in the course
In our dataset, no student was absent from the course for more than one month. Therefore, we only look at the second condition to find dropout students.
```{r}
# Filter out for Solutions and get number of unique video ids for each student
condition_2 <- video_int %>%
filter(!(str_detect(resource_display_name, "Solutions"))) %>%
group_by(anon_screen_name) %>%
summarise(num_videos = length(unique(video_id))) %>%
mutate(prop_videos = num_videos / max(num_videos)) %>%
filter(prop_videos < 0.5000)
total_dropout <- condition_2 %>%
select(anon_screen_name) %>%
pull()
paste(length(total_dropout), "Dropout Students")
```
* Dropout Rate: 65%
### At what number of days did the dropouts fall off the curve?
```{r}
# Clustering of fall_off_ts (dropout students)
fall_off_ts <- event_xtract %>%
filter(anon_screen_name %in% total_dropout) %>%
select(anon_screen_name) %>%
inner_join(weekly_effort, by = "anon_screen_name") %>%
select(anon_screen_name, week, effort_sec) %>%
dplyr::distinct()
fall_off_ts_clust <- fall_off_ts %>%
spread(key = week, value = effort_sec) %>%
replace_na(list("1" = 0,
"2" = 0,
"3" = 0,
"4" = 0,
"5" = 0,
"6" = 0,
"7" = 0,
"8" = 0,
"9" = 0,
"10" = 0)) %>% as.data.frame()
rownames(fall_off_ts_clust) <- fall_off_ts_clust$anon_screen_name
fall_off_ts_clust <- fall_off_ts_clust %>% select(-anon_screen_name, -"11")
fall_off_ts_tot_withinss <- map_dbl(1:10, function(k){
model <- kmeans(x = fall_off_ts_clust, centers = k)
model$tot.withinss
})
fall_off_ts_elbow_df <- data.frame(
k = 1:10 ,
tot_withinss = fall_off_ts_tot_withinss
)
# Plot of Elbow Method
ggplot(fall_off_ts_elbow_df, aes(x = k, y = tot_withinss)) +
geom_line() +
scale_x_continuous(breaks = 1:10) +
labs(x = "k",
y = "Total within-cluster sum of squares",
title = "Elbow Method of Clustering on Dropouts",
subtitle = "No clear Elbow. Try Silhouette Analysis")
```
```{r, eval = FALSE}
sil_width <- map_dbl(2:10, function(k){
model <- pam(x = fall_off_ts_clust, k = k)
model$silinfo$avg.width
})
sil_df <- data.frame(
k = 2:10,
sil_width = sil_width
)
ggplot(sil_df, aes(x = k, y = sil_width)) +
geom_line() +
scale_x_continuous(breaks = 2:10) +
labs(x = "K",
y = "Average Silhouette Widths") +
ggtitle("Average Silhouette Widths over K=2:10",
subtitle = "Average Silhoutte Width plot shows us that k = 2 is the best. Let's proceed to run k-means on k=2")
```
```{r}
set.seed(42)
fall_off_ts_clust_cluster <- kmeans(scale(fall_off_ts_clust), centers = 2)$cluster
fall_off_clust_final <- fall_off_ts_clust %>%
mutate(cluster = fall_off_ts_clust_cluster,
anon_screen_name = rownames(fall_off_ts_clust))
# Cluster1 of anon_screen_names of dropouts
anon_screen_name_cluster_1 <- fall_off_clust_final %>%
filter(cluster == 1) %>%
pull(anon_screen_name)
# Cluster2 of anon_screen_names of dropouts
anon_screen_name_cluster_2 <- fall_off_clust_final %>%
filter(cluster == 2) %>%
pull(anon_screen_name)
# Final Grades
final_grade <- read_csv("final_grades.csv")
final_grade_1 <- final_grade %>%
filter(anon_id %in% anon_screen_name_cluster_1) %>%
summarise(final_grade_avg = mean(final_grade),
final_grade_sd = sd(final_grade))
final_grade_2 <- final_grade %>%
filter(anon_id %in% anon_screen_name_cluster_2) %>%
summarise(final_grade_avg = mean(final_grade),
final_grade_sd = sd(final_grade))
effort_hrs_1 <- weekly_effort %>%
filter(anon_screen_name %in% anon_screen_name_cluster_1) %>%
summarise(effort_hrs_avg = mean(effort_sec) / 3600,
effort_hrs_sd = sd(effort_sec) / 3600)
effort_hrs_2 <- weekly_effort %>%
filter(anon_screen_name %in% anon_screen_name_cluster_2) %>%
summarise(effort_hrs_avg = mean(effort_sec) / 3600,
effort_hrs_sd = sd(effort_sec) / 3600)
final_grade <- final_grade_1 %>%
rbind(final_grade_2)
effort_hrs <- effort_hrs_1 %>%
rbind(effort_hrs_2)
table_total <- final_grade %>% cbind(effort_hrs)
rownames(table_total) <- c("Cluster1", "Cluster2")
table_total %>%
round(2) %>%
kableExtra::kable(col.names = c("Average of Final Grade",
"Standard Deviation of Final Grade",
"Average of Effort in Hours",
"Standard Deviation of Effort in Hours")) %>%
kable_styling(bootstrap_options = c("striped", "hover", "responsive"))
```
```{r}
fall_off_clust_final %>%
gather("1":"10", key = week, value = effort_level) %>%
mutate(effort_level = effort_level / 3600,
week = as.integer(week),
cluster = if_else(cluster == 1, "Cluster 1",
"Cluster 2")) %>%
ggplot(aes(x = week, y = effort_level)) +
geom_line(aes(group = anon_screen_name), alpha = 0.1) +
scale_x_continuous(breaks = 1:10) +
geom_smooth(color = "red") +
facet_wrap(~cluster) +
labs(x = "Weeks", y = "Effort Levels in Hours") +
theme_few()
```
* K-Means Clustering
* We set the optimal number of k=2 (supported by Silhouette Analysis)
* `r length(anon_screen_name_cluster_1)` students in Cluster 1
* `r length(anon_screen_name_cluster_2)` students in Cluster 2
* Effort level of Cluster 1 Students falls off after approximately 2 weeks.
* Effort level of Cluster 2 Students is consistently low.
* Table of Avg / Std of Final Grades and Effort show that more effort correlates with higher final grades.
### What are the types of modules that the users are interacting with?
```{r}
course <- activity_grade %>%
filter(str_detect(module_id, "course")) %>%
add_count(module_id) %>%
summarise(norm_module = (sum(unique(n)) / length(unique(module_id))) / (length(unique(anon_screen_name)))) %>% mutate(module_type = "course")
```
```{r}
seq <- activity_grade %>%
filter(str_detect(module_id, "sequential")) %>%
add_count(module_id) %>%
summarise(norm_module = (sum(unique(n)) / length(unique(module_id))) / (length(unique(anon_screen_name)))) %>% mutate(module_type = "sequential")
```
```{r}
prob <- activity_grade %>%
filter(str_detect(module_id, "problem")) %>%
add_count(module_id) %>%
summarise(norm_module = (sum(unique(n)) / length(unique(module_id))) / (length(unique(anon_screen_name)))) %>%
mutate(module_type = "problem")
vid <- activity_grade %>%
filter(str_detect(module_id, "video")) %>%
add_count(module_id) %>%
summarise(norm_module = (sum(unique(n)) / length(unique(module_id))) / (length(unique(anon_screen_name)))) %>%
mutate(module_type = "video")
chapter <- activity_grade %>%
filter(str_detect(module_id, "chapter")) %>%
add_count(module_id) %>%
summarise(norm_module = (sum(unique(n)) / length(unique(module_id))) / (length(unique(anon_screen_name)))) %>%
mutate(module_type = "chapter")
final_module <- rbind(course, seq, prob, vid, chapter)
final_module %>%
# Removed course module since its count is 1.0
# Meaning: every other module falls under the umbrella of a course module.
filter(module_type != "course") %>%
ggplot(aes(x = module_type, y = norm_module, fill = module_type)) +
geom_col() +
theme_bw() +
labs(x = "Module Types",
y = "Normalized Counts of Modules per Student"
) +
guides(fill = FALSE) +
scale_x_discrete(limits = c("problem", "chapter", "sequential", "video"),
labels = c("Problem", "Chapter", "Sequential", "Video")) +
theme_hc()
```
* We removed course module since its count is 1.0, meaning that every other module falls under the umbrella of a course module.
* Problem module is the most popular- many students focused on solving questions.
* Video was the least popular- Perhaps, this means students already knew material and didn't need to learn from video lecture.
### Is there a discrepancy in Mean effort per Week for Dropouts vs Non-Dropouts?
```{r}
event_xtract %>%
mutate(course_complete = ifelse(anon_screen_name %in% total_dropout, "no",
"yes")) %>%
select(anon_screen_name, course_complete) %>%
inner_join(weekly_effort, by = "anon_screen_name") %>%
group_by(week, course_complete, anon_screen_name) %>%
summarise(mean_effort_hrs = mean(effort_sec) / 3600) %>%
filter(week < 10) %>%
ggplot(aes(x = as.factor(week), y = mean_effort_hrs, fill = course_complete)) +
geom_boxplot(outlier.alpha = 0.35) +
scale_fill_hue(labels = c("Not Completed", "Completed")) +
labs(x = "Week",
y = "Average Effort in Hours",
fill = "Completion of Course"
) +
ggtitle("Distribution of Average Effort in Hours Per Week\nBy Completion of Course") +
theme_hc()
```
* As expected, non-dropout students put in significantly higher average effort than dropout students.
* There are many outliers in this dataset, suggesting high variability in weekly effort level.
### Proporational Barchart of different effort levels per week
```{r}
# Get min, q1, median, q3, and maximum value for effort_level
summary_effort <- summary(weekly_effort$effort_sec)
# Plot
weekly_effort %>%
mutate(effort_level = ifelse(effort_sec < summary_effort[2] & effort_sec >= summary_effort[1], "low", ifelse(effort_sec <= summary_effort[5] &
effort_sec >= summary_effort[2], "med",
ifelse(effort_sec > summary_effort[5] & effort_sec <= summary_effort[6], "high", "na")))) %>%
mutate(course_complete = ifelse(anon_screen_name %in% total_dropout, "Dropouts",
"Non-Dropouts")) %>%
filter(week != 11) %>%
count(week, course_complete, effort_level) %>%
group_by(week, course_complete) %>%
mutate(n_percentage = round(n / sum(n), 2)) %>%
ggplot(aes(x = as.factor(week), y = n_percentage, fill = factor(effort_level, levels = c("high", "med", "low")))) +
geom_col(position = "fill") +
facet_wrap(~course_complete) +
scale_y_continuous(labels = scales::percent_format()) +
scale_fill_hue(labels = c("High", "Medium", "Low")) +
labs(x = "Week",
y = "Percentage",
fill = "Effort Level") +
theme_few()
```
Categorized effort level into High, Medium, and Low Effort using Five-Number Summary:
* 3rd Quartile < High Effort <= Maximum
* 1st Quartile <= Medium Effort <= 3rd Quartile
* Minimum <= Low Effort < 1st Quartile
* Plot aligns with our expectations: dropout students put in less "High Effort" and more "Low Efort" levels.
# Clustering in Stages
```{r}
# Make new dataframe for clustering
# Decided to just go with students in weekly effort dataset
# Instead of the object students I saved in the beginning.
# Similiar number of students (7659 vs 7664)
weekly_effort_new <- event_xtract %>%
select(anon_screen_name) %>%
inner_join(weekly_effort, by = "anon_screen_name") %>%
select(anon_screen_name, week, effort_sec) %>%
dplyr::distinct() %>%
filter(week != 11) %>% # Only look at 10 weeks-filter out week11 because only 14 students have data for week11
as.data.frame()
week_seq <- seq(from = as.Date("2014/06/24"), to = as.Date("2014/09/08"), by = "week")
# Make new dataframe out of video dataset (Number of times students pressed "Play")
video_int_clus <- video_int %>%
mutate(time = as.Date(time),
video_week = case_when(
time <= week_seq[2] & time >= week_seq[1] ~ 1,
time <= week_seq[3] & time >= as.Date("2014-07-02") ~ 2,
time <= week_seq[4] & time >= as.Date("2014-07-09") ~ 3,
time <= week_seq[5] & time >= as.Date("2014-07-16") ~ 4,
time <= week_seq[6] & time >= as.Date("2014-07-23") ~ 5,
time <= week_seq[7] & time >= as.Date("2014-07-30") ~ 6,
time <= week_seq[8] & time >= as.Date("2014-08-06") ~ 7,
time <= week_seq[9] & time >= as.Date("2014-08-13") ~ 8,
time <= week_seq[10] & time >= as.Date("2014-08-20") ~ 9,
time <= week_seq[11] & time >= as.Date("2014-08-27") ~ 10,
TRUE ~ 0)) %>%
filter(video_week != 0) %>%
select(anon_screen_name, video_week, event_type) %>%
filter(event_type == "play_video") %>%
group_by(anon_screen_name, video_week) %>%
summarise(play_video_num = n()) %>%
spread(key = video_week, value = play_video_num) %>%
replace_na(list("1" = 0,
"2" = 0,
"3" = 0,
"4" = 0,
"5" = 0,
"6" = 0,
"7" = 0,
"8" = 0,
"9" = 0,
"10" = 0)) %>%
as.data.frame()
# Manipulate weekly_effort
weekly_effort_new %<>%
spread(key = week, value = effort_sec) %>%
replace_na(list("1" = 0,
"2" = 0,
"3" = 0,
"4" = 0,
"5" = 0,
"6" = 0,
"7" = 0,
"8" = 0,
"9" = 0,
"10" = 0))
video_int_clus %<>% column_to_rownames(var = "anon_screen_name")
weekly_effort_new %<>% column_to_rownames(var = "anon_screen_name")
names(video_int_clus) <- paste("week", names(video_int_clus), "video", sep = "_")
names(weekly_effort_new) <- paste("week", names(weekly_effort_new), "effort", sep = "_")
video_int_clus %<>% rownames_to_column(var = "anon_screen_name")
weekly_effort_new %<>% rownames_to_column(var = "anon_screen_name")
new_clus <- weekly_effort_new %>%
inner_join(video_int_clus, by = "anon_screen_name")
# First Stage
new_clust_first <- new_clus %>%
select(anon_screen_name, week_1_effort, week_1_video,
week_2_effort, week_2_video,
week_3_effort, week_3_video) %>%
column_to_rownames(var = "anon_screen_name")
# Second Stage
new_clust_second <- new_clus %>%
select(anon_screen_name, week_1_effort, week_1_video,
week_2_effort, week_2_video,
week_3_effort, week_3_video, week_4_effort, week_4_video,
week_5_effort, week_5_video,
week_6_effort, week_6_video) %>%
column_to_rownames(var = "anon_screen_name")
# Third Stage
new_clust_third <- new_clus %>%
select(anon_screen_name, week_1_effort, week_1_video,
week_2_effort, week_2_video,
week_3_effort, week_3_video, week_4_effort, week_4_video,
week_5_effort, week_5_video,
week_6_effort, week_6_video, week_7_effort, week_7_video,
week_8_effort, week_8_video,
week_9_effort, week_9_video,
week_10_effort, week_10_video) %>%
column_to_rownames(var = "anon_screen_name")
```
* Looked at two weekly features, effort level and number of times student played video.
* These features are allocated in three different "stages"
1. First stage: Two features measured during Weeks 1~3
2. Second Stage: Two features measured during Weeks 1~6
3. Third Stage: Two features measured during Weeks 1~10 (all of available data)
```{r}
# Set seed
set.seed(42)
# K-Means on First Stage
new_clust_first_kmeans <- kmeans(scale(new_clust_first), centers = 6)
# K-Means on Second Stage
new_clust_second_kmeans <- kmeans(scale(new_clust_second), centers = 6)
# K-Means on Third Stage
new_clust_third_kmeans <- kmeans(scale(new_clust_third), centers = 6)
# Clean Up First Stage using broom::augment
new_clust_first_kmeans <- broom::augment(new_clust_first_kmeans, new_clust_first)
new_clust_first_kmeans <- new_clust_first_kmeans %>%
rename(anon_screen_name = .rownames,
cluster = .cluster)
new_clust_first_kmeans %<>% remove_rownames()
# Clean Up Second Stage using broom::augment
new_clust_second_kmeans <- broom::augment(new_clust_second_kmeans, new_clust_second)
new_clust_second_kmeans <- new_clust_second_kmeans %>%
rename(anon_screen_name = .rownames,
cluster = .cluster)
new_clust_second_kmeans %<>% remove_rownames()
# Clean Up Third Stage using broom::augment
new_clust_third_kmeans <- broom::augment(new_clust_third_kmeans, new_clust_third)
new_clust_third_kmeans <- new_clust_third_kmeans %>%
rename(anon_screen_name = .rownames,
cluster = .cluster)
new_clust_third_kmeans %<>% remove_rownames()
```
```{r}
# Function to create "similarity matrix"
student_id_match <- function(x, y) {
# Define similarity matrix
similarity_mat <- matrix(nrow = 6, ncol = 6)
# For loops...sorry!
# ii = rows and jj = columns
for (ii in c(1:6)) {
for (jj in c(1:6)) {
first_group <- x %>%
filter(cluster == ii) %>%
select(anon_screen_name) %>%
pull()
second_group <- y %>%
filter(cluster == jj) %>%
select(anon_screen_name) %>%
pull()
similarity_mat[ii, jj] <- mean(first_group %in% second_group)
}
}
return(similarity_mat)
}
# Create similarity matrix
first_second_sim <- student_id_match(x = new_clust_first_kmeans,
y = new_clust_second_kmeans)
first_third_sim <- student_id_match(x = new_clust_first_kmeans,
y = new_clust_third_kmeans)
second_third_sim <- student_id_match(x = new_clust_second_kmeans,
y = new_clust_third_kmeans)
# First, set the dimension names first
dimnames(first_second_sim) <- list(paste("first_group", "_cluster_", 1:6, sep =""),
paste("second_group", "_cluster_", 1:6, sep = ""))
dimnames(first_third_sim) <- list(paste("first_group", "_cluster_", 1:6, sep =""),
paste("third_group", "_cluster_", 1:6, sep = ""))
dimnames(second_third_sim) <- list(paste("second_group", "_cluster_", 1:6, sep =""),
paste("third_group", "_cluster_", 1:6, sep = ""))
# Reshape melt
first_second_sim <- reshape2::melt(first_second_sim)
first_third_sim <- reshape2::melt(first_third_sim)
second_third_sim <- reshape2::melt(second_third_sim)
# Round values to two decimal places
first_second_sim <- first_second_sim %>%
mutate(value = round(value, digits = 2))
first_third_sim <- first_third_sim %>%
mutate(value = round(value, digits = 2))
second_third_sim <- second_third_sim %>%
mutate(value = round(value, digits = 2))
# Plot Heatmaps
ggplot(data = first_second_sim, aes(x=Var1, y=Var2, fill=value)) +
geom_tile() +
geom_text(aes(Var1, Var2, label = value), color = "black", size = 2.75) +
scale_fill_viridis(name = "Value") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x = NULL,
y = NULL) +
scale_y_discrete(labels= c("Second Group (Cluster1)",
"Second Group (Cluster2)",
"Second Group (Cluster3)",
"Second Group (Cluster4)",
"Second Group (Cluster5)",
"Second Group (Cluster6)")) +
scale_x_discrete(labels = c("First Group (Cluster1)",
"First Group (Cluster2)",
"First Group (Cluster3)",
"First Group (Cluster4)",
"First Group (Cluster5)",
"First Group (Cluster6)"))
```
```{r}
ggplot(data = first_third_sim, aes(x=Var1, y=Var2, fill=value)) +
geom_tile() +
geom_text(aes(Var1, Var2, label = value), color = "black", size = 2.75) +
scale_fill_viridis(name = "Value") +
scale_x_discrete(labels= c("First Group (Cluster1)",
"First Group (Cluster2)",
"First Group (Cluster3)",
"First Group (Cluster4)",
"First Group (Cluster5)",
"First Group (Cluster6)")) +
scale_y_discrete(labels = c("Third Group (Cluster1)",
"Third Group (Cluster2)",
"Third Group (Cluster3)",
"Third Group (Cluster4)",
"Third Group (Cluster5)",
"Third Group (Cluster6)")) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x = NULL, y = NULL)
```
```{r}
ggplot(data = second_third_sim, aes(x=Var1, y=Var2, fill=value)) +
geom_tile() +
geom_text(aes(Var1, Var2, label = value), color = "black", size = 2.75) +
scale_fill_viridis(name = "Value") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(x = NULL,
y = NULL) +
scale_x_discrete(labels= c("Second Group (Cluster1)",
"Second Group (Cluster2)",
"Second Group (Cluster3)",
"Second Group (Cluster4)",
"Second Group (Cluster5)",
"Second Group (Cluster6)")) +
scale_y_discrete(labels = c("Third Group (Cluster1)",
"Third Group (Cluster2)",
"Third Group (Cluster3)",
"Third Group (Cluster4)",
"Third Group (Cluster5)",
"Third Group (Cluster6)"))
```
* Function of Similarity matrix: Find proportion of overlapping students among three different groups, or stages (each group has six clusters of students)
* Results: We found very high overlap between First Group (Cluster 6), Second Group (Cluster 3), and Third Group (Cluster 4)
* Now we move on to comparing the students in these 18 clusters to our ground truth of dropout and non-dropout students.
### Barchart of Dropouts among different clusters
```{r}
# First Group
bar_first <- new_clust_first_kmeans %>%
mutate(cluster = as.character(cluster)) %>%
group_by(cluster) %>%
summarise(dropout_prop = mean(anon_screen_name %in% total_dropout)) %>%
mutate(group = "first")
# Second Group
bar_second <- new_clust_second_kmeans %>%
mutate(cluster = as.character(cluster)) %>%
group_by(cluster) %>%
summarise(dropout_prop = mean(anon_screen_name %in% total_dropout)) %>%
mutate(group = "second")
# Third Group
bar_third <- new_clust_third_kmeans %>%
mutate(cluster = as.character(cluster)) %>%
group_by(cluster) %>%
summarise(dropout_prop = mean(anon_screen_name %in% total_dropout)) %>%
mutate(group = "third")
# Combine three groups
bar_total <- rbind(bar_first, bar_second, bar_third)
# Facet labeller
three_group <- list(
"first" = "Weeks 1~3",
"second" = "Weeks 1~6",
"third" = "Weeks 1~10"
)
three_group_labeller <- function(variable,value){
return(three_group[value])
}
# Draw Barchart
bar_total %>%
mutate(dropout_prop = round(dropout_prop, 2)) %>%
ggplot(aes(x = cluster, y = dropout_prop)) +
geom_col(aes(fill = cluster)) +
geom_text(aes(label = dropout_prop), position = position_stack(vjust = 0.5),
color = "black", size = 2.4) +
facet_wrap(~group, labeller = three_group_labeller) +
#scale_fill_viridis(name = "Value") +
labs(x = "Clusters",
y = "Dropout Proportions",
fill = "Clusters") +
theme_tufte()
```
* Overall outcome of Clustering in Stages: The K-Means clustering algorithm found the one cluster in each group that had an extremely high proportion of dropout students.
* We assume that the algorithm clustered the dropout students into one cluster (Cluster 6 in Group 1, Cluster 3 in Group 2, and Cluster 4 in Group 3).
# Discover characteristics of dropouts
```{r}
# Dropout clusters:
# Group1: Cluster 6
# Group2: Cluster 3
# Group3: Cluster 4
dropout_group1 <- new_clust_first_kmeans %>%
mutate(cluster = as.character(cluster)) %>%
filter(cluster == "6")
dropout_group2 <- new_clust_second_kmeans %>%
mutate(cluster = as.character(cluster)) %>%
filter(cluster == "3")
dropout_group3 <- new_clust_third_kmeans %>%
mutate(cluster = as.character(cluster)) %>%
filter(cluster == "4")
# Effort / Video number of seconds for Dropouts vs Non-Dropouts (Group 1)
new_clust_first_kmeans %>%
mutate(cluster = as.character(cluster)) %>%
mutate(is_dropout = if_else(cluster == "6", "Dropout", "Non-Dropout"),
is_dropout = factor(is_dropout, levels = c("Non-Dropout", "Dropout"))) %>%
select(-anon_screen_name, -cluster) %>%
group_by(is_dropout) %>%
summarise_all(mean, na.rm = TRUE) %>%
gather(week_1_effort:week_3_video, key = "metric", value = avg_sec) %>%
mutate(week = str_extract(metric, "\\-*\\d+\\.*\\d*"),
video_effort = str_sub(metric, start = 8),
video_effort = factor(video_effort, labels = c("Effort Level",
"Video Level"))) %>%
mutate(week = as.integer(week)) %>%
mutate(avg_hr = avg_sec / 3600) %>%
mutate(week = as.character(week),
week = factor(paste("Week", week, sep = " "),
levels = c("Week 1",
"Week 2",
"Week 3"
))) %>%
ggplot(aes(x = week, y = avg_hr)) +
geom_line(aes(group = is_dropout, col = is_dropout)) +
facet_wrap(~video_effort, scales = "free_y") +
labs(x = NULL, y = "Average Hours",
col = "") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Effort / Video number of seconds for Dropouts vs Non-Dropouts (Group 2)
new_clust_second_kmeans %>%
mutate(cluster = as.character(cluster)) %>%
mutate(is_dropout = if_else(cluster == "3", "Dropout", "Non-Dropout"),
is_dropout = factor(is_dropout, levels = c("Non-Dropout", "Dropout"))) %>%
select(-anon_screen_name, -cluster) %>%
group_by(is_dropout) %>%
summarise_all(mean, na.rm = TRUE) %>%
gather(week_1_effort:week_6_video, key = "metric", value = avg_sec) %>%
mutate(week = str_extract(metric, "\\-*\\d+\\.*\\d*"),
video_effort = str_sub(metric, start = 8),
video_effort = factor(video_effort, labels = c("Effort Level",
"Video Level"))) %>%
mutate(week = as.integer(week)) %>%
mutate(avg_hr = avg_sec / 3600) %>%
mutate(week = as.character(week),
week = factor(paste("Week", week, sep = " "),
levels = c("Week 1",
"Week 2",
"Week 3",
"Week 4",
"Week 5",
"Week 6"))) %>%
ggplot(aes(x = week, y = avg_hr)) +
geom_line(aes(group = is_dropout, col = is_dropout)) +
facet_wrap(~video_effort, scales = "free_y") +
labs(x = NULL,
y = "Average Hours",
col = "") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Effort / Video number of seconds for Dropouts vs Non-Dropouts (Group 3)
new_clust_third_kmeans %>%
mutate(cluster = as.character(cluster)) %>%
mutate(is_dropout = if_else(cluster == "4", "Dropout", "Non-Dropout"),
is_dropout = factor(is_dropout, levels = c("Non-Dropout", "Dropout"))) %>%
select(-anon_screen_name, -cluster) %>%
group_by(is_dropout) %>%
summarise_all(mean, na.rm = TRUE) %>%
gather(week_1_effort:week_10_video, key = "metric", value = avg_sec) %>%
mutate(week = str_extract(metric, "\\-*\\d+\\.*\\d*"),
video_effort = str_sub(metric, start = 8)) %>%
mutate(week = as.integer(week)) %>%
mutate(avg_hr = avg_sec / 3600) %>%
mutate(video_effort = str_replace(video_effort, "_", ""),
video_effort = factor(video_effort, labels = c("Effort Level",
"Video Level"))) %>%
mutate(week = as.character(week),
week = factor(paste("Week", week, sep = " "),
levels = c("Week 1",
"Week 2",
"Week 3",
"Week 4",
"Week 5",
"Week 6",
"Week 7",
"Week 8",
"Week 9",
"Week 10"))) %>%
ggplot(aes(x = week, y = avg_hr)) +
geom_line(aes(group = is_dropout, col = is_dropout)) +
facet_wrap(~video_effort, scales = "free_y") +
labs(x = NULL,
y = "Average Hours",
col = "") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
```
# Clustering of Different Averages
```{r}
# Manipulate video interaction data
video_int_clus_avg <- video_int_clus %>%
gather(-anon_screen_name, key = "video_week", value = "number_play") %>%
mutate(week = as.integer(substr(video_week, 6, 6))) %>%
select(anon_screen_name, week, number_play)
```
```{r}
# Make dataframe of weekly efforts + number of times video played
weekly_effort_clustering <- event_xtract %>%
select(anon_screen_name) %>%
filter(anon_screen_name %in% students) %>%
inner_join(weekly_effort, by = "anon_screen_name") %>%
inner_join(video_int_clus_avg, by = c("anon_screen_name", "week")) %>%
group_by(anon_screen_name) %>%
summarise(effort_sec_avg = mean(effort_sec, na.rm = TRUE),
number_play_avg = mean(number_play, na.rm = TRUE)) %>%
as.data.frame()
```
```{r}
# Wrangle data in form for K-Means clustering
rownames(weekly_effort_clustering) <- weekly_effort_clustering$anon_screen_name
weekly_effort_clustering <- weekly_effort_clustering %>% select(-anon_screen_name)
# Run KMeans Algorithm with 2 centers
weekly_effort_clustering_kmeans <- kmeans(scale(weekly_effort_clustering), centers = 2)
# Assign clusters (1 or 2) to each student
weekly_effort_clustering_kmeans_clust <- broom::augment(weekly_effort_clustering_kmeans, weekly_effort_clustering)
```
```{r}
# Rename columns
weekly_effort_clustering_kmeans_clust <- weekly_effort_clustering_kmeans_clust %>%
rename(anon_screen_name = .rownames,
cluster = .cluster)
# Wrangle data
# Use magrittr's compound assignment pipe-operator
weekly_effort_clustering_kmeans_clust %<>% remove_rownames
# Pull out students in cluster 1
weekly_effort_clustering_kmeans_clust1 <- weekly_effort_clustering_kmeans_clust %>%
filter(cluster == 1) %>%
select(anon_screen_name) %>%
pull()
# Pull out students in cluster 2
weekly_effort_clustering_kmeans_clust2 <- weekly_effort_clustering_kmeans_clust %>%
filter(cluster == 2) %>%
select(anon_screen_name) %>%
pull()
```
```{r}
# Cluster for first half (Earlier than Week 5 or Week 5)
weekly_effort_clustering_first_half <- event_xtract %>%
select(anon_screen_name) %>%
# The object students: Unique students in all four datasets
filter(anon_screen_name %in% students) %>%
inner_join(weekly_effort, by = "anon_screen_name") %>%
inner_join(video_int_clus_avg, by = c("anon_screen_name", "week")) %>%
dplyr::distinct() %>%
filter(week <= 5) %>%
group_by(anon_screen_name) %>%
summarise(effort_sec_avg_first_half = mean(effort_sec, na.rm = TRUE),
number_play_avg_first_half = mean(number_play, na.rm = TRUE)) %>%
as.data.frame()
weekly_effort_clustering_second_half <- event_xtract %>%
select(anon_screen_name) %>%
filter(anon_screen_name %in% students) %>%
inner_join(weekly_effort, by = "anon_screen_name") %>%
inner_join(video_int_clus_avg, by = c("anon_screen_name", "week")) %>%
dplyr::distinct() %>%
filter(week > 5) %>%
group_by(anon_screen_name) %>%
summarise(effort_sec_avg_second_half = mean(effort_sec, na.rm = TRUE),
number_play_avg_second_half = mean(number_play, na.rm = TRUE)) %>%
as.data.frame()
weekly_effort_clustering_half <- weekly_effort_clustering_first_half %>%
left_join(weekly_effort_clustering_second_half, by = "anon_screen_name") %>%
replace_na(list(effort_sec_avg_second_half = 0, number_play_avg_second_half = 0))
weekly_effort_clustering_half %<>% column_to_rownames(var = "anon_screen_name")
# K-Means clustering
weekly_effort_clustering_half_kmeans <- kmeans(scale(weekly_effort_clustering_half), centers = 2)
weekly_effort_clustering_half_kmeans_final <- broom::augment(weekly_effort_clustering_half_kmeans, weekly_effort_clustering_half)
weekly_effort_clustering_half_kmeans_final <- weekly_effort_clustering_half_kmeans_final %>%
rename(anon_screen_name = .rownames,
cluster = .cluster)
weekly_effort_clustering_half_kmeans_final %<>% remove_rownames()
weekly_effort_clustering_half_kmeans_final_cluster1 <- weekly_effort_clustering_half_kmeans_final %>%
filter(cluster == 1) %>%
select(anon_screen_name) %>%
pull()
weekly_effort_clustering_half_kmeans_final_cluster2 <-
weekly_effort_clustering_half_kmeans_final %>%
filter(cluster == 2) %>%
select(anon_screen_name) %>%
pull()
# Clustering by Quarters
weekly_effort_clustering_first_quarter <- event_xtract %>%
select(anon_screen_name) %>%
filter(anon_screen_name %in% students) %>%
inner_join(weekly_effort, by = "anon_screen_name") %>%
inner_join(video_int_clus_avg, by = c("anon_screen_name", "week")) %>%
dplyr::distinct() %>%
filter(week <= 3) %>%
mutate(effort_sec_first_quarter =
ifelse(week == 3, effort_sec / 2, effort_sec),
number_play_first_quarter =
ifelse(week == 3, number_play / 2, number_play)) %>%
group_by(anon_screen_name) %>%
summarise(effort_sec_avg_first_quarter = mean(effort_sec_first_quarter, na.rm = TRUE),
number_play_avg_first_quarter = mean(number_play_first_quarter, na.rm = TRUE)) %>%
as.data.frame()
weekly_effort_clustering_second_quarter <- event_xtract %>%
select(anon_screen_name) %>%