# # Chapter 4 -- Bayesian Computation With R # Demonstrates Priors # # Remove all objects just to be safe # rm(list=ls(all=TRUE)) # library(LearnBayes) # data(marathontimes) attach(marathontimes) #attach(marathontimes,warn.conflicts = FALSE) # # normchi2post in the LearnBayes library computes the log of the # posterior density of (mu, sigma^2) # mycontour in the LearnBayes library accesses the "contour" command # in R # d <- mycontour(normchi2post, c(220, 330, 500, 9000), time, xlab="",ylab="",main="",font=2) # Main title mtext("Contour Plot of the Joint Posterior of\nthe mean and variance of a normal sampling model",side=3,line=1.00,cex=1.2,font=2) # x-axis title mtext("Mean",side=1,line=2.75,font=2,cex=1.2) # y-axis title mtext("Variance",side=2,line=2.75,font=2,cex=1.2) # # S is the sum of squares of the marathon times # S <- sum((time - mean(time))^2) n <- length(time) # # sigma2 is a 1000 random draw with entries equal to S divided by draws from the # chi-square distribution with n-1 degrees of freedom # sigma2 <- S/rchisq(1000, n - 1) # # mu is a 1000 random draw from a N(mean, sd) distribution # mu <- rnorm(1000, mean = mean(time), sd = sqrt(sigma2)/sqrt(n)) # # Here the points are plotted onto the contour lines # points(mu, sigma2) muquantile <- quantile(mu, c(0.025, 0.975)) sigmaquantile <- quantile(sqrt(sigma2), c(0.025, 0.975)) # # this detaches the data -- pos=3 is were it is stored # detach(marathontimes, pos=3)