"""Reproducible analysis for: Anxiety Before and After a Programme

Generated by thericerca on 2026-07-30 21:07 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 whether a stress-management programme changes participants' anxiety, comparing each participant's pre-test and post-test scores.

Objectives:
  - To describe the anxiety_score of the participants.
  - To determine whether anxiety_score differs across phase (the pre-test and post-test).

"""

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 'pre_post.csv'.
df = pd.read_csv('pre_post.csv')
print(f'Loaded {len(df)} rows, {len(df.columns)} columns.')
df.head()


# ------------------------------------------------------------------------------
# [1/2] Descriptive Statistics
#   Question: To describe the anxiety_score of the participants.
# ------------------------------------------------------------------------------
cols = ['anxiety_score']
print(df[cols].describe(include='all').T)


# ------------------------------------------------------------------------------
# [2/2] Paired-Samples t-Test
#   Question: To determine whether anxiety_score differs across phase (the pre-test and post-test).
# ------------------------------------------------------------------------------
pivoted = df.pivot_table(index=df.index.name or 'index', columns='phase',
                          values='anxiety_score', aggfunc='first').dropna()
if pivoted.shape[1] != 2: raise ValueError('Paired t-test needs exactly two phases per subject.')
before, after = pivoted.iloc[:, 0], pivoted.iloc[:, 1]
t, p_value = stats.ttest_rel(before, after)
print(f't({len(before)-1}) = {t:.3f}, p = {p_value:.4g}, mean diff = {(after-before).mean():.3f}')


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