Bnormal: Bayesian modelling of a normal (Gaussian) distribution

View source: R/Bnormal.R

Bayesian normal estimationR Documentation

Bayesian modelling of a normal (Gaussian) distribution

Description

Bayesian estimation of centre (μ) and scale (spread) (σ) of a normal distribution based on a sample. Bnormal uses a gamma prior on the precision, τ = 1/σ^2, while Bnormal2 applies a gamma prior to σ.

Usage

Bnormal(y, priors=NULL,
            chains=3, draws=10000, burnin=100, ...)

Bnormal2(y, priors=NULL,
            chains=3, draws=3e4, burnin=0, thin=1, adapt=1000,
            doPriorsOnly=FALSE, parallel=NULL, seed=NULL, ...)

Arguments

y

a vector (length > 1) with observed sample values; missing values not allowed.

priors

an optional list with elements specifying the priors for the centre and scale; see Details.

doPriorsOnly

if TRUE, Bnormal2 returns MCMC chains representing the prior distributions, not the posterior distributions for your data set.

chains

the number of MCMC chains to run.

draws

the number of MCMC draws per chain to be returned.

thin

thinning rate. If set to n > 1, n steps of the MCMC chain are calculated for each one returned. This is useful if autocorrelation is high.

burnin

number of steps to discard as burn-in at the beginning of each chain.

adapt

number of steps for adaptation.

seed

a positive integer (or NULL): the seed for the random number generator, used to obtain reproducible output if required.

parallel

if NULL or TRUE and > 3 cores are available, the MCMC chains are run in parallel. (If TRUE and < 4 cores are available, a warning is given.)

...

other arguments to pass to the function.

Details

The function generates vectors of random draws from the posterior distributions of the population centre (μ) and scale (σ). Bnormal uses a Gibbs sampler implemented in R, while Bnormal2 uses JAGS (Plummer 2003).

Priors for all parameters can be specified by including elements in the priors list. For both functions, μ has a normal prior, with mean muMean and standard deviation muSD. For Bnormal, a gamma prior is used for the precision, τ = 1\σ^2, with parameters specified by tauShape and tauRate. For Bnormal2, a gamma prior is placed on σ, with parameters specified by mode, sigmaMode, and SD, sigmaSD.

When priors = NULL (the default), Bnormal uses improper flat priors for both μ and τ, while Bnormal2 uses a broad normal prior (muMean = mean(y), muSD = sd(y)*5) for μ and a uniform prior on (sd(y) / 1000, sd(y) * 1000) for σ.

Value

Returns an object of class mcmcOutput.

Author(s)

Mike Meredith, Bnormal based on code by Brian Neelon, Bnormal2 adapted from code by John Kruschke.

References

Kruschke, J K. 2013. Bayesian estimation supersedes the t test. Journal of Experimental Psychology: General 142(2):573-603. doi: 10.1037/a0029146

Plummer, Martyn (2003). JAGS: A Program for Analysis of Bayesian Graphical Models Using Gibbs Sampling, Proceedings of the 3rd International Workshop on Distributed Statistical Computing (DSC 2003), March 20-22, Vienna, Austria. ISSN 1609-395X

Examples

# Generate a sample from a normal distribution, maybe the head-body length of a
#   carnivore in mm:
HB <- rnorm(10, 900, 15)
Bnormal(HB)  # with improper flat priors for mu and tau
Bnormal(HB, priors=list(muMean=1000, muSD=200))
Bnormal(HB, priors=list(muMean=1, muSD=0.2)) # a silly prior produces a warning.

if(requireNamespace("rjags")) {
  Bnormal2(HB, chains=2)  # with broad normal prior for mu, uniform for sigma
  Bnormal2(HB, chains=2, priors=list(muMean=1000, muSD=200, sigmaMode=20, sigmaSD=10))
}

wiqid documentation built on Nov. 18, 2022, 1:07 a.m.