GPT答: |
7i24.cn
|
以下是2024年版本的R软件编程和作图示例:
```R
# 定义总体分布为二项分布
p <- 0.7
n <- 10000 # 总体容量
# 模拟抽样,计算样本均值
sample_means_2 <- replicate(1000, mean(rbinom(2, n, p)))
sample_means_10 <- replicate(1000, mean(rbinom(10, n, p)))
sample_means_30 <- replicate(1000, mean(rbinom(30, n, p)))
sample_means_50 <- replicate(1000, mean(rbinom(50,n,p)))
# 绘制频数分布直方图和正态分布曲线
par(mfrow=c(2,2))
hist(sample_means_2,breaks=20,freq=FALSE,xlim=c(p-0.1,p+0.1),main="n=2",xlab="Sample Mean")
curve(dnorm(x,p,sqrt(p*(1-p)/2)),add=TRUE,col="blue")
hist(sample_means_10,breaks=20,freq=FALSE,xlim=c(p-0.1,p+0.1),main="n=10",xlab="Sample Mean")
curve(dnorm(x,p,sqrt(p*(1-p)/10)),add=TRUE,col="blue")
hist(sample_means_30,breaks = 20,freq = FALSE,xlim = c(p - 0.1 ,p + 01), main ="n =30" , xlab ="Sample Mean" )
curve (dnorm (x ,p ,sqrt (p * (l - p ) /3O) ), add=T RUE,col =" blue " )
hist(sample _means _50,breaks =20,freq=FAL SE,xli m=c(p - O . l ,p + O.l) ,mai n ="n =5O ", xla b ="S ample M ean ")
cur ve(dno rm(x,P,sq rt(P *(l-P )/5O)) ,ad d=T RUE,c ol="bl ue ")
```
这段代码使用了rbinom函数模拟了不同样本容量下的样本均值,并绘制了对应的频数分布直方图和正态分布曲线。通过观察直方图和曲线可以验证中心极限定理。
|