# 1st question
power <- 0.8
alpha <- 0.05
z0 <- qnorm(p = 0.975)
z1 <- qnorm(p = 0.2)
n <- 2 * (z0 - z1) ^ 2
n
## [1] 15.69776
# relations between sample size N with delta(effect size)
N <- NULL
Del <- NULL
for(del in c(1:1000)){
  Del <- c(Del,del)
  N <- c(N, (2 * (z0 - z1)^2 / del ^ 2))
}
plot(x = log(Del), y = N)

# relations between sample size N with variance
Var_a <- NULL
N <- NULL
for(var in c(1:1000)){
  Var_a <- c(Var_a, var)
  N <- c(N, ((2 * var) * (z0 - z1)^2))
}
plot(x = Var_a, y = N)

# rolling dice
# using chi square test to test if they are fair dices
# test goodness of fitting
p1 <- 1/6
p0 <- 5/6
chisq.test(x = c(48,35,15,3),
           p = c(
                p0 ^ 3, 
                choose(3,1) * p1 * (p0 ^ 2),
                choose(3,2) * (p1 ^ 2) * p0,
                p1 ^ 3
                )
           )
## Warning in chisq.test(x = c(48, 35, 15, 3), p = c(p0^3, choose(3, 1) * p1
## * : Chi-squared approximation may be incorrect
## 
##  Chi-squared test for given probabilities
## 
## data:  c(48, 35, 15, 3)
## X-squared = 24.676, df = 3, p-value = 1.804e-05