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_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)
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)
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)))
dspinner
computes the likelhood function of spinners given a vector of spinner outcomes.dspinner(c(1, 3, 4, 1, 2), LIKE)
(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)
(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)
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_update)
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)))
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)
(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)
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)))
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)
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)))
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.