# One-Sided and Two-Sided Tests
# Module 4 - One-Sided and Two-Sided Tests
# Dataset: GPA1

# One-Sided and Two-Sided Tests
#
# 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 = "GPA1.DTA"
VARIABLES = ['colGPA', 'hsGPA', 'ACT']
if not os.path.exists(DATASET):
    raise FileNotFoundError(
        "Dataset file not installed yet\n"
        "Dataset: GPA1\n"
        "Variables needed: colGPA, hsGPA, ACT\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["colGPA"]
X = sm.add_constant(df[['hsGPA', 'ACT']])
model = sm.OLS(y, X).fit()
print(model.summary())
print("Focus coefficient:", "ACT", model.params["ACT"])
print("Standard error:", model.bse["ACT"])
print("t statistic:", model.tvalues["ACT"])
print("two-sided p-value:", model.pvalues["ACT"])

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

# %% Cell 7
estimate = model.params["ACT"]
se = model.bse["ACT"]
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.
