R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
243: Multiple Testing Correction Methods
I was looking at some analysis from a colleague recently where they tested 50 different variables to see if they correlated with customer churn. They came back thrilled, reporting that seven of those variables were "statistically significant" (p < 0.05). My immediate reaction wasn't excitement—it was skepticism. When you run 50 tests, the laws of probability dictate that you'll find some "winners" even if the data is complete garbage.
The "Wait, that can't be right" moment
Let's simulate this. I'll create a scenario where we are testing 20 different "wellness supplements" to see if they lower blood pressure. In reality, I'm going to make it so that 19 of these supplements do absolutely nothing, and only one actually works. We should only find one significant result, right?
# Setup: 20 supplements, 10 people per group
set.seed(42)
results <- replicate(20, {
# Group A: Placebo, Group B: Supplement
# Most are just random noise (mean=120 for both)
group_a <- rnorm(10, 120, 5)
group_b <- rnorm(10, 120, 5)
t.test(group_a, group_b)$p.value
})
# Let's force the 20th one to actually be significant
results[20] <- 0.001
# How many look "significant" at the 0.05 level?
sum(results < 0.05)
When I run this, I often see 2 or 3 "significant" results. I know for a fact that only one supplement actually works, yet R is telling me others are significant too. This is the "Multiple Comparisons Problem." If you roll a die 20 times, you're probably going to hit a six eventually; that doesn't mean the die is magic, it just means you tried a lot of times.
Applying the Sledgehammer: Bonferroni
The most intuitive way to fix this is to be way stricter. If I'm doing 20 tests, I'll just divide my alpha (0.05) by 20. This is the Bonferroni correction. It's the "sledgehammer" approach—it kills false positives, but it might kill your actual discoveries too.
# Using the built-in p.adjust function
bonf_p <- p.adjust(results, method = "bonferroni")
sum(bonf_p < 0.05)
Now we're likely back to just 1 significant result. It worked! But here's the catch: Bonferroni is incredibly conservative. If I had 1,000 variables (like in genomic data), my adjusted p-value would have to be 0.00005 to be considered significant. I'd end up throwing away real, valuable signals because the bar was set impossibly high.
Finding a Middle Ground with FDR
I usually prefer the Benjamini-Hochberg (BH) method. Instead of trying to ensure there are zero false positives (which is what Bonferroni does), BH controls the False Discovery Rate. It basically says, "I'm okay if 5% of my 'significant' results are fake, as long as the rest are real."
Let's see how the BH method handles our supplement data compared to the others:
# Comparing methods
comparison <- data.frame(
Raw = results,
Bonferroni = p.adjust(results, method = "bonferroni"),
BH = p.adjust(results, method = "BH")
)
head(comparison)
If you look at the BH column, the p-values are higher than the raw ones, but lower than the Bonferroni ones. It's a sliding scale. In a professional pipeline, this is almost always the right choice when you're doing exploratory data analysis. It gives you the power to find effects without being fooled by the sheer volume of tests you're running.
Which one do you actually use?
I've found a simple rule of thumb over the years: If the cost of a False Positive is catastrophic (e.g., approving a drug that might be toxic), use Bonferroni. If you're in the "discovery" phase—trying to find interesting leads to investigate further—use BH. Just remember that any time you see a p-value in a paper or a report, your first question should be: "How many other tests did they run before they found this one?"
📋 Practical Task
Filtering Differentially Expressed Genes
You are analyzing a transcriptomics dataset. You have a vector of p-values representing 100 different genes. You suspect that only a few are actually differentially expressed, but the rest are noise.
Your task:
- Create a vector of 100 p-values. Make 95 of them random values between 0.01 and 1.0, and 5 of them very small (e.g., between 0.0001 and 0.001).
- Apply the Benjamini-Hochberg (BH) correction using
p.adjust(). - Create a logical vector that identifies which genes remain significant at an adjusted alpha level of 0.05.
- Print the number of genes that passed the raw 0.05 threshold versus the number that passed the BH-adjusted 0.05 threshold.
There are no comments for now.