0.1 随机变量的概率分布

load("D:\\New_Folder\\Study_Programming\\R_Programme\\Applied Statistics\\datas - Copy\\example\\ch4\\example4_1.RData")

0.1.1 计算期望

mymean<-sum(example4_1$不合格品数*example4_1$概率)
mymean
## [1] 0.43

0.1.2 计算方差

myvar<-sum((example4_1$不合格品数-mymean)^2*example4_1$概率)
myvar
## [1] 0.7051
cat('sqrt(myvar)=',sqrt(myvar))
## sqrt(myvar)= 0.8397023

0.2 二项分布:Binary distribution

k=seq(0.1,0.9,0.1) #seq(from,to,lenghth)生成一组数据
par(mfrow=c(3,3),mai=c(0.6,0.5,0.2,0.1)) #布局绘图环境
for(i in 1:9)
  barplot(dbinom(0:5,5,k[i]),xlab="x",ylab="p",ylim=c(0,0.6),main=substitute(B(5,b),list(b=k[i])),col="red")

#注: dbinom(x,n,p) 表示进行n次成功概率为p的伯努力事件,成功次数的概率分布。

0.3 正态分布 Normal distribution

par(mai=c(0.75,0.75,0.1,0.1),cex=0.9)
curve(dnorm(x,-2,1),from=-6,to=2,xlim = c(-6,6),ylab="f(X)",lty=1,lwd=1.5,col="black")
abline(h=0) #添加一条x=0的水平线
segments(-2,0,-2,dnorm(-2,-2,1),lwd=1.5,col="black") #在两点间绘制一条线segments(0,0,1,1)
curve(dnorm(x,2,1),from=-2,to=6,add=TRUE,lty=2,lwd=1.5,col="blue")
abline(h=0) #添加一条x=0的水平线
segments(2,0,2,dnorm(2,2,1),lwd=1.5,col="blue",lty=2) #在两点间绘制一条线segments(x0,y0,x1,y1)
legend(x="topright",legend=c("N(-2,1)","N(2,1)"),lty=1:2,col=c("blue","black" ),cex=0.8)

curve(dnorm(x,0,sqrt(1/2)),from=-3,to=3,xlim = c(-4,4),ylab="f(X)",lty=1,lwd=1.5,col=2)
abline(h=0) #dnorm(x,mean,std)
segments(0,0,0,dnorm(0,0,sqrt(1/2)),lwd=1.5,col="blue",lty=2) #在两点间绘制一条线segments(x0,y0,x1,y1)
curve(dnorm(x,0,1),from=-4,to=4,add=TRUE,lty=2,lwd=1.5) #叠加图形到上一个图形
curve(dnorm(x,0,sqrt(2)),from=-4,to=4,add=TRUE,lty=3,lwd=1.5,col=4) #叠加图形到上一个图形
legend(x="topright",legend=c("N(0,0.5)","N(0,1)","N(0,2)"),lty=1:3,col=c(2,"black",4 ))

#N(均值,方差)

pnorm(x,mean,sd)为分布函数,计算随机变量小于等于某值的概率;
qnorm(p,mean,sd)为分位数函数

0.3.1 课本p115 例题

paste('pnorm(40,mean=50,sd=10)',pnorm(40,mean=50,sd=10))
## [1] "pnorm(40,mean=50,sd=10) 0.158655253931457"
pnorm(40,mean=50,sd=10)-pnorm(30,mean=50,sd=10)
## [1] 0.1359051
pnorm(2.5,mean=0,sd=1)
## [1] 0.9937903
pnorm(2,mean=0,sd=1)-pnorm(-1.5,mean=0,sd=1)
## [1] 0.9104427
pnorm(40,mean=50,sd=10)
## [1] 0.1586553
pnorm(40,mean=50,sd=10)-pnorm(30,mean=50,sd=10)
## [1] 0.1359051
pnorm(2.5,mean=0,sd=1)
## [1] 0.9937903
pnorm(2,mean=0,sd=1)-pnorm(-1.5,mean=0,sd=1)
## [1] 0.9104427
qnorm(0.025,mean=0,sd=1)
## [1] -1.959964

