# T Tests with WAGE1
# Module 4 - Testing a Single Coefficient Against Zero
# Dataset: WAGE1

# T Tests with WAGE1
#
# Learning goal: run a real multiple-regression inference workflow and write a careful interpretation.

# Dataset check
# This cell confirms that the dataset file is available. If the file is missing, the notebook gives a safe warning rather than fake results.

# %% Cell 3
import os
DATASET = "WAGE1.DTA"
VARIABLES = ['lwage', 'educ', 'exper', 'tenure']
if not os.path.exists(DATASET):
    raise FileNotFoundError(
        "Dataset file not installed yet\n"
        "Dataset: WAGE1\n"
        "Variables needed: lwage, educ, exper, tenure\n"
        "Course data folder: https://drive.google.com/drive/folders/1_STdcydIcst-opcbwOKRFzUXsgxQgBoS?usp=sharing\n"
        "Admin upload instruction: upload the dataset in Admin -> Datasets, publish it, and make the file available to the notebook runner."
    )
print("Ready:", DATASET)

# Estimate the model
# Estimate OLS and inspect the coefficient, standard error, t statistic, and p-value.

# %% Cell 5
import pandas as pd
import statsmodels.api as sm
from scipy import stats

df = pd.read_stata(DATASET)[VARIABLES].dropna()
y = df["lwage"]
X = sm.add_constant(df[['educ', 'exper', 'tenure']])
model = sm.OLS(y, X).fit()
print(model.summary())
print("Focus coefficient:", "educ", model.params["educ"])
print("Standard error:", model.bse["educ"])
print("t statistic:", model.tvalues["educ"])
print("two-sided p-value:", model.pvalues["educ"])

# Inference calculation
# Compute the lesson-specific inference object and connect it to the hypothesis.

# %% Cell 7
estimate = model.params["educ"]
se = model.bse["educ"]
df_resid = int(model.df_resid)
critical = stats.t.ppf(0.975, df_resid)
ci_low, ci_high = estimate - critical * se, estimate + critical * se
print({"df": df_resid, "critical_95": critical, "ci_95": (ci_low, ci_high)})
print("Interpretation prompt: write one sentence that names the unit, control variables, uncertainty, and limitation.")

# Check your understanding
# Write one sentence that distinguishes statistical significance from practical importance for this model.
