library(knitr)
## Warning: package 'knitr' was built under R version 4.4.3
library(ProbBayes)
## Warning: package 'ProbBayes' was built under R version 4.4.3
## Loading required package: LearnBayes
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.4.3
## Loading required package: gridExtra
## Warning: package 'gridExtra' was built under R version 4.4.3
## Loading required package: shiny
## Warning: package 'shiny' was built under R version 4.4.3
bayes_table=data.frame(
theta=seq(0,1,by=0.1),
Prior=rep(1/11,11)# all equally likely
)
We know that the likelihood function p(y|θ) ~Binom(n,θ) , where our observed data is n=1000 and y=320. We use the dbinom() function to compute p(y|θ) for all θ values.
bayes_table$Likelihood=dbinom(320,1000,prob = bayes_table$theta)
We use the bayesian_crank function to calculate the posterior distribution p(θ|y) for all θ values by computing Bayes Rule.
#computing posterior
bayes_table=bayesian_crank(bayes_table)
kable(bayes_table) # prints out the bayes_table
| theta | Prior | Likelihood | Product | Posterior |
|---|---|---|---|---|
| 0.0 | 0.0909091 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.1 | 0.0909091 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.2 | 0.0909091 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.3 | 0.0909091 | 0.0105527 | 0.0009593 | 0.9999971 |
| 0.4 | 0.0909091 | 0.0000000 | 0.0000000 | 0.0000029 |
| 0.5 | 0.0909091 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.6 | 0.0909091 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.7 | 0.0909091 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.8 | 0.0909091 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.9 | 0.0909091 | 0.0000000 | 0.0000000 | 0.0000000 |
| 1.0 | 0.0909091 | 0.0000000 | 0.0000000 | 0.0000000 |
NNB Insert discussion of post dist
I will choose the following discrete prior instead based on that I strongly believe that θ ∈ [0.3, 0.45] is much more likely than θ is not an element of [0.3,0.45].
p(θ) = {0.001,0.05,0.1,0.25,0.25,0.13,0.1,0.066,0.05,0.002,0.001}
A valid distribution has the following properties: 1. 0 <=p(θ)<= 1 2. Summation of p(θ) for all θ =1
bayes_table_b=data.frame(
theta_b=seq(0,1,by=0.1),
Prior=c(0.001,0.05,0.1,0.25,0.25,0.13,0.1,0.066,0.05,0.002,0.001)
)#creating data frame with new prior distribution
max(bayes_table_b$Prior)#max value of p(θ)<=1
## [1] 0.25
min(bayes_table_b$Prior)#min value of p(θ)>=0
## [1] 0.001
sum(bayes_table_b$Prior) # sum of p(θ) for all θ=1
## [1] 1
Therefore my prior is a valid distribution.
I will evaluate the new posterior distribution by using the same method as in part A.
bayes_table_b$Likelihood=dbinom(320,1000,prob = bayes_table_b$theta_b) #finding p(y|θ)
bayes_table_b=bayesian_crank(bayes_table_b)#finding p(θ|y)
kable(bayes_table_b) #plotting table of posterior
| theta_b | Prior | Likelihood | Product | Posterior |
|---|---|---|---|---|
| 0.0 | 0.001 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.1 | 0.050 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.2 | 0.100 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.3 | 0.250 | 0.0105527 | 0.0026382 | 0.9999971 |
| 0.4 | 0.250 | 0.0000000 | 0.0000000 | 0.0000029 |
| 0.5 | 0.130 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.6 | 0.100 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.7 | 0.066 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.8 | 0.050 | 0.0000000 | 0.0000000 | 0.0000000 |
| 0.9 | 0.002 | 0.0000000 | 0.0000000 | 0.0000000 |
| 1.0 | 0.001 | 0.0000000 | 0.0000000 | 0.0000000 |
prior_post_plot(bayes_table_b)#plotting prior and post
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## ℹ The deprecated feature was likely used in the ProbBayes package.
## Please report the issue to the authors.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
NNB Discuss findings and comment on the utility of discrete prior(how
cant be 0.45)
I am going to use the beta.select function to choose shape parameters a and b to reflect my belief that that θ ∈ [0.3, 0.45] is more likely than θ is not an element of [0.3, 0.45]. I’m going to specify that