{"id":18555382,"url":"https://github.com/bearloga/bcda","last_synced_at":"2025-04-09T23:32:14.847Z","repository":{"id":54671594,"uuid":"55538190","full_name":"bearloga/BCDA","owner":"bearloga","description":"This R package is a set of tools for Bayesian analysis of categorical data, specifically 2×2 contingency tables.","archived":false,"fork":false,"pushed_at":"2021-02-04T15:49:27.000Z","size":350,"stargazers_count":6,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T13:44:13.539Z","etag":null,"topics":["bayesian-data-analysis","bayesian-inference","r","r-package","rstats"],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bearloga.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-05T19:13:05.000Z","updated_at":"2021-02-04T15:49:26.000Z","dependencies_parsed_at":"2022-08-13T23:30:46.915Z","dependency_job_id":null,"html_url":"https://github.com/bearloga/BCDA","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bearloga%2FBCDA","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bearloga%2FBCDA/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bearloga%2FBCDA/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bearloga%2FBCDA/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bearloga","download_url":"https://codeload.github.com/bearloga/BCDA/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248129916,"owners_count":21052661,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bayesian-data-analysis","bayesian-inference","r","r-package","rstats"],"created_at":"2024-11-06T21:26:23.458Z","updated_at":"2025-04-09T23:32:13.375Z","avatar_url":"https://github.com/bearloga.png","language":"R","readme":"---\ntitle: Bayesian Categorical Data Analysis\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r setup, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\noptions(digits = 2)\nset.seed(0)\nggplot2::theme_set(ggplot2::theme_minimal(base_size = 12))\n```\n\n# BCDA\n\nThis is a set of tools for Bayesian analysis of categorical data, specifically 2×2 contingency tables.\n\nUse:\n\n- `beta_binom()` for analysis using the Beta-Binomial model\n  - `print()`, `tidy()`, `glance()`, and `plot()` to view the results\n  - `present_bbfit()` if you want a nicely formatted table of summaries to include in a presentation or report\n  - `update()` if you have additional data\n- `est_multinom()` for estimating multinomial cell probabilities\n\n## Installation\n\nYou can install the development version from [GitHub](https://github.com/) with:\n\n``` r\n# install.packages(\"remotes\")\nremotes::install_github(\"bearloga/BCDA\")\n```\n## Example\n\n`beta_binom()` works with a table of the following format:\n\n|         | Success | Failure |\n|--------:|:-------:|:-------:|\n| Group 1 |   n11   |   n12   |\n| Group 2 |   n21   |   n22   |\n\nand uses the Beta-Binomial model to estimate:\n\n|         | Success | Failure |\n|--------:|:-------:|:-------:|\n| Group 1 |    p1   |  1 - p1 |\n| Group 2 |    p2   |  1 - p2 |\n\nwhich allows inference on the difference (`p1 - p2`), the risk ratio (RR), and the odds ratio (OR), defined as follows:\n\n- `RR = p1 / p2` -- how much more likely success in group 1 is, relative to success in group 2\n- `OR = (p1/(1-p1)) / (p2/(1-p2))` -- the ratio of the odds -- where `p1/(1-p1)` is the odds of success in group 1 and `p2/(1-p2)` is the odds of success in group 2\n\nAll examples will use the following fake data:\n\n```{r fake-data}\nfake_data \u003c- matrix(c(200, 150, 250, 300), nrow = 2, byrow = TRUE)\ncolnames(fake_data) \u003c- c('Safe' ,'Dangerous')\nrownames(fake_data) \u003c- c('Animals', 'Plants')\n```\n```{r, echo=FALSE}\nknitr::kable(fake_data)\n```\n\n**Note** that `beta_binom()` uses the Jeffreys prior by default and that this README was knit with the seed set to 0 at the start for reproducibility.\n\n```{r example-initial}\nlibrary(BCDA)\n\n(fit \u003c- beta_binom(fake_data))\n```\n\nThe credible intervals above are calculated using quantiles. If we have the **coda** package installed, we can also obtain the high posterior density intervals:\n\n```{r highest-posterior-density}\nprint(fit, interval_type = \"HPD\")\n```\n\n```{r plotting-params, fig.height=4, fig.width=8, dpi=72, fig.cap='Preview of visualization of the posterior draws.', fig.path='figures/'}\nplot(fit)\n```\n\n## Presentation of the results\n\nThe package includes a variety of functions for looking at the results from fitting a `beta_binom()` model. To aid in functional programming, we implemented the `tidy()` and `glance()` verbs:\n\n```{r tidy-output, results='asis'}\nlibrary(magrittr) # for %\u003e%\nfit %\u003e%\n  tidy %\u003e%\n  knitr::kable()\n```\n\nWhen knitting R Markdown to HTML, you can use the [{gt}](https://gt.rstudio.com/) package for creating tables:\n\n```{r tidy-output-gt, eval=FALSE}\nlibrary(gt)\nfit %\u003e%\n  tidy %\u003e%\n  gt(rowname_col = \"term\") %\u003e%\n  tab_header(md(\"Results of `beta_binom()`\"))\n```\n\n`glance()` is used to\n\n\u003e Construct a single row summary \"glance\" of a model, fit, or other object\n\n```{r glance_output}\nfit %\u003e%\n  glance\n```\n\nThis is perfectly okay in an interactive data analysis scenario, but not when presenting the results in a report. `glance()` is actually a special case of the `present_bbfit()` function which generates all those nicely formatted credible intervals but outputs a Markdown/LaTeX-formatted table by default:\n\n```{r, results='asis'}\npresent_bbfit(fit)\n```\n\nThe point estimates include credible intervals by default but these can be turned off:\n\n```{r, results='asis'}\npresent_bbfit(fit, conf_interval = FALSE, digits = 3)\n```\n\nSince the underlying code uses `tidy()` to compute the summaries, we can specify a particular credible level and the type of interval we want (e.g. highest posterior density):\n\n```{r, results='asis'}\npresent_bbfit(fit, conf_level = 0.89, interval_type = \"HPD\")\n```\n\nIt also supports multiple models, which can be provided as a named or an unnamed list. See the example below.\n\n## Updating the posterior\n\nIn Bayesian statistics, we can reuse a previously computed posterior as a prior if we have additional data, allowing us to update the parameter estimates as new data becomes available. Suppose we collect 40 observations from 2 groups (20 per group) on the first day of the A/B test, and 10 observations per day for the next 2 weeks. Here we see what happens when we update the posterior with additional data on a daily basis:\n\n![](updating.gif)\n\n### Example Code\n\n```{r updating_posterior, results='asis'}\nfit_2 \u003c- update(fit, x = c(100, 200), n = c(400, 600))\npresent_bbfit(list(\"Day 1\" = fit, \"Day 2\" = fit_2))\n```\n\n## See also\n\nOther packages for Bayesian analysis of A/B tests include: [LearnBayes](https://cran.r-project.org/web/packages/LearnBayes/index.html) (GPL), [conting](https://cran.r-project.org/web/packages/conting/index.html) (GPL), [bandit](https://cran.r-project.org/web/packages/bandit/index.html) (GPL), [testr](https://github.com/ayakubovich/testr) (MIT).\n\n---------\n\nPlease note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbearloga%2Fbcda","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbearloga%2Fbcda","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbearloga%2Fbcda/lists"}