Bayesian modelling of a normal (Gaussian) distribution

Share:

Description

Bayesian estimation of centre (μ) and scale (σ) 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

1
2
3
4
5
6
Bnormal(y, priors=NULL,
            chains=3, sample=10000, burnin=100)

Bnormal2(y, priors=NULL, 
            chains=3, sample=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.

sample

the number of MCMC observations 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 samples 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.)

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 Bwiqid, which is a data frame with a column for each parameter in the model.

There are print and plot methods for class Bwiqid, as well as diagnostic plots.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 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.

Bnormal2(HB)  # with broad normal prior for mu, uniform for sigma
Bnormal2(HB, priors=list(muMean=1000, muSD=200, sigmaMode=20, sigmaSD=10))
 

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.