提交 3bb7948f 编写于 作者: W wizardforcel

6

上级 05036ba4
......@@ -790,7 +790,7 @@ class CohenResampler(Resampler):
pyplot.xlim(self.xlim)
```
现在我们可以实例化一个'CohenResampler`并绘制采样分布。
现在我们可以实例化一个`CohenResampler`并绘制采样分布。
```py
resampler = CohenResampler(male_sample, female_sample)
......@@ -841,15 +841,13 @@ COLOR5 = '#386cb0'
### 第一部分
As an example, let's look at differences between groups. The example I use in _Think Stats_ is first babies compared with others. The `first` module provides code to read the data into three pandas Dataframes.
作为一个例子,让我们看看分组之间的差异。 我在 Think Stats 中使用的例子是与其他婴儿相比的第一个婴儿。`first`模块提供代码,将数据读入三个 pandas 数据帧。
```py
live, firsts, others = first.MakeFrames()
```
The apparent effect we're interested in is the difference in the means. Other examples might include a correlation between variables or a coefficient in a linear regression. The number that quantifies the size of the effect, whatever it is, is the "test statistic".
我们感兴趣的表观效应是均值的差异。其他示例可能包括变量之间的相关性或线性回归中的系数。量化效应量的数字,无论它是什么,都是“测试统计量”。
```py
def TestStatistic(data):
......@@ -858,14 +856,14 @@ def TestStatistic(data):
return test_stat
```
For the first example, I extract the pregnancy length for first babies and others. The results are pandas Series objects.
对于第一个例子,我提取了第一个婴儿和其他人的怀孕时间。结果是 pandas `Series`对象。
```py
group1 = firsts.prglngth
group2 = others.prglngth
```
The actual difference in the means is 0.078 weeks, which is only 13 hours.
平均值的实际差异为 0.078 周,仅为13小时。
```py
actual = TestStatistic((group1, group2))
......@@ -874,14 +872,14 @@ actual
# 0.078037266777549519
```
The null hypothesis is that there is no difference between the groups. We can model that by forming a pooled sample that includes first babies and others.
零假设是组之间没有差异。我们可以通过形成包括第一个婴儿和其他婴儿的合并样本来对其进行建模。
```py
n, m = len(group1), len(group2)
pool = numpy.hstack((group1, group2))
```
Then we can simulate the null hypothesis by shuffling the pool and dividing it into two groups, using the same sizes as the actual sample.
然后我们可以通过打乱池子,并将其分成两组来模拟零假设,使用与实际样本相同的大小。
```py
def RunModel():
......@@ -890,7 +888,7 @@ def RunModel():
return data
```
The result of running the model is two NumPy arrays with the shuffled pregnancy lengths:
运行该模型的结果是两个 NumPy 数组,其具有打乱的孕期长度:
```py
RunModel()
......@@ -898,7 +896,7 @@ RunModel()
# (array([36, 40, 39, ..., 43, 42, 40]), array([43, 39, 32, ..., 37, 35, 41]))
```
Then we compute the same test statistic using the simulated data:
然后我们使用模拟数据计算相同的测试统计量:
```py
TestStatistic(RunModel())
......@@ -906,7 +904,7 @@ TestStatistic(RunModel())
# 0.081758440969863955
```
If we run the model 1000 times and compute the test statistic, we can see how much the test statistic varies under the null hypothesis.
如果我们运行模型 1000 次并计算测试统计量,我们可以看到测试统计量在零假设下变化了多少。
```py
test_stats = numpy.array([TestStatistic(RunModel()) for i in range(1000)])
......@@ -915,7 +913,7 @@ test_stats.shape
# (1000,)
```
Here's the sampling distribution of the test statistic under the null hypothesis, with the actual difference in means indicated by a gray line.
这是零假设下的检验统计量的抽样分布,其中均值的实际差异用灰线表示。
```py
def VertLine(x):
......@@ -931,8 +929,7 @@ None
![png](6.3_files/6.3_22_0.png)
The p-value is the probability that the test statistic under the null hypothesis exceeds the actual value.
p 值是零假设下的检验统计量超过实际值的概率。
```py
pvalue = sum(test_stats >= actual) / len(test_stats)
......@@ -941,14 +938,13 @@ pvalue
# 0.14999999999999999
```
In this case the result is about 15%, which means that even if there is no difference between the groups, it is plausible that we could see a sample difference as big as 0.078 weeks.
在这种情况下,结果约为 15%,这意味着即使两组之间没有差异,我们也可以看到样本差异大到 0.078 周。
We conclude that the apparent effect might be due to chance, so we are not confident that it would appear in the general population, or in another sample from the same population.
我们的结论是,表观效应可能是偶然的,所以我们不相信它会出现在一般总体或同一总体的另一个样本中。
Part Two
========
### 第二部分
We can take the pieces from the previous section and organize them in a class that represents the structure of a hypothesis test.
我们可以从上一节中获取部分,并将它们组织在一个表示假设检验结构的类中。
```py
class HypothesisTest(object):
......@@ -1015,7 +1011,7 @@ class HypothesisTest(object):
```
`HypothesisTest` is an abstract parent class that encodes the template. Child classes fill in the missing methods. For example, here's the test from the previous section.
`HypothesisTest`是一个对模板进行编码的抽象父类。子类填写缺少的方法。 例如,这是上一节的测试。
```py
class DiffMeansPermute(HypothesisTest):
......@@ -1047,7 +1043,7 @@ class DiffMeansPermute(HypothesisTest):
return data
```
Now we can run the test by instantiating a DiffMeansPermute object:
现在我们可以通过实例化`DiffMeansPermute`对象来运行测试:
```py
data = (firsts.prglngth, others.prglngth)
......@@ -1065,7 +1061,7 @@ means permute pregnancy length
ts max = 0.173695697482
And we can plot the sampling distribution of the test statistic under the null hypothesis.
我们可以在零假设下绘制检验统计量的抽样分布。
```py
ht.PlotHist()
......@@ -1074,7 +1070,7 @@ ht.PlotHist()
![png](6.3_files/6.3_33_0.png)
As an exercise, write a class named `DiffStdPermute` that extends `DiffMeansPermute` and overrides `TestStatistic` to compute the difference in standard deviations. Is the difference in standard deviations statistically significant?
作为练习,编写一个名为`DiffStdPermute`的类,它扩展了`DiffMeansPermute`并覆盖`TestStatistic`来计算标准差的差异。标准差的差异是否具有统计学意义?
```py
class DiffStdPermute(DiffMeansPermute):
......@@ -1105,7 +1101,7 @@ ts max = 0.44299505029
'''
```
Now let's run DiffMeansPermute again to see if there is a difference in birth weight between first babies and others.
现在让我们再次运行`DiffMeansPermute`,看看第一胎和其他婴儿的出生体重是否有差异。
```py
data = (firsts.totalwgt_lb.dropna(), others.totalwgt_lb.dropna())
......@@ -1124,8 +1120,8 @@ ts max = 0.0917504268392
'''
```
In this case, after 1000 attempts, we never see a sample difference as big as the observed difference, so we conclude that the apparent effect is unlikely under the null hypothesis. Under normal circumstances, we can also make the inference that the apparent effect is unlikely to be caused by random sampling.
在这种情况下,在 1000 次尝试之后,我们从未看到与观察到的差异一样大的样本差异,因此我们得出结论,表观效应不太可能在零假设下。在正常情况下,我们也可以推断出表观效应不太可能是由随机抽样引起的。
One final note: in this case I would report that the p-value is less than 1/1000 or 0.001. I would not report that p=0, because the apparent effect is not impossible under the null hypothesis; just unlikely.
最后一点:在这种情况下,我会报告p值小于 1/1000 或 0.001。 我不会报告`p = 0`,因为在零假设下,标贯效应并非不可能,而是不太可能。
This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/data-science-ipython-notebooks).
> 这个笔记本由 [Donne Martin](http://donnemartin.com) 编写。来源和协议信息在 [GitHub](https://github.com/donnemartin/data-science-ipython-notebooks) 上。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册