Laboratory 01

When Can Confounding Reverse an Association?

Can an association between two variables reverse direction when we account for a third variable? How strong must the relationships be for this to happen? And can we predict the reversal before observing the data?

Date & time Friday, November 6, 2026
10:00–12:00
Location GPH, Future Room
Host Nicola Orsini
Biostatistics Team, GPH, KI

Prerequisites: Basic familiarity with statistical concepts is helpful, but no advanced statistical knowledge or programming experience is required.

Intended audience: Doctoral students and researchers.

The statistical challenge

How strong is strong enough?

In this laboratory, we will investigate how a third variable can change the magnitude or direction of an observed association.

We will make predictions, propose explanations, explore numerical examples, and investigate simple data-generating mechanisms.

The aim is to understand when and why associations change, and what we can learn by moving between statistical reasoning, observed data, and simulation.

Resources

Laboratory materials

Explore the data-generating mechanism, generate new samples, or download the datasets used during the first laboratory.

Individual-level datasets

These simulated datasets were generated from the common mechanism used in the laboratory. Each group first received a pilot study of 200 participants and later requested a larger study.

Group Study Sample size Text Stata
1 Pilot 200 TXT DTA
1 Larger 10,000 TXT DTA
2 Pilot 200 TXT DTA
2 Larger 10,000 TXT DTA
3 Pilot 200 TXT DTA
3 Larger 10,000 TXT DTA
4 Pilot 200 TXT DTA
4 Larger 1,000 TXT DTA

TXT files are tab-delimited and can be imported into most statistical software. DTA files are provided for Stata users.

Reveal and discussion

Contains spoilers: these slides reveal the data-generating mechanism and explain the reversal. Open them after completing the investigation.

Generate your own sample

The following programs generate one sample of 1,000 observations from the mechanism used in the laboratory. Change n to explore how sampling variability changes with sample size.

\[ Z \sim \operatorname{Bernoulli}(0.40) \] \[ X \mid Z=z \sim \operatorname{Bernoulli}(0.30+0.40z) \] \[ Y \mid X=x,Z=z \sim \operatorname{Bernoulli}(0.15-0.10x+0.40z) \]

Stata

clear
set seed 123
set obs 1000

g z = rbinomial(1, .40)
g x = rbinomial(1, .30 + .40*z)
g y = rbinomial(1, .15 - .10*x + .40*z)

R

set.seed(123)
n <- 1000

z <- rbinom(n, 1, .40)
x <- rbinom(n, 1, .30 + .40*z)
y <- rbinom(n, 1, .15 - .10*x + .40*z)

dat <- data.frame(z, x, y)

Python

import numpy as np
import pandas as pd

rng = np.random.default_rng(123)
n = 1000

z = rng.binomial(1, .40, n)
x = rng.binomial(1, .30 + .40*z)
y = rng.binomial(1, .15 - .10*x + .40*z)

dat = pd.DataFrame({"z": z, "x": x, "y": y})

The seed makes each program reproducible within that software. Random-number generators differ, so the same seed will not usually produce identical samples in Stata, R, and Python.