博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
r 随机数
阅读量:4334 次
发布时间:2019-06-07

本文共 2236 字,大约阅读时间需要 7 分钟。

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

 

转载于:https://www.cnblogs.com/awishfullyway/p/6676996.html

你可能感兴趣的文章
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>
ActiveMQ学习笔记(1)----初识ActiveMQ
查看>>
Java与算法之(2) - 快速排序
查看>>
Windows之IOCP
查看>>
机器学习降维之主成分分析
查看>>
CTP2交易所成交回报
查看>>
WebSocket & websockets
查看>>
openssl 升级
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>