## code to accompany the CBS presidential poll example ## lecture 8 of PSTAT 215A, Bayesian Inference, UCSB ## ## Robert B. Gramacy adapted from Peter Hoff ## rdirichlet ## ## sample from a dirichlet distribution rdirichlet <- function(n, alpha) { m <- length(alpha) x <- matrix(rgamma(n*m, alpha), ncol=m, byrow=TRUE) return(x/apply(x, 1, sum)) } ## sample fromt he posterior in the poll example theta <- rdirichlet(10000, alpha=c(728,584,138)) ## create a histogram of the evidence in favor of Bush ## over Dukakis hist(theta[,1] - theta[,2], bty="n", main=NULL, xlab="theta1 - theta2")