{ "cells": [ { "cell_type": "markdown", "id": "d068cb54", "metadata": {}, "source": [ "# Statistics and inference for one and two sample Poisson rates\n", "\n", "Author: Josef Perktold\n", "\n", "This notebook provides a brief overview of hypothesis tests, confidence intervals and other statistics for Poisson rates in one and two sample case. See docstrings for more options and additional details.\n", "\n", "All functions in `statsmodels.stats.rates` take summary statistics of the data as arguments. Those are counts of events and number of observations or total exposure. Some functions for Poisson have an option for excess dispersion. Functions for negative binomial, NB2, require the dispersion parameter. Excess dispersion and dispersion parameter need to be provided by the user and can be estimated from the original data with GLM-Poisson and discrete NegativeBinomial model, respectively.\n", "\n", "Note, some parts are still experimental and will likely change, some features are still missing and will be added in future versions.\n", "\n", "[One sample functions](#One-sample-functions) \n", "[Two sample functions](#Two-sample-functions)" ] }, { "cell_type": "code", "execution_count": 1, "id": "59f2450e", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:54.932005Z", "iopub.status.busy": "2024-04-19T16:39:54.931752Z", "iopub.status.idle": "2024-04-19T16:39:57.085693Z", "shell.execute_reply": "2024-04-19T16:39:57.085013Z" } }, "outputs": [], "source": [ "import numpy as np\n", "from numpy.testing import assert_allclose\n", "import statsmodels.stats.rates as smr\n", "from statsmodels.stats.rates import (\n", " # functions for 1 sample\n", " test_poisson,\n", " confint_poisson,\n", " tolerance_int_poisson,\n", " confint_quantile_poisson,\n", " \n", " # functions for 2 sample\n", " test_poisson_2indep,\n", " etest_poisson_2indep,\n", " confint_poisson_2indep,\n", " tost_poisson_2indep,\n", " nonequivalence_poisson_2indep,\n", " \n", " # power functions\n", " power_poisson_ratio_2indep,\n", " power_poisson_diff_2indep,\n", " power_equivalence_poisson_2indep,\n", " power_negbin_ratio_2indep,\n", " power_equivalence_neginb_2indep,\n", " \n", " # list of statistical methods\n", " method_names_poisson_1samp,\n", " method_names_poisson_2indep,\n", " )" ] }, { "cell_type": "markdown", "id": "cab009f6", "metadata": {}, "source": [ "## One sample functions\n", "\n", "The main functions for one sample Poisson rates currently are test_poisson and confint_poisson. Both have several methods available, most of them are consistent between hypothesis test and confidence interval. Two additional functions are available for tolerance intervals and for confidence intervals of quantiles. \n", "See docstrings for details." ] }, { "cell_type": "code", "execution_count": 2, "id": "199276f5", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.089165Z", "iopub.status.busy": "2024-04-19T16:39:57.088514Z", "iopub.status.idle": "2024-04-19T16:39:57.107076Z", "shell.execute_reply": "2024-04-19T16:39:57.106512Z" } }, "outputs": [ { "data": { "text/plain": [ "0.11655577679568745" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count1, n1 = 60, 514.775\n", "count1 / n1" ] }, { "cell_type": "code", "execution_count": 3, "id": "c57f1dfa", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.109783Z", "iopub.status.busy": "2024-04-19T16:39:57.109363Z", "iopub.status.idle": "2024-04-19T16:39:57.121513Z", "shell.execute_reply": "2024-04-19T16:39:57.120971Z" } }, "outputs": [ { "data": { "text/plain": [ "\n", "statistic = nan\n", "pvalue = 0.23913820865664664\n", "distribution = 'Poisson'\n", "method = 'midp-c'\n", "alternative = 'two-sided'\n", "rate = 0.11655577679568745\n", "nobs = 514.775\n", "tuple = (nan, 0.23913820865664664)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_poisson(count1, n1, value=0.1, method=\"midp-c\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "ae000bb9", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.124204Z", "iopub.status.busy": "2024-04-19T16:39:57.123782Z", "iopub.status.idle": "2024-04-19T16:39:57.227715Z", "shell.execute_reply": "2024-04-19T16:39:57.227113Z" } }, "outputs": [ { "data": { "text/plain": [ "(0.0897357524941493, 0.1490015282355224)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "confint_poisson(count1, n1, method=\"midp-c\")" ] }, { "cell_type": "markdown", "id": "d27cff72", "metadata": {}, "source": [ "The available methods for hypothesis tests and confidence interval are available in the dictionary `method_names_poisson_1samp`. See docstring for details." ] }, { "cell_type": "code", "execution_count": 5, "id": "f5b89f88", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.235266Z", "iopub.status.busy": "2024-04-19T16:39:57.234805Z", "iopub.status.idle": "2024-04-19T16:39:57.384620Z", "shell.execute_reply": "2024-04-19T16:39:57.383998Z" } }, "outputs": [ { "data": { "text/plain": [ "{'test': ['wald',\n", " 'score',\n", " 'exact-c',\n", " 'midp-c',\n", " 'waldccv',\n", " 'sqrt-a',\n", " 'sqrt-v',\n", " 'sqrt'],\n", " 'confint': ['wald',\n", " 'score',\n", " 'exact-c',\n", " 'midp-c',\n", " 'jeff',\n", " 'waldccv',\n", " 'sqrt-a',\n", " 'sqrt-v',\n", " 'sqrt',\n", " 'sqrt-cent',\n", " 'sqrt-centcc']}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "method_names_poisson_1samp" ] }, { "cell_type": "code", "execution_count": 6, "id": "ac9d09a9", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.391495Z", "iopub.status.busy": "2024-04-19T16:39:57.390786Z", "iopub.status.idle": "2024-04-19T16:39:57.406709Z", "shell.execute_reply": "2024-04-19T16:39:57.406134Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wald 0.2712232025335152\n", "score 0.23489608509894766\n", "exact-c 0.2654698417416039\n", "midp-c 0.23913820865664664\n", "waldccv 0.27321266612309003\n", "sqrt-a 0.25489746088635834\n", "sqrt-v 0.2281700763432699\n", "sqrt 0.2533006997208508\n" ] } ], "source": [ "for meth in method_names_poisson_1samp[\"test\"]:\n", " tst = test_poisson(count1, n1, method=meth, value=0.1,\n", " alternative='two-sided')\n", " print(\"%-12s\" % meth, tst.pvalue)" ] }, { "cell_type": "code", "execution_count": 7, "id": "8f224485", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.409601Z", "iopub.status.busy": "2024-04-19T16:39:57.409243Z", "iopub.status.idle": "2024-04-19T16:39:57.524721Z", "shell.execute_reply": "2024-04-19T16:39:57.524151Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wald (0.08706363801159746, 0.14604791557977745)\n", "score (0.0905597500576385, 0.15001420714831387)\n", "exact-c (0.08894433674907924, 0.15003038882355074)\n", "midp-c (0.0897357524941493, 0.1490015282355224)\n", "jeff (0.08979284758964944, 0.14893677466593855)\n", "waldccv (0.08694100904696915, 0.14617054454440576)\n", "sqrt-a (0.08883721953786133, 0.14800553586080228)\n", "sqrt-v (0.08975547672311084, 0.14897854470462502)\n", "sqrt (0.08892923891524183, 0.14791351648342183)\n", "sqrt-cent (0.08883721953786133, 0.1480055358608023)\n", "sqrt-centcc (0.0879886777703761, 0.1490990831089978)\n" ] } ], "source": [ "for meth in method_names_poisson_1samp[\"confint\"]:\n", " tst = confint_poisson(count1, n1, method=meth)\n", " print(\"%-12s\" % meth, tst)" ] }, { "cell_type": "markdown", "id": "31a7f349", "metadata": {}, "source": [ "Two additional functions are currently available for one sample poisson rates, `tolerance_int_poisson` for tolerance intervals and `confint_quantile_poisson` for confidence intervals of Poisson quantiles. \n", "\n", "Tolerance intervals are similar to prediction intervals that combine the randomness of a new observation and uncertainty about the estimated Poisson rate. If the rate were known, then we can compute a Poisson interval for a new observation using the inverse cdf at the given rate. The tolerance interval adds uncertainty about the rate by using the confidence interval for the rate estimate.\n", "\n", "A tolerance interval is specified by two probabilities, `prob` is the coverage of the Poisson interval, `alpha` is the confidence level for the confidence interval of the rate estimate. \n", "Note, that probabilities cannot be exactly equal to the nominal probabilites because counts are discrete random variables. The properties of the intervals are specified in term of inequalities, coverage is at least `prob`, coverage of the confidence interval of the estimated rate is at least `1 - alpha`. However, most methods will not guarantee that the coverage inequalities hold in small samples even if the distribution is correctly specified.\n", "\n", "In the following example, we can expect to observe between 4 and 23 events if the total exposure or number of observations is 100, at given coverage `prob` and confidence level `alpha`. The tolerance interval is larger than the Poisson interval at the observed rate, (5, 19), because the tolerance interval takes uncertainty about the parameter estimate into account." ] }, { "cell_type": "code", "execution_count": 8, "id": "05f9882e", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.529553Z", "iopub.status.busy": "2024-04-19T16:39:57.528050Z", "iopub.status.idle": "2024-04-19T16:39:57.545723Z", "shell.execute_reply": "2024-04-19T16:39:57.545145Z" } }, "outputs": [ { "data": { "text/plain": [ "(4.0, 23.0)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exposure_new = 100\n", "tolerance_int_poisson(count1, n1, prob=0.95, exposure_new=exposure_new, method=\"score\", alpha=0.05, alternative='two-sided')" ] }, { "cell_type": "code", "execution_count": 9, "id": "97342d26", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.550496Z", "iopub.status.busy": "2024-04-19T16:39:57.548987Z", "iopub.status.idle": "2024-04-19T16:39:57.558690Z", "shell.execute_reply": "2024-04-19T16:39:57.558086Z" } }, "outputs": [ { "data": { "text/plain": [ "(5.0, 19.0)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from scipy import stats\n", "stats.poisson.interval(0.95, count1 / n1 * exposure_new)" ] }, { "cell_type": "markdown", "id": "86de470d", "metadata": {}, "source": [ "Aside: We can force the tolerance interval to ignore parameter uncertainty by specifying `alpha=1`." ] }, { "cell_type": "code", "execution_count": 10, "id": "45696c02", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.563439Z", "iopub.status.busy": "2024-04-19T16:39:57.561995Z", "iopub.status.idle": "2024-04-19T16:39:57.577366Z", "shell.execute_reply": "2024-04-19T16:39:57.576779Z" } }, "outputs": [ { "data": { "text/plain": [ "(5.0, 19.0)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tolerance_int_poisson(count1, n1, prob=0.95, exposure_new=exposure_new, method=\"score\", alpha=1, alternative='two-sided')" ] }, { "cell_type": "markdown", "id": "d6e62fdf", "metadata": {}, "source": [ "The last function returns a confidence interval for a Poisson quantile. A quantile is the inverse of the cdf function, named `ppf` in scipy.stats distributions.\n", "\n", "The following example shows the confidence interval for the upper bound of the Poisson interval at cdf probability 0.975. The upper confidence limit using the one-tail coverage probability is the same as the upper limit of the tolerance interval." ] }, { "cell_type": "code", "execution_count": 11, "id": "1a7df495", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.581427Z", "iopub.status.busy": "2024-04-19T16:39:57.580385Z", "iopub.status.idle": "2024-04-19T16:39:57.594356Z", "shell.execute_reply": "2024-04-19T16:39:57.593752Z" } }, "outputs": [ { "data": { "text/plain": [ "(15.0, 23.0)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "confint_quantile_poisson(count1, n1, prob=0.975, exposure_new=100, method=\"score\", alpha=0.05, alternative='two-sided')" ] }, { "cell_type": "markdown", "id": "f5107acc", "metadata": {}, "source": [ "## Two sample functions\n", "\n", "Statistical function for two samples can compare the rates by either the ratio or the difference. Default is comparing the rates ratio.\n", "\n", "The `etest` functions can be directly accessed through `test_poisson_2indep`." ] }, { "cell_type": "code", "execution_count": 12, "id": "d6c7d341", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.603793Z", "iopub.status.busy": "2024-04-19T16:39:57.597361Z", "iopub.status.idle": "2024-04-19T16:39:57.608348Z", "shell.execute_reply": "2024-04-19T16:39:57.607799Z" } }, "outputs": [], "source": [ "count1, n1, count2, n2 = 60, 514.775, 30, 543.087" ] }, { "cell_type": "code", "execution_count": 13, "id": "5aba830a", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.613139Z", "iopub.status.busy": "2024-04-19T16:39:57.611571Z", "iopub.status.idle": "2024-04-19T16:39:57.622481Z", "shell.execute_reply": "2024-04-19T16:39:57.621896Z" } }, "outputs": [ { "data": { "text/plain": [ "\n", "statistic = 3.4174018390002145\n", "pvalue = 0.0005672617581628009\n", "distribution = 'poisson'\n", "compare = 'ratio'\n", "method = 'etest-score'\n", "alternative = 'two-sided'\n", "rates = (0.11655577679568745, 0.055239768213932575)\n", "ratio = 2.10999757175465\n", "diff = 0.06131600858175487\n", "value = 1\n", "rates_cmle = None\n", "ratio_null = 1\n", "tuple = (3.4174018390002145, 0.0005672617581628009)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_poisson_2indep(count1, n1, count2, n2, method='etest-score')" ] }, { "cell_type": "code", "execution_count": 14, "id": "52d1cd76", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.627198Z", "iopub.status.busy": "2024-04-19T16:39:57.625665Z", "iopub.status.idle": "2024-04-19T16:39:57.685081Z", "shell.execute_reply": "2024-04-19T16:39:57.684516Z" } }, "outputs": [ { "data": { "text/plain": [ "(1.3659624311981189, 3.2593061483872257)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "confint_poisson_2indep(count1, n1, count2, n2, method='score',\n", " compare=\"ratio\")" ] }, { "cell_type": "code", "execution_count": 15, "id": "3642594e", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.689824Z", "iopub.status.busy": "2024-04-19T16:39:57.688381Z", "iopub.status.idle": "2024-04-19T16:39:57.736645Z", "shell.execute_reply": "2024-04-19T16:39:57.736069Z" } }, "outputs": [ { "data": { "text/plain": [ "(0.026579645509259224, 0.0989192191413259)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "confint_poisson_2indep(count1, n1, count2, n2, method='score',\n", " compare=\"diff\")" ] }, { "cell_type": "markdown", "id": "b30dfd37", "metadata": {}, "source": [ "The two sample test function, `test_poisson_2indep`, has a `value` option to specify null hypothesis that do not specify equality. This is useful for superiority and noninferiority testing with one-sided alternatives.\n", "\n", "As an example, the following test tests the two-sided null hypothesis that the rates ratio is 2. The pvalue for this hypothesis is 0.81 and we cannot reject that the first rate is twice the second rate." ] }, { "cell_type": "code", "execution_count": 16, "id": "85046eb5", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.747522Z", "iopub.status.busy": "2024-04-19T16:39:57.746444Z", "iopub.status.idle": "2024-04-19T16:39:57.753972Z", "shell.execute_reply": "2024-04-19T16:39:57.753413Z" } }, "outputs": [ { "data": { "text/plain": [ "\n", "statistic = 0.23946504079843253\n", "pvalue = 0.813504857205675\n", "distribution = 'poisson'\n", "compare = 'ratio'\n", "method = 'etest-score'\n", "alternative = 'two-sided'\n", "rates = (0.11655577679568745, 0.055239768213932575)\n", "ratio = 2.10999757175465\n", "diff = 0.06131600858175487\n", "value = 2\n", "rates_cmle = None\n", "ratio_null = 2\n", "tuple = (0.23946504079843253, 0.813504857205675)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_poisson_2indep(count1, n1, count2, n2, value=2, method='etest-score')" ] }, { "cell_type": "markdown", "id": "4831cf3f", "metadata": {}, "source": [ "The `method_names_poisson_2indep` dictionary shows which methods are available when comparing two samples by either rates ratio or rates difference.\n", "\n", "We can use the dictionary to compute p-values and confidence intervals using all available methods." ] }, { "cell_type": "code", "execution_count": 17, "id": "e6a6aec0", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.757980Z", "iopub.status.busy": "2024-04-19T16:39:57.756907Z", "iopub.status.idle": "2024-04-19T16:39:57.778748Z", "shell.execute_reply": "2024-04-19T16:39:57.778176Z" } }, "outputs": [ { "data": { "text/plain": [ "{'test': {'ratio': ['wald',\n", " 'score',\n", " 'score-log',\n", " 'wald-log',\n", " 'exact-cond',\n", " 'cond-midp',\n", " 'sqrt',\n", " 'etest-score',\n", " 'etest-wald'],\n", " 'diff': ['wald', 'score', 'waldccv', 'etest-score', 'etest-wald']},\n", " 'confint': {'ratio': ['waldcc',\n", " 'score',\n", " 'score-log',\n", " 'wald-log',\n", " 'sqrtcc',\n", " 'mover'],\n", " 'diff': ['wald', 'score', 'waldccv', 'mover']}}" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "method_names_poisson_2indep" ] }, { "cell_type": "code", "execution_count": 18, "id": "302b0bf4", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.782722Z", "iopub.status.busy": "2024-04-19T16:39:57.781606Z", "iopub.status.idle": "2024-04-19T16:39:57.810732Z", "shell.execute_reply": "2024-04-19T16:39:57.810138Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ratio\n", " wald 0.0007120093285061108\n", " score 0.0006322188820470972\n", " score-log 0.0003992519661848979\n", " wald-log 0.0008399438093390379\n", " exact-cond 0.0006751826586863219\n", " cond-midp 0.0005572624066190538\n", " sqrt 0.0005700355621795108\n", " etest-score 0.0005672617581628009\n", " etest-wald 0.0006431446124897875\n", "diff\n", " wald 0.0007120093285061094\n", " score 0.0006322188820470944\n", " waldccv 0.0007610462660136599\n", " etest-score 0.000567261758162795\n", " etest-wald 0.0006431446124897808\n" ] } ], "source": [ "for compare in [\"ratio\", \"diff\"]:\n", " print(compare)\n", " for meth in method_names_poisson_2indep[\"test\"][compare]:\n", " tst = test_poisson_2indep(count1, n1, count2, n2, value=None,\n", " method=meth, compare=compare,\n", " alternative='two-sided')\n", " print(\" %-12s\" % meth, tst.pvalue)" ] }, { "cell_type": "markdown", "id": "b306fa76", "metadata": {}, "source": [ "In a similar way we can compute confidence intervals for the rate ratio and rate difference for all currently available methods." ] }, { "cell_type": "code", "execution_count": 19, "id": "e2658911", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.814780Z", "iopub.status.busy": "2024-04-19T16:39:57.813715Z", "iopub.status.idle": "2024-04-19T16:39:57.970771Z", "shell.execute_reply": "2024-04-19T16:39:57.970138Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ratio\n", " waldcc (1.354190544703406, 3.233964238781885)\n", " score (1.3659624311981189, 3.2593061483872257)\n", " score-log (1.3903411228996467, 3.4348249508085043)\n", " wald-log (1.3612801263025065, 3.2705169691290763)\n", " sqrtcc (1.29635711135392, 3.132234781692197)\n", " mover (1.3614682485833316, 3.258622814678696)\n", "diff\n", " wald (0.02581223514639487, 0.09681978201711487)\n", " score (0.026579645509259224, 0.0989192191413259)\n", " waldccv (0.025618973109117968, 0.09701304405439178)\n", " mover (0.026193641039269785, 0.09864127183950336)\n" ] } ], "source": [ "for compare in [\"ratio\", \"diff\"]:\n", " print(compare)\n", " for meth in method_names_poisson_2indep[\"confint\"][compare]:\n", " ci = confint_poisson_2indep(count1, n1, count2, n2,\n", " method=meth, compare=compare)\n", " print(\" %-12s\" % meth, ci)" ] }, { "cell_type": "markdown", "id": "65ce8685", "metadata": {}, "source": [ "We have two additional functions for hypothesis tests that specify interval hypothesis, \n", "`tost_poisson_2indep` and `nonequivalence_poisson_2indep`.\n", "\n", "The `TOST` function implements equivalence tests where the alternative hypothesis specifies that the two rates are within an interval of each other.\n", "\n", "The `nonequivalence` tests implements a test where the alternative specifies that the two rates differ by at least a given nonzero value. This is also often called a minimum effect test. This test uses two one-sided tests similar to TOST however with null and alternative hypothesis reversed compared to the equivalence test.\n", "\n", "Both functions delegate to `test_poisson_2indep` and, therefore, the same `method` options are available." ] }, { "cell_type": "markdown", "id": "74e1de07", "metadata": {}, "source": [ "The following equivalence test specifies the alternative hypothesis that the rate ratio is between 0.8 and 1/0.8. The observed rate ratio is 0.89. The pvalue is 0.107 and we cannot reject the null hypothesis in favor of the alternative hypothesis that the two rates are equivalent at the given margins. Thus the hypothesis test does not provide evidence that the two rates are equivalent.\n", "\n", "In the second example we test equivalence in the rate difference, where equivalence is defined by margins (-0.04, 0.04). The pvalue is around 0.2 and the test does not support that the two rates are equivalent." ] }, { "cell_type": "code", "execution_count": 20, "id": "8ee7a0ba", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.973932Z", "iopub.status.busy": "2024-04-19T16:39:57.973514Z", "iopub.status.idle": "2024-04-19T16:39:57.986756Z", "shell.execute_reply": "2024-04-19T16:39:57.986133Z" } }, "outputs": [ { "data": { "text/plain": [ "\n", "statistic = 1.2403473458920846\n", "pvalue = 0.10742347370282446\n", "method = 'score'\n", "compare = 'ratio'\n", "equiv_limits = (0.8, 1.25)\n", "results_larger = \n", " statistic = 1.2403473458920846\n", " pvalue = 0.10742347370282446\n", " distribution = 'normal'\n", " compare = 'ratio'\n", " method = 'score'\n", " alternative = 'larger'\n", " rates = (0.2, 0.225)\n", " ratio = 0.888888888888889\n", " diff = -0.024999999999999994\n", " value = 0.8\n", " rates_cmle = None\n", " ratio_null = 0.8\n", " tuple = (1.2403473458920846, 0.10742347370282446)\n", "results_smaller = \n", " statistic = -4.0311288741492755\n", " pvalue = 2.7754797240370253e-05\n", " distribution = 'normal'\n", " compare = 'ratio'\n", " method = 'score'\n", " alternative = 'smaller'\n", " rates = (0.2, 0.225)\n", " ratio = 0.888888888888889\n", " diff = -0.024999999999999994\n", " value = 1.25\n", " rates_cmle = None\n", " ratio_null = 1.25\n", " tuple = (-4.0311288741492755, 2.7754797240370253e-05)\n", "title = 'Equivalence test for 2 independent Poisson rates'\n", "tuple = (1.2403473458920846, 0.10742347370282446)" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "low = 0.8\n", "upp = 1 / low\n", "\n", "count1, n1, count2, n2 = 200, 1000, 450, 2000\n", "\n", "tost_poisson_2indep(count1, n1, count2, n2, low, upp, method='score', compare='ratio')" ] }, { "cell_type": "code", "execution_count": 21, "id": "6d92005e", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:57.990775Z", "iopub.status.busy": "2024-04-19T16:39:57.989673Z", "iopub.status.idle": "2024-04-19T16:39:58.002707Z", "shell.execute_reply": "2024-04-19T16:39:58.002135Z" } }, "outputs": [ { "data": { "text/plain": [ "\n", "statistic = 0.8575203124598336\n", "pvalue = 0.19557869693808477\n", "method = 'score'\n", "compare = 'diff'\n", "equiv_limits = (-0.04, 0.04)\n", "results_larger = \n", " statistic = 0.8575203124598336\n", " pvalue = 0.19557869693808477\n", " distribution = 'normal'\n", " compare = 'diff'\n", " method = 'score'\n", " alternative = 'larger'\n", " rates = (0.2, 0.225)\n", " ratio = 0.888888888888889\n", " diff = -0.024999999999999994\n", " value = -0.04\n", " rates_cmle = (0.19065363652113884, 0.23065363652113885)\n", " ratio_null = None\n", " tuple = (0.8575203124598336, 0.19557869693808477)\n", "results_smaller = \n", " statistic = -3.4807277010355238\n", " pvalue = 0.00025002679047994814\n", " distribution = 'normal'\n", " compare = 'diff'\n", " method = 'score'\n", " alternative = 'smaller'\n", " rates = (0.2, 0.225)\n", " ratio = 0.888888888888889\n", " diff = -0.024999999999999994\n", " value = 0.04\n", " rates_cmle = (0.24581855699051405, 0.20581855699051405)\n", " ratio_null = None\n", " tuple = (-3.4807277010355238, 0.00025002679047994814)\n", "title = 'Equivalence test for 2 independent Poisson rates'\n", "tuple = (0.8575203124598336, 0.19557869693808477)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "upp = 0.04\n", "low = -upp\n", "tost_poisson_2indep(count1, n1, count2, n2, low, upp, method='score', compare='diff')" ] }, { "cell_type": "markdown", "id": "b7329c90", "metadata": {}, "source": [ "The function `nonequivalence_poisson_2indep` tests the alternative hypothesis that the two rates differ by a non-neglibile amount.\n", "\n", "In the following example, the alternative hypothesis specifies that the rate ratio is outside the interval (0.95, 1/0.95). The null hypothesis is that the ratio ratio is in the interval. If the test rejects the null hypothesis, then it provides evidence that the rate ratio differ by more than the unimportant amount specified by the interval limits.\n", "\n", "A note on the relationship between point hypothesis test and interval hypothesis test in large samples. The point null hypothesis of test_poisson_2indep will reject any small deviation from the null hypothesis if the null hypothesis does not hold exactly and the sample size is large enough. The nonequivalence or minimum effect test will not reject the null hypothesis in large samples (sample approaches infinite) if rates differ by not more than the specified neglibible amount.\n", "\n", "In the example neither the point nor the interval null hypothesis are rejected. We do not have enough evidence to say that the rates are statistically different.\n", "Following that, we increase the sample size 20 times while keeping observed rates constant. In this case, the point null hypothesis test is rejected, the pvalue is 0.01, while the interval null hypothesis is not rejected, the pvalue is equal to 1.\n", "\n", "Note: The nonequivalence test is in general conservative, its size is bounded by alpha, but in the large sample limit with fixed nonequivalence margins the size approaches alpha / 2. If the nonequivalence interval shrinks to a single point, then the nonequivalence test is the same as the point hypothesis test. (see docstring)" ] }, { "cell_type": "code", "execution_count": 22, "id": "32ad0255", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:58.006728Z", "iopub.status.busy": "2024-04-19T16:39:58.005652Z", "iopub.status.idle": "2024-04-19T16:39:58.018478Z", "shell.execute_reply": "2024-04-19T16:39:58.017861Z" } }, "outputs": [ { "data": { "text/plain": [ "\n", "statistic = -1.1654330934961301\n", "pvalue = 1.0232437381644721\n", "method = 'score'\n", "results_larger = \n", " statistic = 0.02913582733740325\n", " pvalue = 0.5116218690822361\n", " distribution = 'normal'\n", " compare = 'ratio'\n", " method = 'score'\n", " alternative = 'smaller'\n", " rates = (0.2, 0.21)\n", " ratio = 0.9523809523809524\n", " diff = -0.009999999999999981\n", " value = 0.95\n", " rates_cmle = None\n", " ratio_null = 0.95\n", " tuple = (0.02913582733740325, 0.5116218690822361)\n", "results_smaller = \n", " statistic = -1.1654330934961301\n", " pvalue = 0.8780781359377093\n", " distribution = 'normal'\n", " compare = 'ratio'\n", " method = 'score'\n", " alternative = 'larger'\n", " rates = (0.2, 0.21)\n", " ratio = 0.9523809523809524\n", " diff = -0.009999999999999981\n", " value = 1.0526315789473684\n", " rates_cmle = None\n", " ratio_null = 1.0526315789473684\n", " tuple = (-1.1654330934961301, 0.8780781359377093)\n", "title = 'Equivalence test for 2 independent Poisson rates'\n", "tuple = (-1.1654330934961301, 1.0232437381644721)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count1, n1, count2, n2 = 200, 1000, 420, 2000\n", "low = 0.95\n", "upp = 1 / low\n", "nf = 1\n", "nonequivalence_poisson_2indep(count1 * nf, n1 * nf, count2 * nf, n2 * nf, low, upp, method='score', compare='ratio')" ] }, { "cell_type": "code", "execution_count": 23, "id": "61beae8f", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:58.022531Z", "iopub.status.busy": "2024-04-19T16:39:58.021463Z", "iopub.status.idle": "2024-04-19T16:39:58.042740Z", "shell.execute_reply": "2024-04-19T16:39:58.042134Z" } }, "outputs": [ { "data": { "text/plain": [ "\n", "statistic = -0.5679618342470648\n", "pvalue = 0.5700608835629815\n", "distribution = 'normal'\n", "compare = 'ratio'\n", "method = 'score'\n", "alternative = 'two-sided'\n", "rates = (0.2, 0.21)\n", "ratio = 0.9523809523809524\n", "diff = -0.009999999999999981\n", "value = 1\n", "rates_cmle = None\n", "ratio_null = 1\n", "tuple = (-0.5679618342470648, 0.5700608835629815)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_poisson_2indep(count1 * nf, n1 * nf, count2 * nf, n2 * nf, method='score', compare='ratio')" ] }, { "cell_type": "code", "execution_count": 24, "id": "62b98f47", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:58.046793Z", "iopub.status.busy": "2024-04-19T16:39:58.045698Z", "iopub.status.idle": "2024-04-19T16:39:58.058699Z", "shell.execute_reply": "2024-04-19T16:39:58.058132Z" } }, "outputs": [ { "data": { "text/plain": [ "1.1036704302254083" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nf = 20\n", "nonequivalence_poisson_2indep(count1 * nf, n1 * nf, count2 * nf, n2 * nf, low, upp, method='score', compare='ratio').pvalue" ] }, { "cell_type": "code", "execution_count": 25, "id": "212ca284", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:58.062671Z", "iopub.status.busy": "2024-04-19T16:39:58.061616Z", "iopub.status.idle": "2024-04-19T16:39:58.077339Z", "shell.execute_reply": "2024-04-19T16:39:58.076738Z" } }, "outputs": [ { "data": { "text/plain": [ "0.01108516638060269" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_poisson_2indep(count1 * nf, n1 * nf, count2 * nf, n2 * nf, method='score', compare='ratio').pvalue" ] }, { "cell_type": "markdown", "id": "0dc3f3bc", "metadata": {}, "source": [ "## Power\n", "\n", "Statsmodels has limited support for computing statistical power for the comparison of 2 sample Poisson and Negative Binomial rates. Those are based on Zhu and Lakkis and Zhu for ratio comparisons for both distributions, and basic normal based comparison for the Poisson rate difference. Other methods that correspond more closely to the available methods in the hypothesis test function, especially Gu, are not yet available.\n", "\n", "The available functions are" ] }, { "cell_type": "code", "execution_count": 26, "id": "2479efb8", "metadata": { "execution": { "iopub.execute_input": "2024-04-19T16:39:58.081350Z", "iopub.status.busy": "2024-04-19T16:39:58.080284Z", "iopub.status.idle": "2024-04-19T16:39:58.094696Z", "shell.execute_reply": "2024-04-19T16:39:58.094132Z" } }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "power_poisson_ratio_2indep\n", "power_equivalence_poisson_2indep\n", "power_negbin_ratio_2indep\n", "power_equivalence_neginb_2indep\n", " \n", "power_poisson_diff_2indep" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.14" } }, "nbformat": 4, "nbformat_minor": 5 }