假设检验
- 根据一定假设条件,由样本推断总体的一种统计学方法。基本思路是先提出假设(虚无假设),使用统计学方法进行计算,根据计算结果判断是否拒绝假设。
- 假设检验的方法有很多,如卡方检验,T检验等。
- spark实现的是皮尔森卡方检验,它可以实现适配度检验和独立性检验。
皮尔森卡方检验
- 皮尔森卡方检验是最常用的卡方检验,可以分为适配度检验和独立检验。
- 适配度检验:验证观察值的次数分配和理论值是否相等。
- 独立性检验:两个变量抽样到的观察值是否相互独立。
学习假设检验
判断性别与左撇子是否存在关系
男性 | 女性 | 合计 | |
---|---|---|---|
右利手 | 127 | 147 | 274 |
左利手 | 19 | 10 | 29 |
合计 | 146 | 157 | 303 |
实践假设检验
导入linalg和stat模块
scala> import org.apache.spark.mllib.{linalg,stat}
读入数据
scala> val data = linalg.Matrices.dense(2,2,Array(127,19,147,10))
data: org.apache.spark.mllib.linalg.Matrix =
127.0 147.0
19.0 10.0
皮尔森卡方检验
scala> stat.Statistics.chiSqTest(data)
res0: org.apache.spark.mllib.stat.test.ChiSqTestResult =
Chi squared test summary:
method: pearson
degrees of freedom = 1
statistic = 3.8587031204632654
pValue = 0.049488567227318536
Strong presumption against null hypothesis: the occurrence of the outcomes is statistically independent..
结论
- pValue<0.05,因此假设条件出现的概率非常小,
- strong presumption against null hypothesis: the occurrence of the outcomes is statistically independent。
强烈的推测否定虚无假设(结果的出现是统计上独立的)。因此,性别与左利手之间是有联系的。