Learn R Programming

sortinghat (version 0.1)

simdata_normal: Generates random variates from K multivariate normal populations.

Description

We generate $n_k$ observations $(k = 1, \ldots, K)$ from each of $K$ multivariate normal distributions. Let the $k$th population have a $p$-dimensional multivariate normal distribution, $N_p(\mu_k, \Sigma_k)$ with mean vector $\mu_k$ and positive-definite covariance matrix $\Sigma_k$.

Usage

simdata_normal(n, mean, cov, seed = NULL)

Arguments

n
a vector (of length K) of the sample sizes for each population
mean
a vector or a list (of length K) of mean vectors
cov
a symmetric matrix or a list (of length K) of symmetric covariance matrices.
seed
seed for random number generation (If NULL, does not set seed)

Value

  • named list containing: [object Object],[object Object]

Details

The number of populations, K, is determined from the length of the vector of sample sizes, code{n}. The mean vectors and covariance matrices each can be given in a list of length K. If one covariance matrix is given (as a matrix or a list having 1 element), then all populations share this common covariance matrix. The same logic applies to population means.

Examples

Run this code
# Generates 10 observations from each of two multivariate normal populations
# with equal covariance matrices.
mean_list <- list(c(1, 0), c(0, 1))
cov_identity <- diag(2)
data_generated <- simdata_normal(n = c(10, 10), mean = mean_list,
                                 cov = cov_identity, seed = 42)
dim(data_generated$x)
table(data_generated$y)

# Generates 10 observations from each of three multivariate normal
# populations with unequal covariance matrices.
set.seed(42)
mean_list <- list(c(-3, -3), c(0, 0), c(3, 3))
cov_list <- list(cov_identity, 2 * cov_identity, 3 * cov_identity)
data_generated2 <- simdata_normal(n = c(10, 10, 10), mean = mean_list,
                                  cov = cov_list)
dim(data_generated2$x)
table(data_generated2$y)

Run the code above in your browser using DataLab