An unofficial Python version of the RealRisk calculator, allowing Python scripts to automate the conversion of statistical risks to expected frequencies and plotting icon-arrays (isotype-grids) using an API.
Odds ratios and hazard ratios may be comfortable measures of association to obtain,
but they are not the easiest to interpret.
Absolute risks are much easier to communicate to a general audience,
and expected frequencies are a clearer way to present risks.
Once we convert an odds-ratio of some desired risk-factor to absolute risk,
we can ask how many people we expect to see in a hypothetical population of size N (usually 100).
Working with absolute risks allows us to see how many people
without the risk-factor we expect to see in that population,
and how many individuals will be added with the risk-factor.
This formulation enables a clear visualization of the risk in a population
in the form of an isotype grid:
In order to calculate expected frequencies you'll need:
- A statistical measure of association. One of:
- Odds ratio (OR) - usually obtained by logistic regression
- Hazard ratio (HR) - usually obtained by CoxPH regression
- Risk ratio (RR) - also referred to as relative risk / rate ratio
- Percentage change
- Baseline risk.
the fraction of individuals without the risk factor (i.e. in the control group) that experienced the event (outcome) of interest.
The minimum required to calculate risk as expected frequencies is:
from expected_frequencies import expected_frequencies
baseline_risk = 10.2 / 100
odds_ratio = 5.21
result = expected_frequencies(baseline_risk, odds_ratio, "odds_ratio")
Which returns a Result
object that includes:
- Expected frequencies of the baseline (without risk-factor), and the exposed (with risk-factor) groups
- An isotype-grid chart
- A textual phrasing describing the results
There are two separate functions for generating only the plot or the textual phrasing:
from expected_frequencies import plot_expected_frequencies, phrase_expected_frequencies
To generate the chart above run:
from expected_frequencies import expected_frequencies
baseline_risk = 10.2 / 100
odds_ratio = 5.21
result = expected_frequencies(
baseline_risk, odds_ratio, "odds_ratio",
population_name="hospitalized patients",
event_name="ARDS",
risk_factor_name="test positive for SARS-CoV-2",
plot_kwargs={"chart_width": 450, "chart_height": 380},
plot_text=True
)
result.chart.show()
A more technical method to display multiple risk factors is with a forest plot.
Here, they can be displayed with additional text
Or as multi-panel and/or color-grouping plot
The package is dependent on:
- Altair >= 4.1.0 (may also work with previous versions, but not tested)