Data

Search for Best Configuration

M1 <- 1
M2 <- 1000
Xsum <- sapply(M1:M2, red_and_black)
names(Xsum) <- M1:M2
Xsum %>%
  summary %>%
  round(2) 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.99    5.20    7.60    8.24   10.44   25.74
Xsum %>%
  sd %>%
  round(2)
## [1] 3.98
Xsum %>%
  `<=`(2) %>%
  which %>%
  `[`(Xsum, .) %>%
  round(2)
##   58  203  243  351  475  688  752  810  841  878 
## 1.92 1.26 1.64 0.99 1.54 1.65 1.37 1.92 1.92 1.54
Xmin <- names(Xsum[which(Xsum == min(Xsum))])
Xmin
## [1] "351"

Plot

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

plot(density(Xsum), xlim = c(0, 25), ylim = c(0, 0.15), main = "Density Estimation of Xsum")
lines(x, dchisq(x, df = 9), col = "red")

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] 0.9859181

학번 홀짝

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

출생 시기

tbl3 <- class_roll$yob %>%
    cut(breaks = c(-Inf, 1980, 1995, Inf), 
        labels =c ("X세대", "Y세대", "Z세대" )) %>%
    table(class_roll$group, .) 
tbl3 %>%
  pander
  X세대 Y세대 Z세대
Red 4 6 7
Black 3 8 6
X3min <- tbl3 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X3min
## X-squared 
## 0.5054945

email 서비스

isp <- class_roll$email %>%
    strsplit("@", fixed = TRUE) %>%
    sapply("[", 2) %>%
    strsplit("[.]", fixed = FALSE) %>%
    sapply("[", 1)
tbl4 <- isp %>%
    `%in%`(c("naver", "gmail")) %>%
    `!` %>%
    ifelse("기타서비스", isp) %>%
    factor(levels = c("naver", "gmail", "기타서비스"),
           labels = c("네이버", "구글", "기타서비스")) %>%
    table(class_roll$group, .) 
tbl4 %>%
  pander
  네이버 구글 기타서비스
Red 9 6 2
Black 9 6 2
X4min <- tbl4 %>%
    chisq.test(simulate.p.value = TRUE) %>%
    `[[`(1) 
X4min
## X-squared 
##         0

성별

tbl5 <- class_roll$gender %>%
    table(class_roll$group, .) 
tbl5 %>%
  pander
  f m
Red 9 8
Black 8 9
X5min <- tbl5 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1) 
X5min
## X-squared 
## 0.1176471

성씨 분포

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 3 3 11
Black 3 2 12
X6min <- tbl6 %>%
  chisq.test(simulate.p.value = TRUE) %>%
  `[[`(1)
X6min
## X-squared 
## 0.2434783

Sum of Chi_Squares

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