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)
Laboratory 01
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?
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
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
Explore the data-generating mechanism, generate new samples, or download the datasets used during the first laboratory.
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.
Contains spoilers: these slides reveal the data-generating mechanism and explain the reversal. Open them after completing the investigation.
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.
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)
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)
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.