| rmnorm | R Documentation |
This function generates random numbers (i.e. variates) from the (conditional) multivariate normal distribution.
rmnorm(
n,
mean,
sigma,
given_ind = numeric(),
given_x = numeric(),
dependent_ind = numeric(),
is_validation = TRUE,
n_cores = 1L
)
n |
positive integer representing the number of random variates
to be generated from the (conditional) multivariate normal distribution.
If |
mean |
numeric vector representing an expectation of the multivariate normal vector (distribution). |
sigma |
positively defined numeric matrix representing the covariance matrix of the multivariate normal vector (distribution). |
given_ind |
numeric vector representing indexes of a multivariate
normal vector which are conditioned on the values given by the
|
given_x |
numeric vector whose |
dependent_ind |
numeric vector representing indexes of the unconditioned elements (components) of a multivariate normal vector. |
is_validation |
logical value indicating whether input
arguments should be validated. Set it to |
n_cores |
positive integer representing the number of CPU cores
used for parallel computing. Currently it is not recommended to set
|
This function uses Cholesky decomposition to generate multivariate normal variates from independent standard normal variates.
This function returns a numeric matrix whose rows are random
variates from the (conditional) multivariate normal distribution with the
mean equal to mean and the covariance equal to sigma.
If given_x and given_ind are also provided, then random
variates will be from the
conditional multivariate normal distribution. Please, see the details
section of cmnorm to get additional information on
the conditioning procedure.
# Consider a multivariate normal vector:
# X = (X1, X2, X3, X4, X5) ~ N(mean, sigma)
# Prepare the multivariate normal vector parameters
# the expected value
mean <- c(-2, -1, 0, 1, 2)
n_dim <- length(mean)
# the correlation matrix
cor <- c( 1, 0.1, 0.2, 0.3, 0.4,
0.1, 1, -0.1, -0.2, -0.3,
0.2, -0.1, 1, 0.3, 0.2,
0.3, -0.2, 0.3, 1, -0.05,
0.4, -0.3, 0.2, -0.05, 1)
cor <- matrix(cor, ncol = n_dim, nrow = n_dim, byrow = TRUE)
# the covariance matrix
sd_mat <- diag(c(1, 1.5, 2, 2.5, 3))
sigma <- sd_mat %*% cor %*% t(sd_mat)
# Simulate random variates from this distribution
rmnorm(n = 3, mean = mean, sigma = sigma)
# Simulate random variate from (X1, X3, X5 | X1 = -1, X4 = 1)
given_x <- c(-1, 1)
given_ind <- c(1, 4)
rmnorm(n = 1, mean = mean, sigma = sigma,
given_x = given_x, given_ind = given_ind)
# Simulate random variate from (X1, X3, X5 | X1 = -1, X4 = 1)
# and (X1, X3, X5 | X1 = 2, X4 = 3)
given_x = rbind(c(-1, 1), c(2, 3))
rmnorm(n = nrow(given_x), mean = mean, sigma = sigma,
given_x = given_x, given_ind = given_ind)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.