# 가설검정과 유의성 판단 — 검정력 80%에 필요한 집단별 표본 수 계산
# 적용 범위: power.t.test()의 공식 정의를 두 독립 정규 집단·공통 표준편차 1·참 평균차 0.5·양측 유의수준 0.05라는 명시한 학습 설계에 적용했습니다. strict=TRUE로 양측의 반대 방향 기각 확률도 포함합니다. 가정한 효과는 관측 자료에서 추정한 효과가 아닙니다.
# 출처: https://stat.ethz.ch/R-manual/R-patched/library/stats/html/power.t.test.html
# 실행 확인 환경: R version 4.6.2 (2026-10-29)
# 아래 코드를 실행하면 출력과 그림을 확인할 수 있습니다.

design <- power.t.test(delta = 0.5, sd = 1, sig.level = 0.05, power = 0.8,
  type = "two.sample", alternative = "two.sided", strict = TRUE)
n_each <- ceiling(design$n)
power_for_n <- function(n) power.t.test(n = n, delta = 0.5, sd = 1,
  sig.level = 0.05, type = "two.sample", alternative = "two.sided", strict = TRUE)$power
achieved <- power_for_n(n_each)
cat("계산된 집단별 n:", round(design$n, 6), "\n")
cat("올림한 집단별 수:", n_each, " / 두 집단 전체 수:", 2 * n_each, "\n")
cat("올림한 표본 수에서의 검정력:", round(achieved, 6), "\n")
n_grid <- 10:150
powers <- vapply(n_grid, power_for_n, numeric(1))
plot(n_grid, powers, type = "l", lwd = 3, col = "#2563EB", ylim = c(0, 1),
  xlab = "집단별 표본 수", ylab = "검정력",
  main = "평균차 0.5·공통 표준편차 1의 양측 설계")
abline(h = 0.8, lty = 2, col = "#6B7280")
abline(v = n_each, lty = 3, col = "#D97706")
points(n_each, achieved, pch = 19, col = "#D97706", cex = 1.2)
text(n_each + 6, achieved - 0.08, paste0("집단별 ", n_each, "개"), adj = 0, cex = 0.85)
legend("bottomright", c("표본 수에 따른 검정력", "목표 검정력 80%"),
  col = c("#2563EB", "#6B7280"), lty = c(1, 2), lwd = c(3, 1), bty = "n", cex = 0.85)
