Solved – How to calculate a 95% Credible Interval for a Bernouli sampling in R

bayesiancredible-intervalr

How does one calculate a 95% Credible Interval for Bernouli sampling?
I have looked into the package emdbook and the function tcredint() but i am unsure if this the way to go.

If anyone has references, I'm grateful.

Create data with 2000 rows of (0,1)
set.seed(100)
data<-data.frame(replicate(1,sample(0:1,2000,replace=TRUE)))
colnames(data) <- c("sample")
require(psych)
mean(data$sample)
sd(data$sample)'

Confidence Interval
require(Publish)
ci.mean(data)]

Credible Interval
install.packages("gmodels")
require(gmodels)
require(stats)
p=pbeta(data$sample, shape1=2, shape2=2, ncp = 0, lower.tail = TRUE, log.p = FALSE)
ci(p)

Best Answer

Technically, you've not supplied enough information to answer as we'd need to know your prior.

However, I will assume you're looking at a $p\sim$Beta$(\alpha, \alpha)$ prior. In that case, if you have $N$ trials with a total of $Y$ successes the posterior distribution is $p | \mathcal D \sim$Beta$(\alpha + N - Y, \alpha + Y)$.

You can use the distribution function for Beta to calculate the necessary tails using those parameters.

Related Question