From 04643d312cc974b153dc57bc8da8591dfe7d8be5 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Fri, 2 Dec 2022 21:07:08 +1100 Subject: [PATCH] Add unit test for RegressionResult.bootstrap --- tests/test_regress.py | 21 +++++++++++++++++++++ yli/regress.py | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/test_regress.py b/tests/test_regress.py index 912b23f..eccd8c5 100644 --- a/tests/test_regress.py +++ b/tests/test_regress.py @@ -47,6 +47,27 @@ def test_regress_ols_ol11_4(): assert result.terms['SoilPh'].beta.ci_lower == approx(-10.15, abs=0.01) assert result.terms['SoilPh'].beta.ci_upper == approx(-5.57, abs=0.01) +def test_regress_bootstrap_ols_ol11_4(): + """Compare RegressionResult.bootstrap for Ott & Longnecker (2016) example 11.4/11.7""" + + df = pd.DataFrame({ + 'SoilPh': [3.3, 3.4, 3.4, 3.5, 3.6, 3.6, 3.7, 3.7, 3.8, 3.8, 3.9, 4.0, 4.1, 4.2, 4.3, 4.4, 4.5, 5.0, 5.1, 5.2], + 'GrowthRet': [17.78, 21.59, 23.84, 15.13, 23.45, 20.87, 17.78, 20.09, 17.78, 12.46, 14.95, 15.87, 17.45, 14.35, 14.64, 17.25, 12.57, 7.15, 7.50, 4.34] + }) + + result = yli.regress(sm.OLS, df, 'GrowthRet', 'SoilPh') + + np.random.seed(0) + result_bs = result.bootstrap(1000) + + assert result_bs.terms['(Intercept)'].beta.point == result.terms['(Intercept)'].beta.point + assert result_bs.terms['(Intercept)'].beta.ci_lower == approx(result.terms['(Intercept)'].beta.ci_lower, rel=0.05) + assert result_bs.terms['(Intercept)'].beta.ci_upper == approx(result.terms['(Intercept)'].beta.ci_upper, rel=0.05) + + assert result_bs.terms['SoilPh'].beta.point == result.terms['SoilPh'].beta.point + assert result_bs.terms['SoilPh'].beta.ci_lower == approx(result.terms['SoilPh'].beta.ci_lower, rel=0.1) + assert result_bs.terms['SoilPh'].beta.ci_upper == approx(result.terms['SoilPh'].beta.ci_upper, rel=0.05) + def test_regress_ols_ol13_5(): """Compare yli.regress for Ott & Longnecker (2016) chapter 13.5""" diff --git a/yli/regress.py b/yli/regress.py index 3f6cbf8..404303f 100644 --- a/yli/regress.py +++ b/yli/regress.py @@ -353,7 +353,7 @@ class RegressionResult: def bootstrap(self, samples=1000): """ - Fit a statsmodels regression model, using bootstrapping to compute confidence intervals and *p* values + Use bootstrapping to recompute confidence intervals and *p* values for the terms in the regression model :param samples: Number of bootstrap samples to draw :type samples: int