Simulate Bivariate and Multivariate Normal Distribution in R (original) (raw)

Last Updated : 26 Jan, 2022

In this article, we will learn how to simulate Bivariate and Multivariate Normal distribution in the R Programming Language.

To simulate a Multivariate Normal Distribution in the R Language, we use the mvrnorm() function of the MASS package library. The mvrnorm() function is used to generate a multivariate normal distribution of random numbers with a specified mean value in the R Language. The mvrnorm() function takes random sample size, a vector with mean for each variable in final distribution, and a positive-definite symmetric matrix specifying the covariance matrix of the variables as an argument and returns a multivariate matrix with required normal distribution.

Syntax: mvrnorm( n, mu, sigma )

where,

Simulate Bivariate Normal Distribution

To simulate a bivariate normal distribution, we will restrict the arguments of the mvrnorm() function to two variable values. We will use two values in the mean vector and a 2X2 matrix as mu and sigma argument respectively. In this way, the mvrnorm() function will create a bivariate normal distribution instead of the multivariate normal distribution.

Example: Here, is a simulation of bivariate data distribution.

R `

load library MASS

library(MASS)

set seed and create data vectors

set.seed(98989) sample_size <- 100
sample_meanvector <- c(10, 5)
sample_covariance_matrix <- matrix(c(10, 5, 2, 9), ncol = 2)

create bivariate normal distribution

sample_distribution <- mvrnorm(n = sample_size, mu = sample_meanvector, Sigma = sample_covariance_matrix)

print top of distribution

head(sample_distribution)

`

Output:

Simulate Multivariate Normal Distribution:

To simulate a Multivariate normal distribution, we will use the mvrnorm() function of the MASS package library. If we need n-variable distribution, we will use n variable mean vector and nXn matrix as mu and sigma argument respectively for the mvrnorm() function. In this way, the mvrnorm() function will create a required variable normal distribution.

Example: Here, is a simulation of 5 variable data distribution.

R `

load library MASS

library(MASS)

set seed and create data vectors

set.seed(98989) sample_size <- 1000
sample_meanvector <- c(10, 5, 7, 9, 20)
sample_covariance_matrix <- matrix(c(5, 4, 3, 2, 1, 4, 5, 4, 3, 2, 3, 4, 5, 4, 3, 2, 3, 4, 5, 4, 1, 2, 3, 4, 5), ncol = 5)

create multivariate normal distribution

sample_distribution <- mvrnorm(n = sample_size, mu = sample_meanvector, Sigma = sample_covariance_matrix)

print top of distribution

head(sample_distribution)

`

Output: