我正在从多元正态分布中生成大量数据用于仿真。我想知道是否有人知道哪个命令最有效。如果是mvrnorm(来自“ MASS”软件包)或rmvnorm(来自“ mvtnorm”软件包)。
答案 0 :(得分:1)
通过选择不同的方法可以轻松回答这些问题。让
library(microbenchmark)
library(MASS)
library(mvtnorm)
n <- 10000
k <- 50
mu <- rep(0, k)
rho <- 0.2
Sigma <- diag(k) * (1 - rho) + rho
通过这种方式,我们有50个变量,其单位方差和相关性为0.2。产生10000个观测值
microbenchmark(mvrnorm(n, mu = mu, Sigma = Sigma),
rmvnorm(n, mean = mu, sigma = Sigma, method = "eigen"),
rmvnorm(n, mean = mu, sigma = Sigma, method = "svd"),
rmvnorm(n, mean = mu, sigma = Sigma, method = "chol"),
times = 100)
# Unit: milliseconds
# expr min lq mean median uq max neval cld
# mvrnorm(n, mu = mu, Sigma = Sigma) 65.04667 73.02912 85.30384 81.70611 92.69137 148.6959 100 a
# rmvnorm(n, mean = mu, sigma = Sigma, method = "eigen") 71.14170 81.08311 95.12891 88.84669 100.62174 237.0012 100 b
# rmvnorm(n, mean = mu, sigma = Sigma, method = "svd") 71.32999 81.30640 93.40939 88.54804 104.00281 208.3690 100 b
# rmvnorm(n, mean = mu, sigma = Sigma, method = "chol") 71.22712 78.59898 94.13958 89.04653 108.27363 158.7890 100 b
因此,mvrnorm
的性能可能会稍好一些。考虑到特定的应用程序,应将n
,k
和Sigma
设置为更适合此应用程序的值。