# 분류분석 — 의사결정나무·로지스틱 회귀·인공신경망 — 공식 predict.glm() 예제의 이항 반응곡선
# 적용 범위: predict.glm() 공식 예제의 12개 이항 관측과 sex*ldose 모형을 사용했습니다. 원문의 원래 용량 로그축 대신 로그2 용량을 가로축으로 표시하고 색·범례를 추가했습니다.
# 출처: https://stat.ethz.ch/R-manual/R-patched/library/stats/html/predict.glm.html
# 실행 확인 환경: R version 4.6.2 (2026-10-29)
# 아래 코드를 실행하면 출력과 그림을 확인할 수 있습니다.

# 재현 설정: 같은 난수 방식과 출력 자릿수를 사용합니다.
RNGkind("Mersenne-Twister", "Inversion", "Rejection")
set.seed(2026)
options(width = 90, digits = 7, scipen = 7)

# 공식 예제의 용량·성별·사망 개체 수를 사용합니다.
ldose <- rep(0:5, 2)
numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16)
sex <- factor(rep(c("M", "F"), each = 6))
fit <- glm(cbind(numdead, 20 - numdead) ~ sex * ldose, family = binomial)
round(coef(fit), 4)
round(head(predict(fit, type = "response"), 6), 4)
plot(ldose, numdead / 20, pch = ifelse(sex == "M", 16, 17),
     col = ifelse(sex == "M", "#2563EB", "#DC2626"),
     xlab = "로그2 용량", ylab = "사망 비율·예측확률", main = "이항 로지스틱 회귀", ylim = c(0, 1))
for (s in levels(sex)) {
  grid <- data.frame(ldose = seq(0, 5, length.out = 100),
                     sex = factor(s, levels = levels(sex)))
  lines(grid$ldose, predict(fit, grid, type = "response"),
        col = ifelse(s == "M", "#2563EB", "#DC2626"), lwd = 2)
}
legend("topleft", c("수컷 관측·적합", "암컷 관측·적합"),
       col = c("#2563EB", "#DC2626"), pch = c(16, 17), lty = 1, bty = "n")
