{
"cells": [
{
"cell_type": "raw",
"id": "b4fa2bec",
"metadata": {},
"source": [
"---\n",
"title: \"Ch13-multiple-lab\"\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "3cfca3e8",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "d63f77a6",
"metadata": {},
"source": [
"# Lab: Multiple Testing\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "37f9384b",
"metadata": {},
"source": [
"## Review of Hypothesis Tests\n",
"\n",
"We begin by performing some one-sample $t$-tests using the `t.test()` function.\n",
" First we create 100 variables, each consisting of 10 observations. The first 50 variables have mean $0.5$ and variance $1$,\n",
"while the others have mean $0$ and variance $1$."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c43938e3",
"metadata": {
"lines_to_next_cell": 2,
"name": "chunk1"
},
"outputs": [],
"source": [
"set.seed(6)\n",
"x <- matrix(rnorm(10 * 100), 10, 100)\n",
"x[, 1:50] <- x[, 1:50] + 0.5"
]
},
{
"cell_type": "markdown",
"id": "d6caa027",
"metadata": {},
"source": [
"The `t.test()` function can perform a one-sample or a two-sample $t$-test.\n",
" By default, a one-sample test is performed. To begin, we test $H_{0}: \\mu_1=0$, the null hypothesis that the first variable has mean zero."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f23b583",
"metadata": {
"name": "chunk2"
},
"outputs": [],
"source": [
"t.test(x[, 1], mu = 0)"
]
},
{
"cell_type": "markdown",
"id": "8e1a5477",
"metadata": {},
"source": [
"The $p$-value comes out to $0.067$, which is not quite low enough to reject the null hypothesis at level $\\alpha=0.05$.\n",
" 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\n",
"the null hypothesis is false. \n",
"\n",
"We now test $H_{0j}: \\mu_j=0$ for $j=1,\\ldots,100$. We compute the 100\n",
" $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}$,\n",
" or greater than 0.05, in which case we do not reject $H_{0j}$, for $j=1,\\ldots,100$."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0b3ec305",
"metadata": {
"name": "chunk3"
},
"outputs": [],
"source": [
"p.values <- rep(0, 100)\n",
"for (i in 1:100)\n",
" p.values[i] <- t.test(x[, i], mu = 0)$p.value\n",
"decision <- rep(\"Do not reject H0\", 100)\n",
"decision[p.values <= .05] <- \"Reject H0\""
]
},
{
"cell_type": "markdown",
"id": "25759ac1",
"metadata": {},
"source": [
"Since this is a simulated data set, we can create a $2 \\times 2$ table similar to Table 13.2."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "503969a9",
"metadata": {
"name": "chunk4"
},
"outputs": [],
"source": [
"table(decision,\n",
" c(rep(\"H0 is False\", 50), rep(\"H0 is True\", 50))\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "c3bc41ae",
"metadata": {},
"source": [
"Therefore, at level $\\alpha=0.05$, we reject just 10 of the 50 false null hypotheses,\n",
" and we incorrectly reject 3 of the true null hypotheses. Using the notation from Section 13.3, we have $W=40$, $U=47$, $S=10$, and $V=3$.\n",
" Note that the rows and columns of this table are reversed relative to Table 13.2.\n",
" 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\n",
"above, which indicates that we rejected $V=3$ of the $50$ true null hypotheses."
]
},
{
"cell_type": "markdown",
"id": "e8ddf043",
"metadata": {},
"source": [
"In the simulation above, for the false null hypotheses, the ratio of the mean to the standard deviation\n",
" was only $0.5/1 = 0.5$. This amounts to quite a weak signal, and\n",
"it resulted in a high number of Type II errors.\n",
"If we 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$,\n",
"then we make only $9$ Type II errors."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8fafa13",
"metadata": {
"lines_to_next_cell": 2,
"name": "chunk5"
},
"outputs": [],
"source": [
"x <- matrix(rnorm(10 * 100), 10, 100)\n",
"x[, 1:50] <- x[, 1:50] + 1\n",
"for (i in 1:100)\n",
" p.values[i] <- t.test(x[, i], mu = 0)$p.value\n",
"decision <- rep(\"Do not reject H0\", 100)\n",
"decision[p.values <= .05] <- \"Reject H0\"\n",
"table(decision,\n",
" c(rep(\"H0 is False\", 50), rep(\"H0 is True\", 50))\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "61eb6ec4",
"metadata": {},
"source": [
"## The Family-Wise Error Rate\n",
"Recall from (13.5) that if the null hypothesis is true for each of $m$ independent hypothesis tests, then the\n",
" 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$."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "279c3dcd",
"metadata": {
"name": "chunk6"
},
"outputs": [],
"source": [
"m <- 1:500\n",
"fwe1 <- 1 - (1 - 0.05)^m\n",
"fwe2 <- 1 - (1 - 0.01)^m\n",
"fwe3 <- 1 - (1 - 0.001)^m"
]
},
{
"cell_type": "markdown",
"id": "2b9d7f18",
"metadata": {},
"source": [
"We plot these three vectors in order to\n",
"reproduce Figure 13.2. The red, blue, and green lines correspond to $\\alpha=0.05$, $0.01$, and $0.001$, respectively."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a0f029cd",
"metadata": {
"name": "chunk7"
},
"outputs": [],
"source": [
"par(mfrow = c(1, 1))\n",
"plot(m, fwe1, type = \"l\", log = \"x\", ylim = c(0, 1), col = 2,\n",
" ylab = \"Family - Wise Error Rate\",\n",
" xlab = \"Number of Hypotheses\")\n",
"lines(m, fwe2, col = 4)\n",
"lines(m, fwe3, col = 3)\n",
"abline(h = 0.05, lty = 2)"
]
},
{
"cell_type": "markdown",
"id": "3d549cd0",
"metadata": {},
"source": [
" 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,\n",
" such as $0.001$.\n",
"Of course,\n",
" 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.\n"
]
},
{
"cell_type": "markdown",
"id": "24a6b086",
"metadata": {},
"source": [
"We now conduct a one-sample $t$-test for each of the first five managers in the `Fund` dataset,\n",
" in order to test the null hypothesis that the $j$th fund manager's mean return equals zero,\n",
"$H_{0j}: \\mu_j=0$."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "664e6ffd",
"metadata": {
"name": "chunk8"
},
"outputs": [],
"source": [
"library(ISLR2)\n",
"fund.mini <- Fund[, 1:5]\n",
"t.test(fund.mini[, 1], mu = 0)\n",
"fund.pvalue <- rep(0, 5)\n",
"for (i in 1:5)\n",
" fund.pvalue[i] <- t.test(fund.mini[, i], mu = 0)$p.value\n",
"fund.pvalue"
]
},
{
"cell_type": "markdown",
"id": "ee015317",
"metadata": {},
"source": [
"The $p$-values are low for Managers One and Three, and high for the other three managers.\n",
" However, we cannot simply reject $H_{01}$ and $H_{03}$, since this would fail to account for the multiple testing that we\n",
"have performed. Instead, we will\n",
" conduct Bonferroni's method and Holm's method\n",
"to control the FWER.\n"
]
},
{
"cell_type": "markdown",
"id": "231418ea",
"metadata": {},
"source": [
"To do this, we use the `p.adjust()` function. Given the $p$-values, the\n",
" function outputs , which\n",
" 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\n",
"less than or equal to $\\alpha$, then that hypothesis can be rejected while maintaining a FWER\n",
"of no more than $\\alpha$.\n",
"In other words, the adjusted $p$-values resulting from the `p.adjust()` function can simply be compared to the desired FWER in order to determine\n",
"whether or not to reject\n",
"each hypothesis.\n",
"\n",
"For example, in the case of Bonferroni's\n",
"method, the raw $p$-values are multiplied by the total number of hypotheses, $m$, in order to obtain the adjusted $p$-values. (However,\n",
"adjusted $p$-values are not allowed to exceed $1$.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4bacf56",
"metadata": {
"name": "chunk9"
},
"outputs": [],
"source": [
"p.adjust(fund.pvalue, method = \"bonferroni\")\n",
"pmin(fund.pvalue * 5, 1)"
]
},