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

rwish

Random draws from a Wishart (or Inverse-Wishart) distribution.


Description

Generates a random samples from a Wishart distribution defined as W(Ψ, ν), or an Inverse-Wishart distribution defined as W^{-1}(Ψ, ν).

Usage

rwish(n, Psi, nu, inv = FALSE)

Arguments

n

number of samples to draw.

Psi

scale matrix.

nu

degrees of freedom.

inv

logical. Setting inv = TRUE returns random matrices from an Inverse-Wishart distribution. See Details.

Details

Setting inv = TRUE replaces Ψ by Psi^{-1} and inverts the output random matrices, such that they are being generated from an Inverse-Wishart W^{-1}(Ψ, ν) distribution.

Value

Returns an array of Wishart (or Inverse-Wishart) draws of size 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 matrix
n <- 1e4

Sigma <- rwish(n, Psi, nu)

# for any vector a, X = (a' Sigma a) has a const * chi^2 distribution
a <- rnorm(d)
X <- apply(Sigma, 3, function(S) crossprod(a, S %*% a))
const <- a %*% Psi %*% a

hist(X, breaks = 100, freq = FALSE,
    main = parse(text = "\"Histogram of \"*X==a*minute*Sigma*a"),
    xlab = parse(text = "X==a*minute*Sigma*a"))
curve(dchisq(x/const, df = nu)/const,
     from = min(X), to = max(X), col = "red", add = TRUE)

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.