# General Linear Restrictions
# Module 4 - General Linear Restrictions
# Dataset: HPRICE1

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

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

# %% Cell 7
print(model.f_test("sqrft = 0, bdrms = 0"))
print("Exercise: state the restricted model in words.")

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