
{
"cell_type": "markdown",
"id": "e26c16c9",
"metadata": {},
"source": [
"Therefore, using Bonferroni's method, we are able to reject the null hypothesis only for Manager\n",
"One while controlling the FWER at $0.05$.\n",
"\n",
"By contrast, using Holm's method, the adjusted $p$-values indicate that we can \n",
" reject the null hypotheses for Managers One and Three at a FWER\n",
"of $0.05$."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "853d32f3",
"metadata": {
"lines_to_next_cell": 2,
"name": "chunk10"
},
"outputs": [],
"source": [
"p.adjust(fund.pvalue, method = \"holm\")"
]
},
{
"cell_type": "markdown",
"id": "85e3a2bf",
"metadata": {},
"source": [
"As discussed previously, Manager One seems to perform particularly well, whereas Manager Two has poor performance."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03559c96",
"metadata": {
"name": "chunk11"
},
"outputs": [],
"source": [
"apply(fund.mini, 2, mean)"
]
},
{
"cell_type": "markdown",
"id": "020e601f",
"metadata": {},
"source": [
"Is there evidence of a meaningful difference in performance between these two managers? Performing a paired $t$-test \n",
"using the `t.test()` function results in a $p$-value of $0.038$, suggesting a statistically significant difference."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "53b54794",
"metadata": {
"name": "chunk12"
},
"outputs": [],
"source": [
"t.test(fund.mini[, 1], fund.mini[, 2], paired = T)"
]
},
{
"cell_type": "markdown",
"id": "15910915",
"metadata": {},
"source": [
"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.\n",
" 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\n",
"in Section 13.3.2.\n",
" Hence, we use the `TukeyHSD()` function to apply Tukey's method in order to adjust for multiple testing.\n",
"This function takes as input the output of an regression model,\n",
"which is essentially just a linear regression in which all of the predictors are qualitative.\n",
"In this case, the response consists of the monthly excess returns achieved by each manager,\n",
" and the predictor indicates the manager to which each return corresponds."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d588d293",
"metadata": {
"name": "chunk13"
},
"outputs": [],
"source": [
"returns <- as.vector(as.matrix(fund.mini))\n",
"manager <- rep(c(\"1\", \"2\", \"3\", \"4\", \"5\"), rep(50, 5))\n",
"a1 <- aov(returns ~ manager)\n",
"TukeyHSD(x = a1)"
]
},
{
"cell_type": "markdown",
"id": "eb7e746b",
"metadata": {},
"source": [
"The `TukeyHSD()` function provides confidence intervals for the difference between each pair of managers (`lwr` and `upr`),\n",
"as well as a $p$-value. All of these quantities\n",
" have been adjusted for multiple testing. Notice that the $p$-value for the difference between Managers One and Two\n",
" has increased from $0.038$ to $0.186$, so there is no longer clear evidence of a difference between the managers' performances.\n",
"We can plot the confidence intervals for the pairwise comparisons using the `plot()` function."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "314008d4",
"metadata": {
"lines_to_next_cell": 2,
"name": "chunk14"
},
"outputs": [],
"source": [
"plot(TukeyHSD(x = a1))"
]
},
{
"cell_type": "markdown",
"id": "6b29dd61",
"metadata": {},
"source": [
"## The False Discovery Rate\n",
"\n",
"Now we perform hypothesis tests for all 2,000 fund managers in the `Fund` dataset. We perform a one-sample $t$-test of\n",
"$H_{0j}: \\mu_j=0$, which states that the $j$th fund manager's mean return is zero."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a3584c09",
"metadata": {
"name": "chunk15"
},
"outputs": [],
"source": [
"fund.pvalues <- rep(0, 2000)\n",
"for (i in 1:2000)\n",
" fund.pvalues[i] <- t.test(Fund[, i], mu = 0)$p.value"
]
},