3.1随机抽样
sample(1:40,5)#从1到40中随机取5个数,默认为无放回抽样
[1] 19 3 20 35 13
sample(c("H","t"),10,replace=T)#模拟10次投掷硬币,replace=T有放回抽样参数
[1] “H” “H” “t” “t” “t” “H” “t” “H” “t” “t”
3.2概率论计算和排列组合
sample(c("succ","fail"),10,replace=T,prob = c(0.9,0.1))#prob参数模拟具有不相等概率的数据
[1] “succ” “succ” “succ” “succ” “succ” “succ” “succ” “succ” “succ” “succ”
1/prod(40:36)#累乘函数
[1] 1.266449e-08
prod(5:1)/prod(40:36)
[1] 1.519738e-06
1/choose(40,5)#choose为组合函数
[1] 1.519738e-06
3.3离散分布
3.4连续分布
3.5R中的内置分布
3.5.1密度
x<-seq(-4,4,0.1)
plot(x,dnorm(x),type = "l")#绘制正态分布的密度函数线
curve(dnorm(x),from = -4,to=4)#同上
x<-0:50
plot(x,dbinom(x,size = 50,prob=.33),type = "h")#绘制n=50,p=0.33的二项分布图形,h为针形图
3.5.2累积分布函数
1-pnorm(160,mean=132,sd=13)#pnorm函数返回一个在给定均值为132,标准差为13的正态分布下取到小于160的概率
[1] 0.01562612
pbinom(16,size=20,prob = .5)#
[1] 0.9987116
1-pbinom(15,size=20,prob = .5)
[1] 0.005908966
pbinom(4,20,.5)#参数以正确的顺序给出,可不使用size和prob关键字
[1] 0.005908966
1-pbinom(15,20,.5)+pbinom(4,20,.5)#1-pbinom为16或更多
[1] 0.01181793
3.5.3分位数
xbar<-83
sigma<-12
n<-5
sem<-sigma/sqrt(n)#标准差为
sem
[1] 5.366563
xbar+sem*qnorm(0.025)#2.5%分位数点
[1] 72.48173
xbar+sem*qnorm(0.975)#97.5%分位数点
[1] 93.51827
3.5.4随机数
rnorm(10)
[1] 0.41956052 0.25759940 0.69012141 -0.47932737 -1.02003932
[6] 0.31874663 -0.53690831 -0.13565610 0.09019251 0.37229407
rnorm(10)
[1] 1.5106657 -1.0306662 -1.0183139 -0.4975699 0.5327055 -1.8277768
[7] 1.7719537 0.6349547 1.3073196 0.3095579
rnorm(10,mean=7,sd=5)#控制均值与方差
[1] 11.9441279 -3.6666113 0.4073246 2.0410807 10.4215748 5.7507122
[7] 7.4608988 12.4506186 4.4587588 -1.9938817
rbinom(10,size=20,prob=.5)#控制二项分布的n与p
[1] 6 11 11 6 8 10 14 11 14 10
|
请发表评论