library(learnr) knitr::opts_chunk$set(echo = FALSE, warning=FALSE, message=FALSE)
Suppose we have two spinners -- one has four regions with areas 2, 1, 1, 2, and the second has four regions with areas 1, 2, 2, 1. Suppose you choose one spinner at random and observe its spin. Based on this spin you are going to revise your probabilities using Bayes' rule.
Here are pictures of the two spinners:
library(TeachBayes) spinners <- list(c(2, 1, 1, 2), c(1, 2, 2, 1)) many_spinner_plots(spinners)
You first choose one of the two spinners at random. Create a new data frame bayes_df
with two variables: Model
contains the names of the two spinners, and Prior
contains your initial probabilities of the spinner you chose.
bayes_df <- data.frame(Model = paste("Spinner", c("A", "B")), Prior = c()) bayes_df
Next suppose you spin the unknown spinner once and you observe a 3 (in the blue region).
library(TeachBayes) spinners <- list(c(2, 1, 1, 2), c(1, 2, 2, 1)) many_spinner_plots(spinners)
bayes_df <- data.frame(Model = paste("Spinner", c("A", "B")), Prior = c(0.5, 0.5))
Add a new variable Likelihood
to the data frame bayes_df
consisting of the likelihoods, the probabilities of observing a 3 (blue) for each of the two spinners.
bayes_df$Likelihood <- c() bayes_df
Now we can update our probabilities of the two spinners by using Bayes' rule.
library(TeachBayes) spinners <- list(c(2, 1, 1, 2), c(1, 2, 2, 1)) many_spinner_plots(spinners)
library(TeachBayes) bayes_df <- data.frame(Model = paste("Spinner", c("A", "B")), Prior = c(0.5, 0.5), Likelihood = c(1/6, 1/3))
Use the bayesian_crank
function with argument bayes_df
to compute the updated probabilities of the spinners.
library(TeachBayes) bayes_df <- data.frame(Model = paste("Spinner", c("A", "B")), Prior = c(0.5, 0.5), Likelihood = c(1/6, 1/3)) bayes_df <- bayesian_crank(bayes_df)
The Prior and Posterior probabilities are stored in the data frame bayes_df
. Use the prior_post_plot
function with argument bayes_df
to compare the prior and posterior probabilities of the two spinners.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.