Lab. 4 Bayes’ Box and R.
In this lab you will need to be able to make the Bayes’ Box for Binomial experiments.
Objectives:
- Review classical methods
- Learn how the components of a Bayes’ box are constructed using R.
- Learn how to construct a Bayes’ box using an R function.
- Learn how to make graphical output from the Bayes box
- Learn how to make both point and interval estimates from a Bayes’ box
- Learn how to automate the process with an all inclusive function.
Output:
- Make an Rmd document for this lab ( ALL formulae need to be put in latex)
- At the conclusion render into the 3 file types
- Upload the 4 files to the server.
Bayes’ Rule
Experiment
A biased coin is tossed
- times
- successes (Heads)
- Each trial is independent
- There are only two possible outcomes per trial
- is constant
Tasks:
- Classical estimates for
- What is the classical point estimate for
- Using
- find the classical 95% ci for
- interpret the interval (see http://onlinestatbook.com/2/estimation/confidence.html )
- How many samples were used to make the interval?
- How many samples are assumed to give the interpretation?
- Bayesian methods – Bayes’ rule. We will assume discrete values of and a uniform prior on the values of
- Suppose we take theta = seq(0, 1, length = 20)
- Plot the prior using base R.
- Write down the formula for the likelihood in this case – you can substitute the n and x so that it is a function of
- Plot the likelihood over the discrete values of theta. Place in your document.
- Now add the prior plot onto the same graph. Place in your document.
- We need the posterior in order to obtain the third and last plot – what formula will we use to obtain this? Write it down.
- Bayes’ Box – below is Bayes’ box – it will need more rows!
theta | P(theta) | P(x|theta) | h(theta) | P(theta|x) |
Total |
- Make a matrix in R that will hold the 5 column vectors. Show here.
- Rewrite Bayes’ theorem in terms of h = prior*lik. Show formula here.
- Now make the a plot similar to the one below (except it will be for the data given above) from the matrix using ggplot
- Put your name on it
- Make sure it has the right colors
- Make an improvement to the plot by changing the x ticks to represent the number of theta values.
- Now make a function called mybinpost() that will do the following:
- Take arguments n, x, theta, alpha
- n = number of trials
- x = number of successes
- Take arguments n, x, theta, alpha
- theta = vector of possible theta values (sequence from 0 to 1)
- alpha/2 = tail probability (used to obtain 1-alpha Bayesian credible interval)
- Assume uniform prior
- The function will produce
- the above plot.
- A list containing
- the Bayes box as a matrix
- the Bayes box in latex (package “xtable”)
- the number of trials n
- the number of successes x
- the Bayesian point estimate
- The 95% Bayesian credible interval (BCI)
- Now use your function and include all output into your document for the following invocations
- mybinpost(n=20,x=12,theta = seq(0,1,length=50, alpha=0.05)
- mybinpost(n=50, x=25, theta=seq(0,1,length=30), alpha=0.1)