# Chapter 3 R examples: # load the 'bayesrules' package # May need to type # install.packages("bayesrules") # first. library(bayesrules) library(ggplot2) ##### ## Run this multiplot function code ONCE: # Multiplot function for later: just run this once: multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { library(grid) # Make a list from the ... arguments and plotlist plots <- c(list(...), plotlist) numPlots = length(plots) # If layout is NULL, then use 'cols' to determine layout if (is.null(layout)) { # Make the panel # ncol: Number of columns of plots # nrow: Number of rows needed, calculated from # of cols layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), ncol = cols, nrow = ceiling(numPlots/cols)) } if (numPlots==1) { print(plots[[1]]) } else { # Set up the page grid.newpage() pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) # Make each plot, in the correct location for (i in 1:numPlots) { # Get the i,j matrix positions of the regions that contain this subplot matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE)) print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col)) } } } ## End multiplot function code ############# # Plots of different beta distributions: b1.5 <- plot_beta(1,5)+ggtitle("Beta(1,5)") b1.2 <- plot_beta(1,2)+ggtitle("Beta(1,2)") b3.7 <- plot_beta(3,7)+ggtitle("Beta(3,7)") b1.1 <- plot_beta(1,1)+ggtitle("Beta(1,1)") b5.5 <- plot_beta(5,5)+ggtitle("Beta(5,5)") b20.20 <- plot_beta(20,20)+ggtitle("Beta(20,20)") b7.3 <- plot_beta(7,3)+ggtitle("Beta(7,3)") b2.1 <- plot_beta(2,1)+ggtitle("Beta(2,1)") b5.1 <- plot_beta(5,1)+ggtitle("Beta(5,1)") multiplot(b1.5, b1.2, b3.7, b1.1,b5.5,b20.20,b7.3,b2.1,b5.1, cols=3) ## Plots of different beta prior choices: # Plot the Beta(9,11) prior plot_beta(9,11,mean=T,mode=T) # Plot the Beta(18,22) prior plot_beta(18,22,mean=T,mode=T) # Plot the Beta(45, 55) prior plot_beta(45,55,mean=T,mode=T) # Comparing the prior and the posterior in the political candidate example: plot_beta_binomial(alpha = 45, beta = 55, y = 30, n = 50) # prior and the posterior summary statistics in the political candidate example: summarize_beta_binomial(alpha = 45, beta = 55, y = 30, n = 50) # Credit Card Debt Example ## Researchers studied whether doctors completing residency take on high credit card debt. # Let pi= population proportion of all medical residents with over $3000 in credit card debt # Prior belief: 20% do?? ########### Analysis with beta(2,8) prior: # Plot the Beta(2, 8) prior plot_beta(2,8,mean=T,mode=T) # Sample of 115 residents revealed 22 had credit card debt over $3000. # Comparing the prior and the posterior in the credit card debt example: plot_beta_binomial(alpha = 2, beta = 8, y = 22, n = 115) # prior and the posterior summary statistics in the credit card debt example: summarize_beta_binomial(alpha = 2, beta = 8, y = 22, n = 115) ########### Analysis with beta(1,1) (i.e., Uniform) prior: # Sample of 115 residents revealed 22 had credit card debt over $3000. # Comparing the prior and the posterior in the credit card debt example: plot_beta_binomial(alpha = 1, beta = 1, y = 22, n = 115) # prior and the posterior summary statistics in the credit card debt example: summarize_beta_binomial(alpha = 1, beta = 1, y = 22, n = 115)