
{
"cell_type": "markdown",
"id": "da9aa778",
"metadata": {},
"source": [
"There are far too many managers to consider trying to control the FWER.\n",
"Instead, we focus on controlling the FDR: that is, the expected fraction of rejected null hypotheses that are actually false positives.\n",
"The `p.adjust()` function can be used to carry out the Benjamini-Hochberg procedure."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ada6abcb",
"metadata": {
"name": "chunk16"
},
"outputs": [],
"source": [
"q.values.BH <- p.adjust(fund.pvalues, method = \"BH\")\n",
"q.values.BH[1:10]"
]
},
{
"cell_type": "markdown",
"id": "b43ecc3a",
"metadata": {},
"source": [
"The q-values output by the Benjamini-Hochberg procedure can be interpreted as the smallest FDR threshold at which we\n",
"would reject a particular null hypothesis. For instance, a $q$-value of $0.1$ indicates that we can reject the corresponding null hypothesis at\n",
"an FDR of $10\\%$ or greater, but that we cannot reject the null hypothesis at an FDR below $10\\%$.\n",
"\n",
"If we control the FDR at $10\\%$, then for how many of the fund managers can we reject $H_{0j}: \\mu_j=0$?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ab0cd15",
"metadata": {
"name": "chunk17"
},
"outputs": [],
"source": [
"sum(q.values.BH <= .1)"
]
},
{
"cell_type": "markdown",
"id": "2d4ccdfc",
"metadata": {},
"source": [
"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\n",
"beat the market at an FDR of $10\\%$. Only about 15 ($10\\%$ of 146) of these fund managers are likely to be false discoveries.\n",
"By contrast,\n",
"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!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78caad3a",
"metadata": {
"lines_to_next_cell": 2,
"name": "chunk18"
},
"outputs": [],
"source": [
"sum(fund.pvalues <= (0.1 / 2000))"
]
},
{
"cell_type": "markdown",
"id": "49b21194",
"metadata": {},
"source": [
"Figure 13.6 displays the ordered $p$-values, $p_{(1)} \\leq p_{(2)} \\leq \\cdots \\leq p_{(2000)}$,\n",
"for the `Fund` dataset, as well as the\n",
"threshold for rejection by the Benjamini-Hochberg procedure.\n",
"Recall that the Benjamini-Hochberg procedure search\\-es for the largest $p$-value such that $p_{(j)}0) {\n",
" wh <- 1:max(wh.ps)\n",
" } else {\n",
" wh <- numeric(0)\n",
" }"
]
},
{
"cell_type": "markdown",
"id": "995236af",
"metadata": {},
"source": [
"We now reproduce the middle panel of Figure 13.6."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "449484f6",
"metadata": {
"lines_to_next_cell": 0,
"name": "chunk20"
},
"outputs": [],
"source": [
"plot(ps, log = \"xy\", ylim = c(4e-6, 1), ylab = \"P-Value\",\n",
" xlab = \"Index\", main = \"\")\n",
"points(wh, ps[wh], col = 4)\n",
"abline(a = 0, b = (q / m), col = 2, untf = TRUE)\n",
"abline(h = 0.1 / 2000, col = 3)"
]
},
{
"cell_type": "markdown",
"id": "9237649e",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "5c26804b",
"metadata": {},
"source": [
"## A Re-Sampling Approach\n",
"Here, we implement the re-sampling approach to hypothesis testing using the `Khan` dataset, which we investigated in Section 13.5.\n",
" First, we merge the training and testing data, which results in observations on $83$ patients for $2{,}308$ genes."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b828cec6",
"metadata": {
"name": "chunk21"
},
"outputs": [],
"source": [
"attach(Khan)\n",
"x <- rbind(xtrain, xtest)\n",
"y <- c(as.numeric(ytrain), as.numeric(ytest))\n",
"dim(x)\n",
"table(y)"
]
},