{"id":27642242,"url":"https://github.com/m-py/bayesed","last_synced_at":"2025-07-02T03:06:28.005Z","repository":{"id":80958657,"uuid":"156735071","full_name":"m-Py/bayesEd","owner":"m-Py","description":"Bayes factor education: Understanding the Bayesian t-Test","archived":false,"fork":false,"pushed_at":"2023-02-23T11:30:08.000Z","size":696,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-23T23:53:49.499Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/m-Py.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-11-08T16:20:01.000Z","updated_at":"2023-05-14T17:14:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"e4c2dbaf-85c1-41d9-b4fb-f186af19d578","html_url":"https://github.com/m-Py/bayesEd","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/m-Py/bayesEd","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-Py%2FbayesEd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-Py%2FbayesEd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-Py%2FbayesEd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-Py%2FbayesEd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/m-Py","download_url":"https://codeload.github.com/m-Py/bayesEd/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m-Py%2FbayesEd/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263066557,"owners_count":23408387,"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":[],"created_at":"2025-04-23T23:53:48.757Z","updated_at":"2025-07-02T03:06:27.993Z","avatar_url":"https://github.com/m-Py.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\nauthor: Martin Papenberg\noutput: \n  md_document:\n    variant: markdown_github\nbibliography: ./man/lit.bib\n---\n\n```{r, echo = FALSE}\nknitr::opts_chunk$set(warning = FALSE)\n```\n\n# bayesEd\n\n`bayesEd` is an R-package that aims to help to understand the logic of\nBayes factors by visualization and by the example of the Bayesian t-test\nfor independent groups. Much of the code in the package was inspired by\na [blog post by Jeff Rouder](https://psyarxiv.com/nvsm5/)\nand extends it to easily usable functions. The code is primarily meant\nfor educational purposes; to actually compute Bayes factors, I encourage\nthe use of the `[`BayesFactor` package](https://richarddmorey.github.io/BayesFactor/)\n[@R-BayesFactor].\n\n# Installation\n\n```R\nlibrary(\"devtools\") # if not available: install.packages(\"devtools\")\ninstall_github(\"m-Py/bayesEd\")\n```\n\n```{r}\n# load the package via \nlibrary(\"bayesEd\")\n```\n\n# Visualize prior distributions\n\nBayes factors contrast the ability of hypotheses to predict observed\ndata. In the case of the t-test, two hypotheses -- a null hypothesis and\nan alternative hypothesis -- are contrasted in their ability to predict\nan observed t-value. In the context of Bayesian statistics, hypotheses\nare also often referred to by the term \"prior\". The first step in\nunderstanding Bayes factors is to understand the hypotheses/priors. In\nthe case of a Bayesian t-test, both hypotheses have assumptions about\nthe population effect size $d$ [@cohen1988]. The null hypothesis always\nassumes that $d = 0$, i.e., that there is no effect in the\npopulation. The default alternative hypothesis -- proposed to be used by\npsychologists by @rouder2009 -- assumes that there is an effect in the\npopulation. It postulates that the distribution of $d$ is described by a\nCauchy distribution. The following plot visualizes the Cauchy\ndistribution:\n\n```{r}\n## Draw default hypotheses:\nvisualize_prior()\n```\n\nThe function `visualize_prior` is the first function from the package\n`bayesEd` that we are using; it simply draws the hypotheses. I called\nthe function without specifying any arguments (see `?visualize_prior`\nfor a description of all parameters that can be passed). In this case\nthe function assumes a Cauchy alternative hypothesis by default. The\nshape of the Cauchy distribution is described by a scaling parameter\n$r$; it describes the proportion of probability that lies between $-r$\nand $r$. The default scaling parameter is the square root of 2 divided\nby 2 (≈ 0.71), which is the also the default setting in the package\n`BayesFactor`.  The following plot illustrates this scaling parameter:\n\n```{r}\nvisualize_prior()\nlines(x = rep(-sqrt(2)/2, 2), y = c(0, dcauchy(sqrt(2) / 2, scale = sqrt(2)/2)),\n      lwd = 3, lty = 2, col = \"darkgrey\")\nlines(x = rep(sqrt(2)/2, 2), y = c(0, dcauchy(sqrt(2) / 2, scale = sqrt(2)/2)),\n      lwd = 3, lty = 2, col = \"darkgrey\")\nlegend(\"topright\", legend = \"Scaling parameter r\", lty = 2, lwd = 3, col = \"darkgrey\",\n       bty = \"n\")\n```\n\nThus, the default alternative hypothesis is 50% confident that Cohen's\n$d$ lies between the values of -0.71 and 0.71. Larger effect sizes are\nassumed to be less likely. It is also possible to specify a different\nprior as the alternative hypothesis. In this case, we have to specify\nthe argument `alternative`; `alternative` is usually a function object\ndescribing our believe in the distribution of the population effect\nsize. For example, we may specify an alternative that describes the\npopulation effect size as a normal distribution with mean = 0.5 and *SD*\n= 0.3.\n\n```{r}\nnormal_alt \u003c- function(x) dnorm(x, mean = 0.5, sd = 0.3)\nvisualize_prior(alternative = normal_alt)\n```\n\nHere, we specify a \"subjective prior\" that a priori assumes a\ndirectional effect, i.e., that $d$ is most likely larger than 0. The\nalternative hypothesis is specified by the function `dnorm`, which is\nthe function for the probability density of a normal\ndistribution. Hypothesis objects will typically be a \"d\"-function, i.e.,\na probability density function like `dnorm`, `dcauchy` or `dt`.\n\nAs an additional example, the following illustrates a change of the\nscaling parameter in the Cauchy distribution. \n\n```{r}\ncauchy_narrow \u003c- function(x) dcauchy(x, scale = 0.4)\nvisualize_prior(alternative = cauchy_narrow)\n```\n\nHere, we assume that effect sizes are likely to be smaller -- 50% of\neffect sizes are assumed to be in the interval of [-0.4, 0.4] --, which\nmay be a realistic assumption in psychological research. Note that in\nany case, the Cauchy distribution assumes that very large effect sizes\nare more likely than a normal distribution would assume; the Cauchy\ndistribution has \"fat tails\".\n\n# Visualize predictions\n\nA statistical hypothesis makes predictions about the distribution of\nobserved data. When specifying a probability distribution of Cohen's\n$d$, we also create specific expectations about the distribution of\nobserved t-values. In the case of the null hypothesis, the distribution\nof t-values is described by the t-distribution with $n - 2$ degrees of\nfreedom, where $n$ is the total sample size:\n\n```{r}\nvisualize_predictions(alternative = NULL, n1 = 50, n2 = 50)\n```\n\nThe function `visualize_predictions` illustrates the predictions of\nhypotheses. Here, I only wanted to illustrate the predictions of the\nnull hypothesis and therefore, I set the argument `alternative` to\n`NULL`. Note that the predictions of a hypothesis do not only depend on\nthe prior, but also on the sample size. For this reason, I used the\nparameters `n1` and `n2` to specify two hypothetical group sizes of 50,\nyielding a total $n$ of 100.\n\nThe argument `alternative` works in the same way as for the function\n`visualize_priors`. Usually, it is a function object describing the\ndistribution of effect sizes according to the alternative\nhypothesis. Again, the default alternative is a Cauchy prior with\nscaling parameter ≈ 0.71. The following call illustrates the predictions\nof the Cauchy prior and the null hypothesis for a sample size of 100:\n\n```{r}\nvisualize_predictions(n1 = 50, n2 = 50)\n```\n\nWe can see that under the alternative hypothesis, larger t-values are\nincreasingly more likely than under the null hypothesis; according to\nthe null hypothesis, smaller t-values are relatively more likely. The\nBayes factor is the relative probability of an observed t-value under\nboth hypotheses. We can pass an observed t-value to\n`visualize_predictions` to obtain an illustration of the Bayes factor:\n\n```{r}\nvisualize_predictions(n1 = 50, n2 = 50, observed_t = 4)\n```\n\nIn this case, we assume that we have observed $t = 4$. The resulting\nBayes factor of 184 yields astonishing evidence for the alternative\nhypothesis that there is an effect in the population. The observed\nt-value is 184 times more likely under the alternative hypothesis than\nunder the null hypothesis. Note that a t-value of 4 would also result in\na highly significant p-value under the null hypothesis. The orange dots\nillustrate the so called *marginal likelihoods*, i.e., the probability\ndensity of the observed data under each hypothesis. The ratio of the\nmarginal likelihoods is the Bayes factor.\n\n## Evidence for the null hypothesis\n\nOne of the strengths of Bayes factors is that they can quantify relative\nevidence in favor of a null hypothesis; classical significance tests\nrelying on p-values do not offer this feature. Look at the following\nexample code:\n\n```{r}\n\n## Sample from a polulation with a null effect:\ngroupn \u003c- 100\ngroup1 \u003c- rnorm(100, 0, 1)\ngroup2 \u003c- rnorm(100, 0, 1)\n\ntvalue \u003c- t.test(group1, group2)$statistic\n\nvisualize_predictions(n1 = groupn, n2 = groupn, observed_t = tvalue, BF10 = FALSE)\n\n```\n\nHere, I sample 100 observations per group that do not differ in their\npopulation mean, i.e., the effect size in the population is zero. More\noften than not, I will obtain a Bayes factor that favors the null\nhypothesis over the alternative hypothesis. To illustrate the evidence\nin favor of the null as compared to the alternative hypothesis, I set\nthe argument `BF10` to `FALSE`; run the code to see what happens when\nthis is not done .\n\n\nUsing the function `marginal_likelihood` from the package `bayesEd`, we\ncan compute this Bayes factor as the ratio of two marginal likelihoods:\n\n```{r}\nnull_marginal \u003c- dt(tvalue, df = groupn * 2 - 2)\nalt_marginal \u003c- marginal_likelihood(tvalue, n1 = groupn, n2 = groupn)\nBF01 \u003c- null_marginal / alt_marginal\nBF01\n```\n\nHere, the probability of the data under the null hypothesis is given by\nthe probability density function for the t distribution `dt`. To compute\nthe probability of the data under the alternative hypothesis, we use the\nfunction `marginal_likelihood`. As the previous functions that we saw\n(`visualize_prior` and `visualize_predictions`), `marginal_likelihood`\nalso has an argument `alternative` that specifies the prior distribution\nfor the alternative hypothesis. It also has the same Cauchy default.\n\nI encourage to use the package `BayesFactor` to compute Bayes factors\nfor the t-test that offers more functionality like, for example, the\npossibility to compute Bayes factors for paired t-tests. To reproduce my\nanalysis above, we can also use the following code employing the package\n`BayesFactor`:\n\n```{r}\nlibrary(\"BayesFactor\")\n1 / ttestBF(group1, group2) # inverse Bayes factor; evidence for null hypothesis\n```\n\n## Predictions of different priors\n\nThe primary strength of the package `bayesEd` is that Bayes factors for\ndifferent priors can be computed and visualized. This may be used for\neducational purposes, e.g., to teach the Bayes factor to students. The\nfollowing is a gallery for different priors and their predictions. I use\nthe data from the example above where I sampled 2x 100 observations from\na population null effect:\n\n```{r}\n## Use a point hypothesis as the alternative. In this case, the argument\n## alternative is not a function object, but a scalar number:\nvisualize_prior(0.4)\nvisualize_predictions(alternative = 0.4, n1 = groupn, n2 = groupn, observed_t = tvalue)\n\n## Use a normal distribution as the alternative\nnormal_alt \u003c- function(x) dnorm(x, mean = 0.5, sd = 0.3)\nvisualize_prior(normal_alt)\nvisualize_predictions(alternative = normal_alt, n1 = groupn, n2 = groupn,\n                      observed_t = tvalue, from = -4, to = 8, BFx = 4,\n                      BFy = dt(0, 198) * 0.9, BF10 = FALSE)\n```\n\nAlso see `?visualize_predictions` for a description of all arguments of\nthe function.\n\n# References\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm-py%2Fbayesed","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fm-py%2Fbayesed","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm-py%2Fbayesed/lists"}