今天在B站看了毕导的《我给自己发了2亿个红包,才发现先抢和后抢的差距这么大!》的视频,非常有意思,大家感兴趣也可以到B站观看。
视频地址
https://www.bilibili.com/video/av84581638?from=search&seid=2721954210688527324
娱乐之余,记录一下视频中涉及到的统计学知识点。
5个人,发150次红包,每次50块,为了排除其他变量的干扰比如人品等因素,每抢30次调换一下顺序。然后对数据进行统计。
因为没有原始数据,看完整个视频后根据毕导总结的规律模拟数据 数据的基本规律是
df<-data.frame(Group=c(rep("A",150),rep("B",150),rep("C",150),rep("D",150),rep("E",150)),
Money=c(runif(150,0.01,20),runif(150,0.01,24.99),
runif(150,0.01,33.32),runif(150,0.01,49.96),
runif(150,0.01,49.96)))
df
library(ggplot2)
library(RColorBrewer)
ggplot(df,aes(x=Group,y=Money,color=Group))+
geom_jitter(width=0.15,size=1.5)+
theme_bw()+
scale_color_manual(values=rainbow(5))+
scale_x_discrete(labels=c("First","Second","Third","Fourth","Fifth"))+
labs(x="")+
theme(legend.position = "None")
image.png
runif()
https://stat.ethz.ch/R-manual/R-devel/library/stats/html/Uniform.html通过散点图观察第一个抢红包的人的金额分布可以初步推断数据符合0~20的均匀分布
df1<-df[1:150,]
ggplot(df1,aes(x=Group,y=Money))+
geom_point(color="red",alpha=0.6)+
theme_bw()
image.png
ks.test()
https://stat.ethz.ch/R-manual/R-devel/library/stats/html/ks.test.html假设检验的原假设H0是数据符合指定分布,P值小于0.05拒绝原假设
> ks.test(df1$Money,"punif")
One-sample Kolmogorov-Smirnov test
data: df1$Money
D = 0.96667, p-value = 1.221e-15
alternative hypothesis: two-sided
这里不知道什么原因计算出来的P值竟然小于0.05,说明结果不符合均匀分布
搜索原因的时候找到了 https://stats.stackexchange.com/questions/137408/low-p-value-in-test-of-uniformity-of-uniformly-distributed-data 暂时还没看懂
也找到了一个函数
install.packages('swfscMisc')
library('swfscMisc')
uniform.test(hist(runif(100,0.01,20)), B = 1000)
这个函数计算出来的P值是大于0.05的。
主要作用是检验几个样本是否来自同一总体
R语言里的实现函数是ad.test()
install.packages("nortest")
library(nortest)
ad.test(rnorm(100))
输入一个参数可以检验数据是否符合正态分布,原假设是数据符合正态分布 https://www.statology.org/how-to-conduct-an-anderson-darling-test-in-r/
多样本检验 https://rdrr.io/cran/kSamples/man/ad.test.html
x<-rnorm(10)
y<-rnorm(10)
z<-rnorm(10)
install.packages("kSamples")
kSamples::ad.test(list(x,y,z),method="exact")
Anderson-Darling k-sample test.
Number of samples: 3
Sample sizes: 10, 10, 10
Number of ties: 0
Mean of Anderson-Darling Criterion: 2
Standard deviation of Anderson-Darling Criterion: 0.99539
T.AD = ( Anderson-Darling Criterion - mean)/sigma
Null Hypothesis: All samples come from a common population.
Based on Nsim = 10000 simulations
AD T.AD asympt. P-value sim. P-value
version 1: 6.949 4.972 0.001924 0.0013
version 2: 7.020 5.046 0.001787 0.0013
接下来的视频内容还提到了
这两个算法如何用R语言来实现可是真的不会了!