R中随机生成的多次运行 [英] Multiple runs of random generation in R
本文介绍了R中随机生成的多次运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个变量 A、B 和 C,它们具有回报的时间序列.我会使用 MASS
包中的 mvrnorm
运行随机抽样,为 A、B 和 C 生成 30 个值.
I have three variables A, B, and C with a time-series of returns. I would to run a random sampling using mvrnorm
from package MASS
, to generate 30 values for A, B and C.
我希望能够重复此操作 10000 次,每次为 A、B、C 生成 30 个值.
I would like to be able to repeat this operation 10000 times, each time generating 30 values for A, B, C.
我使用以下方法执行一次迭代:
I execute one iteration using:
sim.ret = mvrnorm(n = 30, mu = mu, Sigma = sigma)
我怎样才能运行这个函数 10,000 次?然后我使用每次迭代的结果做一些计算.
How can I run this function 10,000 times? I then use the result of each iteration to do some computations.
任何帮助将不胜感激!谢谢.
Any help would be appreciated! Thanks.
推荐答案
使用replicate():
N = 10000
results = replicate(N, mvrnorm(n = 30, mu = mu, Sigma = sigma))
print(head(results, 10))
这篇关于R中随机生成的多次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文