0.写在前面
统计学知识刷起来。其实这又是一个系列,晦涩难懂没人看的那种。但这样的知识确实非常有价值,能够带给我以及认真学习的读者们实打实的进步,所以不管阅读量高低,我都写。 这一篇是讲中心极限定理的。啥子意思呢?
中心极限定理指的是给定一个任意分布的总体。每次从这些总体中随机抽取 n 个抽样,一共抽 m 次。 然后把这 m 组抽样分别求出平均值。 这些平均值的分布接近正态分布。
用statquest小哥的话说:even if you’re not normal,the average is normal.
不管总体符合什么分布(除了无法计算均值的分布以外),均值都符合正态分布,所以不考虑原数据的分布。神奇!课程中以均匀分布和指数分布为例,论证了这一定理。
在B站搜索statquest即可找到视频哦。
在用R语言实现了一下老师在视频中画的图~非常好耍。
1.准备数据
rm(list = ls())
library(ggplot2)
library(patchwork)
df = data.frame(x = 1:100,
y1 = dnorm(1:100,50,20),
y2 = dunif(1:100,1,100),
y3 = dexp(1:100,0.06))
set.seed(1004)
rn1 = rnorm(100,50,20)
set.seed(1004)
rn2 = runif(100,1,100)
set.seed(1004)
rn3 = rexp(100,0.06)
rn = data.frame(x = 1:100,
rn1 = rn1,
rn2 = rn2,
rn3 = rn3)
head(df)
## x y1 y2 y3
## 1 1 0.0009918677 0.01010101 0.05650587
## 2 2 0.0011197265 0.01010101 0.05321523
## 3 3 0.0012609110 0.01010101 0.05011621
## 4 4 0.0014163519 0.01010101 0.04719767
## 5 5 0.0015869826 0.01010101 0.04444909
## 6 6 0.0017737296 0.01010101 0.04186058
head(rn)
## x rn1 rn2 rn3
## 1 1 37.84318 27.89301 12.995556
## 2 2 65.35258 25.35061 28.127434
## 3 3 46.71456 78.08598 4.341248
## 4 4 49.42446 97.92214 0.180288
## 5 5 50.27116 44.04110 14.291344
## 6 6 35.68566 91.49909 36.579238
两个数据框,一个是符合某分布的某个数值大小的概率,一个是符合某分布的具体数值,两个数据框的二三四列分别是正态分布、均匀分布和指数分布。
2.三种分布的图
#1.正态分布
p1 = ggplot(df,aes(x = x,y = y1))+
geom_line()+theme_classic()
#2.均匀分布
p2 = ggplot(df,aes(x = x,y = y2))+
geom_line()+theme_classic()
#3.指数分布
p3 = ggplot(df,aes(x = x,y = y3))+
geom_line()+theme_classic()
p1+p2+p3
3.正态分布数据的均值分布
#画均值竖线
a1 = p1
n = c()
for(i in 1:100){
n[[i]] = mean(sample(rn$rn1,50))
a1 = a1 + geom_vline(xintercept = n[[i]],color = "red",size = 0.3,alpha = 0.3)
}
#画直方图
dat = data.frame(n = n)
b1 = ggplot(dat,aes(x = n,y = ..density..))+
geom_histogram(color = "#D0505D",
fill = "#D0505D",
alpha = 0.4,binwidth = 1)+
theme_classic()+
scale_y_continuous(expand = c(0,0))
#加正态曲线
y = data.frame(
x = seq(40,62,0.2),
y1 = dnorm(seq(40,62,0.2),50,2))
b1 = b1 + geom_line(aes(x = x,y = y1),data = y)
a1 + b1
结论:正态分布数据的均值符合正态分布
4.均匀分布数据的均值分布
#画均值竖线
a2 = p2
n = c()
for(i in 1:100){
n[[i]] = mean(sample(rn$rn2,50))
a2 = a2 + geom_vline(xintercept = n[[i]],color = "red",size = 0.3,alpha = 0.3)
}
#画直方图
dat = data.frame(n = n)
b2 = ggplot(dat,aes(x = n,y = ..density..))+
geom_histogram(color = "#D0505D",
fill = "#D0505D",
alpha = 0.4,binwidth = 1)+
theme_classic()+
scale_y_continuous(expand = c(0,0))
#加正态曲线
y = data.frame(
x = 40:62,
y1 = dnorm(40:62,50,3))
b2 = b2 + geom_line(aes(x = x,y = y1),data = y)
a2 + b2
结论:均匀分布数据的均值符合正态分布
3.指数分布数据的均值分布
#画均值竖线
a3 = p3
n = c()
for(i in 1:100){
n[[i]] = mean(sample(rn$rn3,50))
a3 = a3 + geom_vline(xintercept = n[[i]],color = "red",size = 0.3,alpha = 0.3)
}
#画直方图
dat = data.frame(n = n)
b3 = ggplot(dat,aes(x = n,y = ..density..))+
geom_histogram(color = "#D0505D",
fill = "#D0505D",
alpha = 0.4,binwidth = 1)+
theme_classic()+
scale_y_continuous(expand = c(0,0))
#加正态曲线
y = data.frame(
x = seq(11,22,0.1),
y1 = dnorm(seq(11,22,0.1),16.5,1.5))
b3 = b3 + geom_line(aes(x = x,y = y1),data = y)
a3 + b3
结论:指数分布数据的均值也也耶符合正态分布
6.最后来个全家福吧~
(p1+p2+p3)/(a1+a2+a3)/(b1+b2+b3)
R语言真是学统计的好玩具!
网友评论