如何在R中模拟具有固定限制的正态分布?
为了模拟正态分布,我们可以使用R中的rnorm函数,但是我们无法限制模拟值的范围。如果我们想模拟具有固定限制的此分布,则可以使用truncnorm包的truncnorm函数。在这个函数中,我们可以带或不带均值和标准差传递限制。
加载和安装truncnorm包:
>install.packages("truncnorm") >library(truncnorm)
示例
rtruncnorm(n=10,a=0,b=10)
[1] 0.76595522 0.33315633 1.29565988 0.67154230 0.04957334 0.38338705 [7] 0.75753005 0.65265304 0.63616552 0.45710877
rtruncnorm(n=50,a=0,b=100)
[1] 0.904997947 0.035692016 0.402963452 1.001102057 1.445190636 0.109245234 [7] 0.205630845 0.312428027 0.465876772 0.424647787 0.309222394 0.442172805 [13] 0.365503292 1.277570451 0.235747661 1.128447123 0.598871145 0.308236501 [19] 0.286879685 0.895142846 0.466224583 0.943387991 1.064863073 1.249080962 [25] 0.085842256 0.976095792 1.593634799 0.571843828 0.351724413 0.005417383 [31] 0.219855141 0.882968293 0.284468927 0.172589884 0.770687691 0.100598918 [37] 0.059773068 0.891556100 0.665798487 1.380145993 0.765260739 0.594810520 [43] 0.248493619 0.069473449 1.310351419 0.917684778 0.667651666 0.259194497 [49] 0.167753536 0.330810042
rtruncnorm(n=50,a=0,b=100,mean=50,sd=1)
[1] 48.43188 50.93369 50.88936 48.59129 48.52075 49.21784 49.26158 49.33846 [9] 49.96582 50.40672 49.71015 49.42354 49.67626 51.59695 48.93448 50.34689 [17] 51.99731 51.37153 49.12334 50.93896 49.27460 51.26491 51.32564 49.77611 [25] 48.93062 50.29788 50.68128 50.83197 50.89842 49.78674 50.49390 50.69491 [33] 51.10983 48.19102 50.75529 50.53974 49.45053 50.03020 51.42043 50.18587 [41] 47.93208 51.91030 50.50000 49.99998 49.65636 48.22464 49.06306 49.71244 [49] 50.30020 49.90839
rtruncnorm(n=50,a=100,b=1000,mean=50,sd=1)
[1] 100.0269 100.0092 100.0073 100.0349 100.0253 100.0212 100.0281 100.0131 [9] 100.0427 100.0017 100.0236 100.0160 100.0003 100.0083 100.0333 100.0551 [17] 100.0891 100.0014 100.0078 100.0125 100.0075 100.0224 100.0208 100.0357 [25] 100.0280 100.0054 100.0066 100.0291 100.0282 100.0031 100.0226 100.0220 [33] 100.0086 100.0018 100.0157 100.0019 100.0002 100.0149 100.0083 100.0199 [41] 100.0369 100.0041 100.0128 100.0206 100.0437 100.0164 100.0144 100.0192 [49] 100.0011 100.0236
rtruncnorm(n=50,a=100,b=1000,mean=500,sd=50)
[1] 595.5563 429.8853 540.3906 478.8280 431.6173 448.3574 521.7457 526.5902 [9] 494.9212 515.5765 421.4012 526.5601 582.1344 470.9365 451.7576 486.2514 [17] 444.7247 433.0940 529.2073 543.7203 431.2336 546.4882 405.7282 437.9558 [25] 504.2556 547.4954 514.5959 496.1003 584.9742 516.8204 500.2668 528.0662 [33] 516.5181 475.7749 527.6779 550.5650 533.0702 426.9620 519.6474 481.8744 [41] 502.4718 489.8762 558.6940 501.7477 425.1007 497.6584 546.2818 529.8647 [49] 375.1648 476.3856
rtruncnorm(n=50,a=100,b=200,mean=110,sd=5)
[1] 113.1825 105.8578 104.3472 106.5285 112.1612 111.7534 103.0770 109.6500 [9] 107.8533 106.5465 107.8265 109.5017 106.1975 110.3207 107.9621 111.5617 [17] 104.9291 109.8316 112.7709 108.7816 112.3979 102.2871 121.4476 112.6206 [25] 112.0858 108.1987 105.6250 108.7097 109.6807 107.6362 102.9960 105.7521 [33] 109.5967 110.5990 114.3580 104.9275 111.2434 111.5357 116.5991 113.9652 [41] 102.3476 110.6442 106.7582 108.8348 109.9405 115.4166 110.0341 110.5289 [49] 107.1778 110.0811
rtruncnorm(n=50,a=100,b=200,mean=150,sd=5)
[1] 142.9484 152.6989 146.2057 145.6735 148.5461 147.9396 147.3329 156.8228 [9] 151.5704 146.8341 145.9517 147.5259 148.7496 161.7972 150.6340 142.4240 [17] 144.7043 153.4120 148.7925 143.5692 143.9185 141.9358 156.4370 135.4372 [25] 152.0932 151.2913 151.2202 149.4101 149.7199 152.1015 146.9029 145.0410 [33] 150.3115 156.3192 150.4947 156.8857 155.3724 147.3547 154.7947 150.1900 [41] 152.4731 144.9843 148.2241 150.2902 145.1237 147.7011 153.4473 151.6803 [49] 149.5316 151.8152
rtruncnorm(n=50,a=1,b=10,mean=4.5,sd=0.96)
[1] 2.764336 4.321889 5.497576 5.268754 2.999608 5.234163 4.547379 3.709919 [9] 4.230514 4.918631 4.684352 5.333103 4.222377 3.933808 3.785856 5.055154 [17] 3.517594 6.469732 5.119742 3.884569 4.707952 6.648635 3.269040 5.608978 [25] 3.787292 4.409029 5.088346 5.855674 3.771203 4.145066 3.969038 5.237388 [33] 3.357416 5.847389 5.399381 4.363114 3.449466 4.213360 5.310839 3.639487 [41] 4.912864 4.811979 3.923816 3.702720 4.293520 5.311537 4.590208 5.074307 [49] 4.035909 3.574105
round(rtruncnorm(n=50,a=1,b=10,mean=4.5,sd=0.96),0)
[1] 5 4 4 5 4 4 6 5 3 4 6 2 5 5 3 6 4 5 5 5 6 4 4 2 4 4 5 5 3 6 4 3 4 4 3 5 4 5 [39] 6 4 5 5 4 4 5 3 5 4 4 5
round(rtruncnorm(n=100,a=1,b=50,mean=25,sd=2.5),0)
[1] 27 26 24 26 24 28 26 26 25 22 25 24 24 27 28 22 24 23 27 26 27 25 28 25 24 [26] 20 26 27 24 25 26 26 28 25 25 30 29 26 21 24 25 23 25 25 22 27 26 24 25 25 [51] 25 28 24 22 22 19 30 28 28 26 26 25 21 24 25 27 26 24 24 22 31 25 27 29 23 [76] 20 22 26 25 25 25 26 25 25 24 25 29 24 27 22 24 20 27 26 28 28 26 30 25 25
round(rtruncnorm(n=100,a=1,b=5,mean=4,sd=2.5),2)
[1] 2.75 3.03 1.22 4.63 4.34 2.33 4.25 3.92 4.36 1.86 4.68 4.73 1.67 3.25 1.35 [16] 2.75 2.36 4.66 3.74 3.16 1.72 2.75 3.96 1.97 1.27 3.93 3.41 2.05 4.93 2.65 [31] 1.95 4.70 2.36 3.71 3.61 3.54 2.28 4.77 1.93 4.50 3.18 3.11 2.67 4.66 3.78 [46] 2.59 1.30 2.34 4.54 3.77 3.80 3.85 4.66 2.51 4.82 4.10 1.80 4.11 2.87 3.27 [61] 4.21 3.27 3.37 3.34 2.56 3.55 1.74 2.53 4.01 4.94 4.55 2.88 4.16 4.40 4.32 [76] 4.18 2.63 2.07 1.53 4.79 3.49 4.22 4.17 4.99 1.78 1.07 4.90 4.39 3.17 3.90 [91] 4.15 3.34 4.04 3.72 4.61 3.27 2.20 4.66 1.88 2.74
round(rtruncnorm(n=100,a=10,b=99,mean=54,sd=5),1)
[1] 55.6 47.7 57.3 58.8 49.6 56.4 56.7 50.5 59.2 53.2 61.3 46.1 49.2 58.7 55.6 [16] 53.5 57.5 58.0 57.1 54.7 51.3 61.1 50.6 52.5 55.5 56.2 54.0 52.0 67.2 51.6 [31] 50.3 58.5 50.1 49.5 55.4 51.9 57.0 45.2 45.1 58.8 47.4 55.3 59.7 54.1 53.1 [46] 60.1 50.8 57.8 54.3 55.0 45.9 43.5 59.0 55.7 49.5 48.2 53.1 54.8 54.6 52.6 [61] 56.0 53.0 55.5 52.2 60.6 52.4 55.9 60.2 54.7 58.3 56.5 54.3 53.3 56.0 50.1 [76] 49.9 55.5 56.8 45.2 49.7 55.3 51.3 44.2 57.9 52.3 55.1 54.0 53.0 47.1 56.6 [91] 56.6 49.0 56.5 58.7 52.7 63.0 59.3 55.9 55.1 52.3
广告