TaeTrix 2022. 12. 1. 19:07
#permutation - 2
set.seed(1)
x <- rnorm(200, 1, 1)
y <- rexp(300, 1)

R <- 10000
t0 <- t.test(x,y)$statistic;t0 
#H0가정하에 이뤄진 t통계량 
#즉 통계량(theta.hat)을 abs(mean(x) - mean(y)로 잡으면 
#t0 = (theta.hat - 0)/se(theta.hat)
z <- c(x, y)
K <- length(z)
reps <- numeric(R)

for(i in 1:R){
  k <- sample(K, size = length(x) , replace = F)
  x1 <- z[k]
  y1 <- z[-k]
  reps[i] <- t.test(x1,y1)$statistic
}
p <- mean(c(t0,reps) >= t0);p
#under H0이므로 t통계량에 처음 t통게량 값을 넣고 계산
#t0보다 크다는 건 abs(mean(x) - mean(y)) 값이 크다는 이야기 이므로 
# 클수로 h0 기각
hist(reps, freq = F)
points(p,0)