bayesplain.compare_rates¶
-
bayesplain.compare_rates(events, exposure=
(1.0, 1.0), prior='jeffreys', labels=None, estimand='ratio', unit='', n_draws=None, seed='unset')[source]¶ Compare the rate of events between two groups.
The rate ratio has an exact posterior – a scaled beta-prime distribution – so the headline answer involves no sampling. The difference in rates is drawn from the two exact Gamma posteriors and subtracted.
- Parameters:¶
- events : array_like¶
Two event counts,
[y1, y2].- exposure : array_like, default (1.0, 1.0)¶
Two exposures,
[t1, t2], in the same units. Unequal exposure is the whole reason to compare rates rather than counts: 40 crashes in ten years is a lower rate than 12 in two.- prior : str, tuple, or RatePrior, default 'jeffreys'¶
Prior applied to each group’s rate.
- labels : sequence of str, optional¶
Group names. Defaults to
('group 1', 'group 2').- estimand : {'ratio', 'difference'}, default 'ratio'¶
Which comparison to report, oriented as group 2 relative to group 1. The ratio is the conventional one for rates, and is exact.
- unit : str, optional¶
How to print a rate, e.g.
"per year". Used for the difference and for each group’s own rate.- n_draws : int, optional¶
Number of draws. Defaults to the package setting.
- seed : int or None, optional¶
Seed for the draws.
- Returns:¶
Call
.summary()for the full report.- Return type:¶
Examples
Pedestrian collisions before and after a road diet: 31 in the four years before, 9 in the two years since.
>>> import bayesplain as bp >>> res = bp.compare_rates( ... events=[31, 9], exposure=[4, 2], labels=["before", "after"], ... unit="per year", ... ) >>> round(res.point(), 2) 0.59 >>> round(res.probability("<", 1.0), 3) 0.931