开发者

How do you generate multivariate Gaussian random numbers in R?

开发者 https://www.devze.com 2023-02-28 02:12 出处:网络
How do we generate data points following a Gaussian (normal) distribution in R? Suppose I want to generate points in 2d (or higher dimensiona开发者_如何学Pythonl) space that follow a Gaussian distri

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

Suppose I want to generate points in 2d (or higher dimensiona开发者_如何学Pythonl) space that follow a Gaussian distribution. How do I do this using R?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消