I am working on a binary classification problem and am trying to do univariate analysis to get their p-values and risk ratios.

I can do this only via IBM SPSS? Can't it be done via Python?

Can someone help with this?

Feat1  'Yes'n (%)   'No' n (%)       RR      95% CI       P
Feat2   43 (72.9)   822 (8.4)       28.0    15.9-49.6   <.001
Feat3   11 (18.6)   721 (7.3)       2.87    1.50-5.50   .004
Feat4   18 (30.5)   1,654 (16.8)    2.16    1.24-3.75   .008
Feat5   31 (52.5)   3,679 (37.4)    1.84    1.11-3.07   .02
  • $\begingroup$ I'm not aware of any python tools which can yield this table exactly. You'd likely have to code it up yourself $\endgroup$ Jan 3, 2020 at 3:41

1 Answer 1


While it cannot create the table in exactly how you specified, you can calculate risk ratios (and other measures) using the zEpid library. This library supports both calculating from summary counts (details here) and directly from pandas DataFrame objects (details here).

The library does not directly calculate p-values, but you can easily do this by a little extra code. Below is a quick example and code snippet for the pandas DataFrame object

import numpy as np
import pandas as pd
from scipy.stats import norm
from zepid import RiskRatio

# creating an example data set
df = pd.DataFrame()
df['A'] = [1, 0, 1, 0, 1, 1]
df['B'] = [1, 1, 0, 0, 0, 0]

# calculating risk ratio
rr = RiskRatio()
rr.fit(df, exposure='A', outcome='B')

# calculating p-value
est= rr.results['RiskRatio'][1]
std = rr.results['SD(RR)'][1]
z_score = np.log(est)/std
p_value = norm.sf(abs(z_score))*2

You can easily generalize this by using the following function

def calculate_pvalue(data, exposure, outcome):
    rr = RiskRatio()
    rr.fit(data, exposure=exposure, outcome=outcome)

    # calculating p-value
    est = rr.results['RiskRatio'][1]
    std = rr.results['SD(RR)'][1]
    z_score = np.log(est) / std
    p_value = norm.sf(abs(z_score)) * 2
    return est, p_value

Disclosure: I am the creator of this library. I don't have p-values directly calculated to discourage the misuse of statistical significance. I would recommend reporting confidence intervals over p-values, since they provide more information than a p-value.

  • $\begingroup$ Hi, thanks for the response. Upvoted. Will try and update the answer tomo without fail. If not, you can leave a comment here. $\endgroup$
    – The Great
    Jan 3, 2020 at 14:59
  • $\begingroup$ Hi, how do I get the confidence interval from your sample code? $\endgroup$
    – The Great
    Jan 4, 2020 at 5:52
  • $\begingroup$ Running rr.summary() will print all the different measures calculated to the console. To access those items, you can use rr.results. The lower CL is rr.results['RR_LCL'] and the upper is rr.results['RR_UCL'] $\endgroup$
    – pzivich
    Jan 4, 2020 at 13:47
  • 1
    $\begingroup$ @pzivich hey, could you check this post ? stackoverflow.com/q/70778082/5893454 I'm using your package to deal with risk ratios, but with no success (yet). Thank you! $\endgroup$
    – Luis
    Jan 19, 2022 at 23:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.