Beginning Bayesian

Beginning Bayes in R

Introduction to Bayesian thinking

library(TeachBayes)
areas <- c(2,1,2,1,2)
spinner_plot(areas)

spinner_probs(areas)
##   Region  Prob
## 1      1 0.250
## 2      2 0.125
## 3      3 0.250
## 4      4 0.125
## 5      5 0.250
df = data.frame(Region = 1:5, areas, Probability = areas/sum(areas))

df
##   Region areas Probability
## 1      1     2       0.250
## 2      2     1       0.125
## 3      3     2       0.250
## 4      4     1       0.125
## 5      5     2       0.250
spins = spinner_data(areas, 1000)


"

bar_plot(spins)

S = summarise(group_by(data.frame(Region=spins),Region), N=n())
S
## # A tibble: 5 x 2
##   Region     N
##    <int> <int>
## 1      1   279
## 2      2   130
## 3      3   219
## 4      4   128
## 5      5   244

Construct frequency table of spins:

table(spins)
## spins
##   1   2   3   4   5 
## 279 130 219 128 244

Find fraction of spins equal to 2:

mean(spins==2 )
## [1] 0.13
bayes_df = data.frame(Model=paste("Spinner", c("A","B","C","D"))) 

bayes_df$Prior = rep(0.25,4)

bayes_df$Likelihood = round(c(1/3,1/2,1/4,1/6),2)

bayes_df = bayesian_crank(bayes_df)

bayes_df
##       Model Prior Likelihood Product Posterior
## 1 Spinner A  0.25       0.33  0.0825     0.264
## 2 Spinner B  0.25       0.50  0.1250     0.400
## 3 Spinner C  0.25       0.25  0.0625     0.200
## 4 Spinner D  0.25       0.17  0.0425     0.136
prior_post_plot(bayes_df)

Comments