Data

Search for Best Configuration

M1 <- 1
M2 <- 1000000
Xsum <- numeric(0)
Xsum <- sapply(M1:M2, red_and_black)
# Values_mat <- numeric(0)
# for(k in M1:M2){
#   set.seed(k)
#   N <- nrow(class_roll) 
#   class_roll$group <- 
#     sample(1:N) %%
#     2 %>%
#     factor(levels = c(0, 1), labels = c("Red", "Black"))
#   Xsum <- c(Xsum, red_and_black(class_roll)$Xsum)
#   Values_mat <- rbind(Values_mat, red_and_black(class_roll)$Values)
# }
# colnames(Values_mat) <- paste0("X", 1:6)
# Values_mat
# pairs(Values_mat)
# cor(Values_mat) %>%
#   round(4)
names(Xsum) <- M1:M2
Xsum %>%
  summary %>%
  round(2) 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    2.82   16.35   20.35   21.03   24.98   68.58
Xsum %>%
  sd %>%
  round(2)
## [1] 6.51
Xsum %>%
  `<=`(5) %>%
  which %>%
  `[`(Xsum, .) %>%
  round(2)
##    876   1042   4262   6394   7007  17465  23390  24361  29595  78694  79397 
##   4.76   4.59   4.78   4.68   4.50   4.78   4.45   4.83   4.85   4.71   4.84 
##  80385  87992  90382  96472  99387 106431 119091 130377 140413 141038 149661 
##   4.80   3.71   4.55   4.95   4.46   4.93   4.23   4.30   4.93   4.92   4.25 
## 197184 208694 210634 215432 224017 224154 234248 234976 247898 252672 256214 
##   4.17   4.12   4.89   3.93   4.17   3.95   4.07   4.63   4.77   3.74   4.80 
## 278486 285414 285729 301184 327089 327576 328700 335410 342744 354340 368268 
##   4.86   4.72   3.78   4.45   3.77   4.51   4.85   4.52   4.90   4.80   4.90 
## 372901 386000 398499 399252 405767 408041 432867 435431 443070 445113 452208 
##   4.82   4.38   4.96   4.29   4.51   4.65   4.76   4.76   4.83   4.77   4.68 
## 455811 458836 459122 459938 470486 479848 482507 489040 489474 493815 499885 
##   4.87   4.04   3.47   4.89   4.29   4.98   4.74   3.90   4.44   4.68   4.98 
## 502397 508531 512025 516110 520909 527420 544063 560565 565855 568623 578441 
##   4.68   4.99   3.90   4.78   4.40   4.82   4.09   4.88   4.40   4.02   2.82 
## 582364 594312 627811 643913 651395 654763 665606 668010 669067 670602 693752 
##   4.96   4.95   4.84   4.88   4.73   4.52   3.83   3.90   4.32   4.37   4.01 
## 705393 711252 718817 721392 732439 735093 739249 740953 762677 788726 790252 
##   4.70   4.18   4.09   4.94   4.69   4.06   4.27   4.65   4.87   4.47   4.54 
## 791808 792848 813430 814132 823296 834840 847008 850047 850236 853639 861257 
##   4.98   4.66   4.97   4.88   4.32   4.63   4.36   4.71   4.46   4.45   4.27 
## 861363 872185 872743 890577 890687 895385 896017 898292 904480 920273 950305 
##   4.87   4.84   3.85   4.85   4.98   4.14   3.66   4.85   4.94   3.42   4.81 
## 955386 959415 959739 967728 969954 972048 978560 982710 995106 995648 997073 
##   4.70   3.97   4.51   4.47   4.69   4.00   4.90   4.85   4.09   4.99   4.91
Xmin <- names(Xsum[which(Xsum == min(Xsum))])
Xmin
## [1] "578441"

Plot

