credInt: Calculate a credible interval from a numerically specified...

Description Usage Arguments Details Value Examples

View source: R/credInt.r

Description

Calculates a lower, upper, or two-sided credible interval from the numerical posterior CDF or from a sample from the posterior.

Usage

1
credInt(theta, cdf = NULL, conf = 0.95, type="twosided")

Arguments

theta

either a sample from the posterior density or the values over which the the posterior CDF is specified

cdf

the values of the CDF, F(θ) = \int_{-∞}^{θ}f(t).df where f(t) is the PDF. This only needs to be specified if a numerically specified posterior is being used

conf

the desired 'confidence' level

type

the type of interval to return, 'lower' = one sided lower bound, 'two-sided' = two - sided, or 'upper' = one sided upper bound. It is sufficient to use 'l','t' or 'u'

Details

This function uses linear interpolation to calculate bounds for points that may not be specified by CDF

Value

a list containing the elements lower.bound, uppper.bound or both depending on type

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
## commands for calculating a numerical posterior CDF.
## In this example, the likelihood is proportional to
## \eqn{\theta^{3/2}\times \exp(-\theta/4)} and a N(6, 9) prior is used.
theta <- seq(from = 0.001, to = 40, by = 0.001)
prior <- dnorm(theta,6,3)
ppnLike <- theta^1.5*exp(-theta/4)
ppnPost <- prior*ppnLike
scaleFactor <- sintegral(theta, ppnPost)$int
posterior <- ppnPost/scaleFactor
cdf <- sintegral(theta, posterior)$y
ci<-credInt(theta, cdf)
par(mfrow=c(2,2))
plot(prior ~ theta, type = 'l',  main = "Prior N(6, 9)")
plot(ppnLike ~ theta, type = 'l', main = "Proportional likelihood")
plot(posterior ~ theta, type = 'l', main = "Posterior")
abline(v=c(unlist(ci)))

## Use an inverse method to take a random sample of size 1000
## from the posterior
suppressWarnings(Finv<-approxfun(cdf,theta))
thetaSample<-Finv(runif(1000))
ci<-credInt(thetaSample)

Example output

Credible interval is : (2.02114790458962,11.4246333371552)
Credible interval is (2.04906582319898,11.4611942703114)

Bolstad2 documentation built on May 2, 2019, 2:16 a.m.