这里我们看到,在<mathdisplay="inline"><semantics><mrow><mi>p</mi><mo>≤</mo><mn>0.5</mn></mrow><annotationencoding="application/x-tex">p \ le 0.5</annotation></semantics></math>的零假设下,有阳性体征的个体比例并不十分令人惊讶,鉴于观测值实际上小于<mathdisplay="inline"><semantics><mn>0.5</mn><annotationencoding="application/x-tex">0.5</annotation></semantics></math>,这一点并不应该令我们感到惊讶。
我们也可以用学生的 t 检验来问这个问题,你在本书前面已经遇到过了。我们将平均数称为<mathdisplay="inline"><semantics><mover><mi>x</mi></mover><annotationencoding="application/x-tex">\ bar { x }</annotation></semantics></math>而假设的人口平均数称为<mathdisplay="inline"><semantics><mi>【μ</mi><annotationencoding="application/x-tex">\ mu</annotation></semantics></math>。然后,单个平均值的 t 检验为:
<mathxmlns:epub="http://www.idpf.org/2007/ops"display="block"><semantics><mrow><mi>t</mi><mo>=</mo><mfrac><mrow><mover><mi>x</mi></mover><mi>μ</mi></mrow><mrow><mi>s</mi><mi>e</mi><mi>m</mi></mrow></mfrac></mrow><annotationencoding="application/x-tex">t = \ frac { \ bar { x }-\ mu } { SEM }</annotation></semantics></math>
我们也可以用学生的 t 检验来问这个问题,你在本书前面已经遇到过了。我们将平均数称为$\bar{X}$而假设的人口平均数称为$\mu$。然后,单个平均值的 t 检验为:
![Left: Violin plot showing distributions of TV watching separated by regular marijuana use. Right: Violin plots showing data for each group, with a dotted line connecting the predicted values for each group, computed on the basis of the results of the linear model.. ](img/file80.png)
![Left: Violin plot showing distributions of TV watching separated by regular marijuana use. Right: Violin plots showing data for each group, with a dotted line connecting the predicted values for each group, computed on the basis of the results of the linear model.. ](img/file80.png)
我们也可以使用学生 t 检验来检验两组独立观察值之间的差异(正如我们在前面章节中看到的);我们将在本章后面讨论观测值不独立的情况。提醒一下,比较两个独立组的 t 统计量计算如下:
我们也可以使用学生 t 检验来检验两组独立观察值之间的差异(正如我们在前面章节中看到的);我们将在本章后面讨论观测值不独立的情况。提醒一下,比较两个独立组的 t 统计量计算如下:
<semantics><mrow><mi><mo>=</mo><mrow><mover><mi><mi><mn>n</mn></mi></mi></mover></mrow></mi></mrow><annotationencoding="application/x-tex">【t = \ frac】{ \ bar { x _ 1 }-\ bar { x _ 2 } } { \ sqrt }+\ frac { s _ 1 ^ 2 } { n _ 1 }+\ frac</annotation></semantics>
其中<mathxmlns:epub="http://www.idpf.org/2007/ops"display="inline"><semantics><msub><mover><mi>x</mi><moaccent="true">‾</mo></mover><mn>1</mn></msub><annotationencoding="application/x-tex">\ bar { x } _ 1</annotation></semantics></math>和<mathxmlns:epub="http://www.idpf.org/2007/ops"display="inline"><semantics><msub><mover><mi>x</mi><moaccent="true">‾</mo></mover><mn>2</mn></msub><annotationencoding="application/x-tex">\ bar { x } _ 2</annotation></semantics></math>是两者的意思 <mathxmlns:epub="http://www.idpf.org/2007/ops"display="inline"><semantics><msubsup><mi>s</mi><mn>1</mn><mn>2</mn></msubsup><annotationencoding="application/x-tex">s^2_1</annotation></semantics></math>和<mathxmlns:epub="http://www.idpf.org/2007/ops"display="inline"><semantics><msubsup><mi>s</mi><mn>2</mn><mn>2</mn></msubsup><annotationencoding="application/x-tex">s^2_2</annotation></semantics></math>是每个组的方差,而 <mathxmlns:epub="http://www.idpf.org/2007/ops"display="inline"><semantics><semantics><msub>在均值间无差异的零假设下,该统计量根据 t 分布进行分布,自由度使用 Welch 检验(如前所述)进行计算,因为两组之间的个体数量不同。在这种情况下,我们从特定的假设开始,即吸食大麻与更多地看电视有关,因此我们将使用单尾检验。以下是我们的统计软件得出的结果:</msub></semantics></semantics></math>
$$
t = \frac{\bar{X_1} - \bar{X_2}}{\sqrt{\frac{S_1^2}{n_1} + \frac{S_2^2}{n_2}}}
$$
其中$\bar{X}_1$ 和 $\bar{X}_2$是两组的均值, $S^2_1$ and $S^2_2$是两组的方差,而在均值间无差异的零假设下,该统计量根据 t 分布进行分布,自由度使用 Welch 检验(如前所述)进行计算,因为两组之间的样本数量不同。在这种情况下,我们从特定的假设开始,即吸食大麻与更多地看电视有关,因此我们将使用单尾检验。以下是我们的统计软件得出的结果:
由于吸毒是一个二元变量,我们将其视为一个*虚拟变量*,就像我们在上一章中讨论的那样,对于吸烟者将其值设置为 1,对于不吸烟者将其值设置为 0。 既然如此,$\hat{\beta_1}$就是两组的均值之差,$\hat{\beta_0}$可以使用统计软件中的一般线性模型函数来拟合该模型,并看到它给出了与上述 t 检验相同的 t 统计量,只是在这种情况下它是正的,这是因为我们的软件排列组的方式:
```
```
##
##
...
@@ -139,7 +147,7 @@ t 检验通常作为比较均值的专门工具,但它也可以被视为一般
...
@@ -139,7 +147,7 @@ t 检验通常作为比较均值的专门工具,但它也可以被视为一般
## F-statistic: 11.4 on 1 and 198 DF, p-value: 0.000872
## F-statistic: 11.4 on 1 and 198 DF, p-value: 0.000872
![Left: Violin plot of systolic blood pressure on first and second recording, from NHANES. Right: Same violin plot with lines connecting the two data points for each individual.](img/file81.png)
![Left: Violin plot of systolic blood pressure on first and second recording, from NHANES. Right: Same violin plot with lines connecting the two data points for each individual.](img/file81.png)
...
@@ -207,11 +217,11 @@ t 检验通常作为比较均值的专门工具,但它也可以被视为一般
...
@@ -207,11 +217,11 @@ t 检验通常作为比较均值的专门工具,但它也可以被视为一般
![Histogram of difference scores between first and second BP measurement. The vertical line represents the mean difference in the sample.](img/file82.png)
![Histogram of difference scores between first and second BP measurement. The vertical line represents the mean difference in the sample.](img/file82.png)
使用 ANOVA,我们希望在均值间无差异的零假设下,测试模型解释的方差是否大于我们随机预期的方差。而对于 t 分布,在零假设下期望值为零,但这里不是这样,因为平方和总是正数。幸运的是,有另一种理论分布描述了在零假设下平方和的比率是如何分布的:分布 *F* (见图 [15.5](#fig:FDist) )。这种分布有两个自由度,分别对应于分子(本例中为模型)和分母(本例中为误差)的自由度。
使用 ANOVA,我们希望在均值间无差异的零假设下,测试模型解释的方差是否大于我们随机预期的方差。而对于 t 分布,在零假设下期望值为零,但这里不是这样,因为平方和总是正数。幸运的是,有另一种理论分布描述了在零假设下平方和的比率是如何分布的:分布 *F* (见图 [15.5](#fig:FDist) )。这种分布有两个自由度,分别对应于分子(本例中为模型)和分母(本例中为误差)的自由度。
...
@@ -306,7 +320,7 @@ t 检验通常作为比较均值的专门工具,但它也可以被视为一般
...
@@ -306,7 +320,7 @@ t 检验通常作为比较均值的专门工具,但它也可以被视为一般
图 15.5:不同自由度值的零假设下的 f 分布。
图 15.5:不同自由度值的零假设下的 f 分布。
为了创建 ANOVA 模型,我们扩展了您在上一章中遇到的*虚拟编码*的概念。请记住,对于比较两个平均值的 t 检验,我们创建了一个虚拟变量,其中一个条件的值为 1,其他条件的值为 0。在这里,我们通过创建两个虚拟变量来扩展这一思想,一个编码药物 1 的条件,另一个编码药物 2 的条件。就像在 t 检验中一样,我们将有一个条件(在这种情况下,安慰剂)没有虚拟变量,因此代表了与其他条件进行比较的基线;它的平均值定义了模型的截距。使用药品 1 和 2 的虚拟编码,我们可以使用与上一章相同的方法来拟合模型:
为了创建 ANOVA 模型,我们扩展了您在上一章中遇到的*虚拟编码*的概念。请记住,对于比较两个平均值的 t 检验,我们创建了一个虚拟变量,其中一个条件的值为 1,其他条件的值为 0。在这里,我们通过创建两个虚拟变量来扩展这一思想,一个编码药物 1 的条件,另一个编码药物 2 的条件。就像在 t 检验中一样,我们将有一个条件(在这种情况下,安慰剂)没有虚拟变量,因此代表了与其他条件进行比较的基线;它的平均值定义了模型的截距。使用药品1和药品2的虚拟编码,我们可以使用与上一章相同的方法来拟合模型:
```
```
##
##
...
@@ -330,7 +344,7 @@ t 检验通常作为比较均值的专门工具,但它也可以被视为一般
...
@@ -330,7 +344,7 @@ t 检验通常作为比较均值的专门工具,但它也可以被视为一般
## F-statistic: 10.7 on 2 and 105 DF, p-value: 5.83e-05
## F-statistic: 10.7 on 2 and 105 DF, p-value: 5.83e-05
```
```
这个命令的输出为我们提供了两件事。首先,它向我们展示了每个虚拟变量的 t 检验结果,这基本上告诉我们每个条件是否分别不同于安慰剂;似乎药物 1 有,而药物 2 没有。但是,请记住,如果我们想要解释这些测试,我们需要校正 p 值,以说明我们已经进行了多重假设测试的事实;我们将在下一章看到如何做到这一点的例子。
这个命令的输出为我们提供了两个信息。首先,它向我们展示了每个虚拟变量的 t 检验结果,这基本上告诉我们每个条件是否分别不同于安慰剂;似乎药物 1 有,而药物 2 没有。但是,请记住,如果我们想要解释这些测试,我们需要校正 p 值,以说明我们已经进行了多重假设测试的事实;我们将在下一章看到如何做到这一点的例子。
请记住,我们开始想要测试的假设是,任何条件之间是否有任何差异;我们称之为*综合*假设检验,它是由 F 统计提供的检验。F 统计量基本上告诉我们,我们的模型是否优于只包含截距的简单模型。在这种情况下,我们看到 f 检验非常显著,与我们的印象一致,即两组之间似乎确实存在差异(事实上我们知道存在差异,因为我们创建了数据)。
请记住,我们开始想要测试的假设是,任何条件之间是否有任何差异;我们称之为*综合*假设检验,它是由 F 统计提供的检验。F 统计量基本上告诉我们,我们的模型是否优于只包含截距的简单模型。在这种情况下,我们看到 f 检验非常显著,与我们的印象一致,即两组之间似乎确实存在差异(事实上我们知道存在差异,因为我们创建了数据)。
...
@@ -357,9 +371,9 @@ t 检验通常作为比较均值的专门工具,但它也可以被视为一般
...
@@ -357,9 +371,9 @@ t 检验通常作为比较均值的专门工具,但它也可以被视为一般
### 15.8.1 作为线性模型的成对 t 检验
### 15.8.1 作为线性模型的成对t检验
我们也可以根据一般的线性模型来定义配对 t 检验。为此,我们将每个受试者的所有测量结果作为数据点包含在内(在一个整洁的数据框内)。然后,我们在模型中包含一个变量,该变量对每个人的身份进行编码(在本例中,ID 变量包含每个人的主题 ID)。这被称为*混合模型*,因为它包括独立变量的影响以及个人的影响。标准的模型拟合过程`lm()`无法做到这一点,但我们可以使用一个流行的 R 包 *lme4* 中的`lmer()`函数来做到这一点,该包专门用于估计混合模型。公式中的`(1|ID)`告诉`lmer()`为`ID`变量的每个值(即数据集中的每个个体)估计一个单独的截距(这就是`1`所指的截距),然后估计一个与 BP 时间点相关的公共斜率。
我们也可以根据一般的线性模型来定义成对t检验。为此,我们将每个受试者的所有测量结果作为数据点包含在内(在一个整洁的数据框内)。然后,我们在模型中包含一个变量,该变量对每个人的身份进行编码(在本例中,ID 变量包含每个人的主题 ID)。这被称为*混合模型*,因为它包括独立变量的影响以及个人的影响。标准的模型拟合过程`lm()`无法做到这一点,但我们可以使用一个流行的 R 包 *lme4* 中的`lmer()`函数来做到这一点,该包专门用于估计混合模型。公式中的`(1|ID)`告诉`lmer()`为`ID`变量的每个值(即数据集中的每个个体)估计一个单独的截距(这就是`1`所指的截距),然后估计一个与 BP 时间点相关的公共斜率。
![Scatterplot of matrices for the nine variables in the self-control dataset. The diagonal elements in the matrix show the histogram for each of the individual variables. The lower left panels show scatterplots of the relationship between each pair of variables, and the upper right panel shows the correlation coefficient for each pair of variables.](img/file85.png)
![Scatterplot of matrices for the nine variables in the self-control dataset. The diagonal elements in the matrix show the histogram for each of the individual variables. The lower left panels show scatterplots of the relationship between each pair of variables, and the upper right panel shows the correlation coefficient for each pair of variables.](img/file85.png)
![Heatmap of the correlation matrix for the nine self-control variables. The brighter yellow areas in the top left and bottom right highlight the higher correlations within the two subsets of variables.](img/file86.png)
![Heatmap of the correlation matrix for the nine self-control variables. The brighter yellow areas in the top left and bottom right highlight the higher correlations within the two subsets of variables.](img/file86.png)
![A heatmap showing the correlation coefficient of brain activity between 316 regions in the left hemisphere of a single individiual. Cells in yellow reflect strong positive correlation, whereas cells in blue reflect strong negative correlation. The large blocks of positive correlation along the diagonal of the matrix correspond to the major connected networks in the brain](img/file87.png)
![A heatmap showing the correlation coefficient of brain activity between 316 regions in the left hemisphere of a single individiual. Cells in yellow reflect strong positive correlation, whereas cells in blue reflect strong negative correlation. The large blocks of positive correlation along the diagonal of the matrix correspond to the major connected networks in the brain](img/file87.png)
![A depiction of the Euclidean distance between two points, (1,2) and (4,3). The two points differ by 3 along the X axis and by 1 along the Y axis.](img/file88.png)
![A depiction of the Euclidean distance between two points, (1,2) and (4,3). The two points differ by 3 along the X axis and by 1 along the Y axis.](img/file88.png)
图 16.4:两点(1,2)和(4,3)之间欧几里得距离的描述。这两个点沿 X 轴相差 3 °,沿 Y 轴相差 1°。
在选择了我们希望找到的聚类数( *K* )之后,我们必须得出 K 个位置,这将是我们对聚类中心的开始猜测(因为我们最初不知道中心在哪里)。一种简单的开始方式是随机选择 K 个实际数据点,并使用它们作为我们的起点,它们被称为*质心*。然后,我们计算每个数据点到每个质心的欧几里德距离,并根据每个点最近的质心将其分配到一个聚类中。使用这些新的聚类分配,我们通过平均分配给该聚类的所有点的位置来重新计算每个聚类的质心。然后重复这一过程,直到找到稳定的解决方案;我们称之为*迭代*过程,因为它会迭代直到答案不变,或者直到达到某种其他限制,比如最大可能迭代次数。
![A two-dimensional depiction of clustering on the latitude and longitude of countries across the world. The square black symbols show the starting centroids for each cluster, and the lines show the movement of the centroid for that cluster across the iterations of the algorithm.](img/file89.png)
![A two-dimensional depiction of clustering on the latitude and longitude of countries across the world. The square black symbols show the starting centroids for each cluster, and the lines show the movement of the centroid for that cluster across the iterations of the algorithm.](img/file89.png)
![A visualization of the clustering results from 10 runs of the K-means clustering algorithm with K=3\. Each row in the figure represents a different run of the clustering algorithm (with different random starting points), and variables sharing the same color are members of the same cluster.](img/file90.png)
![A visualization of the clustering results from 10 runs of the K-means clustering algorithm with K=3\. Each row in the figure represents a different run of the clustering algorithm (with different random starting points), and variables sharing the same color are members of the same cluster.](img/file90.png)
图 16.6:K = 3 的 K 均值聚类算法的 10 次运行的聚类结果的可视化。图中的每一行代表聚类算法的不同运行(具有不同的随机起点),共享相同颜色的变量是同一聚类的成员。
图 16.6:K = 3 的 K 均值聚类算法的 10 次运行的聚类结果的可视化。图中的每一行代表聚类算法的不同运行(具有不同的随机起点),共享相同颜色的变量是同一聚类的成员。
![A dendrogram depicting the relative similarity of the nine self-control variables. The three colored vertical lines represent three different cutoffs, resulting in either two (blue line), three (green line), or four (red line) clusters.](img/file91.png)
![A dendrogram depicting the relative similarity of the nine self-control variables. The three colored vertical lines represent three different cutoffs, resulting in either two (blue line), three (green line), or four (red line) clusters.](img/file91.png)
我们将从一个只有两个变量的简单例子开始,以便直观地了解它是如何工作的。首先,我们为变量 X 和 Y 生成一些合成数据,这两个变量之间的相关性为 0.7。主成分分析的目标是找到数据集中观察变量的线性组合,它将解释最大的方差;这里的想法是,数据中的方差是信号和噪声的组合,我们希望找到变量之间最强的共同信号。第一个主成分是解释最大方差的组合。第二个分量解释了最大剩余方差,同时也与第一个分量无关。有了更多的变量,我们可以继续这个过程,以获得与变量一样多的分量(假设观测值多于变量),尽管在实践中,我们通常希望找到少量的分量来解释大部分的方差。
我们将从一个只有两个变量的简单例子开始,以便直观地了解它是如何工作的。首先,我们为变量 X 和 Y 生成一些合成数据,这两个变量之间的相关性为 0.7。主成分分析的目标是找到数据集中自变量的线性组合,它将解释最大的方差;这里的想法是,数据中的方差是信号和噪声的组合,我们希望找到变量之间最强的共同信号。第一个主成分是解释最大方差的组合。第二个分量解释了最大剩余方差,同时也与第一个分量正交。有了更多的变量,我们可以继续这个过程,以获得与变量一样多的分量(假设观测值多于变量),尽管在实践中,我们通常希望找到少量的分量来解释大部分的方差。
在我们的二维例子中,我们可以计算主成分,并将它们绘制在数据上(图 [16.8](#fig:pcaPlot) )。我们看到的是,第一个主成分(显示为绿色)遵循最大方差的方向。这条线与线性回归线相似,但不完全相同;当线性回归解决方案最小化在相同 X 值的每个数据点和回归线之间的距离(即垂直距离)时,主成分最小化数据点和表示该成分的线之间的欧几里德距离(即垂直于该成分的距离)。第二个分量指向与第一个分量垂直的方向(相当于不相关)。
在我们的二维例子中,我们可以计算主成分,并将它们绘制在数据上(图 [16.8](#fig:pcaPlot) )。我们看到的是,第一个主成分(显示为绿色)遵循最大方差的方向。这条线与线性回归线相似,但不完全相同;当线性回归意在解决最小化在相同 X 值的每个数据点和回归线之间的距离(即垂直距离)时,主成分最小化数据点和表示该成分的线之间的欧几里德距离(即垂直于该成分的距离)。第二个分量指向与第一个分量垂直的方向(相当于不相关)。
![A plot of synthetic data, with the first principal component plotted in green and the second in red.](img/file92.png)
![A plot of synthetic data, with the first principal component plotted in green and the second in red.](img/file92.png)
从 A 到 Z 研究的实际数据是不公开的,所以我们将使用他们论文中报告的汇总数据来生成一些合成数据,这些数据大致与他们研究中获得的数据相匹配,每组的均值和标准差都相同。一旦我们有了数据,我们可以将它们可视化,以确保没有异常值。箱线图有助于观察分布的形状,如图 [17.1](#fig:AtoZBMIChangeDensity) 所示。这些数据看起来相当合理——在单个组中有几个异常值(由方框图外的点表示),但对于其他组来说,它们似乎并不极端。我们还可以看到,这些分布的方差似乎有点不同,阿特金斯比其他人显示出更大的可变性。这意味着任何假设组间方差相等的分析都可能是不合适的。幸运的是,我们计划使用的方差分析模型对此相当稳健。
<mathdisplay="block"><semantics><mrow><mtextmathvariant="normal">因变量</mtext><mo>∩</mo><mtextmathvariant="normal">自变量</mtext></mrow><annotationencoding="application/x-tex">\ text {因变量} \ sim \ text {自变量}</annotation></semantics></math>
![Left: A simulation of the winner's curse as a function of statistical power (x axis). The solid line shows the estimated effect size, and the dotted line shows the actual effect size. Right: A histogram showing effect size estimates for a number of samples from a dataset, with significant results shown in blue and non-significant results in red. ](img/file104.png)
![Left: A simulation of the winner's curse as a function of statistical power (x axis). The solid line shows the estimated effect size, and the dotted line shows the actual effect size. Right: A histogram showing effect size estimates for a number of samples from a dataset, with significant results shown in blue and non-significant results in red. ](img/file104.png)
Bem 在这里建议的被称为 *p-hacking* ,指的是尝试许多不同的分析,直到发现一个重要的结果。贝姆是正确的,如果一个人要对自己的数据分析工作做一次报告,那么这种方法就不是“不讲武德的”。然而,很少看到论文讨论对数据集进行的所有分析;更确切地说,论文经常只呈现*起作用*的分析——这通常意味着他们发现了一个具有统计学意义的结果。有许多不同的方法可以用来破解:
Bem 在这里建议的被称为 *p-hacking* ,指的是尝试许多不同的分析,直到发现一个重要的结果。贝姆是正确的,如果一个人要报告对数据进行的每一次分析,那么这种方法就不是“不道德的”。然而,很少看到论文讨论对数据集进行的所有分析;更确切地说,论文经常只呈现*起作用*的分析——这通常意味着他们发现了一个具有统计学意义的结果。有许多不同的方法可以用来破解:
> 作者必须在数据收集开始前决定终止数据收集的规则,并在文章中报告该规则。* The author must collect at least 20 observations per cell, otherwise provide a convincing proof of data collection cost.* The author must list all variables collected in the study. The author must report all the experimental conditions, including the failed operation.* If the observations are deleted, the author must also report the statistical results if they are included.* If the analysis contains covariates, the author must report the statistical results of the analysis without covariates.T13】