怎样用R生成均匀随机数,正态随机数?卡方?
关键词:r生成正态分布随机数,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)
转载请注明:数据分析 » 怎样用R生成随机数_r生成正态分布随机数