如何在R中将仿真重复固定次数?
通常,我们会模拟R中来自不同分布的随机值。基本R为相同的函数提供了一些内置函数,如果我们想重复进行固定次数的模拟,则可以一次又一次地编写这些内置函数。但是我们可以在复制函数的帮助下使用一行代码进行多次仿真,这意味着如果我们想对十个均匀随机变量进行十次仿真,则可以使用复制函数来完成。
例子
replicate(10,runif(5,2,5)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 4.021137 4.973298 2.423433 3.485005 3.993855 4.860645 4.958935 3.091090 [2,] 2.284574 2.529052 2.579930 2.249342 2.937469 4.436915 2.880817 3.327777 [3,] 3.477788 4.440306 4.524055 3.061653 3.217069 4.346546 3.198053 2.470142 [4,] 3.384656 2.205340 4.159742 4.907626 4.988232 2.803634 4.436395 3.746616 [5,] 3.125650 3.201349 2.801636 3.874143 4.565247 4.286455 2.231455 4.910487 [,9] [,10] [1,] 4.968500 2.807881 [2,] 2.529356 3.407753 [3,] 3.626391 2.515400 [4,] 3.152912 3.107568 [5,] 4.028492 4.176216 replicate(10,rnorm(10)) [,1] [,2] [,3] [,4] [,5] [,6] [1,] -0.03472603 -0.1587546 -1.0479844 2.49766159 1.51974503 1.80314191 [2,] 0.78763961 1.4645873 1.4411577 0.66706617 -0.30874057 -0.33113204 [3,] 2.07524501 -0.7660820 -1.0158475 0.54132734 -1.25328976 -1.60551341 [4,] 1.02739244 -0.4302118 0.4119747 -0.01339952 0.64224131 0.19719344 [5,] 1.20790840 -0.9261095 -0.3810761 0.51010842 -0.04470914 0.26317565 [6,] -1.23132342 -0.1771040 0.4094018 -0.16437583 -1.73321841 -0.98582670 [7,] 0.98389557 0.4020118 1.6888733 0.42069464 0.00213186 -2.88892067 [8,] 0.21992480 -0.7317482 1.5865884 -0.40024674 -0.63030033 -0.64048170 [9,] -1.46725003 0.8303732 -0.3309078 -1.37020788 -0.34096858 0.57050764 [10,] 0.52102274 -1.2080828 -2.2852355 0.98783827 -1.15657236 -0.05972328 [,7] [,8] [,9] [,10] [1,] -0.098178744 -2.00016494 1.1462284 0.2700549 [2,] 0.560820729 -0.54479074 -2.4030962 -0.4221840 [3,] -1.186458639 -0.25567071 0.5727396 -1.1891133 [4,] 1.096777044 -0.16612104 0.3747244 -0.3310330 [5,] -0.005344028 1.02046391 -0.4252677 -0.9398293 [6,] 0.707310667 0.13622189 0.9510128 -0.2589326 [7,] 1.034107735 0.40716760 -0.3892372 0.3943792 [8,] 0.223480415 -0.06965481 -0.2843307 -0.8518571 [9,] -0.878707613 -0.24766434 0.8574098 2.6491669 [10,] 1.162964556 0.69555081 1.7196273 0.1560117 replicate(10,rpois(10,2)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 4 2 1 1 2 3 4 3 4 4 [2,] 2 0 2 5 1 4 1 1 1 1 [3,] 0 1 1 2 3 1 2 2 1 4 [4,] 4 3 0 0 2 3 2 1 3 1 [5,] 3 4 3 2 3 5 3 1 2 3 [6,] 1 3 2 2 2 5 1 2 1 1 [7,] 0 1 2 0 2 1 1 1 0 0 [8,] 0 2 2 2 2 1 1 2 0 4 [9,] 3 1 3 5 1 5 2 2 3 3 [10,] 3 5 3 3 2 0 0 4 2 4 replicate(10,rbinom(10,5,0.5)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 3 2 3 3 1 4 3 0 3 2 [2,] 4 3 3 3 3 4 2 3 3 3 [3,] 2 1 4 1 4 3 1 4 3 3 [4,] 2 3 3 3 3 3 3 3 1 2 [5,] 3 2 3 3 2 3 2 2 3 2 [6,] 1 3 2 3 3 1 2 4 2 2 [7,] 2 4 2 2 3 2 0 2 2 2 [8,] 3 2 1 2 1 3 1 3 4 4 [9,] 3 2 2 0 5 4 2 2 1 1 [10,] 4 3 2 4 2 3 3 1 1 3 replicate(10,rexp(10)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 0.7822614 0.64055999 0.03590114 1.1074904 0.02485982 0.09323768 2.8820092 [2,] 0.8264378 3.52891739 0.25049067 1.2416415 1.16104002 0.72035249 2.0840196 [3,] 1.6971206 0.01968048 1.74404420 1.1209358 0.91593295 0.28866203 1.2839715 [4,] 0.5542900 2.05536616 1.08620612 0.2659871 0.45566092 0.43113440 0.7463572 [5,] 1.6520526 0.32750367 0.02387113 0.1682497 0.39945753 1.09981407 1.3789315 [6,] 0.1233453 1.70961534 0.49022027 0.4341817 0.53964602 0.03520212 0.1551040 [7,] 1.7054235 0.24085585 0.78616815 1.0889313 1.29652089 0.02508916 4.6104278 [8,] 0.1305380 0.86247276 0.35421220 0.1085667 0.15360932 0.44083157 0.7275564 [9,] 0.4543620 0.72478187 1.08493705 0.4859448 2.30119268 1.40085866 1.2067531 [10,] 0.3309523 3.01016040 0.02198192 0.6393966 0.35040744 0.38360324 1.0219670 [,8] [,9] [,10] [1,] 0.6233317 4.3840724 0.04492254 [2,] 1.1629399 1.6790634 0.57846298 [3,] 0.1625552 0.6637980 0.38950670 [4,] 1.0158195 0.5336855 2.14465830 [5,] 6.5845330 0.7842684 3.61503168 [6,] 2.1322730 1.8916008 0.80238862 [7,] 2.4546722 1.4278140 0.40788367 [8,] 4.8369268 1.5715413 0.22545376 [9,] 1.0361297 2.2472477 0.02716078 [10,] 2.1053047 3.1449373 0.44848939 replicate(10,rnorm(10,mean=0,sd=1)) [,1] [,2] [,3] [,4] [,5] [,6] [1,] -0.55654217 0.6963785 1.24215556 -0.4450052 0.19801302 -0.604711661 [2,] -0.18058414 0.9151825 0.62015220 0.0696951 0.39734910 0.539054406 [3,] 1.44744029 -0.9233743 0.09990307 -0.1546717 0.02922549 -0.076830886 [4,] -0.60713145 1.1468733 1.80770349 -0.8312635 2.56027339 1.849919560 [5,] 0.67936243 -0.6358650 -1.50242998 0.7615444 1.25712771 -0.854907551 [6,] -0.09355764 -0.8864433 0.28564047 -0.5765069 -0.53453769 0.032637295 [7,] -0.49008629 -2.3331367 0.84570696 -0.6263682 -0.62522743 -1.025059481 [8,] 1.41065938 -0.1454908 -0.99534264 0.4813353 0.91384869 -0.982249076 [9,] -0.22457379 0.3169640 -0.25685412 1.6952711 1.00719953 0.004101957 [10,] -0.21249549 -0.7074700 -0.05585604 -1.7612263 0.71929182 -0.233427178 [,7] [,8] [,9] [,10] [1,] -0.49888822 -0.99119725 -1.2128550 -0.1948464 [2,] 1.54971296 -0.09449734 -0.2189642 -0.2952820 [3,] 0.08749692 -2.87514168 0.5642682 0.4966404 [4,] 1.31870113 -0.24686610 -0.5254344 0.4849128 [5,] -0.98122412 0.01474449 0.7443742 0.0187845 [6,] -0.24562259 -1.91908770 0.1289818 0.6347746 [7,] -1.40393384 -0.28781374 1.4882743 0.7544441 [8,] 1.44089315 -0.34663745 -0.6626820 0.8335890 [9,] -0.98135999 -1.83958858 -1.1606550 0.9657613 [10,] 1.47424490 0.89858894 0.3587742 1.2938800