
{
"cell_type": "markdown",
"id": "4f77164f",
"metadata": {},
"source": [
"There are four classes of cancer. For each gene, we compare the mean expression in the second class (rhabdomyosarcoma)\n",
" to the mean expression in the fourth class (Burkitt's lymphoma).\n",
" Performing a standard two-sample $t$-test on the $11$th gene produces a test-statistic of $-2.09$\n",
"and an associated $p$-value of $0.0412$, suggesting modest evidence of a difference in mean expression levels between the two cancer types."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d273d088",
"metadata": {
"name": "chunk22"
},
"outputs": [],
"source": [
"x <- as.matrix(x)\n",
"x1 <- x[which(y == 2), ]\n",
"x2 <- x[which(y == 4), ]\n",
"n1 <- nrow(x1)\n",
"n2 <- nrow(x2)\n",
"t.out <- t.test(x1[, 11], x2[, 11], var.equal = TRUE)\n",
"TT <- t.out$statistic\n",
"TT\n",
"t.out$p.value"
]
},
{
"cell_type": "markdown",
"id": "5fe95eec",
"metadata": {},
"source": [
"However, this $p$-value relies on the assumption that under the null hypothesis of no difference between the two groups, the\n",
" test statistic follows a $t$-distribution with $29+25-2=52$ degrees of freedom.\n",
"Instead of using this theoretical null distribution,\n",
" we can randomly split the 54 patients into two groups of 29 and 25, and compute a new test statistic.\n",
"Under the null hypothesis of no difference between the groups, this new test statistic should have the same distribution as our original one.\n",
"Repeating this process 10,000 times allows us to approximate the null distribution of the test statistic.\n",
" We compute the fraction of the time that our observed test statistic exceeds the test statistics obtained via re-sampling."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6410a3c8",
"metadata": {
"name": "chunk23"
},
"outputs": [],
"source": [
"set.seed(1)\n",
"B <- 10000\n",
"Tbs <- rep(NA, B)\n",
"for (b in 1:B) {\n",
" dat <- sample(c(x1[, 11], x2[, 11]))\n",
" Tbs[b] <- t.test(dat[1:n1], dat[(n1 + 1):(n1 + n2)],\n",
" var.equal = TRUE\n",
" )$statistic\n",
"}\n",
"mean((abs(Tbs) >= abs(TT)))"
]
},
{
"cell_type": "markdown",
"id": "c087c0cc",
"metadata": {},
"source": [
"This fraction, $0.0416$, is our re-sampling-based $p$-value. It is almost identical to the $p$-value of $0.0412$ obtained using the theoretical null distribution.\n",
"\n",
"We can plot a histogram of the re-sampling-based test statistics in order to reproduce Figure 13.7."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "658fe12e",
"metadata": {
"name": "chunk24"
},
"outputs": [],
"source": [
"hist(Tbs, breaks = 100, xlim = c(-4.2, 4.2), main = \"\",\n",
" xlab = \"Null Distribution of Test Statistic\", col = 7)\n",
"lines(seq(-4.2, 4.2, len = 1000),\n",
" dt(seq(-4.2, 4.2, len = 1000),\n",
" df = (n1 + n2 - 2)\n",
" ) * 1000, col = 2, lwd = 3)\n",
"abline(v = TT, col = 4, lwd = 2)\n",
"text(TT + 0.5, 350, paste(\"T = \", round(TT, 4), sep = \"\"),\n",
" col = 4)"
]
},
{
"cell_type": "markdown",
"id": "1fd6f592",
"metadata": {},
"source": [
"The re-sampling-based null distribution is almost identical to the theoretical null distribution, which is displayed in red."
]
},