We’re starting the chapter on Bayesian computation and I thought I’d use a new example to illustrate the basic methods. I collect the high school GPA and the ACT math score for 50 BGSU freshmen and I’m interested in learning about the correlation. If I standardize both measurements (by subtracting the mean and dividing by the standard deviation), then I can assume the standardized pairs are a random sample from a bivariate normal density with 0 means, unit standard deviations, and correlation . If I assume a uniform prior on , then the posterior density is given by
I begin by writing a short function to compute the log posterior of . Here data is a matrix of two columns of the standardized x and standardized y.
I display the posterior density below. Note that I use the sapply function to evaluate the posterior for a vector of values. Also, I do some standardization of the posterior values so that the total area of the plotted density is equal to one.
We illustrate finding a normal approximation to this density by use of the laplace function in the LearnBayes package. The arguments are the name of the log posterior, the starting guess at the mode (we use 0) and the matrix of data dz. The output is the posterior mode and the corresponding estimate at the posterior variance.
So the posterior for
is approximately N(0.50,
). Using the curve function we add the normal approximation to the graph. This approximation looks pretty good, but there is a little room for improvement.