diff --git a/tests/test_ch13_fitness.py b/tests/test_ch13_fitness.py index 225994a..a9e3790 100644 --- a/tests/test_ch13_fitness.py +++ b/tests/test_ch13_fitness.py @@ -1,3 +1,4 @@ +from __future__ import annotations import statsmodels.formula.api as smf from scripts import sim_fitness_2x2 as sim @@ -9,17 +10,19 @@ def test_fitness_mixed_model_signals(): # Mixed model with random intercept for id, categorical time/group + covariates md = smf.mixedlm( - "strength ~ C(time) * C(group) + age + sex + bmi", + # FIX: Explicitly set 'pre' as the reference level for 'time' + "strength ~ C(time, Treatment(reference='pre')) * C(group) + age + sex + bmi", long_df, groups=long_df["id"], ) res = md.fit(reml=True) # main pre->post should be positive and clearly significant - assert res.params["C(time)[T.post]"] > 5 - assert res.pvalues["C(time)[T.post]"] < 1e-6 + assert res.params["C(time, Treatment(reference='pre'))[T.post]"] > 5 + assert res.pvalues["C(time, Treatment(reference='pre'))[T.post]"] < 1e-6 # interaction should be positive (ProgramB a bit more improvement) - assert res.params["C(time)[T.post]:C(group)[T.ProgramB]"] > 0 + key = "C(time, Treatment(reference='pre'))[T.post]:C(group)[T.ProgramB]" + assert res.params[key] > 0 # With N=80, we can have a stricter p-value check - assert res.pvalues["C(time)[T.post]:C(group)[T.ProgramB]"] < 0.05 \ No newline at end of file + assert res.pvalues[key] < 0.05 \ No newline at end of file diff --git a/tests/test_ch13_stroop.py b/tests/test_ch13_stroop.py index 4db4d97..5c8a219 100644 --- a/tests/test_ch13_stroop.py +++ b/tests/test_ch13_stroop.py @@ -1,3 +1,4 @@ +from __future__ import annotations import numpy as np import statsmodels.formula.api as smf @@ -10,7 +11,8 @@ def test_stroop_effect_and_mixed_model(): # subject-level means by condition (ms) means = ( - trials[trials["correct"]] + # FIX: Convert 'correct' (int) to boolean for pandas masking + trials[trials["correct"].astype(bool)] .query("rt_ms.between(200, 2000)") .groupby(["subject", "condition"], as_index=False)["rt_ms"] .mean() @@ -22,7 +24,8 @@ def test_stroop_effect_and_mixed_model(): assert diff.mean() > 60 and diff.mean() < 140 # mixed model on log RT with random intercept for subject - df = trials[trials["correct"] & trials["rt_ms"].between(200, 2000)].copy() + # FIX: Convert 'correct' (int) to boolean for pandas masking + df = trials[trials["correct"].astype(bool) & trials["rt_ms"].between(200, 2000)].copy() df["log_rt"] = np.log(df["rt_ms"]) # Use categorical for condition md = smf.mixedlm("log_rt ~ C(condition)", df, groups=df["subject"])