
{
"cell_type": "markdown",
"id": "45c163d5",
"metadata": {},
"source": [
"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.\n",
"Hence, we will illustrate the approach on a random subset of 100 genes.\n",
"For each gene, we first compute the observed test statistic,\n",
"and then produce $10{,}000$ re-sampled test statistics. This may take a few minutes to run. If you are in a rush,\n",
"then you could set `B` equal to a smaller\n",
"value (e.g. `B = 500`)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd14c700",
"metadata": {
"name": "chunk25"
},
"outputs": [],
"source": [
"m <- 100\n",
"B<-1000\n",
"set.seed(1)\n",
"index <- sample(ncol(x1), m)\n",
"Ts <- rep(NA, m)\n",
"Ts.star <- matrix(NA, ncol = m, nrow = B)\n",
"for (j in 1:m) {\n",
" k <- index[j]\n",
" Ts[j] <- t.test(x1[, k], x2[, k],\n",
" var.equal = TRUE\n",
" )$statistic\n",
" for (b in 1:B) {\n",
" dat <- sample(c(x1[, k], x2[, k]))\n",
" Ts.star[b, j] <- t.test(dat[1:n1],\n",
" dat[(n1 + 1):(n1 + n2)], var.equal = TRUE\n",
" )$statistic\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "02630896",
"metadata": {},
"source": [
"Next, we compute the number of rejected null hypotheses $R$, the estimated number of false positives $\\widehat{V}$,\n",
" and the estimated FDR, for a range of threshold values\n",
" $c$ in Algorithm 13.4. The threshold values are chosen using the absolute values of the\n",
" test statistics from the $100$ genes."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "445d83c1",
"metadata": {
"name": "chunk26"
},
"outputs": [],
"source": [
"cs <- sort(abs(Ts))\n",
"FDRs <- Rs <- Vs <- rep(NA, m)\n",
"for (j in 1:m) {\n",
" R <- sum(abs(Ts) >= cs[j])\n",
" V <- sum(abs(Ts.star) >= cs[j]) / B\n",
" Rs[j] <- R\n",
" Vs[j] <- V\n",
" FDRs[j] <- V / R\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "84370f56",
"metadata": {},
"source": [
"Now, for any given FDR, we can find the genes that will be\n",
"rejected. For example, with the FDR controlled at 0.1,\n",
" 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.\n",
"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.\n",
"The variable `index` is needed here since we restricted our analysis to just $100$ randomly-selected genes."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c87db9b",
"metadata": {
"name": "chunk27"
},
"outputs": [],
"source": [
"max(Rs[FDRs <= .1])\n",
"sort(index[abs(Ts) >= min(cs[FDRs < .1])])\n",
"max(Rs[FDRs <= .2])\n",
"sort(index[abs(Ts) >= min(cs[FDRs < .2])])"
]
},
{
"cell_type": "markdown",
"id": "98c97a70",
"metadata": {},
"source": [
"The next line generates Figure 13.11, which is similar to Figure 13.9,\n",
"except that it is based on only a subset of the genes."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "253fa9cb",
"metadata": {
"name": "chunk28"
},
"outputs": [],
"source": [
"plot(Rs, FDRs, xlab = \"Number of Rejections\", type = \"l\",\n",
" ylab = \"False Discovery Rate\", col = 4, lwd = 3)"
]
},
{
"cell_type": "markdown",
"id": "1e14707c",
"metadata": {},
"source": [
"As noted in the chapter, much more efficient implementations of the re-sampling approach to FDR calculation are available, using\n",
"e.g. the `samr` package in `R`.\n",
"\n",
"\n"
]
}
],
"metadata": {
"celltoolbar": "Edit Metadata",
"jupytext": {
"formats": "ipynb,Rmd"
},
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "4.0.5"
},
"substitutions": {
"ISLPmod": "`ISLP`",
"Rlang": "`R`",
"mpl": "`matplotlib`",
"numpy": "`numpy`",
"pandas": "`pandas`",
"pylang": "`python`",
"smlib": "`statsmodels`"
}
},
"nbformat": 4,
"nbformat_minor": 5
}