Created
June 17, 2022 19:26
-
-
Save MJacobs1985/f2873d0ac5f5731b8ec739a7524823f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(ggplot2) | |
library(sjPlot) | |
library(sjmisc) | |
p_grid <- seq(from=0 , to=1 , length.out=100) | |
prior <- dbinom(60, size=100 , prob=p_grid) | |
likelihood <- dbinom(50, size=100 , prob=p_grid) | |
unstd.posterior <- likelihood * prior | |
posterior <- unstd.posterior / sum(unstd.posterior) | |
bayesdata <- as.data.frame(cbind(p_grid, prior, likelihood, unstd.posterior, posterior)) | |
ggplot(bayesdata, aes(x=p_grid))+ | |
geom_area(aes(y=prior, fill="Prior"),alpha=0.5)+ | |
geom_area(aes(y=likelihood, fill="Likelihood"),alpha=0.5)+ | |
geom_area(aes(y=unstd.posterior, fill="Unstandardized Posterior"),alpha=0.5)+ | |
geom_area(aes(y=posterior,fill="Posterior"),alpha=0.5)+ | |
scale_fill_manual(values=c("seagreen","red","orange","brown"))+ | |
ggtitle("Posterior Probability given Prior and Likelihood")+ | |
xlab("Probability") + ylab("Posterior Probability")+ | |
guides(fill=guide_legend(title="Bayesian Analysis"))+ | |
theme_bw() | |
nth(prior, 50) | |
nth(likelihood, 50) | |
nth(posterior, 50) | |
prior_est<-bayesdata %>% | |
slice(which.max(prior))%>%dplyr::select(p_grid) | |
like_est<-bayesdata %>% | |
slice(which.max(likelihood))%>%dplyr::select(p_grid) | |
post_est<-bayesdata %>% | |
slice(which.max(posterior))%>%dplyr::select(p_grid) | |
ggplot(bayesdata, aes(x=p_grid))+ | |
geom_area(aes(y=prior, fill="Prior"),alpha=0.5)+ | |
geom_vline(xintercept = prior_est[[1]], color="orange", lty=2) + | |
geom_area(aes(y=likelihood, fill="Likelihood"),alpha=0.5)+ | |
geom_vline(xintercept = like_est[[1]], color="seagreen", lty=2) + | |
geom_area(aes(y=unstd.posterior, fill="Unstandardized Posterior"),alpha=0.5)+ | |
geom_area(aes(y=posterior,fill="Posterior"),alpha=0.5)+ | |
geom_vline(xintercept = post_est[[1]], color="red", lty=2) + | |
scale_fill_manual(values=c("seagreen","red","orange","brown"))+ | |
ggtitle("Posterior Probability given Prior and Likelihood")+ | |
xlab("Probability") + ylab("Posterior Probability")+ | |
guides(fill=guide_legend(title="Bayesian Analysis"))+ | |
xlim(0.4,0.7)+ | |
theme_bw() | |
set.seed(11454) | |
plot(density(rbinom(100,1,0.5454545))) | |
hist(rbinom(100,1,0.5454545)) | |
hist(rbinom(100,100,0.5454545),breaks=10) | |
hist(rbinom(1000,100,0.5454545),breaks=30) | |
hist(rbinom(100,1000,0.5454545),breaks=10) | |
p_grid <- seq(from=0 , to=1 , length.out=1000) | |
prior <- dbinom(600, size=1000 , prob=p_grid) | |
likelihood <- dbinom(500, size=1000 , prob=p_grid) | |
unstd.posterior <- likelihood * prior | |
posterior <- unstd.posterior / sum(unstd.posterior) | |
bayesdata <- as.data.frame(cbind(p_grid, prior, likelihood, unstd.posterior, posterior)) | |
prior_est<-bayesdata %>% | |
slice(which.max(prior))%>%dplyr::select(p_grid) | |
like_est<-bayesdata %>% | |
slice(which.max(likelihood))%>%dplyr::select(p_grid) | |
post_est<-bayesdata %>% | |
slice(which.max(posterior))%>%dplyr::select(p_grid) | |
ggplot(bayesdata, aes(x=p_grid))+ | |
geom_area(aes(y=prior, fill="Prior"),alpha=0.5)+ | |
geom_vline(xintercept = prior_est[[1]], color="orange", lty=2) + | |
geom_area(aes(y=likelihood, fill="Likelihood"),alpha=0.5)+ | |
geom_vline(xintercept = like_est[[1]], color="seagreen", lty=2) + | |
geom_area(aes(y=unstd.posterior, fill="Unstandardized Posterior"),alpha=0.5)+ | |
geom_area(aes(y=posterior,fill="Posterior"),alpha=0.5)+ | |
geom_vline(xintercept = post_est[[1]], color="red", lty=2) + | |
scale_fill_manual(values=c("seagreen","red","orange","brown"))+ | |
ggtitle("Posterior Probability given Prior and Likelihood")+ | |
xlab("Probability") + ylab("Posterior Probability")+ | |
guides(fill=guide_legend(title="Bayesian Analysis"))+ | |
xlim(0.4,0.7)+ | |
theme_bw() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment