binodp {Bolstad}R Documentation

Binomial sampling with a discrete prior

Description

Evaluates and plots the posterior density for theta, the probability of a success in a Bernoulli trial, with binomial sampling and a discrete prior on theta

Usage

binodp(x,n,uniform = TRUE, n.theta = 10, theta = NULL, theta.prior = NULL, ret = FALSE)

Arguments

x the number of observed successes in the binomial experiment.
n the number of trials in the binomial experiment.
uniform if true then a discrete uniform prior for theta will be used.
n.theta the number of possible theta values in the prior
theta a vector of possibilities for the probability of success in a single trial.
theta.prior the associated prior probability mass.
ret if true then the likelihood and posterior are returned as a list.

Value

If ret is true, then a list will be returned with the following components:

f.cond the conditional distribution of x given theta and n
f.joint the joint distribution of x and theta given n
f.marg the marginal distribution of x
likelihood the scaled likelihood function of x given theta and n
posterior the posterior probability of theta given x and n
theta the vector of possible theta values used in the prior
theta.prior the associated probability mass for the values in theta

See Also

binobp binogcp

Examples

## 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 theta
binodp(6,8,n.theta=100)

## 6 successes, 8 trials and a non-uniform discrete prior
theta<-seq(0,1,by=0.01)
theta.prior<-runif(101)
theta.prior<-sort(theta.prior/sum(theta.prior))
binodp(6,8,uniform=FALSE,theta=theta,theta.prior=theta.prior)

## 5 successes, 6 trials, non-uniform prior
theta<-c(0.3,0.4,0.5)
theta.prior<-c(0.2,0.3,0.5)
results<-binodp(5,6,uniform=FALSE,theta=theta,theta.prior=theta.prior,ret=TRUE)

## plot the results from the previous example using a side-by-side barplot
results.matrix<-rbind(results$theta.prior,results$posterior)
colnames(results.matrix)<-theta
barplot(results.matrix,col=c("red","blue"),beside=TRUE
        ,xlab=expression(theta),ylab=expression(Probability(theta)))
box()
legend(1,0.65,legend=c("Prior","Posterior"),fill=c("red","blue"))

[Package Bolstad version 0.1-8 Index]