odds_ratio¶
- mpsci.stats.odds_ratio(table, kind='conditional', alternative='two-sided')¶
Compute the odds ratio for a 2x2 contingency table.
- Parameters:
table (array_like of ints) – A 2x2 contingency table. Elements must be non-negative integers.
kind (str, optional) – Which kind of odds ratio to compute, either the sample odds ratio (
kind='sample'
) or the conditional odds ratio (kind='conditional'
). Default is'conditional'
.alternative ({'two-sided', 'less', 'greater'}, optional) –
Defines the alternative hypothesis. The following options are available (default is ‘two-sided’):
’two-sided’
’less’: one-sided
’greater’: one-sided
- Returns:
result – The returned object has two computed attributes:
- odds_ratiompmath.mpf
If kind is
'sample'
, this istable[0, 0]*table[1, 1]/(table[0, 1]*table[1, 0])
. This is the prior odds ratio and not a posterior estimate.If kind is
'conditional'
, this is the conditional maximum likelihood estimate for the odds ratio. It is the noncentrality parameter of Fisher’s noncentral hypergeometric distribution with the same hypergeometric parameters as table and whose mean istable[0, 0]
. See [1] and [2].
- pvaluefractions.Fraction or mpmath.mpf
The p-value associated with the computed odds ratio.
If kind is
'sample'
, the p-value is based on the normal approximation to the distribution of the log of the sample odds ratio.If kind is
'conditional'
, the p-value is computed bympsci.stats.fisher_exact()
.
The object also stores the input arguments table, kind and alternative as attributes.
The object has the method odds_ratio_ci that computes the confidence interval of the odds ratio.
- Return type:
OddsRatioResult instance
References
Examples
>>> from mpsci.stats import odds_ratio >>> from mpmath import mp >>> mp.dps = 25
>>> table = [[25, 28], [124, 19]] >>> result = odds_ratio(table, kind='conditional', alternative='two-sided') >>> result.odds_ratio >>> mpf('0.1386439288429951782006148315') >>> result.odds_ratio_ci(0.95) ConfidenceInterval(low=mpf('0.06223766231159558258257537936'), high=mpf('0.3002430126764577947401324812')) >>> float(result.pvalue) 7.14599142505505e-08