Programming lesson
Mastering Nonparametric and Parametric Tests in R: A Guide for MAST20005/MAST90058
Learn how to perform sign, Wilcoxon, and t-tests in R with a step-by-step tutorial. Understand when to use each test, interpret p-values, and compare conclusions using real data examples.
Introduction to Hypothesis Testing in R
Hypothesis testing is a cornerstone of statistical inference. In assignments like MAST20005/MAST90058, you often need to compare two groups or test for differences. This tutorial will guide you through three common tests: the sign test, Wilcoxon signed-rank test, and paired t-test. We'll use R to perform these tests on paired data and discuss how to interpret results. Whether you're a biology student analyzing germination rates or a data scientist evaluating A/B test results, these skills are essential.
Understanding the Data: Paired Differences
Consider a random sample of paired observations (X, Y). We want to test if X and Y differ in location. For example, imagine you're testing a new study app: before (X) and after (Y) scores. The differences D = Y - X are our focus. The null hypothesis H0: median difference = 0 (or mean difference = 0 for t-test). The alternative H1: median difference ≠ 0 (two-sided). We'll use a significance level of 5%.
Test 1: Sign Test
The sign test is a nonparametric test that only considers the sign of differences, ignoring magnitude. It's useful when data are skewed or have outliers. In R, use binom.test on the number of positive differences. For our data, count positives and run: binom.test(pos, n, p=0.5). This test is robust and akin to a coin flip—perfect for when assumptions are violated.
Test 2: Wilcoxon Signed-Rank Test
The Wilcoxon test ranks the absolute differences and then uses the signs. It's more powerful than the sign test because it considers magnitude. In R: wilcox.test(y, x, paired=TRUE). This test assumes symmetry of differences. It's widely used in psychology and medicine. For instance, comparing reaction times before and after a gaming session.
Test 3: Paired t-Test
The t-test assumes differences are normally distributed. Use t.test(y, x, paired=TRUE). This test is the most powerful when assumptions hold. However, with small samples or outliers, it may be unreliable. Check normality with shapiro.test(diff) or a Q-Q plot.
Comparing Conclusions
Often, all three tests yield similar conclusions. If they disagree, investigate data anomalies. For example, if the t-test is significant but sign test is not, maybe a few large differences drive the result. In such cases, prefer nonparametric tests. Always state hypotheses clearly and report p-values. For the MAST20005 assignment, you might find that all tests reject H0, indicating a location shift.
Simulation for Power Analysis
To compare test power, simulate data from known distributions. For instance, X ~ N(30, 32) and Y - X ~ N(3, 52). Generate many samples, run each test, and compute rejection rates. This reveals which test has higher power under specific conditions. In R, use replicate and apply functions. Typically, the t-test has highest power if normality holds, but Wilcoxon is close.
Goodness-of-Fit for Binomial Data
Another common task: test if data follow a binomial distribution. For example, 80 students each run 30 germination experiments. Estimate p as mean/30. Then create bins with expected counts ≥5. Use chisq.test to compare observed vs expected. This is crucial for biology and quality control.
Two-Way ANOVA with Interaction
In experiments with two factors (e.g., panel angle and panel type), use aov in R. Check for interaction with an interaction plot. If interaction is present, interpret main effects cautiously. For the solar panel data, you might find that angle affects output, but the effect depends on panel type.
Conclusion
Mastering these tests in R will serve you well in statistics courses and real-world data analysis. Always verify assumptions, and don't rely solely on p-values—consider effect sizes and practical significance. Happy coding!