Data

Search for Best Configuration

M1 <- 1
M2 <- 1000000
Xsum <- sapply(M1:M2, red_and_black)
names(Xsum) <- M1:M2
Xsum %>%
  summary %>%
  round(2) 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    2.98   16.35   20.34   21.01   24.96   69.56
Xsum %>%
  sd %>%
  round(2)
## [1] 6.49
Xsum %>%
  `<=`(5) %>%
  which %>%
  `[`(Xsum, .) %>%
  round(2)
##  10416  12781  19444  23956  25830  26457  33610  38777  41592  43746  44506 
##   4.86   4.82   4.71   3.99   4.96   4.88   4.97   4.78   3.68   4.91   3.78 
##  46006  51385  53516  65560  66869  72377  79442  82333  89243  90999 106229 
##   4.74   4.92   4.96   3.25   4.65   4.56   4.05   4.24   4.56   4.12   4.70 
## 121540 133690 135284 135564 141590 181736 185079 201144 220535 224397 225279 
##   4.70   4.81   4.54   4.91   4.79   4.63   3.49   4.70   4.99   3.46   4.95 
## 225867 229815 232181 260386 267025 272411 289550 296160 324502 332978 338188 
##   4.64   4.59   4.81   4.47   4.38   4.88   4.53   4.52   4.48   5.00   4.98 
## 351081 352731 370131 382344 402852 412377 413962 415170 417522 421164 430841 
##   4.61   4.19   4.83   5.00   4.22   4.94   4.08   3.93   4.16   4.23   4.90 
## 430880 445541 448275 448551 449242 451948 457907 464801 465271 466870 476266 
##   4.86   4.93   4.27   4.54   3.65   4.93   4.90   4.88   3.99   4.08   4.74 
## 485658 486448 487400 490740 493598 499221 502896 523300 538391 540895 544712 
##   4.98   4.07   4.66   4.68   4.83   4.56   4.63   3.11   4.63   4.67   4.18 
## 569749 591489 592157 600134 621601 626309 647011 658089 665981 678325 680452 
##   4.95   4.83   4.86   4.59   4.85   4.87   4.83   4.33   4.91   4.60   4.84 
## 682920 685741 708913 718034 719529 719610 730211 743524 752463 759878 760633 
##   4.71   4.35   4.18   4.68   3.66   4.76   4.36   4.39   4.95   4.00   4.25 
## 767020 768756 772177 773208 778069 788555 806133 816210 818950 825090 833679 
##   4.45   3.42   4.92   4.68   4.59   4.84   4.48   4.99   4.13   4.55   3.93 
## 833867 839579 849323 870482 880075 884659 887059 888339 911597 921454 944073 
##   4.65   2.98   4.67   4.11   3.94   3.69   3.81   4.37   4.37   4.39   4.97 
## 944807 950722 966591 973439 989835 994882 
##   4.95   4.85   4.75   4.80   4.70   4.87
Xmin <- names(Xsum[which(Xsum == min(Xsum))])
Xmin
## [1] "839579"

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), ylim = c(0, 0.065), 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.978919

학번

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 15 29 56 66 42 65 233
Black 15 29 56 65 42 71 229
X1min <- tbl1 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X1min
## X-squared 
## 0.3059846

학번 홀짝

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

학적 상태

tbl3 <- class_roll$status %>%
  table(class_roll$group, .) 
tbl3 %>%
  pander
  학생 휴학
Red 465 41
Black 464 43
X3min <- tbl3 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X3min
##  X-squared 
## 0.04770835

e-mail 서비스업체

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

전화번호의 분포

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 45 45 46 53 56 49 60 60 41 51
Black 48 47 46 47 61 56 56 54 44 48
X5min <- tbl5 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X5min
## X-squared 
##  1.830121

성씨 분포

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 120 72 37 277
Black 114 70 41 282
X6min <- tbl6 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X6min
## X-squared 
## 0.4308793

Sum of Chi_Squares

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