How to Use the rmvnorm() Function in R


Often you may want to generate a multivariate normal distribution in R.

One of the easiest ways to do so is by using the rmvnorm() function from the fourPNO package in R, which is designed to perform this exact task.

The rmvnorm() function uses the following syntax:

rmvnorm(n, mu, sigma)

where:

  • n: Number of observations to generate
  • mu: A vector that represents the means of the normal distributions
  • sigma: The covariance matrix

Note: The multivariate normal distribution is simply an extension of the normal distribution to higher dimensions. This distribution is used in a variety of scenarios in the real-world in which two random variables tend to be correlated.

The following examples show how to use the rmvnorm() function in practice to generate a multivariate normal distribution in R.

Example: How to Use the rmvnorm() Function in R

We can use the following syntax to generate a random multivariate normal distribution that contains 20 total observations:

library(fourPNO)

#make this example reproducible
set.seed(1)

#generate random multivariate normal distribution with 20 observations
rmvnorm(20, c(0,0), diag(2))

             [,1]        [,2]
 [1,] -0.62645381  0.91897737
 [2,]  0.18364332  0.78213630
 [3,] -0.83562861  0.07456498
 [4,]  1.59528080 -1.98935170
 [5,]  0.32950777  0.61982575
 [6,] -0.82046838 -0.05612874
 [7,]  0.48742905 -0.15579551
 [8,]  0.73832471 -1.47075238
 [9,]  0.57578135 -0.47815006
[10,] -0.30538839  0.41794156
[11,]  1.51178117  1.35867955
[12,]  0.38984324 -0.10278773
[13,] -0.62124058  0.38767161
[14,] -2.21469989 -0.05380504
[15,]  1.12493092 -1.37705956
[16,] -0.04493361 -0.41499456
[17,] -0.01619026 -0.39428995
[18,]  0.94383621 -0.05931340
[19,]  0.82122120  1.10002537
[20,]  0.59390132  0.76317575

We can see that 20 observations have been generated that follow a multivariate normal distribution.

By specifying c(0, 0) for the mu argument, we specified that each random variable should follow a normal distribution with a mean value of 0.

If we’d like to visualize this distribution of values on a 2-D scatterplot, we can store the results in a data frame and then use the plot() function to visualize each observation.

We can use the following syntax to do so:

library(fourPNO)

#make this example reproducible
set.seed(1)

#generate random multivariate normal distribution with 20 observations
data <- as.data.frame(rmvnorm(20, c(0,0), diag(2)))

#rename columns of data frame
names(data) <- c('x', 'y')

#plot distribution on scatterplot
plot(data$x, data$y)

This produces the following plot:

The x-axis displays the values from the first random normal distribution and the y-axis displays the values from the second random normal distribution.

Since the data follows a random multivariate normal distribution, we expect the points to be scattered about the plot in a mostly random manner around the values 0 on each axis, since these are the values that we specified for the normal distribution of each variable.

If we’d like, we can create an even larger sample of data by specifying 200 for the number of observations to be used when generating the distribution.

We can use the following syntax to create this distribution and then plot it once again, this time using pch=16 in the plot() function to specify that the points in the plot should be filled in:

library(fourPNO)

#make this example reproducible
set.seed(1)

#generate random multivariate normal distribution with 200 observations
data <- as.data.frame(rmvnorm(200, c(0,0), diag(2)))

#rename columns of data frame
names(data) <- c('x', 'y')

#plot distribution on scatterplot
plot(data$x, data$y, pch=16)

This produces the following plot:

random multivariate normal distribution in R

We can see that the points in the plot are randomly scattered about the coordinates of (0, 0), just as we expected.

Note: Feel free to specify whatever values you would like for the mu argument of the rmvnorm() function to generate a random multivariate normal distribution centered around different coordinates.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use the Triangular Distribution in R
How to Use the Multinomial Distribution in R
How to Use the Gamma Distribution in R
How to Plot a Beta Distribution in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *