Data

Search for Best Configuration

M1 <- 1
M2 <- 1000
Xsum <- numeric(0)
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)
##         X1      X2      X3      X4      X5      X6
## X1  1.0000 -0.0226  0.0851  0.0252 -0.0027 -0.0130
## X2 -0.0226  1.0000  0.0861 -0.0166  0.0133  0.0567
## X3  0.0851  0.0861  1.0000  0.0103 -0.0042 -0.0175
## X4  0.0252 -0.0166  0.0103  1.0000 -0.0443  0.0334
## X5 -0.0027  0.0133 -0.0042 -0.0443  1.0000 -0.0141
## X6 -0.0130  0.0567 -0.0175  0.0334 -0.0141  1.0000
names(Xsum) <- M1:M2
Xsum %>%
  summary %>%
  round(2) 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    5.45   16.56   20.47   21.19   25.21   49.24
Xsum %>%
  sd %>%
  round(2)
## [1] 6.56
Xsum %>%
  `<=`(10) %>%
  which %>%
  `[`(Xsum, .) %>%
  round(2)
##   17   89  105  109  250  271  390  552  570  648  723  729  786  795  803  872 
## 7.16 8.84 8.03 8.95 9.82 9.24 9.32 9.68 6.61 7.53 9.34 9.85 9.90 9.99 9.71 5.45 
##  885  900 
## 9.03 8.35
Xmin <- names(Xsum[which(Xsum == min(Xsum))])
Xmin
## [1] "872"

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(class_roll)
## $Values
## [1] 2.478052821 0.006499056 0.000000000 0.194153337 2.449369592 0.319199785
## 
## $Xsum
## [1] 5.447275

학번

class_roll$id_2 <-
  class_roll$id %>%
  ifelse(. <= 2015, "2015", .)
tbl1 <- class_roll %$%
  table(.$group, .$id_2 %>% substr(1, 4)) %>%
  `colnames<-`(c("2015 이전", 2016:2021)) 
tbl1 %>%
  pander
  2015 이전 2016 2017 2018 2019 2020 2021
Red 21 29 26 58 19 51 104
Black 16 29 29 50 19 47 118
X1min <- tbl1 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X1min
## X-squared 
##  2.478053

학번 홀짝

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

학적 상태

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

e-mail 서비스업체

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

전화번호의 분포

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 23 32 35 25 25 29 29 34 42 34
Black 24 30 33 28 26 35 26 40 34 32
X5min <- tbl5 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X5min
## X-squared 
##   2.44937

성씨 분포

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 66 47 23 172
Black 62 48 26 172
X6min <- tbl6 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X6min
## X-squared 
## 0.3191998

Sum of Chi_Squares

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