#!/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 2: Kinh tế lượng dữ liệu bảng và chuỗi thời gian tài chính
Buổi học : Buổi 05 — Nhóm 3.5 Lựa chọn dữ liệu bảng và cầu cấu trúc
Bài giảng: B039 — Mô hình Logit dữ liệu bảng hiệu ứng cố định Chamberlain
Tên quốc tế: Chamberlain Conditional FE & Bias-Corrected Panel Logit/Probit
Mã mô hình : panel_logit_fe_cre
Phần mềm : Python 3.11+ — Thư viện: statsmodels
Dữ liệu   : vietlod_panel_logit_fe_cre_ecodata.csv
Kiểm định : Kiểm định Hausman Panel FE Logit vs RE Logit, số đơn vị chuyển trạng thái thực chất — switchers count — cảnh báo loại các đơn vị không đổi $Y_{it} \equiv 0/1$ , Average Partial Effects đã hiệu chỉnh chệch.
"""

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_panel_logit_fe_cre_ecodata.csv"
if not os.path.exists(DATA_FILE):
    DATA_FILE = "https://sachktl.tnsai.vn/src/ecoaz/data/vietlod_panel_logit_fe_cre_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 B039 — Mô hình Logit dữ liệu bảng hiệu ứng cố định Chamberlain
import pandas as pd
df = pd.read_csv('vietlod_panel_logit_fe_cre_ecodata.csv')
print(df.describe())
# ConditionalLogit(df['y_bin'], df[['x1', 'x2']], groups=df['id']).fit()

print("Hoan tat uoc luong mo hinh B039 (panel_logit_fe_cre) tren Python 3.11+.")
