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_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)
spinner_plot
will construct a plot of the spinner.spinner_plot(sp_regions)
spinner_probs
displays the spinner probabilities:spinner_probs(sp_regions)
## Spin Prob
## 1 1 0.3333333
## 2 2 0.1666667
## 3 3 0.1666667
## 4 4 0.3333333
spinner_data
simulates spins from the spinner:spinner_data(sp_regions, nsim=20)
## [1] 1 4 2 3 2 3 4 2 4 4 2 1 4 3 3 2 4 1 3 1
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)
many_spinner_plots
to display all of the spinners.many_spinner_plots(list(sp1, sp2, sp3, sp4))
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 2 3 4 5
## Spinner A 0.3333333 0.3333333 0.3333333 0.0000000 0.0000000
## Spinner B 0.1666667 0.3333333 0.3333333 0.1666667 0.0000000
## Spinner C 0.2500000 0.2500000 0.2500000 0.2500000 0.0000000
## Spinner D 0.1428571 0.1428571 0.2142857 0.2142857 0.2857143
dspinner
computes the likelhood function of spinners given a vector of spinner outcomes.dspinner(c(1, 3, 4, 1, 2), LIKE)
## [,1]
## [1,] 0.0000000000
## [2,] 0.0005144033
## [3,] 0.0009765625
## [4,] 0.0001338728
(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))
## Model Prior Likelihood Product Posterior
## 1 Spinner 1 0.25 0.037037037 0.009259259 0.57123527
## 2 Spinner 2 0.25 0.009259259 0.002314815 0.14280882
## 3 Spinner 3 0.25 0.015625000 0.003906250 0.24098988
## 4 Spinner 4 0.25 0.002915452 0.000728863 0.04496604
Function prior_post_plot
will display the prior and posterior probabilities.
prior_post_plot(bayes_df)
(beta_area, beta_data, beta_draw, beta_interval, beta_prior_post, beta_quantile, ChooseBeta)
beta_draw
displays a single beta curvebeta_draw(c(10, 5))
beta_area
displays a beta area (probability)beta_area(.4, .6, c(10, 5))
beta_quantile
displays a beta quantilebeta_quantile(.7, c(10, 5))
beta_interval
displays an equal-tails interval that contains a specific probability contentbeta_interval(.8, c(10, 5))
beta_data
simulates from a beta densitybeta_data(c(10, 5), nsim=20)
## [1] 0.7320285 0.7535439 0.8597961 0.6176806 0.6335028 0.7670381 0.6678020
## [8] 0.5317556 0.7495730 0.8906396 0.6463841 0.4218039 0.5365313 0.8364366
## [15] 0.4890980 0.7421541 0.7129597 0.6609451 0.5046527 0.6878241
beta_prior_post
will graphically compare two beta curves such as a prior and posterior for a proportionbeta_prior_post(c(4, 4), c(20, 10))
ChooseBeta
runs a Shiny app for selecting a beta curve based on knowledge of the prior median and prior 90th percentile.(normal_area, normal_draw, normal_interval, normal_quantile, many_normal_plots)
normal_area
displays and computes a normal probability.normal_area(90, 105, c(100, 15))
normal_draw
will draw a single normal curve.normal_draw(c(100, 10))
normal_interval
will compute an equal tails probability interval.normal_interval(.8, c(100, 10))
normal_quantile
will compute and show graphically a normal quantile.normal_quantile(.3, c(100, 10))
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)))
(testing_prior, draw_two_p, two_p_update, twoproplike, two_p_summarize)
testing_prior
will construct a discrete prior for two proportions.testing_prior(.1, .5, 5)
## 0.1 0.2 0.3 0.4 0.5
## 0.1 0.100 0.025 0.025 0.025 0.025
## 0.2 0.025 0.100 0.025 0.025 0.025
## 0.3 0.025 0.025 0.100 0.025 0.025
## 0.4 0.025 0.025 0.025 0.100 0.025
## 0.5 0.025 0.025 0.025 0.025 0.100
draw_two_p
constructs a graph of a discrete distribution of two proportions.draw_two_p(testing_prior(.1, .5, 5))
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)))
## 0.1 0.2 0.3 0.4 0.5
## 0.1 0.0084801023 0.016731927 0.038015377 0.04764678 0.03895705
## 0.2 0.0033050719 0.104338779 0.059265113 0.07428024 0.06073316
## 0.3 0.0025552174 0.020166601 0.183276190 0.05742755 0.04695403
## 0.4 0.0013235215 0.010445659 0.023732813 0.11898259 0.02432070
## 0.5 0.0004809513 0.003795823 0.008624209 0.01080920 0.03535135
Function twopropllike
computes a likelihood for two binomial proportions (currently this calculation is already in the two_p_update function).
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)
## # A tibble: 9 × 2
## diff21 Prob
## <dbl> <dbl>
## 1 -0.4 0.0004809513
## 2 -0.3 0.0051193442
## 3 -0.2 0.0216250857
## 4 -0.1 0.0580136838
## 5 0.0 0.4504290143
## 6 0.1 0.1577452822
## 7 0.2 0.1592496481
## 8 0.3 0.1083799381
## 9 0.4 0.0389570522
bar_plot
constructs a frequency bar plot of numeric databar_plot(spinner_data(c(1, 2, 3)))
prob_plot
constructs a plot of a discrete probability distributionprob_plot(data.frame(x=1:5, prob=c(.2, .3, .4, .1, .1)))