Data

Search for Best Configuration

M1 <- 8000001
M2 <- 9000000
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.51   16.36   20.35   21.03   24.97   70.08
Xsum %>%
  sd %>%
  round(2)
## [1] 6.5
Xsum %>%
  `<=`(5) %>%
  which %>%
  `[`(Xsum, .) %>%
  round(2)
## 8002536 8014496 8020224 8034536 8034764 8037662 8043412 8056467 8071113 8077590 
##    4.73    4.65    4.85    4.98    4.31    3.58    4.95    4.52    4.09    4.42 
## 8086000 8089916 8098466 8102118 8103456 8104747 8118771 8132097 8136136 8143056 
##    4.70    4.58    4.52    4.09    4.86    4.78    4.49    4.78    4.17    4.73 
## 8146383 8153948 8159438 8168468 8169342 8176049 8177020 8177778 8179134 8184508 
##    4.88    4.82    4.61    4.89    4.83    3.62    4.72    4.83    4.24    4.81 
## 8191781 8193762 8196381 8197129 8213766 8216514 8217045 8259204 8279864 8281594 
##    4.97    3.68    4.53    4.98    4.98    3.52    4.91    4.84    4.84    3.85 
## 8286809 8291628 8295292 8296894 8301532 8311965 8318198 8328223 8343149 8351602 
##    4.70    4.77    4.99    4.86    4.82    3.82    4.96    4.81    4.83    4.46 
## 8370920 8383512 8385014 8387076 8399905 8407035 8409061 8410367 8428866 8440084 
##    4.58    3.85    3.28    4.31    4.51    4.99    4.25    4.43    4.97    4.90 
## 8456771 8460532 8464605 8472823 8480019 8480109 8481271 8486923 8494329 8504264 
##    3.86    4.36    4.36    4.16    4.91    3.14    4.55    4.64    4.89    4.67 
## 8506296 8507945 8512045 8516877 8517897 8519139 8522850 8539531 8540158 8541684 
##    2.51    4.73    4.08    4.90    4.87    4.10    2.87    4.27    3.74    3.04 
## 8544987 8558134 8561336 8568141 8576653 8580080 8585710 8598106 8604956 8614304 
##    3.37    4.94    4.48    4.52    4.58    4.58    4.67    4.15    4.98    3.72 
## 8621496 8622712 8635553 8642862 8652095 8666744 8667703 8680469 8686855 8688248 
##    4.97    4.65    3.96    3.99    3.86    4.65    3.96    3.30    4.96    4.59 
## 8696048 8697267 8714180 8728638 8734331 8737712 8738958 8740122 8741269 8743309 
##    4.86    4.95    4.06    4.67    4.67    4.57    4.92    4.47    4.77    4.62 
## 8756955 8766890 8768786 8774846 8797459 8806722 8811131 8820577 8821426 8826533 
##    4.79    4.73    4.60    4.21    4.92    4.36    4.40    4.79    3.89    4.57 
## 8833888 8836070 8836281 8837443 8844403 8847957 8848852 8877059 8882259 8884215 
##    4.97    4.29    4.87    4.51    4.91    4.19    4.98    4.47    4.88    4.80 
## 8886680 8939374 8949481 8958758 8961805 8975391 8977193 8984778 8987112 8997870 
##    3.67    3.94    4.66    4.06    4.94    4.32    4.58    4.61    4.09    4.43 
## 8999528 
##    3.57
Xmin <- names(Xsum[which(Xsum == min(Xsum))])
Xmin
## [1] "8506296"

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.512243

학번

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 19 35 58 80 51 136 24
Black 22 36 63 74 50 133 26
X1min <- tbl1 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X1min
## X-squared 
## 0.7960948

학번 홀짝

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

학적 상태

tbl3 <- class_roll$status %>%
  table(class_roll$group, .) 
tbl3 %>%
  pander
  학생 휴학
Red 332 71
Black 327 77
X3min <- tbl3 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X3min
## X-squared 
## 0.2799408

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 42 35 32 42 32 48 42 38 41 50
Black 39 30 36 43 32 50 42 39 41 52
X5min <- tbl5 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X5min
## X-squared 
## 0.8308467

성씨 분포

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 76 58 31 238
Black 72 63 29 240
X6min <- tbl6 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X6min
## X-squared 
##  0.388516

Sum of Chi_Squares

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