#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
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 : Python 3.11+ — Thư viện: statsmodels
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.
"""

import os
import warnings
import numpy as np
import pandas as pd

warnings.filterwarnings("ignore")

# 1. Nạp bộ dữ liệu thực hành chuẩn hóa
DATA_FILE = "vietlod_discrete_time_hazard_ecodata.csv"
if not os.path.exists(DATA_FILE):
    DATA_FILE = "https://sachktl.tnsai.vn/src/ecoaz/data/vietlod_discrete_time_hazard_ecodata.csv"

df = pd.read_csv(DATA_FILE)
print(f"Kich thuoc mau thuc hanh {DATA_FILE}: {df.shape[0]} quan sat x {df.shape[1]} bien so")

# 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
import pandas as pd
df = pd.read_csv('vietlod_discrete_time_hazard_ecodata.csv')
print(df.describe())
# smf.glm('event_it ~ x1 + x2 + C(spell_t)', data=df, family=sm.families.Binomial(sm.families.links.CLogLog())).fit()

print("Hoan tat uoc luong mo hinh B177 (discrete_time_hazard) tren Python 3.11+.")
