|
| 1 | +# R for Biomedical Data Analysis |
| 2 | + |
| 3 | +This directory contains R implementations of essential statistical tests commonly used in biomedical research and clinical studies. These non-parametric tests are fundamental tools for biomedical students and researchers working with medical data. |
| 4 | + |
| 5 | +## 📋 Contents |
| 6 | + |
| 7 | +- [`wilcoxon_signed_rank_test.r`](wilcoxon_signed_rank_test.r) - Wilcoxon Signed-Rank Test for paired samples |
| 8 | +- [`mann_whitney_u_test.r`](mann_whitney_u_test.r) - Mann-Whitney U Test for independent samples |
| 9 | +- [`README.md`](README.md) - This documentation file |
| 10 | + |
| 11 | +## 🏥 Why These Tests Are Essential for Biomedical Students |
| 12 | + |
| 13 | +### 1. **Real-World Medical Data Challenges** |
| 14 | +Medical data rarely follows normal distributions due to: |
| 15 | +- **Skewed distributions**: Lab values, reaction times, survival data |
| 16 | +- **Outliers**: Extreme measurements that are medically significant |
| 17 | +- **Small sample sizes**: Pilot studies, rare diseases, expensive procedures |
| 18 | +- **Ordinal scales**: Pain scores, quality of life indices, severity ratings |
| 19 | + |
| 20 | +### 2. **Robust and Assumption-Free** |
| 21 | +Non-parametric tests like Wilcoxon and Mann-Whitney: |
| 22 | +- **No normality assumption**: Work with any distribution shape |
| 23 | +- **Outlier resistant**: Not affected by extreme values |
| 24 | +- **Rank-based**: Focus on relative ordering rather than exact values |
| 25 | +- **Clinically meaningful**: Often more relevant for medical decisions |
| 26 | + |
| 27 | +### 3. **Common Applications in Medicine** |
| 28 | +- **Clinical trials**: Comparing treatment effectiveness |
| 29 | +- **Before/after studies**: Evaluating intervention outcomes |
| 30 | +- **Biomarker research**: Comparing levels between groups |
| 31 | +- **Quality of life studies**: Analyzing patient-reported outcomes |
| 32 | +- **Diagnostic accuracy**: Comparing test performance |
| 33 | + |
| 34 | +## 📊 Statistical Tests Overview |
| 35 | + |
| 36 | +### Wilcoxon Signed-Rank Test |
| 37 | +**Purpose**: Compare paired samples or test single sample against a median |
| 38 | + |
| 39 | +**When to use**: |
| 40 | +- Before/after treatment comparisons |
| 41 | +- Matched pairs (e.g., twins, matched controls) |
| 42 | +- Repeated measurements on same subjects |
| 43 | +- Testing if median differs from a specific value |
| 44 | + |
| 45 | +**Medical Examples**: |
| 46 | +- Blood pressure before vs after medication |
| 47 | +- Pain scores before vs after treatment |
| 48 | +- Weight loss in diet studies |
| 49 | +- Biomarker changes over time |
| 50 | + |
| 51 | +**Assumptions**: |
| 52 | +- Paired observations or single sample |
| 53 | +- Data can be ranked |
| 54 | +- Differences are symmetrically distributed around the median |
| 55 | + |
| 56 | +### Mann-Whitney U Test (Wilcoxon Rank-Sum Test) |
| 57 | +**Purpose**: Compare two independent groups |
| 58 | + |
| 59 | +**When to use**: |
| 60 | +- Comparing treatment vs control groups |
| 61 | +- Comparing different populations |
| 62 | +- When groups are independent (not paired) |
| 63 | +- Alternative to two-sample t-test |
| 64 | + |
| 65 | +**Medical Examples**: |
| 66 | +- Drug efficacy: treatment vs placebo |
| 67 | +- Gender differences in biomarkers |
| 68 | +- Disease severity between stages |
| 69 | +- Age-related immune responses |
| 70 | +- Comparing diagnostic methods |
| 71 | + |
| 72 | +**Assumptions**: |
| 73 | +- Two independent samples |
| 74 | +- Data can be ranked |
| 75 | +- Similar distribution shapes (for location comparison) |
| 76 | + |
| 77 | +## 🚀 Quick Start Guide |
| 78 | + |
| 79 | +### Installation |
| 80 | +No additional packages required! Just R base installation. |
| 81 | + |
| 82 | +```r |
| 83 | +# Clone or download the files to your R working directory |
| 84 | +# Source the functions |
| 85 | +source("wilcoxon_signed_rank_test.r") |
| 86 | +source("mann_whitney_u_test.r") |
| 87 | +``` |
| 88 | + |
| 89 | +### Basic Usage |
| 90 | + |
| 91 | +#### Wilcoxon Signed-Rank Test |
| 92 | +```r |
| 93 | +# Example: Blood pressure before and after treatment |
| 94 | +before <- c(145, 150, 138, 155, 142, 148, 152, 140) |
| 95 | +after <- c(138, 142, 135, 148, 136, 140, 145, 133) |
| 96 | + |
| 97 | +# Perform the test |
| 98 | +result <- wilcoxon_signed_rank_test(before, after, alternative = "greater") |
| 99 | +print(result) |
| 100 | +``` |
| 101 | + |
| 102 | +#### Mann-Whitney U Test |
| 103 | +```r |
| 104 | +# Example: Comparing treatment vs control groups |
| 105 | +treatment <- c(78, 85, 92, 73, 88, 91, 76, 83) |
| 106 | +control <- c(65, 58, 71, 62, 69, 54, 67, 60) |
| 107 | + |
| 108 | +# Perform the test |
| 109 | +result <- mann_whitney_u_test(treatment, control, alternative = "greater") |
| 110 | +print(result) |
| 111 | +``` |
| 112 | + |
| 113 | +## 🔬 Detailed Examples with Biomedical Data |
| 114 | + |
| 115 | +### Running the Examples |
| 116 | +Each R file contains comprehensive examples with dummy biomedical data: |
| 117 | + |
| 118 | +```r |
| 119 | +# Run Wilcoxon examples |
| 120 | +source("wilcoxon_signed_rank_test.r") |
| 121 | +run_biomedical_examples() |
| 122 | + |
| 123 | +# Run Mann-Whitney examples |
| 124 | +source("mann_whitney_u_test.r") |
| 125 | +run_biomedical_examples() |
| 126 | +``` |
| 127 | + |
| 128 | +## 📈 Understanding the Results |
| 129 | + |
| 130 | +### Key Output Elements |
| 131 | + |
| 132 | +#### For Wilcoxon Signed-Rank Test: |
| 133 | +- **W+**: Sum of ranks for positive differences |
| 134 | +- **W-**: Sum of ranks for negative differences |
| 135 | +- **Test statistic W**: Usually min(W+, W-) |
| 136 | +- **P-value**: Probability of observing the result by chance |
| 137 | +- **Effect size**: Magnitude of the difference |
| 138 | + |
| 139 | +#### For Mann-Whitney U Test: |
| 140 | +- **U1, U2**: U statistics for each group |
| 141 | +- **W1, W2**: Sum of ranks for each group |
| 142 | +- **Test statistic U**: Depends on alternative hypothesis |
| 143 | +- **P-value**: Probability of observing the result by chance |
| 144 | +- **Effect size estimate**: Magnitude of group difference |
| 145 | + |
| 146 | +### Interpreting P-values in Medical Context |
| 147 | +- **p < 0.001**: Highly significant - very strong evidence |
| 148 | +- **p < 0.01**: Very significant - strong evidence |
| 149 | +- **p < 0.05**: Significant - moderate evidence |
| 150 | +- **p ≥ 0.05**: Not significant - insufficient evidence |
| 151 | + |
| 152 | +**Important**: Always consider clinical significance alongside statistical significance! |
| 153 | + |
| 154 | +## 🎯 Choosing the Right Test |
| 155 | + |
| 156 | +| Scenario | Test | Example | |
| 157 | +|----------|------|---------| |
| 158 | +| Same subjects, before/after | Wilcoxon Signed-Rank | Pre/post treatment blood pressure | |
| 159 | +| Paired subjects | Wilcoxon Signed-Rank | Twins, matched controls | |
| 160 | +| Single sample vs reference | Wilcoxon Signed-Rank | Patient cholesterol vs normal (200) | |
| 161 | +| Two independent groups | Mann-Whitney U | Treatment vs control groups | |
| 162 | +| Gender/age comparisons | Mann-Whitney U | Male vs female biomarker levels | |
| 163 | +| Disease stage comparison | Mann-Whitney U | Stage I vs Stage II severity | |
| 164 | + |
| 165 | +## 🧮 Statistical Theory (Simplified) |
| 166 | + |
| 167 | +### Wilcoxon Signed-Rank Test |
| 168 | +1. **Calculate differences** between paired observations |
| 169 | +2. **Rank the absolute differences** (ignore zeros) |
| 170 | +3. **Sum ranks** for positive and negative differences separately |
| 171 | +4. **Test statistic** is typically the smaller sum |
| 172 | +5. **Compare** to expected distribution under null hypothesis |
| 173 | + |
| 174 | +### Mann-Whitney U Test |
| 175 | +1. **Combine all observations** from both groups |
| 176 | +2. **Rank all values** from smallest to largest |
| 177 | +3. **Sum ranks** for each group separately |
| 178 | +4. **Calculate U statistics** using rank sums |
| 179 | +5. **Compare** to expected distribution under null hypothesis |
| 180 | + |
| 181 | +## 🔧 Advanced Features |
| 182 | + |
| 183 | +### Alternative Hypotheses |
| 184 | +- **"two.sided"**: Groups/conditions are different (default) |
| 185 | +- **"greater"**: First group/condition is greater than second |
| 186 | +- **"less"**: First group/condition is less than second |
| 187 | + |
| 188 | +### Handling Ties |
| 189 | +Both implementations use average ranks for tied values, which is the standard approach. |
| 190 | + |
| 191 | +### Effect Size |
| 192 | +- **Wilcoxon**: Based on Z-score and sample size |
| 193 | +- **Mann-Whitney**: Based on U statistic relative to maximum possible |
| 194 | + |
| 195 | +## 📚 Further Reading |
| 196 | + |
| 197 | +### Recommended Resources for Biomedical Students |
| 198 | +1. **"Biostatistics: A Foundation for Analysis in the Health Sciences"** - Wayne Daniel |
| 199 | +2. **"Statistical Methods in Medical Research"** - Armitage, Berry & Matthews |
| 200 | +3. **"Nonparametric Statistical Methods"** - Hollander, Wolfe & Chicken |
| 201 | +4. **"Medical Statistics from Scratch"** - David Bowers |
| 202 | + |
| 203 | +### Online Resources |
| 204 | +- [Laerd Statistics](https://statistics.laerd.com/) - Excellent step-by-step guides |
| 205 | +- [StatsDirect](https://www.statsdirect.com/) - Comprehensive statistical reference |
| 206 | +- [BMJ Statistics Notes](https://www.bmj.com/about-bmj/resources-readers/publications/statistics-square-one) - Medical statistics primer |
| 207 | + |
| 208 | +## ⚠️ Important Considerations |
| 209 | + |
| 210 | +### When NOT to Use These Tests |
| 211 | +- **Large samples with normal data**: t-tests might be more powerful |
| 212 | +- **Survival data**: Use specialized survival analysis methods |
| 213 | +- **Repeated measures**: Consider mixed-effects models |
| 214 | +- **Multiple comparisons**: Adjust p-values appropriately |
| 215 | + |
| 216 | +### Common Pitfalls |
| 217 | +1. **Multiple testing**: Correct for multiple comparisons |
| 218 | +2. **Effect size**: Don't ignore practical significance |
| 219 | +3. **Sample size**: Very small samples need exact methods |
| 220 | +4. **Assumptions**: Ensure data can be meaningfully ranked |
| 221 | + |
| 222 | +### Data Quality Checks |
| 223 | +- Check for outliers and data entry errors |
| 224 | +- Verify assumptions are met |
| 225 | +- Consider the clinical context |
| 226 | +- Validate results with domain experts |
0 commit comments