"""Reproducible analysis for: Homework and Math Scores Across Schools

Generated by thericerca on 2026-07-30 21:03 UTC.
Regenerate the same statistical output on the same data by running this script (or the
matching .ipynb) with the third-party libraries at the top installed.

Statement of the problem:
  This study examines how weekly homework hours relate to students' math scores, acknowledging that students are nested within schools so any raw relationship may confound within-school effects with between-school differences.

Objectives:
  - To describe the math_score and homework_hours across the participating schools.
  - To fit a multilevel (mixed-effects) model of math_score on homework_hours with a random intercept per school, so within-school and between-school variation are separated.

"""

import pandas as pd
import numpy as np
from scipy import stats
import statsmodels.api as sm
import statsmodels.formula.api as smf


# Load the dataset. Point this at your local copy of 'school_math.csv'.
df = pd.read_csv('school_math.csv')
print(f'Loaded {len(df)} rows, {len(df.columns)} columns.')
df.head()


# ------------------------------------------------------------------------------
# [1/2] Descriptive Statistics
#   Question: To describe the math_score and homework_hours across the participating schools.
# ------------------------------------------------------------------------------
cols = ['math_score', 'homework_hours']
print(df[cols].describe(include='all').T)


# ------------------------------------------------------------------------------
# [2/2] Linear Mixed-Effects Model
#   Question: To fit a multilevel (mixed-effects) model of math_score on homework_hours with a random intercept per school, so within-school and between-school variation are separated.
# ------------------------------------------------------------------------------
# Mixed-effects linear model with random intercept per school_id.
model = smf.mixedlm('Q("math_score") ~ Q("homework_hours")', data=df, groups=df['school_id']).fit()
print(model.summary())


# ------------------------------------------------------------------------------
# End of thericerca reproducible analysis.
# ------------------------------------------------------------------------------
