binomixp {Bolstad} | R Documentation |
Evaluates and plots the posterior density for pi, the probability of a success in a Bernoulli trial, with binomial sampling when the prior density for pi is a mixture of two beta distributions, beta(a_0,b_0) and beta(a_1,b_1).
binomixp(x, n, alpha0 = c(1, 1), alpha1 = c(1, 1), p = 0.5, ...)
x |
the number of observed successes in the binomial experiment. |
n |
the number of trials in the binomial experiment. |
alpha0 |
a vector of length two containing the parameters, a0 and b0, for the first component beta prior - must be greater than zero. By default the elements of alpha0 are set to 1. |
alpha1 |
a vector of length two containing the parameters, a1 and b1, for the second component beta prior - must be greater than zero. By default the elements of alpha1 are set to 1. |
p |
The prior mixing proportion for the two component beta priors. That is the prior is p*beta(a0,b0)+(1-p)*beta(a1,b1). p is set to 0.5 by default |
... |
additional arguments that are passed to |
A list will be returned with the following components:
pi |
the values of pi for which the posterior density was evaluated |
posterior |
the posterior density of pi given n and x |
likelihood |
the likelihood function for pi given x and n, i.e. the binomial(n,pi) density |
prior |
the prior density of pi density |
## simplest call with 6 successes observed in 8 trials and a 50:50 mix ## of two beta(1,1) uniform priors binomixp(6,8) ## 6 successes observed in 8 trials and a 20:80 mix of a non-uniform ## beta(0.5,6) prior and a uniform beta(1,1) prior binomixp(6,8,alpha0=c(0.5,6),alpha1=c(1,1),p=0.2) ## 4 successes observed in 12 trials with a 90:10 non uniform beta(3,3) prior ## and a non uniform beta(4,12). ## Plot the stored prior, likelihood and posterior results = binomixp(4, 12, c(3, 3), c(4, 12), 0.9)$mix par(mfrow = c(3,1)) y.lims = c(0, 1.1 * max(results$posterior, results$prior)) plot(results$pi,results$prior,ylim=y.lims,type='l' ,xlab=expression(pi),ylab='Density',main='Prior') polygon(results$pi,results$prior,col='red') plot(results$pi,results$likelihood,type='l', xlab = expression(pi), ylab = 'Density', main = 'Likelihood') polygon(results$pi,results$likelihood,col='green') plot(results$pi,results$posterior,ylim=y.lims,type='l' ,xlab=expression(pi),ylab='Density',main='Posterior') polygon(results$pi,results$posterior,col='blue')