Adjusting for Confounding: A Hands-on Course

What to adjust for, reading odds ratios, continuous variables, interaction, overfitting, and propensity scores — with Framingham and simulations

Published

October 18, 2026

Introduction

English version — machine-assisted translation (by generative AI, Anthropic Claude) of the original Japanese hands-on. 日本語版はこちら.

Many observational studies report “odds ratios (hazard ratios) adjusted for age, sex, …”. Putting variables into a regression model does let you “adjust”, but if you cannot answer questions such as

  • Which variables should be included (and are there variables that must not be included)?
  • What multiple of the risk does the adjusted odds ratio mean?
  • Does entering age as “under 50 / 50 and over” count as adjusting for it?
  • Does “significant in men, not significant in women” mean the effect differs?
  • Is it acceptable to select variables by p-values?
  • Do propensity scores make a study equivalent to a randomized controlled trial?

then you will reach wrong conclusions while believing you have adjusted.

In this hands-on, you will experience the ideas and pitfalls of adjusting for confounding using teaching data from the Framingham Heart Study and simulations where the answer is known, with R running in your browser.

Goals of this hands-on

  1. Explain confounding and check for yourself how adjustment changes an estimate
  2. Draw a DAG (a diagram of causal arrows) and distinguish variables you should adjust for from those you must not (intermediates, colliders)
  3. Understand the difference between odds ratios and risk ratios, and obtain risk differences and risk ratios by standardization
  4. Explain how to handle continuous variables (categories, straight lines, splines) and residual confounding
  5. Read interaction (effect modification) on both the multiplicative and additive scales
  6. Explain the problems of the number of variables and overfitting, and of variable selection by p-values
  7. Use propensity scores (weighting, matching) and explain their limits (unmeasured confounding)

Who this is for

  • People involved in analyzing epidemiological, clinical research, or healthcare data
  • People who have seen proportions, odds ratios, and logistic regression output
  • For R, being able to “read code, modify it a little, and run it” is enough

You can also read this as a continuation of the descriptive statistics hands-on (Chapter 7: Table 1 and confounding) and the chi-squared test hands-on (Chapter 7: Mantel-Haenszel).

R runs in your browser

The code cells in each chapter are executed by R inside your browser (a technology called webR). Edit the code and press “Run Code”, and the results change immediately. No installation is needed.

Note
  • Right after opening each chapter, loading R and the packages takes from a few tens of seconds to about a minute
  • Moving the tab to the background while loading slows it down, so please wait until it appears
  • Viewing on a PC is recommended

We use only functions built into R such as glm(), plus dplyr and ggplot2. Propensity scores are also computed by hand without dedicated packages.

Data used

The first examination (1950s, 4434 people) of the Framingham Heart Study (teaching data), and death and cardiovascular disease (CVD) over the following 24 years.

WarningNotes on the data
  • Part of the data has been altered for anonymization. It cannot be cited as research findings
  • This is data from people in the US in the 1950s, and antihypertensive drugs and treatment at that time were very different from today. The antihypertensive examples are for learning adjustment methods, not conclusions about drug effects

Chapters

Chapter Content
1 What is confounding? (smoking and death, antihypertensive medication and death)
2 What to adjust for: DAGs, intermediates, and colliders (simulations)
3 Reading odds ratios: difference from risk ratios, standardization, non-collapsibility
4 Handling continuous variables: categories, straight lines, splines, residual confounding
5 Interaction: effect modification, multiplicative and additive scales
6 Number of variables, overfitting, and variable selection by p-values
7 Propensity scores: weighting, matching, balance, overlap, limits
8 Exercises and a checklist for adjusting for confounding