r 随机数

R软件一个显著的优点是它提供了丰富的随机数发生器,比SAS、Matlab方面很多,比Excel更不知方便到哪里去了。这无疑为统计学、工程学以及寿险精算学提供了很大的方便,比如我们要产生200个服从正态分布的随机数,在R里面用只需要键入“rnorm(200)”即可得到。操作如下:

>rnorm(200)

[1] -0.12071839  0.05257376 -0.23671186 -0.71413741  2.17362525 -0.25102710
[7] -1.41124223  1.78816754  0.95697026 -1.73518078 -0.71141662  1.27847876
[13] -1.50472436 -0.55267534  0.46788900  0.32977779  0.37641009 -1.22411512
[19] -1.82099431 -1.50026288……

rnorm()函数更通用的用法是rnorm(n,mean,sd),其中n为要产生随机数的个数,mean为所产生随机数的总体所具有的均值,sd是所产生的随机数的总体所具有的标准差。

要产生服从均值为2,标准差为3的随机数100个,在R中:

>rnorm(n=100,mean=2,sd=3)

当然也可以更简便:

>rnorm(100,2,3)


类似的还可以在stat包里得到更多分布的随机数,一些对应函数如下:

rexp The Exponential Distribution

rf The F Distribution

rgamma The Gamma Distribution

rgeom The Geometric Distribution

rhyper The Hypergeometric Distribution

rlnorm The Log Normal Distribution

rlogis The Logistic Distribution

rmultinom The Multinomial Distribution

rnbinom The Negative Binomial Distribution

rnorm The Normal Distribution

rpois The Poisson Distribution

rsignrank Distribution of the Wilcoxon Signed Rank Statistic

rt The Student t Distribution

runif The Uniform Distribution

rweibull The Weibull Distribution

rwilcox Distribution of the Wilcoxon Rank Sum Statistic

rexp The Exponential Distribution

rf The F Distribution

rgamma The Gamma Distribution

rgeom The Geometric Distribution

rhyper The Hypergeometric Distribution

rlnorm The Log Normal Distribution

rlogis The Logistic Distribution

rmultinom The Multinomial Distribution

rnbinom The Negative Binomial Distribution

rnorm The Normal Distribution

rpois The Poisson Distribution

rsignrank Distribution of the Wilcoxon Signed Rank Statistic

rt The Student t Distribution

runif The Uniform Distribution

rweibull The Weibull Distribution

rwilcox Distribution of the Wilcoxon Rank Sum Statistic

用法大致一样。


卡方呢??

解答:

Pearson (χ2)
用这个函数rchisq(n, df)

 

 

rnorm(n,mean=x,sd=y)
n:生成n个随机数
mean:随机数的均值
sd:标准差
 
rnorm(10,mean=0,sd=1)   #产生10个平均值为0,方差为1的随机数
 [1]  0.08934727 -0.95494386 -0.19515038  0.92552126  0.48297852 -0.59631064 -2.18528684
[8] -0.67486594 -2.11906119 -1.26519802
 
runif(10,min=3,max=11)  #产生10个最小值为3,最大值为11
 [1]  3.604562  7.275414  8.130853  7.205915  3.314251  7.366879  5.982105 10.690419
 [9]  5.058733  4.663613

 

posted @ 2017-04-07 10:41  holy_black_cat  阅读(629)  评论(0编辑  收藏  举报