Add unit test for RegressionResult.bootstrap

This commit is contained in:
RunasSudo 2022-12-02 21:07:08 +11:00
parent e4e85db354
commit 04643d312c
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 22 additions and 1 deletions

View File

@ -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"""

View File

@ -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