import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
from ISLP import load_data
13 Lab Chapter 13: Multiple Testing
We include our usual imports seen in earlier labs.
We also collect the new imports needed for this lab.
from scipy.stats import \
(ttest_1samp,
ttest_rel,
ttest_ind,as t_dbn)
t from statsmodels.stats.multicomp import \
pairwise_tukeyhsdfrom statsmodels.stats.multitest import \
as mult_test multipletests
13.1 Review of Hypothesis Tests
We begin by performing some one-sample \(t\)-tests.
First we create 100 variables, each consisting of 10 observations. The first 50 variables have mean \(0.5\) and variance \(1\), while the others have mean \(0\) and variance \(1\).
= np.random.default_rng(12)
rng = rng.standard_normal((10, 100))
X = np.array([0.5]*50 + [0]*50)
true_mean += true_mean[None,:] X
To begin, we use ttest_1samp()
from the scipy.stats
module to test \(H_{0}: \mu_1=0\), the null hypothesis that the first variable has mean zero.
= ttest_1samp(X[:,0], 0)
result result.pvalue
0.9307442156164141
The \(p\)-value comes out to 0.931, which is not low enough to reject the null hypothesis at level \(\alpha=0.05\). In this case, \(\mu_1=0.5\), so the null hypothesis is false. Therefore, we have made a Type II error by failing to reject the null hypothesis when the null hypothesis is false.
We now test \(H_{0,j}: \mu_j=0\) for \(j=1,\ldots,100\). We compute the 100 \(p\)-values, and then construct a vector recording whether the \(j\)th \(p\)-value is less than or equal to 0.05, in which case we reject \(H_{0j}\), or greater than 0.05, in which case we do not reject \(H_{0j}\), for \(j=1,\ldots,100\).
= np.empty(100)
p_values for i in range(100):
= ttest_1samp(X[:,i], 0).pvalue
p_values[i] = pd.cut(p_values,
decision 0, 0.05, 1],
[=['Reject H0',
labels'Do not reject H0'])
= pd.Categorical(true_mean == 0,
truth =[True, False],
categories=True) ordered
Since this is a simulated data set, we can create a \(2 \times 2\) table similar to Table 13.2.
pd.crosstab(decision,
truth,=['Decision'],
rownames=['H0']) colnames
H0 | True | False |
---|---|---|
Decision | ||
Reject H0 | 5 | 15 |
Do not reject H0 | 45 | 35 |
Therefore, at level \(\alpha=0.05\), we reject 15 of the 50 false null hypotheses, and we incorrectly reject 5 of the true null hypotheses. Using the notation from Section 13.3, we have \(V=5\), \(S=15\), \(U=45\) and \(W=35\). We have set \(\alpha=0.05\), which means that we expect to reject around 5% of the true null hypotheses. This is in line with the \(2 \times 2\) table above, which indicates that we rejected \(V=5\) of the \(50\) true null hypotheses.
In the simulation above, for the false null hypotheses, the ratio of the mean to the standard deviation was only \(0.5/1 = 0.5\). This amounts to quite a weak signal, and it resulted in a high number of Type II errors. Let’s instead simulate data with a stronger signal, so that the ratio of the mean to the standard deviation for the false null hypotheses equals \(1\). We make only 10 Type II errors.
= np.array([1]*50 + [0]*50)
true_mean = rng.standard_normal((10, 100))
X += true_mean[None,:]
X for i in range(100):
= ttest_1samp(X[:,i], 0).pvalue
p_values[i] = pd.cut(p_values,
decision 0, 0.05, 1],
[=['Reject H0',
labels'Do not reject H0'])
= pd.Categorical(true_mean == 0,
truth =[True, False],
categories=True)
ordered
pd.crosstab(decision,
truth,=['Decision'],
rownames=['H0']) colnames
H0 | True | False |
---|---|---|
Decision | ||
Reject H0 | 2 | 40 |
Do not reject H0 | 48 | 10 |
13.2 Family-Wise Error Rate
Recall from (13.5) that if the null hypothesis is true for each of \(m\) independent hypothesis tests, then the FWER is equal to \(1-(1-\alpha)^m\). We can use this expression to compute the FWER for \(m=1,\ldots, 500\) and \(\alpha=0.05\), \(0.01\), and \(0.001\). We plot the FWER for these values of \(\alpha\) in order to reproduce Figure 13.2.
= np.linspace(1, 501)
m = plt.subplots()
fig, ax
[ax.plot(m,1 - (1 - alpha)**m,
=r'$\alpha=%s$' % str(alpha))
labelfor alpha in [0.05, 0.01, 0.001]]
'log')
ax.set_xscale('Number of Hypotheses')
ax.set_xlabel('Family-Wise Error Rate')
ax.set_ylabel(
ax.legend()0.05, c='k', ls='--'); ax.axhline(
As discussed previously, even for moderate values of \(m\) such as \(50\), the FWER exceeds \(0.05\) unless \(\alpha\) is set to a very low value, such as \(0.001\). Of course, the problem with setting \(\alpha\) to such a low value is that we are likely to make a number of Type II errors: in other words, our power is very low.
We now conduct a one-sample \(t\)-test for each of the first five managers in the Fund
dataset, in order to test the null hypothesis that the \(j\)th fund manager’s mean return equals zero, \(H_{0,j}: \mu_j=0\).
= load_data('Fund')
Fund = Fund.iloc[:,:5]
fund_mini = np.empty(5)
fund_mini_pvals for i in range(5):
= ttest_1samp(fund_mini.iloc[:,i], 0).pvalue
fund_mini_pvals[i] fund_mini_pvals
array([0.00620236, 0.91827115, 0.01160098, 0.6005396 , 0.75578151])
The \(p\)-values are low for Managers One and Three, and high for the other three managers. However, we cannot simply reject \(H_{0,1}\) and \(H_{0,3}\), since this would fail to account for the multiple testing that we have performed. Instead, we will conduct Bonferroni’s method and Holm’s method to control the FWER.
To do this, we use the multipletests()
function from the statsmodels
module (abbreviated to mult_test()
). Given the \(p\)-values, for methods like Holm and Bonferroni the function outputs adjusted \(p\)-values, which can be thought of as a new set of \(p\)-values that have been corrected for multiple testing. If the adjusted \(p\)-value for a given hypothesis is less than or equal to \(\alpha\), then that hypothesis can be rejected while maintaining a FWER of no more than \(\alpha\). In other words, for such methods, the adjusted \(p\)-values resulting from the multipletests()
function can simply be compared to the desired FWER in order to determine whether or not to reject each hypothesis. We will later see that we can use the same function to control FDR as well.
The mult_test()
function takes \(p\)-values and a method
argument, as well as an optional alpha
argument. It returns the decisions (reject
below) as well as the adjusted \(p\)-values (bonf
).
= mult_test(fund_mini_pvals, method = "bonferroni")[:2]
reject, bonf reject
array([ True, False, False, False, False])
The \(p\)-values bonf
are simply the fund_mini_pvalues
multiplied by 5 and truncated to be less than or equal to 1.
* 5, 1) bonf, np.minimum(fund_mini_pvals
(array([0.03101178, 1. , 0.05800491, 1. , 1. ]),
array([0.03101178, 1. , 0.05800491, 1. , 1. ]))
Therefore, using Bonferroni’s method, we are able to reject the null hypothesis only for Manager One while controlling FWER at \(0.05\).
By contrast, using Holm’s method, the adjusted \(p\)-values indicate that we can reject the null hypotheses for Managers One and Three at a FWER of \(0.05\).
= "holm", alpha=0.05)[:2] mult_test(fund_mini_pvals, method
(array([ True, False, True, False, False]),
array([0.03101178, 1. , 0.04640393, 1. , 1. ]))
As discussed previously, Manager One seems to perform particularly well, whereas Manager Two has poor performance.
fund_mini.mean()
Manager1 3.0
Manager2 -0.1
Manager3 2.8
Manager4 0.5
Manager5 0.3
dtype: float64
Is there evidence of a meaningful difference in performance between these two managers? We can check this by performing a paired \(t\)-test using the ttest_rel()
function from scipy.stats
:
'Manager1'],
ttest_rel(fund_mini['Manager2']).pvalue fund_mini[
0.038391072368079586
The test results in a \(p\)-value of 0.038, suggesting a statistically significant difference.
However, we decided to perform this test only after examining the data and noting that Managers One and Two had the highest and lowest mean performances. In a sense, this means that we have implicitly performed \({5 \choose 2} = 5(5-1)/2=10\) hypothesis tests, rather than just one, as discussed in Section 13.3.2. Hence, we use the pairwise_tukeyhsd()
function from statsmodels.stats.multicomp
to apply Tukey’s method in order to adjust for multiple testing. This function takes as input a fitted ANOVA regression model, which is essentially just a linear regression in which all of the predictors are qualitative. In this case, the response consists of the monthly excess returns achieved by each manager, and the predictor indicates the manager to which each return corresponds.
= np.hstack([fund_mini.iloc[:,i] for i in range(5)])
returns = np.hstack([[i+1]*50 for i in range(5)])
managers = pairwise_tukeyhsd(returns, managers)
tukey print(tukey.summary())
Multiple Comparison of Means - Tukey HSD, FWER=0.05
===================================================
group1 group2 meandiff p-adj lower upper reject
---------------------------------------------------
1 2 -3.1 0.1862 -6.9865 0.7865 False
1 3 -0.2 0.9999 -4.0865 3.6865 False
1 4 -2.5 0.3948 -6.3865 1.3865 False
1 5 -2.7 0.3152 -6.5865 1.1865 False
2 3 2.9 0.2453 -0.9865 6.7865 False
2 4 0.6 0.9932 -3.2865 4.4865 False
2 5 0.4 0.9986 -3.4865 4.2865 False
3 4 -2.3 0.482 -6.1865 1.5865 False
3 5 -2.5 0.3948 -6.3865 1.3865 False
4 5 -0.2 0.9999 -4.0865 3.6865 False
---------------------------------------------------
The pairwise_tukeyhsd()
function provides confidence intervals for the difference between each pair of managers (lower
and upper
), as well as a \(p\)-value. All of these quantities have been adjusted for multiple testing. Notice that the \(p\)-value for the difference between Managers One and Two has increased from \(0.038\) to \(0.186\), so there is no longer clear evidence of a difference between the managers’ performances. We can plot the confidence intervals for the pairwise comparisons using the plot_simultaneous()
method of tukey
. Any pair of intervals that don’t overlap indicates a significant difference at the nominal level of 0.05. In this case, no differences are considered significant as reported in the table above.
13.3 False Discovery Rate
Now we perform hypothesis tests for all 2,000 fund managers in the Fund
dataset. We perform a one-sample \(t\)-test of \(H_{0,j}: \mu_j=0\), which states that the \(j\)th fund manager’s mean return is zero.
= np.empty(2000)
fund_pvalues for i, manager in enumerate(Fund.columns):
= ttest_1samp(Fund[manager], 0).pvalue fund_pvalues[i]
There are far too many managers to consider trying to control the FWER. Instead, we focus on controlling the FDR: that is, the expected fraction of rejected null hypotheses that are actually false positives. The multipletests()
function (abbreviated mult_test()
) can be used to carry out the Benjamini–Hochberg procedure.
= mult_test(fund_pvalues, method = "fdr_bh")[1]
fund_qvalues 10] fund_qvalues[:
array([0.08988921, 0.991491 , 0.12211561, 0.92342997, 0.95603587,
0.07513802, 0.0767015 , 0.07513802, 0.07513802, 0.07513802])
The q-values output by the Benjamini–Hochberg procedure can be interpreted as the smallest FDR threshold at which we would reject a particular null hypothesis. For instance, a \(q\)-value of \(0.1\) indicates that we can reject the corresponding null hypothesis at an FDR of 10% or greater, but that we cannot reject the null hypothesis at an FDR below 10%.
If we control the FDR at 10%, then for how many of the fund managers can we reject \(H_{0,j}: \mu_j=0\)?
<= 0.1).sum() (fund_qvalues
146
We find that 146 of the 2,000 fund managers have a \(q\)-value below 0.1; therefore, we are able to conclude that 146 of the fund managers beat the market at an FDR of 10%. Only about 15 (10% of 146) of these fund managers are likely to be false discoveries.
By contrast, if we had instead used Bonferroni’s method to control the FWER at level \(\alpha=0.1\), then we would have failed to reject any null hypotheses!
<= 0.1 / 2000).sum() (fund_pvalues
0
Figure 13.6 displays the ordered \(p\)-values, \(p_{(1)} \leq p_{(2)} \leq \cdots \leq p_{(2000)}\), for the Fund
dataset, as well as the threshold for rejection by the Benjamini–Hochberg procedure. Recall that the Benjamini–Hochberg procedure identifies the largest \(p\)-value such that \(p_{(j)}<qj/m\), and rejects all hypotheses for which the \(p\)-value is less than or equal to \(p_{(j)}\). In the code below, we implement the Benjamini–Hochberg procedure ourselves, in order to illustrate how it works. We first order the \(p\)-values. We then identify all \(p\)-values that satisfy \(p_{(j)}<qj/m\) (sorted_set_
). Finally, selected_
is a boolean array indicating which \(p\)-values are less than or equal to the largest \(p\)-value in sorted_[sorted_set_]
. Therefore, selected_
indexes the \(p\)-values rejected by the Benjamini–Hochberg procedure.
= np.sort(fund_pvalues)
sorted_ = fund_pvalues.shape[0]
m = 0.1
q = np.where(sorted_ < q * np.linspace(1, m, m) / m)[0]
sorted_set_ if sorted_set_.shape[0] > 0:
= fund_pvalues < sorted_[sorted_set_].max()
selected_ = np.arange(sorted_set_.max())
sorted_set_ else:
= []
selected_ = [] sorted_set_
We now reproduce the middle panel of Figure 13.6.
13.4 A Re-Sampling Approach
Here, we implement the re-sampling approach to hypothesis testing using the Khan
dataset, which we investigated in Section 13.5. First, we merge the training and testing data, which results in observations on 83 patients for 2,308 genes.
= load_data('Khan')
Khan = pd.concat([Khan['xtrain'], Khan['xtest']])
D 'Y'] = pd.concat([Khan['ytrain'], Khan['ytest']])
D['Y'].value_counts() D[
2 29
4 25
3 18
1 11
Name: Y, dtype: int64
There are four classes of cancer. For each gene, we compare the mean expression in the second class (rhabdomyosarcoma) to the mean expression in the fourth class (Burkitt’s lymphoma). Performing a standard two-sample \(t\)-test using ttest_ind()
from scipy.stats
on the \(11\)th gene produces a test-statistic of -2.09 and an associated \(p\)-value of 0.0412, suggesting modest evidence of a difference in mean expression levels between the two cancer types.
= D[lambda df:df['Y'] == 2]
D2 = D[lambda df:df['Y'] == 4]
D4 = 'G0011'
gene_11 = ttest_ind(D2[gene_11],
observedT, pvalue
D4[gene_11],=True)
equal_var observedT, pvalue
(-2.0936330736768185, 0.04118643782678394)
However, this \(p\)-value relies on the assumption that under the null hypothesis of no difference between the two groups, the test statistic follows a \(t\)-distribution with \(29+25-2=52\) degrees of freedom. Instead of using this theoretical null distribution, we can randomly split the 54 patients into two groups of 29 and 25, and compute a new test statistic. Under the null hypothesis of no difference between the groups, this new test statistic should have the same distribution as our original one. Repeating this process 10,000 times allows us to approximate the null distribution of the test statistic. We compute the fraction of the time that our observed test statistic exceeds the test statistics obtained via re-sampling.
= 10000
B = np.empty(B)
Tnull = np.hstack([D2[gene_11], D4[gene_11]])
D_ = D2[gene_11].shape[0]
n_ = D_.copy()
D_null for b in range(B):
rng.shuffle(D_null)= ttest_ind(D_null[:n_],
ttest_
D_null[n_:],=True)
equal_var= ttest_.statistic
Tnull[b] abs(Tnull) > np.abs(observedT)).mean() (np.
0.0398
This fraction, 0.0398, is our re-sampling-based \(p\)-value. It is almost identical to the \(p\)-value of 0.0412 obtained using the theoretical null distribution. We can plot a histogram of the re-sampling-based test statistics in order to reproduce Figure 13.7.
= plt.subplots(figsize=(8,8))
fig, ax
ax.hist(Tnull,=100,
bins=True,
density='y',
facecolor='Null')
label= np.linspace(-4.2, 4.2, 1001)
xval
ax.plot(xval,0]-2),
t_dbn.pdf(xval, D_.shape[='r')
c
ax.axvline(observedT,='b',
c='Observed')
label
ax.legend()"Null Distribution of Test Statistic"); ax.set_xlabel(
The re-sampling-based null distribution is almost identical to the theoretical null distribution, which is displayed in red.
Finally, we implement the plug-in re-sampling FDR approach outlined in Algorithm 13.4. Depending on the speed of your computer, calculating the FDR for all 2,308 genes in the Khan
dataset may take a while. Hence, we will illustrate the approach on a random subset of 100 genes. For each gene, we first compute the observed test statistic, and then produce 10,000 re-sampled test statistics. This may take a few minutes to run. If you are in a rush, then you could set B
equal to a smaller value (e.g. B=500
).
= 100, 10000
m, B = rng.choice(Khan['xtest'].columns, m, replace=False)
idx = np.empty(m)
T_vals = np.empty((m, B))
Tnull_vals
for j in range(m):
= idx[j]
col = ttest_ind(D2[col],
T_vals[j]
D4[col],=True).statistic
equal_var= np.hstack([D2[col], D4[col]])
D_ = D_.copy()
D_null for b in range(B):
rng.shuffle(D_null)= ttest_ind(D_null[:n_],
ttest_
D_null[n_:],=True)
equal_var= ttest_.statistic Tnull_vals[j,b]
Next, we compute the number of rejected null hypotheses \(R\), the estimated number of false positives \(\widehat{V}\), and the estimated FDR, for a range of threshold values \(c\) in Algorithm 13.4. The threshold values are chosen using the absolute values of the test statistics from the 100 genes.
= np.sort(np.abs(T_vals))
cutoffs = np.empty((3, m))
FDRs, Rs, Vs for j in range(m):
= np.sum(np.abs(T_vals) >= cutoffs[j])
R = np.sum(np.abs(Tnull_vals) >= cutoffs[j]) / B
V = R
Rs[j] = V
Vs[j] = V / R FDRs[j]
Now, for any given FDR, we can find the genes that will be rejected. For example, with FDR controlled at 0.1, we reject 15 of the 100 null hypotheses. On average, we would expect about one or two of these genes (i.e. 10% of 15) to be false discoveries. At an FDR of 0.2, we can reject the null hypothesis for 28 genes, of which we expect around six to be false discoveries.
The variable idx
stores which genes were included in our 100 randomly-selected genes. Let’s look at the genes whose estimated FDR is less than 0.1.
sorted(idx[np.abs(T_vals) >= cutoffs[FDRs < 0.1].min()])
['G0097',
'G0129',
'G0182',
'G0714',
'G0812',
'G0941',
'G0982',
'G1020',
'G1022',
'G1090',
'G1320',
'G1634',
'G1697',
'G1853',
'G1854',
'G1994',
'G2017',
'G2115',
'G2193']
At an FDR threshold of 0.2, more genes are selected, at the cost of having a higher expected proportion of false discoveries.
sorted(idx[np.abs(T_vals) >= cutoffs[FDRs < 0.2].min()])
['G0097',
'G0129',
'G0158',
'G0182',
'G0242',
'G0552',
'G0679',
'G0714',
'G0751',
'G0812',
'G0908',
'G0941',
'G0982',
'G1020',
'G1022',
'G1090',
'G1240',
'G1244',
'G1320',
'G1381',
'G1514',
'G1634',
'G1697',
'G1768',
'G1853',
'G1854',
'G1907',
'G1994',
'G2017',
'G2115',
'G2193']
The next line generates Figure 13.11, which is similar to Figure 13.9, except that it is based on only a subset of the genes.