Learn R Programming

bayesSurv (version 2.6)

rWishart: Sample from the Wishart distribution

Description

Sample from the Wishart distribution $$\mbox{Wishart}(\nu, S),$$ where $nu$ are degrees of freedom of the Wishart distribution and $S$ is its scale matrix. The same parametrization as in Gelman (2004) is assumed, that is, if $W~Wishart(nu,S)$ then $$\mbox{E}(W) = \nu S$$.

In the univariate case, $Wishart(nu,S)$ is the same as $Gamma(nu/2, 1/(2*S)).$ Generation of random numbers is performed by the algorithm described in Ripley (1987, pp. 99).

Usage

rWishart(n, df, S)

Arguments

n
number of observations to be sampled.
df
degrees of freedom of the Wishart distribution.
S
scale matrix of the Wishart distribution.

Value

Matrix with sampled points (lower triangles of $W$) in rows.

References

Gelman, A., Carlin, J. B., Stern, H. S., and Rubin, D. B. (2004). Bayesian Data Analysis, Second edition. Boca Raton: Chapman and Hall/CRC.

Ripley, B. D. (1987). Stochastic Simulation. New York: John Wiley and Sons.

Examples

Run this code
### The same as rgamma(n, shape=df/2, rate=1/(2*S))
n <- 1000
df <- 1
S  <- 3
w <- rWishart(n=n, df=df, S=S)
mean(w)    ## should be close to df*S
var(w)     ## should be close to 2*df*S^2

### Multivariate Wishart
n <- 1000
df <- 2
S <- matrix(c(1,3,3,13), nrow=2)
w <- rWishart(n=n, df=df, S=S)
apply(w, 2, mean)                ## should be close to df*S
df*S

df <- 2.5
S <- matrix(c(1,2,3,2,20,26,3,26,70), nrow=3)
w <- rWishart(n=n, df=df, S=S)
apply(w, 2, mean)                ## should be close to df*S
df*S

Run the code above in your browser using DataLab