Total core time: ~145 minutes (about 2.5 hours). This session is structured as a reference guide as much as a tutorial - the decision tables in particular are designed for you to come back to whenever you face a new analysis. A natural break point is after Example 2.
Section
Time
Type
The Key Idea: Match Method to Question
~10 min
Concept
The Decision Framework
~5 min
Concept
Decision Tables by Question Type
~15 min
Reference
Example 1: Navigating to the Right Test
~15 min
Walkthrough
Example 2: Binary Outcome
~15 min
Walkthrough
Assumption Checking & Method Summary
~10 min
Reference
What Can Go Wrong
~10 min
Reading
Exercise 1 (Guided)
~15 min
Practice
Exercise 2 (Semi-guided)
~20 min
Practice
Exercise 3 (Open-ended)
15–30 min
Practice
Comprehension Check
~10 min
Self-test
If you are short on time, work through Example 2 (binary-example): it shows a chi-square test returning p = 0.18 even though the simulation builds in a real 15-percentage-point difference (35% vs 20%) between groups. This is the session’s central warning in miniature - a non-significant result does not mean “no test was needed” or “pick a different test until something is significant”; it may simply mean the sample size was too small to detect a real effect.
TipHow to Use the Code in This Session
Every code chunk below is written so you can copy it into your R console (or an R script) and run it yourself, in the order shown. Where you see a “Run It Yourself” box, stop and run the code above it before reading the explanation that follows.
20.1 When Do You Use This?
Tip
You have data from a clinical study and need to choose the right test. Should you use a t-test or Mann-Whitney? Logistic regression or a chi-square test? This guide helps you navigate from your research question and data type to the appropriate method, and links you to the session where you can learn it properly.
NoteTeacher Note
Before starting, ask learners to write down one analysis they are currently planning or have recently completed (their own research question, outcome type, and comparison). Keep this written down - at the end of the session, in Exercise 3, they will use the decision tables to check whether the test they chose (or are planning to choose) is the appropriate one.
20.2 Learning Objectives
After completing this session you will be able to:
Map any standard research question to an appropriate statistical method
Identify the scale and distribution assumptions that determine test choice
Recognise when assumptions are violated and choose a non-parametric alternative
Find the relevant course session for every method covered
20.3 The Key Idea: Match Method to Question
Estimated time: ~10 minutes (Concept)
Every statistical method in this course was designed to answer a specific type of question. Use the wrong method and your answer is biased or meaningless, even if R runs without errors.
The two most common mistakes are: picking a test based on which gives the smallest p-value (outcome-dependent test selection, which inflates false positives), and defaulting to a t-test when the design calls for something else (paired measurements, a binary outcome, or clustered observations). Before you open R, answer three questions: What type is your outcome? What are you comparing or estimating? Are the assumptions for your first-choice method met? The tables below take you from those three answers to the right method, every time.
20.4 The Decision Framework
Estimated time: ~5 minutes (Concept)
Every statistical test choice starts with three questions:
What is your outcome variable? (continuous, binary, ordered, count, time-to-event)
What are you comparing? (one group vs reference, two groups, =3 groups, association/correlation, prediction)
Are assumptions met? (normality, equal variance, independence, sample size)
Before running decision-example, ask learners to walk through the three decision-framework questions out loud for this scenario: What is the outcome (continuous, binary, etc.)? What is being compared (paired, unpaired, three groups, etc.)? What assumption needs checking before picking the final test? Then run the code and confirm the answer matches.
Scenario: A clinical trial randomises 40 patients to drug vs placebo. You measure systolic blood pressure (continuous) before and after treatment. Which test?
# Step 1: Outcome = continuous (systolic BP)# Step 2: Two paired groups (same patients before/after)# Step 3: Check normality of differencesset.seed(42)before <-rnorm(20, 140, 15)after <- before +rnorm(20, -8, 10) # drug lowers BP ~8 mmHgdiff <- after - before# Shapiro-Wilk test of normalityshapiro.test(diff)
Shapiro-Wilk normality test
data: diff
W = 0.97966, p-value = 0.9297
TipRun It Yourself
You should see:
W = 0.97966, p-value = 0.9297
p = 0.93 is nowhere near significant, so there is no evidence that the before/after differences depart from normality. Step 3 of the framework is satisfied: with a continuous outcome, paired groups, and normal differences, the decision tables (Question 1) point to the paired t-test.
Paired t-test
data: after and before
t = -4.3163, df = 19, p-value = 0.0003723
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
-15.903258 -5.516578
sample estimates:
mean difference
-10.70992
TipRun It Yourself
You should see:
t = -4.3163, df = 19, p-value = 0.0003723
95 percent confidence interval:
-15.903258 -5.516578
mean difference
-10.70992
The mean within-patient drop in systolic BP is about 10.7 mmHg (95% CI 5.5 to 15.9), and this is highly significant (p < 0.001) - consistent with the -8 mmHg effect built into the simulation. Notice that the test statistic (t = -4.3163) is large relative to its standard error precisely because the paired design removes the large between-patient variability in baseline BP; an unpaired t-test on the same data would have a much wider confidence interval.
Decision path: Continuous outcome → Paired groups → Normal differences → Paired t-test → Section 5.1
20.7 Example 2: Binary Outcome - Choosing Between Chi-Square and Logistic Regression
Estimated time: ~15 minutes (Walkthrough)
NoteTeacher Note
The simulation below builds in a real difference: males have a 35% chance of NAFLD vs 20% for females, a 15-percentage-point gap. Before running binary-example, ask learners to predict whether chisq.test() will find this difference statistically significant with n = 200 split roughly evenly between sexes. Most learners expect “yes, obviously” because the built-in difference is large. The actual result is a useful surprise.
Scenario: You want to know if NAFLD diagnosis (yes/no) differs between males and females in a cohort of 200 patients.
set.seed(2024)n <-200sex <-sample(c("Male", "Female"), n, replace =TRUE)# Males have ~35% NAFLD, females ~20%nafld <-rbinom(n, 1, ifelse(sex =="Male", 0.35, 0.20))dat <-tibble(sex, nafld =factor(nafld, labels =c("No", "Yes")))# Chi-square for unadjusted associationchisq.test(table(dat$sex, dat$nafld))
Pearson's Chi-squared test with Yates' continuity correction
data: table(dat$sex, dat$nafld)
X-squared = 1.7904, df = 1, p-value = 0.1809
TipRun It Yourself
You should see:
X-squared = 1.7904, df = 1, p-value = 0.1809
Despite the 15-percentage-point true difference built into the simulation (35% vs 20%), the chi-square test gives p = 0.18 - not significant at the conventional 0.05 threshold. With only 200 patients split between two sexes, the observed counts in this particular random sample are not extreme enough to rule out chance. This is not a reason to try a different test until you find a significant one (see “What Can Go Wrong” below); it may simply reflect limited power. The Power and Sample Size session covers how to plan a study large enough to detect an effect of this size.
If you need to adjust for age and BMI, switch to logistic regression:
age <-rnorm(n, 50, 10)bmi <-rnorm(n, 27, 4)dat2 <- dat %>%mutate(nafld_num =as.integer(nafld) -1, age, bmi, sex =factor(sex))glm(nafld_num ~ sex + age + bmi, data = dat2, family = binomial) %>% broom::tidy(exponentiate =TRUE, conf.int =TRUE) %>%select(term, estimate, conf.low, conf.high, p.value)
The adjusted odds ratio for sexMale (1.60, 95% CI 0.885 to 2.92, p = 0.122) tells the same story as the chi-square test, just on a different scale: males appear to have higher odds of NAFLD, but the confidence interval is wide and includes 1, and the p-value does not reach significance. Logistic regression adds the ability to adjust for age and bmi (neither of which shows an association here) - this is the key reason to move beyond chi-square, not because chi-square “failed” to find significance.
Decision path: Binary outcome → Association with one predictor → Chi-square (unadjusted). Add covariates → Logistic regression → Section 13.1
Choosing based on p-value alone Never choose the test that gives the most significant result. Test choice must be determined by the research question and data type, not by the outcome.
Ignoring paired structure Using a two-sample t-test when data are paired wastes statistical power (you ignore the within-subject correlation). Always account for the study design.
Multiple testing without correction Running 20 tests and reporting the significant ones inflates the type I error. Apply FDR or Bonferroni correction when testing multiple outcomes. See resources on multiple-testing corrections for high-dimensional data.
Treating ordinal as continuous Likert scales (1–5) are ordinal. Means are interpretable but formal inference should use ordinal tests or models unless scales are well-validated and items are summed.
20.11 Exercises
NoteTeacher Note
For each scenario in Exercise 1, ask learners to work through the three decision-framework questions explicitly (outcome type, comparison, assumptions) before consulting the answer - this is the habit the whole session is trying to build, and it transfers directly to Exercise 3, where they apply it to their own research.
20.11.1 Exercise 1 (Guided): Test Selection
Estimated time: ~15 minutes (Practice)
For each scenario below, name the appropriate statistical test and state why:
Comparing mean HbA1c in three treatment groups (normal distribution, equal variance, n = 30 per group).
Comparing median C-reactive protein between responders and non-responders (right-skewed, small n = 12 per group).
Assessing whether smoking status (yes/no) is associated with COPD diagnosis (yes/no) in 500 patients.
Predicting 5-year cardiovascular event (yes/no) from age, sex, and LDL in 1,000 patients.
Estimating time to relapse from surgery, comparing two chemotherapy regimens.
Log-rank test + Cox proportional hazards (time-to-event outcome, two groups; Cox for adjusted estimate).
20.11.2 Exercise 2 (Semi-guided): Assumption Check and Decision
Estimated time: ~20 minutes (Practice)
You have expression levels of a biomarker in healthy (n = 15) vs disease (n = 18) patients. The data look right-skewed with a few large values.
Plot the distribution (histogram and QQ plot).
Run a Shapiro-Wilk test.
Based on your test, choose between Welch t-test and Mann-Whitney U.
Report the result including the effect size (Cohen’s d or rank-biserial r).
20.11.3 Exercise 3 (Open-ended)
Estimated time: 15–30 minutes (Practice)
Take a published paper from your field. For each analysis reported: 1. Identify the outcome type and comparison being made. 2. Assess whether the correct test was used based on the decision tables above. 3. Note one assumption that the authors did or did not check, and state how it could affect the conclusions.
20.12 Comprehension Check
Estimated time: ~10 minutes (Self-test)
A researcher measures body weight (continuous) in mice at three time points (baseline, week 4, week 8). They analyse each time point separately with t-tests. What is wrong with this approach?
You want to test whether a new drug reduces blood pressure. Patients are measured before and after treatment. Should you use a paired or unpaired test? Why?
A chi-square test returns a p-value of 0.03 comparing smoking rates by sex. A colleague says “logistic regression would be better.” When would they be correct?
You run Shapiro-Wilk and get p = 0.04, but your sample size is n = 200. Should you automatically switch to a non-parametric test?
What is the difference between a test of association (chi-square, correlation) and a test of prediction (regression)?
NoteAnswers
Running separate t-tests at each time point ignores the repeated-measures structure (the same mice are measured three times). This inflates type I error (multiple comparisons) and ignores within-mouse correlation. The correct approach is repeated-measures ANOVA or a linear mixed model with time as a factor and mouse as a random effect.
Paired test. The same patient is measured twice; the within-patient change in blood pressure is the quantity of interest. Using a paired test accounts for between-patient variability and is more powerful than an unpaired t-test. Use paired t-test (if differences are normal) or Wilcoxon signed-rank (if not).
The colleague is correct when you need to adjust for confounders (e.g., age, deprivation, comorbidities). Chi-square tests a bivariate association; logistic regression allows you to estimate the sex–smoking association adjusted for other variables, gives odds ratios, and supports prediction. For a simple unadjusted two-by-two table, chi-square is perfectly valid.
Not necessarily. With n = 200, the Shapiro-Wilk test is highly sensitive; it will detect trivial, practically irrelevant deviations from normality. With large samples, the t-test is robust to non-normality (central limit theorem). Check the QQ plot visually: if the tails are only mildly heavy, the t-test is fine. Only switch to a non-parametric test if the distribution is highly skewed or has extreme outliers.
A test of association asks: are two variables related? (Chi-square, Pearson r). A test of prediction models the outcome as a function of predictors, estimates the size of effects, and can be used to predict new observations. Regression also allows adjustment for confounders, which association tests cannot. Use association tests for simple screening; use regression for explanatory modelling or confounding adjustment.
20.13 How to Report
20.13.1 Justifying Your Choice of Statistical Test
Note
In a methods section: “The choice of statistical test was based on the outcome data type (continuous / binary / time-to-event), the number of groups compared, and whether the data met parametric assumptions (normality assessed by Shapiro-Wilk test and QQ-plot; homogeneity of variance assessed by Levene’s test).”
What to always state:
The name of the test used and the R function / package
The outcome variable type that motivated the choice
Whether parametric assumptions were checked and what was found
Any deviation from the pre-specified analysis plan (and the reason)
Template sentences:
“A two-sample t-test was used because [outcome] was approximately normally distributed in both groups (Shapiro-Wilk p > 0.05) and sample sizes exceeded 30.”
“The Wilcoxon rank-sum test was used because [outcome] showed marked right skew (Shapiro-Wilk p = 0.003; QQ-plot confirmed departure from normality).”
“Logistic regression was used because the outcome was binary (event vs. no event). Continuous predictors were not categorised.”
Avoid: Choosing a test after seeing the results (outcome-dependent test selection inflates the false-positive rate). Pre-specify the primary test and document any post-hoc changes.
20.14 Further Reading
Field, Miles, and Field (2013): Discovering Statistics Using R - practical test selection guidance
Glantz (2002): Primer of Biostatistics - clear, clinically-oriented decision guide