R语言代写:Lab. 4 Bayes’ Box and R.

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:

  1. Classical estimates for
    1. What is the classical point estimate for
    2. Using
      1. find the classical 95% ci for
      2. interpret the interval (see http://onlinestatbook.com/2/estimation/confidence.html )
  • How many samples were used to make the interval?
  1. How many samples are assumed to give the interpretation?
  1. Bayesian methods – Bayes’ rule. We will assume discrete values of and a uniform prior on the values of
    1. Suppose we take theta = seq(0, 1, length = 20)
    2. Plot the prior using base R.
    3. Write down the formula for the likelihood in this case – you can substitute the n and x so that it is a function of
    4. Plot the likelihood over the discrete values of theta. Place in your document.
    5. Now add the prior plot onto the same graph. Place in your document.
    6. We need the posterior in order to obtain the third and last plot – what formula will we use to obtain this? Write it down.
  2. Bayes’ Box – below is Bayes’ box – it will need more rows!
theta P(theta) P(x|theta) h(theta) P(theta|x)
         
         
         
Total        

 

  1. Make a matrix in R that will hold the 5 column vectors. Show here.
  2. Rewrite Bayes’ theorem in terms of h = prior*lik. Show formula here.
  3. Now make the a plot similar to the one below (except it will be for the data given above) from the matrix using ggplot
    1. Put your name on it
    2. 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.
  1. Now make a function called mybinpost() that will do the following:
    1. Take arguments n, x, theta, alpha
      1. n = number of trials
      2. x = number of successes
  • theta = vector of possible theta values (sequence from 0 to 1)
  1. alpha/2 = tail probability (used to obtain 1-alpha Bayesian credible interval)
  2. Assume uniform prior
  1. The function will produce
    1. the above plot.
    2. A list containing
      1. the Bayes box as a matrix
      2. the Bayes box in latex (package “xtable”)
      3. the number of trials n
      4. the number of successes x
      5. the Bayesian point estimate
      6. The 95% Bayesian credible interval (BCI)
    3. Now use your function and include all output into your document for the following invocations
      1. mybinpost(n=20,x=12,theta = seq(0,1,length=50, alpha=0.05)
      2. mybinpost(n=50, x=25, theta=seq(0,1,length=30), alpha=0.1)