[英]How do you generate multivariate Gaussian random numbers in R?
How do we generate data points following a Gaussian (normal) distribution in R? 我们如何在R中的高斯(正态)分布后生成数据点?
Suppose I want to generate points in 2d (or higher dimensional) space that follow a Gaussian distribution. 假设我想在2d(或更高维度)空间中生成遵循高斯分布的点。 How do I do this using R?
我如何使用R?
Gaussian distributions are for one dimensional random variables. 高斯分布用于一维随机变量。 You can generate them using
rnorm
. 您可以使用
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. 尝试
mvrnorm
在MASS
包,或rmvnorm
在mvtnorm
包。
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 . 进一步阅读:
?Distributions
和?Distributions
的CRAN任务视图 。
One dimensional: ?rnorm
. 一维:
?rnorm
。 More dimensions: install and load package mvtnorm and use rmvnorm()
. 更多维度:安装和加载包
rmvnorm()
并使用rmvnorm()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.