hist(Xsum, prob = TRUE, nclass = 30, xlim = c(0, 50), ylim = c(0, 0.065))
x <- seq(0, 50, by = 0.1)
lines(x, dchisq(x, df = 21), col = "red")
legend("topright", inset = 0.05, legend = c("Xsum", "Chi-square(21)"), col = c("black", "red"), lty = 1)

plot(density(Xsum), xlim = c(0, 50), main = "Density Estimation of Xsum")
lines(x, dchisq(x, df = 21), col = "red")
legend("topright", inset = 0.05, legend = c("Xsum", "Chi-square(21)"), col = c("black", "red"), lty = 1)

Randomization

set.seed(Xmin)
N <- nrow(class_roll) 
class_roll$group <- 
  sample(1:N) %%
  2 %>%
  factor(levels = c(0, 1), labels = c("Red", "Black"))
red_and_black(Xmin)
## [1] 2.824094

학번

class_roll$id_2 <-
  class_roll$id %>%
  ifelse(. <= 2016, "2016", .)
tbl1 <- class_roll %$%
  table(.$group, .$id_2 %>% substr(1, 4)) %>%
  `colnames<-`(c("2016 이전", 2017:2022)) 
tbl1 %>%
  pander
  2016 이전 2017 2018 2019 2020 2021 2022
Red 23 35 60 78 48 135 24
Black 18 36 61 76 53 134 26
X1min <- tbl1 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X1min
## X-squared 
## 0.9880837

학번 홀짝

tbl2 <- class_roll$id %>%
  as.numeric %>%
  `%%`(2) %>%
  factor(levels = c(1, 0), labels = c("홀", "짝")) %>%
  table(class_roll$group, .) 
tbl2 %>%
  pander
 
Red 205 198
Black 215 189
X2min <- tbl2 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X2min
## X-squared 
## 0.4461591

학적 상태

tbl3 <- class_roll$status %>%
  table(class_roll$group, .) 
tbl3 %>%
  pander
  학생 휴학
Red 331 72
Black 328 76
X3min <- tbl3 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X3min
## X-squared 
## 0.1205262

e-mail 서비스업체

tbl4 <- class_roll$email %>%
  strsplit("@", fixed = TRUE) %>%
  sapply("[", 2) %>%
  `==`("naver.com") %>%
  ifelse("네이버", "기타서비스") %>%
  factor(levels = c("네이버", "기타서비스")) %>%
  table(class_roll$group, .) 
tbl4 %>%
  pander
  네이버 기타서비스
Red 332 71
Black 332 72
X4min <- tbl4 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X4min
##   X-squared 
## 0.005753858

전화번호의 분포

cut_label <- paste(paste0(0:9, "000"), paste0(0:9, "999"), 
                   sep = "~")
tbl5 <- class_roll$cell_no %>%
  substr(start = 8, stop = 11) %>%
  sapply(as.numeric) %>%
  cut(labels = cut_label, 
      breaks = seq(0, 10000, by = 1000)) %>%
  table(class_roll$group, .) 
tbl5 %>%
  pander
  0000~0999 1000~1999 2000~2999 3000~3999 4000~4999 5000~5999 6000~6999 7000~7999 8000~8999 9000~9999
Red 38 32 33 44 32 49 45 37 41 52
Black 43 33 35 41 32 49 39 40 41 50
X5min <- tbl5 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X5min
## X-squared 
##  1.073403

성씨 분포

f_name <- class_roll$name %>%
  substring(first = 1, last = 1) 
tbl6 <- f_name %>%
  `%in%`(c("김", "이", "박")) %>%
  ifelse(f_name, "기타") %>%
  factor(levels = c("김", "이", "박", "기타")) %>%
  table(class_roll$group, .) 
tbl6 %>%
  pander
  기타
Red 72 60 31 240
Black 76 61 29 238
X6min <- tbl6 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X6min
## X-squared 
## 0.1901686

Sum of Chi_Squares

Xsum_min <- X1min + X2min + X3min + X4min + X5min + X6min
Xsum_min
## X-squared 
##  2.824094