Skip to content

Commit

Permalink
Handle changes in statsmodels behavior
Browse files Browse the repository at this point in the history
In the latest stable release (0.14.0) statsmodels does not
yield an exception anymore when data is perfectly separable
this restores the expected behavior, though this may become
a problem in future versions
  • Loading branch information
mgalardini committed May 8, 2023
1 parent a0e1716 commit 0569298
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pyseer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ def fit_null(p, m, cov, continuous, firth=False):
start_vec[0] = np.log(np.mean(p)/(1-np.mean(p)))
null_mod = smf.Logit(p, v)

# statsmodels does not raise exceptions with perfectly separable data
# but just a warning
# this may stop working in the future
# source: https://github.com/statsmodels/statsmodels/blob/e12de82fabd8e3fd71413aae384763c1442c4516/statsmodels/discrete/discrete_model.py#L186
null_mod.raise_on_perfect_prediction = True

try:
if continuous:
null_res = null_mod.fit(disp=False)
Expand Down Expand Up @@ -173,6 +179,11 @@ def fit_lineage_effect(lin, c, k):
axis=1)

lineage_mod = smf.Logit(k, X)
# statsmodels does not raise exceptions with perfectly separable data
# but just a warning
# this may stop working in the future
# source: https://github.com/statsmodels/statsmodels/blob/e12de82fabd8e3fd71413aae384763c1442c4516/statsmodels/discrete/discrete_model.py#L186
lineage_mod.raise_on_perfect_prediction = True
try:
lineage_res = lineage_mod.fit(method='newton', disp=False)

Expand Down Expand Up @@ -287,6 +298,11 @@ def fixed_effects_regression(variant, p, k, m, c, af, pattern,
try:
if continuous:
mod = smf.OLS(p, v)
# statsmodels does not raise exceptions with perfectly separable data
# but just a warning
# this may stop working in the future
# source: https://github.com/statsmodels/statsmodels/blob/e12de82fabd8e3fd71413aae384763c1442c4516/statsmodels/discrete/discrete_model.py#L186
mod.raise_on_perfect_prediction = True

res = mod.fit()
intercept = res.params[0]
Expand All @@ -298,6 +314,11 @@ def fixed_effects_regression(variant, p, k, m, c, af, pattern,

else:
mod = smf.Logit(p, v)
# statsmodels does not raise exceptions with perfectly separable data
# but just a warning
# this may stop working in the future
# source: https://github.com/statsmodels/statsmodels/blob/e12de82fabd8e3fd71413aae384763c1442c4516/statsmodels/discrete/discrete_model.py#L186
mod.raise_on_perfect_prediction = True

start_vec = np.zeros(v.shape[1])
start_vec[0] = np.log(np.mean(p)/(1-np.mean(p)))
Expand Down
1 change: 1 addition & 0 deletions tests/model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
try:
smf.Logit
except AttributeError:
import statsmodels
smf.Logit = statsmodels.discrete.discrete_model.Logit
from pyseer.model import pre_filtering
from pyseer.model import fit_null
Expand Down

0 comments on commit 0569298

Please sign in to comment.