class: title-slide <br> <br> .right-panel[ # Simulating the Beta-Binomial ## Dr. Mine Dogucu Examples from [bayesrulesbook.com](https://bayesrulesbook.com) ] --- ## Prior ```r plot_beta(45, 55) ``` <img src="slide-03a-betabin-sim_files/figure-html/unnamed-chunk-3-1.png" style="display: block; margin: auto;" /> --- ## Data and the Posterior ```r summarize_beta_binomial(45, 55, y = 30, n = 50) ``` ``` ## model alpha beta mean mode var sd ## 1 prior 45 55 0.45 0.4489796 0.002450495 0.04950248 ## 2 posterior 75 75 0.50 0.5000000 0.001655629 0.04068942 ``` --- ```r plot_beta_binomial(45, 55, y = 30, n = 50) ``` <img src="slide-03a-betabin-sim_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> --- ```r library(tidyverse) set.seed(84735) ``` ```r michelle_sim <- data.frame(pi = rbeta(10000, 45, 55)) ``` --- ``` ## pi ## 1 0.4884391 ## 2 0.4430527 ## 3 0.4953429 ## 4 0.4633678 ## 5 0.4963052 ## 6 0.5495968 ``` ``` ## pi ## 9995 0.4741776 ## 9996 0.3756744 ## 9997 0.4660905 ## 9998 0.4791408 ## 9999 0.4394663 ## 10000 0.4229081 ``` --- ```r set.seed(84735) michelle_sim <- data.frame(pi = rbeta(10000, 45, 55)) %>% mutate(y = rbinom(10000, size = 50, prob = pi)) ``` --- ``` ## pi y ## 1 0.4884391 23 ## 2 0.4430527 14 ## 3 0.4953429 35 ## 4 0.4633678 29 ## 5 0.4963052 36 ## 6 0.5495968 26 ``` ``` ## pi y ## 9995 0.4741776 24 ## 9996 0.3756744 13 ## 9997 0.4660905 25 ## 9998 0.4791408 21 ## 9999 0.4394663 24 ## 10000 0.4229081 21 ``` --- ```r michelle_posterior <- michelle_sim %>% filter(y == 30) ``` -- ```r nrow(michelle_posterior) ``` ``` ## [1] 211 ``` -- `\(f(\pi|y =30)\)` --- ```r ggplot(michelle_posterior, aes(x = pi)) + geom_histogram(color = "white", binwidth = 0.025) ``` <img src="slide-03a-betabin-sim_files/figure-html/unnamed-chunk-13-1.png" style="display: block; margin: auto;" /> --- ```r ggplot(michelle_posterior, aes(x = pi)) + geom_density() + xlim(0,1) ``` <img src="slide-03a-betabin-sim_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto;" /> --- .pull-left[ <img src="slide-03a-betabin-sim_files/figure-html/unnamed-chunk-15-1.png" style="display: block; margin: auto;" /> ] .pull-right[ <img src="slide-03a-betabin-sim_files/figure-html/unnamed-chunk-16-1.png" style="display: block; margin: auto;" /> ]