Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
data8-textbook-zh
提交
fd69eae1
D
data8-textbook-zh
项目概览
OpenDocCN
/
data8-textbook-zh
8 个月 前同步成功
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
data8-textbook-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
fd69eae1
编写于
1月 24, 2018
作者:
W
wizardforcel
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ch16.
上级
006f34e1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
133 addition
and
0 deletion
+133
-0
16.md
16.md
+133
-0
未找到文件。
16.md
浏览文件 @
fd69eae1
...
@@ -401,3 +401,136 @@ Empirical P-value: 0.0
...
@@ -401,3 +401,136 @@ Empirical P-value: 0.0
我们使用排列检验来帮助我们确定,类别属性的分布是否与类别相关。 一般来说,排列检验可以这样使用来确定,两个类别分布是否从相同的基本分布随机抽样。
我们使用排列检验来帮助我们确定,类别属性的分布是否与类别相关。 一般来说,排列检验可以这样使用来确定,两个类别分布是否从相同的基本分布随机抽样。
## A/B 测试
我们使用随机排列来查看,两个样本是否从相同的基本分类分布抽取。 如果样本是数值的,则可以使用相同的方法;检验统计量的选择通常比较简单。 在我们使用
`Deflategate`
数据的例子中,我们使用了不同的方法来测试爱国者队和小马队用球是否来自相同的基本分布。
在现代数据分析中,决定两个数值样本是否来自相同的基本分布称为 A/B 测试。 名称是指两个样本 A 和 B 的标签。
### 吸烟者和不吸烟者
我们对随机抽样的母亲及其新生儿进行了许多不同的分析,但是我们还没有查看母亲是否吸烟的数据。 研究的目的之一,是看母亲吸烟是否与出生体重有关。
```
py
baby
=
Table
.
read_table
(
'baby.csv'
)
baby
```
| Birth Weight | Gestational Days | Maternal Age | Maternal Height | Maternal Pregnancy Weight | Maternal Smoker |
| --- | --- | --- | --- | --- | --- |
| 120 | 284 | 27 | 62 | 100 | False |
| 113 | 282 | 33 | 64 | 135 | False |
| 128 | 279 | 28 | 64 | 115 | True |
| 108 | 282 | 23 | 67 | 125 | True |
| 136 | 286 | 25 | 62 | 93 | False |
| 138 | 244 | 33 | 62 | 178 | False |
| 132 | 245 | 23 | 65 | 140 | False |
| 120 | 289 | 25 | 62 | 125 | False |
| 143 | 299 | 30 | 66 | 136 | True |
| 140 | 351 | 27 | 68 | 120 | False |
(省略了 1164 行)
我们首先选择
`Birth Weight`
和
`Maternal Smoker`
。 样本中有 715 名非吸烟者,459 名吸烟者。
```
py
weight_smoke
=
baby
.
select
(
'Birth Weight'
,
'Maternal Smoker'
)
weight_smoke
.
group
(
'Maternal Smoker'
)
```
| Maternal Smoker | count |
| --- | --- |
| False | 715 |
| True | 459 |
下面的第一个直方图显示了样本中非吸烟者的婴儿出生体重的分布。 第二个显示了吸烟者的婴儿出生体重。
```
py
nonsmokers
=
baby
.
where
(
'Maternal Smoker'
,
are
.
equal_to
(
False
))
```
```
py
nonsmokers
.
hist
(
'Birth Weight'
,
bins
=
np
.
arange
(
40
,
181
,
5
),
unit
=
'ounce'
)
```
两种分布都大致是钟形,中心在 120 盎司附近。 当然,这些分布并不相同,这就产生了这样一个问题,即差异是否仅仅反映了机会变异,还是反映了总体分布的差异。
这个问题可以通过假设检验来回答。
原假设:在总体中,不吸烟的母亲的婴儿出生体重的分布和吸烟的母亲相同。 样本中的差异是偶然的。
备选假设:两种分布在总体中是不同的。
检验统计量:出生体重是一个定量变量,所以用均值的绝对差作为检验统计量是合理的。
检验统计量的观测值约为 9.27 盎司。
```
py
means_table
=
weight_smoke
.
group
(
'Maternal Smoker'
,
np
.
mean
)
means_table
```
| Maternal Smoker | Birth Weight mean |
| --- | --- |
| False | 123.085 |
| True | 113.819 |
```
py
nonsmokers_mean
=
means_table
.
column
(
1
).
item
(
0
)
smokers_mean
=
means_table
.
column
(
1
).
item
(
1
)
nonsmokers_mean
-
smokers_mean
9.266142572024918
```
## 排列检验
为了看看原假设下是否有可能出现这种差异,我们将使用排列检验,就像我们在前一节中所做的那样。 我们必须为检验统计量改变代码。 为此,我们将像上面那样计算平均值的差,然后取绝对值。
请记住,在原假设下,出生体重的所有排列与
`Maternal Smoker `
列等可能出现。 所以,就像以前一样,每次重复都是打乱正在比较的变量。
```
py
def
permutation_test_means
(
table
,
variable
,
classes
,
repetitions
):
"""Test whether two numerical samples
come from the same underlying distribution,
using the absolute difference between the means.
table: name of table containing the sample
variable: label of column containing the numerical variable
classes: label of column containing names of the two samples
repetitions: number of random permutations"""
t
=
table
.
select
(
variable
,
classes
)
# Find the observed test statistic
means_table
=
t
.
group
(
classes
,
np
.
mean
)
obs_stat
=
abs
(
means_table
.
column
(
1
).
item
(
0
)
-
means_table
.
column
(
1
).
item
(
1
))
# Assuming the null is true, randomly permute the variable
# and collect all the generated test statistics
stats
=
make_array
()
for
i
in
np
.
arange
(
repetitions
):
shuffled_var
=
t
.
select
(
variable
).
sample
(
with_replacement
=
False
).
column
(
0
)
shuffled
=
t
.
select
(
classes
).
with_column
(
'Shuffled Variable'
,
shuffled_var
)
m_tbl
=
shuffled
.
group
(
classes
,
np
.
mean
)
new_stat
=
abs
(
m_tbl
.
column
(
1
).
item
(
0
)
-
m_tbl
.
column
(
1
).
item
(
1
))
stats
=
np
.
append
(
stats
,
new_stat
)
# Find the empirical P-value:
emp_p
=
np
.
count_nonzero
(
stats
>=
obs_stat
)
/
repetitions
# Draw the empirical histogram of the tvd's generated under the null,
# and compare with the value observed in the original sample
Table
().
with_column
(
'Test Statistic'
,
stats
).
hist
(
bins
=
20
)
plots
.
title
(
'Empirical Distribution Under the Null'
)
print
(
'Observed statistic:'
,
obs_stat
)
print
(
'Empirical P-value:'
,
emp_p
)
permutation_test_means
(
baby
,
'Birth Weight'
,
'Maternal Smoker'
,
5000
)
Observed
statistic
:
9.266142572024918
Empirical
P
-
value
:
0.0
```
原始样本中的观测差异约为 9.27 盎司,与此分布不一致:经验性 P 值为 0,这意味着确切的 P 值确实非常小。 因此,测试的结论是,在总体中,不吸烟者和吸烟者的婴儿出生体重的分布是不同的。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录