### Use of the Bolstad R library # Suppose we draw one ball from an urn containing 5 balls, with # xi = 0,1,2,3,4,5 possible R balls. library(Bolstad) help(binodp) n = 1 # draw one ball x = 1 # one ball drawn and it is a R # The function binodp with one draw with 6 values of the parameter. binodp(x,n, uniform = TRUE, n.theta = 6) # plots of prior and posterior X11() # open new graph window theta = c(0,0.2,0.4,0.6,0.8,1) theta.prior = c(1/6,1/6,1/6,1/6,1/6,1/6) results <- binodp(x,n, uniform = FALSE, theta=theta, theta.prior=theta.prior,ret=TRUE) results.matrix = rbind(results$theta.prior,results$posterior) colnames(results.matrix) = theta barplot(results.matrix,col=c("red","blue"),beside=TRUE ,xlab=expression(theta),ylab=expression(Probability(theta))) box() legend(1,0.25,legend=c("Prior","Posterior"),fill=c("red","blue")) posterior.mean = sum(theta*results$posterior) posterior.mean posterior.sd = sqrt(sum((theta^2)*results$posterior) - posterior.mean^2) posterior.sd # Now suppose we draw twice and observer a R and a G. n = 2 x = 1 X11() # open new graph window binodp(x,n, uniform = TRUE, n.theta = 6) # plots of prior and posterior X11() # open new graph window theta = c(0,0.2,0.4,0.6,0.8,1) theta.prior = c(1/6,1/6,1/6,1/6,1/6,1/6) results <- binodp(x,n, uniform = FALSE, theta=theta, theta.prior=theta.prior,ret=TRUE) results.matrix = rbind(results$theta.prior,results$posterior) colnames(results.matrix) = theta barplot(results.matrix,col=c("red","blue"),beside=TRUE ,xlab=expression(theta),ylab=expression(Probability(theta))) box() legend(1,0.25,legend=c("Prior","Posterior"),fill=c("red","blue")) posterior.mean = sum(theta*results$posterior) posterior.mean posterior.sd = sqrt(sum((theta^2)*results$posterior) - posterior.mean^2) posterior.sd