# Reporting Regression Results
# Module 4 - Reporting Regression Results Professionally
# Dataset: CEOSAL2

# Reporting Regression Results
#
# 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 = "CEOSAL2.DTA"
VARIABLES = ['lsalary', 'lsales', 'lmktval', 'ceoten']
if not os.path.exists(DATASET):
    raise FileNotFoundError(
        "Dataset file not installed yet\n"
        "Dataset: CEOSAL2\n"
        "Variables needed: lsalary, lsales, lmktval, ceoten\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["lsalary"]
X = sm.add_constant(df[['lsales', 'lmktval', 'ceoten']])
model = sm.OLS(y, X).fit()
print(model.summary())
print("Focus coefficient:", "ceoten", model.params["ceoten"])
print("Standard error:", model.bse["ceoten"])
print("t statistic:", model.tvalues["ceoten"])
print("two-sided p-value:", model.pvalues["ceoten"])

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

# %% Cell 7
table = pd.DataFrame({
    "coef": model.params,
    "std_err": model.bse,
    "t": model.tvalues,
    "p_value": model.pvalues,
})
print(table)
print("Draft report sentence: Holding the included controls fixed, interpret the focus coefficient with its uncertainty and a limitation.")

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