segments函数:用于画线

x <- stats::runif(12); y <- stats::rnorm(12)
i <- order(x, y); x <- x[i]; y <- y[i]
plot(x, y, main = "arrows(.) and segments(.)")
## draw arrows from point to point :
s <- seq(length(x)-1)  # one shorter than data
arrows(x[s], y[s], x[s+1], y[s+1], col= 1:3)
s <- s[-length(s)]
segments(x[s], y[s], x[s+2], y[s+2], col= 'pink')

0.4 \(\chi^2\)统计量

n=5000
df<-c(2,5,10,15,20,30)
par(mfrow=c(2,3))#类似于MATLAB中的subplot功能
for(i in 1:6)
{
  x<-rchisq(n,df[i])
  hist(x,xlim=c(0,60),prob=T,col='lightblue',xlab="x",
  ylab="Density",main=paste("df=",df[i]))
  curve(dchisq(x,df[i]),lwd=1.5,col=2,add=T)#注: dchisq为卡方分布的密度函数
}

pchisq(10,df=15) #自由度为15,卡方值小于10的概率  
## [1] 0.1802601
1-pchisq(15,df=25) #自由度为25,卡方值大于15的概率  
## [1] 0.9413826
qchisq(0.95,df=10) #右尾概率为0.05时的反函数  
## [1] 18.30704

0.5 t统计量

curve(dnorm(x,0,1),from=-3,to=3,xlim=c(-4,4),ylab="f(x)",lty=1,col=1)
abline(h=0)
segments(0,0,0,dnorm(0),col="blue",lty=2,lwd=1.5)
curve(dt(x,5),from=-4,to=4,add=TRUE,lty=2,col=2,lwd=1.5)
curve(dt(x,2),from=-4,to=4,add=TRUE,lty=3,col=4,lwd=1.5)
legend(x="topright",legend=c("N(0,1)","t(5)","t(2)"),lty=1:3,col=c(1,2,4))

pt(-2,df=10)
## [1] 0.03669402
1-pt(3,df=15)
## [1] 0.004486369
qt(0.975,df=25)
## [1] 2.059539

0.6 F分布

curve(df(x,10,20),from=0,to=5,xlim=c(0,5),ylab="f(x)",lty=1,lwd=1.5,col=1)
curve(df(x,5,10),from=0,to=5,add=TRUE,lty=2,lwd=1.5,col=2)
curve(df(x,3,5),from=0,to=5,add=TRUE,lty=3,lwd=1.5,col=4)
abline(h=0,v=0)
legend(x="topright",legend =c("F(10,20)","F(5,10)","F(3,5)"),lty=1:3,col=c(1,2,4))

pf(3,df1=10,df2=8)
## [1] 0.9335491
1-pf(2.5,df1=18,df2=15)
## [1] 0.03944963
qf(0.95,df1=25,df2=20)
## [1] 2.07392

0.7 样本统计量的概率分布

0.7.1 统计量及其分布

0.7.2 样本均值的分布

load("D:\\New_Folder\\Study_Programming\\R_Programme\\Applied Statistics\\datas - Copy\\example\\ch4\\example4_7.RData")
xx<-c(2,4,6,8,10)
count<-table(xx)
par(mfrow=c(1,2))#,mai=c(0.7,0.7,0.1,0.1)
barplot(prop.table(count),xlab="总体分布",ylab="频率",cex.axis=0.7,cex.lab=0.7)#注意prob.table函数
hist(example4_7$样本均值,freq=FALSE,col=5,cex.axis=0.7,cex.lab=0.7,breaks=rep(1.5:10.5,by=1))#注意课本上有一句 breaks 函数
curve(dnorm(x,mean(example4_7$样本均值),sd(example4_7$样本均值)),add=T,col="red",lwd=2)

样本均值:中心极限定理
### 其它统计量的分布
(见课本,大量的统计学理论)
### 统计量的标准误:standard error
(见课本,大量的统计学理论)