Evaluates and plots the posterior density for pi, the probability of a success in a Bernoulli trial, with binomial sampling and a discrete prior on pi
1 2 
x 
the number of observed successes in the binomial experiment. 
n 
the number of trials in the binomial experiment. 
pi 
a vector of possibilities for the probability of success in a
single trial. if 
pi.prior 
the associated prior probability mass. 
n.pi 
the number of possible pi values in the prior 
plot 
if 
suppressOutput 
if 
A list will be returned with the following components:
pi 
the vector of possible pi values used in the prior 
pi.prior 
the associated probability mass for the values in pi 
likelihood 
the scaled likelihood function for pi given x and n 
posterior 
the posterior probability of pi given x and n 
f.cond 
the conditional distribution of x given pi and n 
f.joint 
the joint distribution of x and pi given n 
f.marg 
the marginal distribution of x 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25  ## simplest call with 6 successes observed in 8 trials and a uniform prior
binodp(6,8)
## same as previous example but with more possibilities for pi
binodp(6, 8, n.pi = 100)
## 6 successes, 8 trials and a nonuniform discrete prior
pi = seq(0, 1, by = 0.01)
pi.prior = runif(101)
pi.prior = sort(pi.prior / sum(pi.prior))
binodp(6, 8, pi, pi.prior)
## 5 successes, 6 trials, nonuniform prior
pi = c(0.3, 0.4, 0.5)
pi.prior = c(0.2, 0.3, 0.5)
results = binodp(5, 6, pi, pi.prior)
## plot the results from the previous example using a sidebyside barplot
results.matrix = rbind(results$pi.prior,results$posterior)
colnames(results.matrix) = pi
barplot(results.matrix, col = c("red", "blue"), beside = TRUE,
xlab = expression(pi), ylab=expression(Probability(pi)))
box()
legend("topleft", bty = "n", cex = 0.7,
legend = c("Prior", "Posterior"), fill = c("red", "blue"))

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.
Please suggest features or report bugs with the GitHub issue tracker.
All documentation is copyright its authors; we didn't write any of that.