Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you generate multivariate Gaussian random numbers in R?

Tags:

random

r

gaussian

How do we generate data points following a Gaussian (normal) distribution in R?

Suppose I want to generate points in 2d (or higher dimensional) space that follow a Gaussian distribution. How do I do this using R?

like image 697
Pradeep Avatar asked Dec 17 '22 15:12

Pradeep


2 Answers

Gaussian distributions are for one dimensional random variables. You can generate them using rnorm.

rnorm(100, mean = 3, sd = 2)

For the higher dimensional case you want a multivariate normal distribution instead. Try mvrnorm in the MASS package, or rmvnorm in the mvtnorm package.

library(mvtnorm)
rmvnorm(100, mean = c(3, 5), sigma = matrix(c(1, 0.5, 0.5, 2), nrow = 2))

Further reading: ?Distributions and the CRAN Task View on distributions.

like image 90
Richie Cotton Avatar answered Dec 19 '22 06:12

Richie Cotton


One dimensional: ?rnorm. More dimensions: install and load package mvtnorm and use rmvnorm().

like image 25
Nick Sabbe Avatar answered Dec 19 '22 05:12

Nick Sabbe