TeachBayes Package

knitr::opts_chunk$set(fig.width=6, 
                      fig.height=4, fig.path='Figs/',
                      echo=TRUE, warning=FALSE, message=FALSE)

The TeachBayes package was written to assist in the teaching of a "Beginning Bayes" course. There are a number of functions dealing with spinners, proportion, and mean inference. The purpose of this introduction to give a simple example of all of the functions. The material is divided into sections (1) Spinner Functions, (2) Inference for a Proportion, (3) Inference for a Mean, (4) Inference for Two Proportions and (5) Utilities

library(TeachBayes)

Spinner Functions

(spinner_data, spinner_likelihoods, spinner_plot, spinner_probs, dspinner, many_spinner_plots)

A useful randomization device is a spinner that is defined by a vector of areas.

sp_regions <- c(2, 1, 1, 2)
  1. spinner_plot will construct a plot of the spinner.
spinner_plot(sp_regions)
  1. spinner_probs displays the spinner probabilities:
spinner_probs(sp_regions)
  1. spinner_data simulates spins from the spinner:
spinner_data(sp_regions, nsim=20)

Suppose we define multiple spinners:

sp1 <- c(1, 1, 1)
sp2 <- c(1, 2, 2, 1)
sp3 <- c(1, 1, 1, 1)
sp4 <- c(2, 2, 3, 3, 4)
  1. We can use many_spinner_plots to display all of the spinners.
many_spinner_plots(list(sp1, sp2, sp3, sp4))
  1. Function spinner_likelihoods will compute a probability matrix for a group of spinners. This is used in a Bayes' rule calculation.
(LIKE <- spinner_likelihoods(list(sp1, sp2, sp3, sp4)))
  1. Function dspinner computes the likelhood function of spinners given a vector of spinner outcomes.
dspinner(c(1, 3, 4, 1, 2), LIKE)

Bayes' Rule

(bayesian_crank, prior_post_plot)

Given a data frame with variables Model, Prior, and Likelihood, function bayesian_crank computes the posterior probabilities.

bayes_df <- data.frame(Model=paste("Spinner", 1:4),
                       Prior=rep(1/4, 4), 
                       Likelihood=dspinner(c(1, 2, 1), LIKE))
(bayes_df <- bayesian_crank(bayes_df))

Function prior_post_plot will display the prior and posterior probabilities.

prior_post_plot(bayes_df)

Inference for a Proportion

(beta_area, beta_data, beta_draw, beta_interval, beta_prior_post, beta_quantile, ChooseBeta)

  1. beta_draw displays a single beta curve
beta_draw(c(10, 5))
  1. beta_area displays a beta area (probability)
beta_area(.4, .6, c(10, 5))
  1. beta_quantile displays a beta quantile
beta_quantile(.7, c(10, 5))
  1. beta_interval displays an equal-tails interval that contains a specific probability content
beta_interval(.8, c(10, 5))
  1. beta_data simulates from a beta density
beta_data(c(10, 5), nsim=20)
  1. beta_prior_post will graphically compare two beta curves such as a prior and posterior for a proportion
beta_prior_post(c(4, 4), c(20, 10))
  1. Function ChooseBeta runs a Shiny app for selecting a beta curve based on knowledge of the prior median and prior 90th percentile.

Inference for a Mean

(normal_area, normal_draw, normal_interval, normal_quantile, many_normal_plots, normal_update)

  1. Function normal_area displays and computes a normal probability.
normal_area(90, 105, c(100, 15))
  1. Function normal_draw will draw a single normal curve.
normal_draw(c(100, 10))
  1. Function normal_interval will compute an equal tails probability interval.
normal_interval(.8, c(100, 10))
  1. Function normal_quantile will compute and show graphically a normal quantile.
normal_quantile(.3, c(100, 10))
  1. Function many_normal_plots will display many normal curves on the same graph.
many_normal_plots(list(c(100, 10), c(110, 10), c(120, 10)))
  1. Function normal_update will give the posterior mean and standard deviation given the normal prior and the sample info.
prior <- c(100, 10)
ybar <- 120
se <- 15
normal_update(prior, c(ybar, se))
normal_update(prior, c(ybar, se), teach=TRUE)

Inference for Two Proportions

(testing_prior, draw_two_p, two_p_update, twoproplike, two_p_summarize)

  1. Function testing_prior will construct a discrete prior for two proportions.
testing_prior(.1, .5, 5)
  1. Function draw_two_p constructs a graph of a discrete distribution of two proportions.
draw_two_p(testing_prior(.1, .5, 5))
  1. Function two_p_update will update a 2-proportion discrete prior with data.
prior <- testing_prior(.1, .5, 5)
(post <- two_p_update(prior, c(2, 10), c(4, 10)))
  1. Function twopropllike computes a likelihood for two binomial proportions (currently this calculation is already in the two_p_update function).

  2. Function two_p_summarize will find the probability distribution for the difference in two proportions.

prior <- testing_prior(.1, .5, 5)
post <- two_p_update(prior, c(2, 10), c(4, 10))
two_p_summarize(post)

Utilities

  1. bar_plot constructs a frequency bar plot of numeric data
bar_plot(spinner_data(c(1, 2, 3)))
  1. prob_plot constructs a plot of a discrete probability distribution
prob_plot(data.frame(x=1:5, prob=c(.2, .3, .4, .1, .1)))


Try the TeachBayes package in your browser

Any scripts or data that you put into this service are public.

TeachBayes documentation built on May 1, 2019, 9:17 p.m.