library(learnr)
knitr::opts_chunk$set(echo = FALSE,
            warning=FALSE, message=FALSE)

Models and Prior

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)

Exercise

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

The Likelihoods

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))

Exercise

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

The Posterior

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))

Exercise

Use the bayesian_crank function with argument bayes_df to compute the updated probabilities of the spinners.


Compare Prior and Posterior

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)

Exercise

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.




bayesball/TeachBayes documentation built on Jan. 5, 2020, 1:47 a.m.