I’m wondering how can I get odds ratio from a fitted logistic regression models in python statsmodels.
JavaScript
x
30
30
1
>>> import statsmodels.api as sm
2
>>> import numpy as np
3
>>> X = np.random.normal(0, 1, (100, 3))
4
>>> y = np.random.choice([0, 1], 100)
5
>>> res = sm.Logit(y, X).fit()
6
Optimization terminated successfully.
7
Current function value: 0.683158
8
Iterations 4
9
>>> res.summary()
10
<class 'statsmodels.iolib.summary.Summary'>
11
"""
12
Logit Regression Results
13
==============================================================================
14
Dep. Variable: y No. Observations: 100
15
Model: Logit Df Residuals: 97
16
Method: MLE Df Model: 2
17
Date: Sun, 05 Jun 2016 Pseudo R-squ.: 0.009835
18
Time: 23:25:06 Log-Likelihood: -68.316
19
converged: True LL-Null: -68.994
20
LLR p-value: 0.5073
21
==============================================================================
22
coef std err z P>|z| [95.0% Conf. Int.]
23
------------------------------------------------------------------------------
24
x1 -0.0033 0.181 -0.018 0.985 -0.359 0.352
25
x2 0.0565 0.213 0.265 0.791 -0.362 0.475
26
x3 0.2985 0.216 1.380 0.168 -0.125 0.723
27
==============================================================================
28
"""
29
>>>
30
Advertisement
Answer
You can get the odds ratio with:
JavaScript
1
2
1
np.exp(res.params)
2
To also get the confidence intervals (source):
JavaScript
1
6
1
params = res.params
2
conf = res.conf_int()
3
conf['Odds Ratio'] = params
4
conf.columns = ['5%', '95%', 'Odds Ratio']
5
print(np.exp(conf))
6
Disclaimer: I’ve just put together the comments to your question.