# =============================================================================
# HỆ SINH THÁI HỌC THUẬT TNSAI — SACHKTL.TNSAI.VN & VIETLOD.TNSAI.VN
# Khóa học : Khóa 3: Suy diễn nhân quả, đánh giá chính sách, kinh tế lượng không gian và sinh tồn
# Buổi học : Buổi 13 — Nhóm 11.4 Nguy cơ thời gian rời rạc và rủi ro cạnh tranh
# Bài giảng: B177 — Mô hình nguy cơ thời gian rời rạc Prentice–Gloeckler
# Tên quốc tế: Prentice–Gloeckler Complementary Log-Log Discrete-Time Hazard
# Mã mô hình : discrete_time_hazard
# Phần mềm : R 4.x — Gói lệnh: discSurv / stats
# Dữ liệu   : vietlod_discrete_time_hazard_ecodata.csv
# Kiểm định : Tỷ số nguy cơ $\exp$ tương đương chính xác tham số Cox liên tục gốc, kiểm định LR dạng hàm nguy cơ cơ sở theo thời gian, deviance AIC/BIC.
# =============================================================================

options(scipen = 999, digits = 4)

# 1. Nạp bộ dữ liệu thực hành chuẩn hóa
data_file <- "vietlod_discrete_time_hazard_ecodata.csv"
if (!file.exists(data_file)) {
  data_file <- "https://sachktl.tnsai.vn/src/ecoaz/data/vietlod_discrete_time_hazard_ecodata.csv"
}
df <- read.csv(data_file, stringsAsFactors = FALSE)
cat("Kich thuoc mau thuc hanh:", nrow(df), "quan sat,", ncol(df), "bien so\n")

# 2. Thực thi kịch bản ước lượng và kiểm định B177 — Mô hình nguy cơ thời gian rời rạc Prentice–Gloeckler
df <- read.csv('vietlod_discrete_time_hazard_ecodata.csv')
summary(df)
# glm(event_it ~ x1 + x2 + factor(spell_t), family = binomial(link = 'cloglog'), data = df)

cat("Hoan tat uoc luong mo hinh B177 (discrete_time_hazard) tren R 4.x.\n")
