Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

rniw

Random draws from a Normal-Inverse-Wishart distribution.


Description

Generates random draws from a Normal-Inverse-Wishart (NIW) distribution. Can be used to compare prior to posterior parameter distributions.

Usage

rniw(n, lambda, kappa, Psi, nu)

Arguments

n

number of samples to draw.

lambda

location parameter. See Details.

kappa

scale parameter. See Details.

Psi

scale matrix. See Details

nu

degrees of freedom. See Details.

Details

The NIW distribution p(μ, Σ | λ, κ, Ψ, ν) is defined as

Σ \sim W^{-1}(Ψ, ν), \quad μ | Σ \sim N(λ, Σ/κ).

Value

Returns a list with elements mu and Sigma of sizes c(n,length(lambda)) and c(nrow(Psi),ncol(Psi),n).

See Also

Examples

d <- 4 # number of dimensions
nu <- 7 # degrees of freedom
Psi <- crossprod(matrix(rnorm(d^2), d, d)) # scale
lambda <- rnorm(d)
kappa <- 2
n <- 1e4

niw.sim <- rniw(n, lambda, kappa, Psi, nu)

# diagonal elements of Sigma^{-1} are const * chi^2
S <- apply(niw.sim$Sigma, 3, function(M) diag(solve(M)))

ii <- 2
const <- solve(Psi)[ii,ii]
hist(S[ii,], breaks = 100, freq = FALSE,
    main = parse(text = paste0("\"Histogram of \"*(Sigma^{-1})[", ii,ii,"]")),
    xlab = parse(text = paste0("(Sigma^{-1})[", ii,ii,"]")))
curve(dchisq(x/const, df = nu)/const,
     from = min(S[ii,]), to = max(S[ii,]), col = "red", add = TRUE)

# elements of mu have a t-distribution
mu <- niw.sim$mu

ii <- 4
const <- sqrt(Psi[ii,ii]/(kappa*(nu-d+1)))
hist(mu[,ii], breaks = 100, freq = FALSE,
    main = parse(text = paste0("\"Histogram of \"*mu[", ii, "]")),
    xlab = parse(text = paste0("mu[", ii, "]")))
curve(dt((x-lambda[ii])/const, df = nu-d+1)/const, add = TRUE, col = "red")

nicheROVER

(Niche) (R)egion and Niche (Over)lap Metrics for Multidimensional Ecological Niches

v1.0
GPL-2
Authors
Martin Lysy [aut, cre], Ashley D. Stasko [aut, ctb], Heidi K. Swanson [aut, ctb]
Initial release
2014-07-21

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.