HSCI 410 · Lesson 1

A Structured Approach to Data Analysis

Exploratory Data Analysis For Epidemiology

Learning objectives for this lesson:

  1. Explain why a structured, iterative and reproducible workflow, which starts with a causal diagram and runs every step from a script (program mode), gives more trustworthy results than going straight to a multivariable model.
  2. Identify the outcome, the exposure, and any confounders, mediators (intervening variables) and colliders in a causal diagram.
  3. Choose the adjustment set that follows from a causal diagram, and explain the consequence of omitting a confounder, adjusting for a mediator when the total effect is wanted, or adjusting for a collider.
  4. Identify coding faults in a raw data file (missing-value codes that could be real values, text in numeric fields, compound codes, and inconsistent formats or missing-value conventions) and state how each should be corrected according to best practice.
  5. Describe a system for managing data-collection sheets, data entry, files, variables and analyses that protects the original data and leaves an audit trail.
  6. Describe the scripted editing and verification checks made on each variable before analysis, and use their output to identify implausible values and unexpected categories.
  7. Choose how to process the outcome and predictor variables for the planned analysis, choose the unconditional analysis for each pair of variable types, and explain what these first analyses reveal before multivariable modelling.

This course was developed by Dr. Kiffer G. Card based on Dohoo, I. R., Martin, S. W., & Stryhn, H. (2012). Methods in Epidemiologic Research. VER Inc.

Reference

Glossary: Key Terms, People & Concepts

📚 Reference page, available throughout the lesson

This glossary collects the key concepts, people, and ideas you will meet in this lesson. Use it as a reference while you work through the material, or as a review before assessments. Type in the search box to filter entries.

Key Concepts & Ideas
Research Question A focused, answerable statement that frames an analysis. Usually structured around four elements: the population studied, the exposure (or determinant, or intervention), the comparator group, and the outcome. The initials of these elements give the acronym PECO (population, exposure, comparator, outcome), or PICO when the exposure is an intervention. A clear research question drives the entire analytic plan.
Analytic Plan A pre-specified roadmap that links the research question to data collection, variables, statistical models, and decision rules. Reduces ad hoc decisions and protects against fishing expeditions.
Hypothesis A testable prediction about the relationship between variables. The null hypothesis (H₀) typically states no association; the alternative (H₁) states an association exists.
Exposure The factor whose effect on the outcome is of primary interest (e.g., a treatment, behaviour, or environmental agent). Sometimes called the predictor or independent variable.
Outcome The health state or event being predicted or explained (e.g., disease, recovery, death). Also called the dependent variable or response.
Covariate Any variable other than the primary exposure that may influence the outcome. May be a confounder, mediator, effect modifier, or simply a precision variable.
Confounder A variable that affects both the exposure and the outcome and is not on the causal pathway between them. Failing to adjust for confounders biases effect estimates.
DAG (Directed Acyclic Graph) A diagram of variables (nodes) and directed causal arrows (edges) with no cycles. Used to encode assumed causal structure and identify which variables to adjust for.
Data Dictionary / Codebook A document listing every variable in a dataset with its name, definition, type, allowable values, units, and coding rules. Essential for reproducibility and collaboration.
Reproducibility The ability of others (or future-you) to re-run an analysis on the same data and obtain the same results, given the code and documentation.
Data Verification Checking entered data against source records to confirm accuracy. Includes double-entry, range checks, consistency checks, and cross-tabulation against expected patterns.
Multilevel Data Observations nested within higher-level units (e.g., patients within clinics, students within schools). Requires methods that account for clustering and within-unit correlation.
Methods & Statistical Concepts
MCAR (Missing Completely at Random) Missingness is unrelated to any observed or unobserved variable. Complete-case analysis is unbiased but loses efficiency.
MAR (Missing at Random) Missingness depends only on observed variables. Multiple imputation and likelihood-based methods can give unbiased estimates.
MNAR (Missing Not at Random) Missingness depends on the unobserved value itself. Requires sensitivity analyses or explicit modeling assumptions; cannot be fixed by standard imputation.
Unconditional Association The crude (unadjusted) relationship between two variables, ignoring other covariates. Useful as a first look at the data before multivariable modeling; the choice of confounders to adjust for comes from the causal diagram.
Program File / Script A saved file containing analysis code (e.g., a .R script) that can be re-executed to reproduce results. Preferred over interactive (point-and-click) processing.
Data Coding Translating raw responses (e.g., “Yes”, “No”, “Don't know”) into numeric or categorical values suitable for analysis, following pre-specified rules.
Mediator (Intervening Variable) A variable on the causal pathway from the exposure to the outcome, so that part of the exposure’s effect passes through it. Adjusting for a mediator removes that part of the effect from the estimate.
Adjusting For (Conditioning On) Comparing people who share the same value of a third variable, so that the variable cannot explain the difference being studied. It is done by adding the variable to a regression model, by stratifying on it, or by restricting the sample to one of its values.
Replication Repeating a study with new participants and new data to see whether the original finding holds. Reproducibility, by comparison, means obtaining the same results from the same data and code.
Regression Coefficient A number estimated by a regression model that gives the average change in the outcome for a one-unit increase in a predictor, with the other predictors in the model held fixed.
Residual The difference between a person’s observed outcome and the value the model predicts for that person (observed minus predicted).
Variance A measure of spread equal to the average squared distance of the values from their mean (R divides by n − 1). Its square root is the standard deviation.
Standard Error The estimated amount by which a statistic, such as a mean or a regression coefficient, would vary from one sample to the next. A smaller standard error indicates a more precise estimate.
Confidence Interval (CI) A range of values, calculated from the data, that shows how precisely a quantity has been estimated. A 95% confidence interval comes from a method that captures the true value in 95% of repeated samples.
p-Value The probability of a result at least as far from “no effect” as the one observed, if there were truly no effect. A small p-value indicates that the data are hard to reconcile with no effect; the size and importance of the effect are judged from the estimate and its confidence interval.
Correlation (r) A number between −1 and +1 that describes how closely two continuous variables follow a straight-line pattern. Values near 0 indicate no linear relationship, and values near −1 or +1 indicate a tight one.
Collinearity A situation in which two or more predictors are so highly correlated that a model cannot separate their individual effects.
Complete-Case Analysis An analysis that uses only the records with no missing values on the variables involved. It is simple, and it can bias results when the people with missing values differ from the rest.
NA (R’s Missing-Value Code) The code R uses for a missing value (“not available”). Most R functions return NA when asked to summarise data that contain missing values unless told to remove them, for example with na.rm = TRUE.
Factor (in R) R’s variable type for categorical data. A factor stores each value as one of a fixed set of levels, each with a text label, and the first level serves as the reference category in regression models.
Pipe Operator The R symbol |> (or %>% in the tidyverse), which passes the result of one step to the next, so that a sequence of data operations reads from top to bottom.
No matching entries. Try a different search term.
Section 1

Introduction & Data Collection

⏱ Estimated time: 15 minutes
Lesson 1 · HSCI 410

A Structured Approach to Data Analysis

From reading evidence and designing studies to analysing the data those studies produce.

Why it matters now

Reproducibility as a professional expectation

An analysis is only as trustworthy as the steps that came before it.Lesson 1 throughline

Concerns about replication, raised by Ioannidis (2005) and confirmed by large replication projects in the 2010s, led to a manifesto for reproducible science (Munafò et al., 2017) and raised the bar. Structured, scripted, documented workflows are now the expected standard in published public-health research.

Section 1 of 4

Introduction & Data Collection

A structured, iterative workflow and the causal diagram as your first analytic move.

The core principle

Analysis is iterative, not linear

Jumping straight to the sophisticated model almost always produces wrong results.

Tukey distinguished exploratory work (getting to know the data) from confirmatory work (formally testing a stated claim). A structured template makes every iteration cheaper and more defensible.

Causal diagram Clean & verify Unconditional checks Multivariable model iterate
The essential first step

Three DAG structures

Fork

C X Y

C is a confounder. Adjust for it.

Chain

X M Y

M is a mediator. Leave it out of the model when estimating the total effect.

Collider

X Y Z

Do not adjust for Z. Adjusting for a collider creates a false association between X and Y.

DAG to regression

Mediation: education → income → health

Baron & Kenny decomposition

indirect effect (a × b) + direct effect (c′) = total effect (c)

True values chosen for the simulation: 0.30 + 0.30 = 0.60. Estimates from the simulated data: 0.2969 + 0.3506 = 0.6475.

a effect of exposure on mediator b effect of mediator on outcome c' direct effect (ADE, average direct effect) c total effect

About half of the total effect of education on health flows through income (proportion mediated: 0.50 by design of the simulation, 0.46 estimated). The indirect effect a × b is reported as the ACME (average causal mediation effect).

Key caution: the indirect estimate is only as credible as the DAG. An unmeasured confounder on the income→health path biases the result even when the regressions look clean.

Project setup

A reproducible project skeleton

.
+-- R/                  numbered scripts: 01_load.R, 02_clean.R
+-- data/raw/          read-only; never overwritten
+-- data/processed/    regenerated entirely from R/
+-- output/figures/     numbered figures for the paper
+-- project.Rproj       one click reopens the environment

Pipelines read top to bottom with the pipe operator; here() keeps file paths portable across machines.

Data collection

Managing data-collection sheets

Protect originals

Never remove originals from the file. Ship photocopies only. The original is the irreplaceable record.

Track progress

Record insertion of each form so you know how many remain to be collected before further work begins.

Scan for completeness

Once all forms are in, check every sheet for omissions before any other step. Act on gaps promptly.

Carry forward

What to take into the next section

  • Draw the DAG first. It fixes what you are estimating and what goes in the model.
  • Expect iteration. Backing up several steps is normal and necessary.
  • Protect originals and scan for completeness before any analysis begins.

Overview

This course addresses how to analyse the data that epidemiological studies produce. It uses R to apply the main statistical methods of modern public-health analysis, including linear, logistic, multinomial, ordinal, and count regression; mixed models for clustered and longitudinal data; and causal-inference tools for observational data. None of these models is needed to follow this lesson. This first lesson sets the foundation: a disciplined workflow for taking raw data from collection through to analysis-ready files. This section explains why that workflow matters, shows how a causal diagram decides which variables belong in a model before any data are touched, and sets up the storage of the original data, on paper and in files.

Learning Objectives

  • 1.1 Explain why a structured, iterative analytic workflow outperforms diving straight into modelling, and name its steps from the causal diagram to the multivariable model.
  • 2.1 Sketch a causal diagram and identify the outcome, the exposure, and any confounders (forks), mediators or intervening variables (chains) and colliders in it.
  • 3.1 Read the adjustment set from a causal diagram, and explain what goes wrong when a confounder is omitted, a mediator is adjusted for when the total effect is wanted, or a collider is adjusted for or selected on.
  • 5.1 Set up a storage and tracking system for original data-collection sheets.

1.1 Why a Structured, Iterative Workflow

When starting the analysis of a complex dataset, it is very helpful to have a structured approach in mind. For most people, there is a strong tendency to jump straight into the sophisticated analysis that will provide the ultimate answer. This rarely works out, because the results will be wrong when important preliminary steps were skipped. A model fitted before the data are cleaned inherits every coding error in the file, and a model built without a causal diagram has no stated estimand (the effect being estimated) or adjustment set, so its results can look reasonable and still be wrong.

The structured approach has become especially important since the wider scientific community recognised a replication crisis in published research (Ioannidis, 2005; Open Science Collaboration, 2015) and called for a manifesto of reproducible practice (Munafò et al., 2017). The crisis concerns two ideas that are easily confused, and the distinction explains what the habits in this lesson are for.

Replication and Reproducibility

Replication means repeating a study with new participants and new data to see whether the original finding holds. Reproducibility means re-running the original analysis on the original data, with the original code, and obtaining the same numbers. The crisis was named for failed replications, which Ioannidis warned about in 2005 and which large projects documented in the 2010s. The habits taught in this lesson target reproducibility, because a result that cannot be reproduced from its own data and code cannot be checked, corrected, or built upon.

The workflow taught in this course follows a template whose steps are taken in order. The analyst first draws a causal diagram of the question, then cleans and verifies the data, then runs simple unconditional checks (one predictor at a time, with nothing else held constant), and only then fits the multivariable model. The order matters because each step depends on the one before it: the diagram decides which variables belong in the model, and the unconditional checks can be trusted only once the data have been verified. A plan that lets p-values choose the variables reverses this order, because the data, rather than the causal question, then decide what is adjusted for. The principle behind the template is stated below, together with three terms that recur throughout the course.

Key Principle: Analysis Is Iterative

Data analysis is an iterative process which often requires that you back up several steps as you gain more insight into your data, an idea Tukey developed when he distinguished exploratory from confirmatory work (Tukey, 1980). A structured template, while not the only approach, will be applicable in most situations and will serve to guide your initial efforts; tidy-data conventions (Wickham, 2014) make each iteration cheaper.

Three terms recur throughout the course. Exploratory analysis means getting to know the data by summarising and plotting it and looking for patterns, errors, and surprises; confirmatory analysis means formally testing a claim that was stated before the data were examined. Tidy data means a table in which each variable has its own column, each observation (for example, each participant) has its own row, and each cell holds a single value.

Iteration means that the template is a loop rather than a straight line. A problem found while verifying the data, or a surprising unconditional association, sends the analyst back to recode a variable or to reconsider the diagram, and backing up in this way is the normal pattern of an analysis rather than a sign of failure. The first step of the template, taken before any work with the data begins, is the causal diagram.

2.1 Sketching a Causal Diagram

Before you start any work with your data, it is essential to construct a plausible causal diagram of the problem you are about to investigate. The diagram identifies which variables are the important outcomes and predictors, which are potential confounders, and which might be intervening variables between the main predictors and the outcomes. A causal diagram is a drawing in which each variable is a box and each arrow states a belief that one variable causes another. By this we mean that the diagram is a written record of what the analyst assumes about how the variables are related, made before the data can influence those assumptions. Because every later decision about which variables to include in a model is read from these arrows, keep the diagram in mind throughout the entire data-analysis process. With large datasets it will not be possible to include every predictor as a separate box, and this can be handled by drawing blocks of variables in place of the individual variables. For example, age, sex and income might be drawn as a single box labelled “demographic characteristics”, with one arrow for the whole block, which keeps the diagram readable while still recording the role that the block plays.

The callout below recalls the kind of causal diagram used in this course and its three structures, each with the adjustment rule that goes with it; these rules are used in every later lesson, so commit them to memory.

Recall: DAGs and Their Three Structures

When we say “causal diagram” in 410, we mean a directed acyclic graph (DAG): nodes for variables, directed arrows for direct causal effects, no cycles. The three structural pieces you met in an earlier course still do all the work:

  • Fork (X ← C → Y): C is a confounder. Adjust for it.
  • Chain (X → M → Y): M is a mediator. Do not adjust if you want the total effect.
  • Collider (X → Z ← Y): Do not adjust, and watch for it in selection.

The DAG fixes your estimand (what causal quantity you are estimating) and your adjustment set (what goes on the right-hand side of the regression) before you fit anything. In 410 we use it as the bridge from a research question to a regression model.

Worked Example 1.1 applies the three structures to the smoking cohort used in Activity 1.1, building the diagram in six steps from the research question to the adjustment set. Table 1.1 lists the steps and Figure 1.1 shows the finished diagram; as you read, watch how each variable’s role is decided by the direction of its arrows.

Worked Example 1.1: From a research question to a DAG and an adjustment set

The smoking cohort used in Activity 1.1 (cohort.csv) records whether each participant smokes and whether an event occurred during follow-up. Suppose the event is a cardiovascular (CVD) event, such as a heart attack or stroke. The six steps in Table 1.1 turn that study into a causal diagram.

Table 1.1. Six steps from the research question to the adjustment set for smoking and cardiovascular (CVD) events in the smoking cohort.

StepWhat the analyst doesResult for this example
1. State the questionThe question names the population, exposure, comparator, and outcome (PECO).Among adults aged 20 to 79, does smoking, compared with not smoking, increase the risk of a CVD event during follow-up?
2. Add common causesThe analyst lists variables that plausibly cause both the exposure and the outcome, using subject-matter knowledge and earlier studies.Age and sex both influence whether a person smokes and their CVD risk, so each gets an arrow into smoking and an arrow into the CVD event.
3. Add pathway variablesThe analyst lists variables through which the exposure might act on the outcome.Smoking raises blood pressure, and high blood pressure raises CVD risk, so blood pressure sits on a chain from smoking to the event.
4. Add common effectsThe analyst lists variables that both the exposure and the outcome cause, including any variable that determined who entered the data.Smoking-related illness and CVD events both lead to hospital admission, so admission receives arrows from both and is a collider.
5. Classify each variableEach variable is labelled using the three structures: fork, chain, or collider.Age and sex are confounders, blood pressure is a mediator, and hospital admission is a collider.
6. Read off the adjustment setThe analyst chooses the estimand and lists the variables the model must include and exclude.For the total effect of smoking, the model adjusts for age and sex, leaves blood pressure out, and does not restrict the sample to hospital patients.
Causal diagram for the smoking and cardiovascular event example Age (confounder) Sex (confounder) Smoking (exposure) CVD event (outcome) Blood pressure (mediator) Hospital admission (collider)
Figure 1.1. Causal diagram for the worked example. Age and sex are confounders, blood pressure is a mediator, and hospital admission is a collider. For the total effect of smoking on cardiovascular (CVD) events, the adjustment set is age and sex.

Reading the roles off a diagram. Each role is identified from the direction of the arrows, never from the variable’s name. A confounder sends one arrow into the exposure and one into the outcome; a mediator receives an arrow from the exposure and sends one on towards the outcome; a collider receives arrows from both the exposure side and the outcome side. A diagram need not contain every role: if no variable lies on a path that runs from the exposure to the outcome, the diagram contains no mediator, and the correct answer for mediators is “none”. A variable with an arrow only into the outcome, such as a history of hypertension in a birth-weight study, is a cause of the outcome only and is not a confounder.

The example shows that a role belongs to the arrows rather than to the variable. By this we mean that no variable is a confounder or a mediator by nature; it takes on a role only in relation to a particular exposure and outcome. Blood pressure is a mediator here because smoking raises it and it in turn raises CVD risk, so it lies on the path between them. In a study of a different exposure, however, the same variable could play a different part. For example, in a study of whether a new medication prevents CVD events, blood pressure might influence whether a doctor prescribes the medication and also influence CVD risk, which would make it a confounder in that study. This is why each new research question needs its own diagram.

One further role appears in some diagrams and is not covered by the three structures. Some diagrams draw an arrow from a variable to the middle of another arrow, for example from diabetes status to the arrow from physical activity to blood pressure. That variable is a moderator, also called an effect modifier. By this we mean a variable that changes how strongly the exposure affects the outcome, so that the size of the effect differs from one group to another. In the example, physical activity might lower blood pressure more in people without diabetes than in people with diabetes; diabetes status would then modify the effect of physical activity. A moderator is not a confounder, a mediator or a collider, because it does not create a separate path between the exposure and the outcome; instead, it alters the strength of an existing arrow. Because the question it raises is whether the effect differs between groups, a moderator is examined with an interaction term or by reporting the effect separately in each group. An interaction term is the product of the exposure and the moderator added to a regression model, so that the effect of the exposure is allowed to differ across levels of the moderator.

Once every variable has a role, the diagram is ready to answer the practical question of which variables the model should include.

3.1 Reading the Adjustment Set from the Diagram

The diagram fixes two things before any model is fitted: the estimand, which is the causal quantity being estimated (for example, the total effect of smoking on CVD events), and the adjustment set, which is the list of variables that go on the right-hand side of the regression. The estimand comes first because the same data can answer more than one question. For example, the total effect of smoking includes every route by which smoking leads to CVD events, including the route through blood pressure, whereas the direct effect excludes that route. These are different quantities, and each needs its own adjustment set. Once the estimand is chosen, the rules for building the adjustment set are short: adjust for confounders, leave mediators out when the total effect is wanted, and never adjust for or select the sample on a collider. Following them requires a clear idea of what adjusting means.

3.1.1 What Adjusting for a Variable Means

To adjust for a variable (also called controlling for it or conditioning on it) means to compare exposed and unexposed people who have the same value of that variable, so that the variable cannot explain the difference between them. By this we mean that the comparison is made within groups that are alike on the adjusted variable. For example, if smokers in a cohort tend to be older than non-smokers, a crude comparison of CVD rates mixes the effect of smoking with the effect of age. Comparing smokers and non-smokers of the same age removes that mixing, because any remaining difference in CVD rates cannot be due to a difference in age. In practice an analyst adjusts in one of three ways. The most common is to add the variable to the right-hand side of a regression model, for example lm(outcome ~ exposure + age), which estimates the exposure effect among people of the same age. A second is to stratify, which means analysing each level of the variable separately (for example, women and men) and then combining the results. A third is to restrict the sample to one level of the variable, for example by studying only non-smokers or only hospital patients.

The adjustment set matters because each wrong decision changes the answer the model gives. For the smoking cohort of Worked Example 1.1, the three possible mistakes and their costs are these:

  • Omitting a confounder (leaving age out of the smoking model) leaves the path smoking ← age → CVD event open, so the smoking coefficient also absorbs part of the effect of age and is biased. This bias is called confounding.
  • Adjusting for a mediator when the total effect is wanted (adding blood pressure) blocks the part of the smoking effect that travels through blood pressure, so the model estimates a smaller, direct effect and answers a different question.
  • Adjusting for a collider, or selecting the sample on it (adding hospital admission, or studying only inpatients), opens a non-causal path between smoking and the outcome and creates an association that does not exist in the population, as Worked Example 1.3 shows. This is collider or selection bias. It arises because a path through a collider is closed until the collider is conditioned on. Before adjustment, the fact that smoking and CVD events both lead to admission creates no association between them; once only admitted people are compared, knowing one reason for admission tells the analyst something about the other reason, so the two causes become associated within the sample.

The third mistake is the easiest to make without noticing, because it can happen through the design of a study rather than through the model formula.

Restricting the Sample Is a Form of Adjustment

A study that recruits only one level of a variable has conditioned on that variable before any model is fitted. A study that recruits only hospital patients has therefore adjusted for hospital admission, even if its model contains no extra variable, and if admission is a collider, that sampling decision alone can create a false association. Worked Example 1.3 shows the size of the distortion with simulated numbers.

3.1.2 Checking the Adjustment Set in R

For a diagram with many arrows, the adjustment set can be checked with software, and from this point the lesson works in R. If you have not used R before, start with the narrated walkthrough below, and then set up R, RStudio and the packages for this lesson as the callout after it describes.

Learn to do this in R

Narrated R walkthrough: Getting Started with R and RStudio

If you have not used R before, start with this walkthrough. It installs R and RStudio, shows how the RStudio window is laid out, introduces objects, functions and packages, loads the course survey data, and shows how to read a help page, with every line of code explained as it runs.

Open the Getting Started with R and RStudio walkthrough

Getting started: R, RStudio, and the packages for this lesson

The R activities in this course assume a working installation of two free programs. R is the statistics language that does the calculations, and RStudio is the program in which R code is written and run. Both are available from the Posit download page; R is installed first and RStudio second.

  1. Install the packages used in this lesson by typing the line below into the Console pane and pressing Enter. A package is a free add-on that supplies extra functions. Installation needs an internet connection, takes a few minutes, and is done once per computer.
  2. Load the packages at the top of each script with library(). Loading has to be repeated in every new R session.
  3. Open the Code file of each activity, or a new script (File, New File, R Script), and save it. Code typed into a script is saved with the file; code typed only into the Console is lost when RStudio closes. Each activity's Code file reads its data straight from the course website, so there is nothing else to set up.
Code
# Run once per computer, in the Console install.packages(c("dagitty", "mediation", "dplyr")) # Run at the top of every script that uses them library(dagitty) # causal diagrams and adjustment sets library(mediation) # mediation analysis library(dplyr) # data handling

Two ways of running code appear in every lesson. Running a line sends only the current line, or the highlighted lines, to the Console; in RStudio this is the Run button or Ctrl+Enter (Cmd+Return on a Mac). Sourcing a script runs the whole file from top to bottom; this is the Source button or Ctrl+Shift+S (Cmd+Shift+S on a Mac). Running line by line suits exploration, and sourcing confirms that the complete script works from a fresh start.

Any text after a # symbol on a line is a comment. R ignores comments, so they are used to explain what each step does and why. The Code files in this course use comments in this way, and the answer-key scripts follow the same practice.

About dplyr and the tidyverse. dplyr (data manipulation) is one of the tidyverse packages, together with readr (reading and writing files), tidyr (tidying data) and ggplot2 (graphics). The line install.packages("tidyverse") installs them all at once, and library(tidyverse) loads them all; if that installation fails, each package can be installed and loaded on its own, as above.

Worked Example 1.2 encodes the diagram from Worked Example 1.1 with the dagitty package and asks R for the adjustment sets of the total and the direct effect of smoking. Compare each line of its output with the set read from the diagram in Table 1.1.

Worked Example 1.2: Finding the adjustment set with dagitty

The dagitty package reads a DAG written as text and reports which variables must be adjusted for to estimate a chosen effect. The same tool is available as a free web page, dagitty.net, where the diagram is drawn with the mouse; the R version keeps the diagram in the script beside the analysis. The code below encodes the diagram from Worked Example 1.1, with cvd standing for the CVD event and bp for blood pressure. Part 1 of Activity 1.1, at the end of this section, runs it line by line.

Code
library(dagitty) # Each "A -> B" is one arrow: A causes B g <- dagitty("dag { age -> smoker ; age -> cvd sex -> smoker ; sex -> cvd smoker -> bp ; bp -> cvd smoker -> cvd smoker -> hospital ; cvd -> hospital }") # Variables to adjust for to estimate the TOTAL effect of smoking on cvd adjustmentSets(g, exposure = "smoker", outcome = "cvd", effect = "total") # Variables to adjust for to estimate the DIRECT effect (not through bp) adjustmentSets(g, exposure = "smoker", outcome = "cvd", effect = "direct")
Console output
{ age, sex } { age, bp, sex }

Reading the output. The first line answers the first call (the total effect), and the second line answers the second call (the direct effect). Each is a sufficient adjustment set: if the DAG is correct, including these variables in the model removes confounding of that effect. For the total effect of smoking, the set is age and sex. Blood pressure is left out because it is a mediator, and hospital admission is left out because it is a collider. For the direct effect, blood pressure joins the set, because the direct effect is defined as the effect that remains when the mediator is held fixed. The answer is only as good as the arrows typed in: dagitty checks the logic of the diagram, and only subject-matter knowledge can check whether the diagram matches reality.

dagitty therefore confirms the set read from the diagram, and it shows how the set changes with the estimand: blood pressure joins the set only when the direct effect is wanted. The next example shows what happens when the collider in the same diagram is conditioned on through the choice of sample.

3.1.3 A Collider Created by Selecting the Sample

Worked Example 1.3 simulates the distortion described in the callout on restriction, in which a study limited to hospital patients conditions on a collider. Watch the percentage with diabetes among smokers and non-smokers, first in the whole population and then among the inpatients.

Worked Example 1.3: A collider created by studying only hospital patients

The simulation below creates a population of 10,000 people in which smoking and diabetes are unrelated by construction. Each condition raises the chance of a hospital admission, which makes admission a collider (smoking → admission ← diabetes). The code compares the percentage with diabetes among smokers and non-smokers, first in the whole population and then among hospital patients only. The function rbinom(n, 1, p) draws n values of 0 or 1 with probability p of a 1; table() counts each combination of values; prop.table(..., margin = 1) turns the counts into proportions within each row; and subset() keeps the rows that meet a condition. Part 2 of Activity 1.1, at the end of this section, runs this code line by line.

Code
set.seed(2026) n <- 10000 smoker <- rbinom(n, 1, 0.30) # 1 = smokes (30% of people) diabetes <- rbinom(n, 1, 0.10) # 1 = diabetes (10%), unrelated to smoking # Each condition raises the chance of a hospital admission (the collider) p_admit <- 0.05 + 0.30 * smoker + 0.30 * diabetes hospital <- rbinom(n, 1, p_admit) pop <- data.frame(smoker, diabetes, hospital) # Whole population: % with diabetes among non-smokers (0) and smokers (1) round(100 * prop.table(table(smoker = pop$smoker, diabetes = pop$diabetes), margin = 1), 1)
Console output
diabetes smoker 0 1 0 90.0 10.0 1 90.2 9.8
Code
# Hospital patients only: this restriction conditions on the collider inpatients <- subset(pop, hospital == 1) nrow(inpatients) round(100 * prop.table(table(smoker = inpatients$smoker, diabetes = inpatients$diabetes), margin = 1), 1)
Console output
[1] 1688 diabetes smoker 0 1 0 56 44 1 85 15

Reading the output. Rows are smoking status (0 = does not smoke, 1 = smokes) and columns are diabetes status, so the right-hand column is the percentage with diabetes. In the whole population, about 10% of both groups have diabetes (10.0% and 9.8%), which matches the way the data were built. Among the 1,688 hospital patients, 44% of non-smokers and only 15% of smokers have diabetes, so smoking now appears to protect against diabetes. The association is produced entirely by the sampling: a non-smoker in hospital is likely to be there because of diabetes, whereas a smoker in hospital is often there because of smoking. A study that recruited only inpatients would report this false association even with flawless data entry and modelling.

Collider bias therefore needs no error in the data or the model: the choice of who enters the study is enough to produce it. This makes it harder to detect than a coding error, because every number in the inpatient table is correct for the people who were sampled. The problem lies in the question the sample can answer. A sample of inpatients describes the relationship between smoking and diabetes among people who were admitted, and that relationship differs from the one in the population the researcher wants to learn about. Because no check of the data can reveal this, the protection comes from the diagram, which shows in advance that admission is a collider and that selecting on it should be avoided.

3.1.4 Adjusting for a Mediator: A Mediation Example

The third wrong decision, adjusting for a mediator when the total effect is wanted, is easiest to see in a mediation analysis. A DAG of the form X → M → Y with a residual direct path X → Y is the qualitative claim; the Baron & Kenny (1986) procedure introduced in 341 puts numbers on the direct and indirect components. Intuitively, the indirect effect is the part of education's benefit that appears only because more education tends to raise income, and higher income in turn improves health; the direct effect is whatever is left once that income pathway is set aside. The example fits these regressions in R on simulated data, so the answer can be checked against the truth.

Three ideas about regression are enough to follow this example. First, a regression model such as lm(health ~ education) fits the straight line that best describes how the outcome (health) changes as the predictor (education) increases. The model’s regression coefficient for education is the slope of that line: the average change in health for each one-unit increase in education. When a second predictor is added, as in lm(health ~ education + income), the coefficient for education becomes the average change in health per unit of education among people with the same income, which is what adjusting for income means.

Second, the data are simulated. The code invents 800 people and generates their education, income, and health from rules chosen in advance, so the true effects are known exactly: each unit of education raises income by 0.6 (the true path a), each unit of income raises health by 0.5 (the true path b), and each unit of education raises health directly by 0.3 (the true direct effect c′). The true total effect is the direct effect plus the indirect effect, c′ + a × b, which these rules fix at 0.60. Because the truth is known, the estimates can be checked against it, which is impossible with real data.

Third, the variables have no real-world units. Education is drawn with rnorm(), which produces values centred on 0 with a standard deviation of 1, so one unit of education is one standard deviation, and income and health are built from education on the same arbitrary scale. A coefficient of 0.30 therefore means that health rises by 0.30 of a unit for each one-unit (one standard deviation) increase in education, on a scale with no direct translation into years of schooling or dollars.

Figure 1.2 shows how the paths combine. The indirect effect is the product of path a and path b, the direct effect is the path c′ that bypasses income, and their sum is the total effect c. Adjusting for income closes the indirect path. By this we mean that once income is in the model, education is compared only among people with the same income, so the part of education’s benefit that works by raising income can no longer show up in the education coefficient. What remains is the benefit that education brings by other routes. This is why the education coefficient changes from the total effect to the direct effect when income enters the model, and why the two coefficients answer different questions rather than one being a corrected version of the other.

Mediation path diagram: education affects health directly and indirectly through income Education Income Health a b c′ (direct)
Figure 1.2. The indirect effect is the product a × b; the direct effect is the path that does not pass through income. Their sum is the total effect.

Part 3 of Activity 1.1, at the end of this section, fits this model: Baron & Kenny’s three regressions fitted one at a time, then the same result with the mediation package, which gives bootstrap confidence intervals for the indirect effect. Table 1.2 explains each line of that part, and the output that follows can be read before or after you run it.

Table 1.2. What each line of the mediation code in Part 3 of Activity 1.1 does.

CodeWhat it does
library(mediation)This line loads the mediation package, which supplies the mediate() function used at the end.
set.seed(410)This line fixes R’s random-number generator, so the simulated data, and therefore every number in the output, are identical on every computer that runs the code.
n <- 800This line stores the number of simulated people. The arrow <- assigns the value on its right to the name on its left.
education <- rnorm(n)This line draws 800 values from a normal (bell-shaped) distribution with mean 0 and standard deviation 1.
income <- 0.6 * education + rnorm(n)This line builds income so that each unit of education adds 0.6 (the true path a), plus random variation.
health <- 0.3 * education + 0.5 * income + rnorm(n)This line builds health from a direct education effect of 0.3 (the true c′) and an income effect of 0.5 (the true b), plus random variation.
dat <- data.frame(...)This line combines the three variables into one table with 800 rows and three columns.
lm(health ~ education, data = dat)This call fits a linear regression of health on education. The tilde ~ separates the outcome (left) from the predictors (right).
coef(...)["education"]This call extracts the fitted coefficients and keeps the one named education, which in the first model is the total effect c.
fit_M <- lm(income ~ education, ...)This line fits the mediator model; its education coefficient is a.
fit_Y <- lm(health ~ education + income, ...)This line fits the outcome model with both predictors; its education coefficient is c′ and its income coefficient is b.
a * bThis line multiplies the two path coefficients to give the indirect effect.
mediate(fit_M, fit_Y, treat, mediator, boot = TRUE, sims = 1000)This call combines the two models to estimate the indirect, direct, and total effects. treat names the exposure, mediator names the mediator, and boot = TRUE with sims = 1000 requests 1,000 bootstrap resamples to build the confidence intervals.
summary(med)This call prints the table of estimates, confidence intervals, and p-values.

Console output, with the four paths marked. The step-by-step part of the code prints the results below (lines 42, 46 and 49 of the Code file). The highlighted numbers are c (total effect, first result), c′ and b (the education and income coefficients in fit_Y), and a × b (the indirect effect, printed under the name education because a carries that label). The (Intercept) is the predicted health score when every predictor equals zero and is not needed here.

Console output: the step-by-step results
education 0.6474746 (Intercept) education income 0.01002411 0.35055962 0.44774286 education 0.296915

The value of a comes from the mediator model. Line 45 of the Code file, coef(fit_M), prints its coefficients, and the education coefficient is path a:

Console output
(Intercept) education 0.02401282 0.66313739

Table 1.3 gathers the five estimates beside the true values used to simulate the data. Every estimate in it is a number that R prints in the output above.

Table 1.3. The paths of the mediation example: where each estimate comes from in the R output, the estimate, and the true value used in the simulation.

PathWhere it comes fromEstimateTrue value
c (total effect)The education coefficient in lm(health ~ education).0.6470.60
aThe education coefficient in fit_M (income on education).0.6630.60
bThe income coefficient in fit_Y (health on education and income).0.4480.50
c′ (direct effect)The education coefficient in fit_Y.0.3510.30
a × b (indirect effect)The product a * b, printed by line 49 of the Code file and reported again as the ACME in summary(med); in this model it equals the difference between c and c′.0.2970.30

Table 1.3 shows the cost of adjusting for a mediator in numbers. The model without income estimates the total effect of education (c, 0.647), and adding the mediator turns that coefficient into the direct effect (c′, 0.351), a smaller quantity that answers a different question. Each estimate lies close to its true value, and the differences are the random variation of a sample of 800. The mediate() function reports the same quantities with confidence intervals.

Console output: summary(med)
Running nonparametric bootstrap Causal Mediation Analysis Nonparametric Bootstrap Confidence Intervals with the Percentile Method Estimate 95% CI Lower 95% CI Upper p-value ACME 0.29692 0.24316 0.34977 < 2.2e-16 *** ADE 0.35056 0.28202 0.43229 < 2.2e-16 *** Total Effect 0.64747 0.57748 0.72300 < 2.2e-16 *** Prop. Mediated 0.45857 0.38062 0.53752 < 2.2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Sample Size Used: 800 Simulations: 1000

Reading the summary(med) table. Each row is one effect. ACME stands for average causal mediation effect, the indirect effect through income (a × b). ADE stands for average direct effect, the effect of education that does not pass through income (c′). Total Effect is c, and Prop. Mediated is the share of the total effect that travels through income, the ACME divided by the total effect, which R prints as 0.459. The Estimate column repeats the values from the step-by-step regressions, because the bootstrap adds uncertainty intervals without changing the estimates.

The 95% confidence interval. For the ACME, the interval runs from 0.243 to 0.350. A confidence interval shows how precisely an effect has been estimated: the values inside it are those most compatible with the data, and the method that produces it captures the true value in 95% of repeated samples. In this simulation the true indirect effect, 0.30, lies inside the interval. A narrow interval indicates a precise estimate, and an interval that includes 0 indicates that the data cannot rule out the absence of an indirect effect.

The p-value. The p-value answers the question of how often an estimate at least this far from zero would arise by chance if the true effect were zero. R prints < 2.2e-16 for any p-value below 0.00000000000000022. Here the p-value comes from the 1,000 bootstrap resamples, none of which fell on the other side of zero, so the package records it as zero. With 1,000 resamples the smallest p-value distinguishable from zero is 0.002, so the result is reported as p < 0.002. A p-value this small means the data are very hard to reconcile with an effect of zero. The stars repeat the same information using the thresholds listed on the Signif. codes line. The p-value carries no information about the size or importance of an effect; the estimate and its confidence interval carry that information.

A model reporting sentence. “In simulated data (n = 800), the estimated indirect effect of education on health through income was 0.30 units of the simulated health score per standard deviation of education (95% CI 0.24 to 0.35), the direct effect was 0.35 (95% CI 0.28 to 0.43), and the total effect was 0.65 (95% CI 0.58 to 0.72); income carried an estimated 46% of the total effect (95% CI 38% to 54%).”

Two cautions. (1) The estimate of the indirect effect is only as credible as the DAG. If an unmeasured variable confounds income and health, the indirect estimate is biased even though every regression runs cleanly. (2) Both the Baron & Kenny arithmetic and mediate() as called here assume that the effect of income on health is the same at every level of education, which is called the assumption of no exposure-mediator interaction. To relax it, the outcome model is fitted as lm(health ~ education + income + education:income, data = dat), where education:income is the interaction term; mediate() then reports separate indirect effects, labelled ACME (control) and ACME (treated), for education values of 0 and 1.

The mediation example completes the three decisions that the adjustment set governs. One further part of the groundwork comes before any analysis at all: looking after the original data.

5.1 Storing and Tracking the Original Data

It is important to establish a permanent storage system for all original data-collection sheets (survey forms, data-collection forms, etc.) that makes it easy to retrieve individual sheets if they are needed during the analysis. Three practices make up the system: protecting the originals, tracking collection progress, and scanning the sheets for completeness. Expand each panel below to read what each practice involves.

Protect your originals

Do not remove originals from your file. If you need a specific sheet for use at another location, make a photocopy. Never ship the original to another location without first making copies of all forms.

Track collection progress

Set up a system for recording the insertion of data-collection sheets into the file so that you know how many remain to be collected before further work begins.

Scan for completeness

Once all forms have been collected, scan through all sheets for their completeness before doing anything else. If there are omissions, returning to the data source to complete the data will be more likely to succeed if done soon after collection rather than weeks or months later.

The common thread is that the original is the irreplaceable record: copies travel, the originals stay in the file, and gaps are followed up while the source can still fill them, because returning to the source soon after collection is far more likely to succeed than weeks or months later.

The same rule applies once the data are in a computer file: the raw data file is the digital original. Part 4 of Activity 1.1 reads cohort.csv, a small smoking cohort of 600 participants, 36 of whom have no recorded outcome because they were lost to follow-up, and cleans it with one pipeline: read, then clean, then record the decision. The raw data are treated as read-only, and every cleaned version is generated from them by the script, so a cleaning mistake can always be corrected by fixing the code and running it again. In a larger project the same rule is kept with one folder for the raw files, which are never overwritten, one for the processed files, which are fully reproducible from the scripts, one numbered script per task (for example 01_load.R, 02_clean.R) and one folder for the numbered figures of the paper.

Checking the data before and after cleaning. Before any cleaning, glimpse(raw) (line 57 of the Code file) shows every variable on one line, with its type and its first few values. chr marks text (character) columns, int marks whole numbers, and dbl marks numbers that can have decimals (“double” is R’s name for them). The output can be read against the short codebook for the file in Table 1.4.

Table 1.4. Codebook for cohort.csv, against which the glimpse() output of Part 4 of Activity 1.1 is read.

VariableType in RMeaning and codes
idtext (chr)This is the participant identifier, running from C001 to C600.
agewhole number (int)This is age in years, from 20 to 79.
sextext (chr)Sex is recorded as Female or Male.
smokertext, converted to a factor by the pipelineSmoking status is recorded as No or Yes.
followup_yearsnumber (dbl)This is the length of follow-up in years, from 1.0 to 10.0.
outcomewhole number (int)The value is 1 if the event occurred during follow-up and 0 if it did not; it is NA (a blank cell in the file) for participants lost to follow-up.

Counting rows before and after the filter with nrow() confirms what the pipeline did, and count(clean, age_grp) shows how many participants fall in each new age band (100, 176, 202 and 86).

Reading the output. nrow() prints 600 rows for the raw file and 564 for the cleaned file; the rows removed are the 36 participants lost to follow-up. In filter(!is.na(outcome)), is.na() asks whether each outcome is missing and ! means “not”, so the filter keeps the rows whose outcome is known. In the age bands, (30,50] means older than 30 and up to and including 50; a round bracket excludes the boundary and a square bracket includes it.

Dropping missing outcomes is a decision to record. Keeping only participants with a known outcome is a complete-case decision. By this we mean that the analysis uses only the rows that have no missing value on the variables it needs, so it will describe only the people who were followed to the end. This is harmless if the people lost to follow-up are similar to those who stayed, because the remaining rows are then a fair sample of the whole cohort. If those lost to follow-up differ from the rest (for example, if sicker participants were more likely to drop out), the remaining 564 no longer represent the cohort, and the results may be biased. The decision therefore belongs in the file log, with the number of rows removed and the reason; lines 65 and 66 of the Code file write that line. The date in the log will be the date on which the code is run.

The pipe operator |> (or %>%) is the workhorse of the tidyverse. It takes the result of one step and passes it on as the input to the next step, so that several operations can be chained together as verb -> verb -> verb. By this we mean that the cleaning pipeline reads like a recipe: start with raw, then filter it, then mutate the result. Without the pipe, the same steps would have to be nested inside one another and read from the inside out, which is harder to follow and harder to check. Because the Code file reads its data from a web address rather than from a folder on one computer, the same script runs unchanged on any computer, which is the reproducibility this section asks for.

Activity 1.1 brings the section together. Its four parts run the dagitty code of Worked Example 1.2, the collider simulation of Worked Example 1.3, the mediation analysis of Figure 1.2 and the cleaning of cohort.csv; run each part along with the reading, then answer the questions from your own output.

R Activity 1.1: from a causal diagram to clean data

Text version of the activity: the code of the run-along script, its console output and notes on reading it.

library(dagitty)
g <- dagitty("dag {
  age -> smoker ; age -> cvd
  sex -> smoker ; sex -> cvd
  smoker -> bp ; bp -> cvd
  smoker -> cvd
  smoker -> hospital ; cvd -> hospital
}")
adjustmentSets(g, exposure = "smoker", outcome = "cvd", effect = "total")
adjustmentSets(g, exposure = "smoker", outcome = "cvd", effect = "direct")
Console output
{ age, sex } { age, bp, sex }

Each A -> B is one arrow: A causes B. The first line of output answers the first call (the total effect of smoking): adjust for age and sex. Blood pressure is left out because it is a mediator, and hospital admission because it is a collider. The second line answers the second call (the direct effect, the part that does not pass through blood pressure), so blood pressure joins the set. Each is a sufficient adjustment set if the diagram is correct; dagitty checks the logic of the diagram, and only subject-matter knowledge can check the arrows.

set.seed(2026)
n        <- 10000
smoker   <- rbinom(n, 1, 0.30)   # 1 = smokes (30% of people)
diabetes <- rbinom(n, 1, 0.10)   # 1 = diabetes, unrelated to smoking
p_admit  <- 0.05 + 0.30 * smoker + 0.30 * diabetes   # each raises admission
hospital <- rbinom(n, 1, p_admit)
pop      <- data.frame(smoker, diabetes, hospital)
round(100 * prop.table(table(smoker = pop$smoker,
                             diabetes = pop$diabetes), margin = 1), 1)
inpatients <- subset(pop, hospital == 1)   # conditions on the collider
nrow(inpatients)
round(100 * prop.table(table(smoker = inpatients$smoker,
                             diabetes = inpatients$diabetes), margin = 1), 1)
Console output
diabetes smoker 0 1 0 90.0 10.0 1 90.2 9.8 [1] 1688 diabetes smoker 0 1 0 56 44 1 85 15

Smoking and diabetes are unrelated by construction, and each raises the chance of a hospital admission, which makes admission a collider. In the whole population about 10% of both groups have diabetes (10.0% and 9.8%). Among the 1,688 hospital patients, 44% of non-smokers and 15% of smokers have diabetes, so smoking appears to protect against diabetes. The false association is produced entirely by restricting the sample to inpatients, which conditions on the collider.

library(mediation)
set.seed(410)
n         <- 800
education <- rnorm(n)
income    <- 0.6 * education + rnorm(n)                  # path a
health    <- 0.3 * education + 0.5 * income + rnorm(n)   # c' and path b
dat       <- data.frame(education, income, health)
coef(lm(health ~ education, data = dat))["education"]    # total effect c
fit_M <- lm(income ~ education, data = dat)              # mediator model
fit_Y <- lm(health ~ education + income, data = dat)     # outcome model
coef(fit_M)                                              # a
coef(fit_Y)                                              # c' and b
a <- coef(fit_M)["education"]
b <- coef(fit_Y)["income"]
a * b                                                    # indirect effect
med <- mediate(fit_M, fit_Y, treat = "education", mediator = "income",
               boot = TRUE, sims = 1000)
summary(med)
Console output
Loading required package: MASS Loading required package: Matrix Loading required package: mvtnorm Loading required package: sandwich mediation: Causal Mediation Analysis Version: 4.5.1 education 0.6474746 (Intercept) education 0.02401282 0.66313739 (Intercept) education income 0.01002411 0.35055962 0.44774286 education 0.296915 Running nonparametric bootstrap Causal Mediation Analysis Nonparametric Bootstrap Confidence Intervals with the Percentile Method Estimate 95% CI Lower 95% CI Upper p-value ACME 0.29692 0.24316 0.34977 < 2.2e-16 *** ADE 0.35056 0.28202 0.43229 < 2.2e-16 *** Total Effect 0.64747 0.57748 0.72300 < 2.2e-16 *** Prop. Mediated 0.45857 0.38062 0.53752 < 2.2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Sample Size Used: 800 Simulations: 1000

The total effect c is 0.647 (true value 0.60). Path a is 0.663, path b 0.448 and the direct effect c′ 0.351, so the indirect effect a × b is 0.297, which also equals c − c′. Adding the mediator (income) to the model moves the education coefficient from the total effect to the direct effect. In summary(med), ACME is the indirect effect (0.297, 95% CI 0.243 to 0.350, which excludes 0), ADE the direct effect (0.351) and Prop. Mediated the share of the total effect carried by income (0.459, about 46%).

library(dplyr)
raw <- read.csv("https://heal-learn.ca/r-activities/data/cohort.csv")   # course data, read from the website
glimpse(raw)
clean <- raw |>
  filter(!is.na(outcome)) |>
  mutate(age_grp = cut(age, c(0, 30, 50, 70, Inf)),
         smoker  = factor(smoker, levels = c("No", "Yes")))
nrow(raw)
nrow(clean)
count(clean, age_grp)
cat(format(Sys.Date()), "cohort_clean:", nrow(raw) - nrow(clean),
    "participants with no recorded outcome excluded;", nrow(clean), "remain\n")
Console output
Attaching package: 'dplyr' The following object is masked from 'package:MASS': select The following objects are masked from 'package:stats': filter, lag The following objects are masked from 'package:base': intersect, setdiff, setequal, union Rows: 600 Columns: 6 $ id <chr> "C001", "C002", "C003", "C004", "C005", "C006", "C007",… $ age <int> 62, 76, 36, 76, 47, 51, 30, 38, 21, 60, 51, 79, 69, 73,… $ sex <chr> "Female", "Female", "Male", "Male", "Female", "Male", "… $ smoker <chr> "No", "Yes", "No", "No", "No", "No", "Yes", "Yes", "No"… $ followup_years <dbl> 3.8, 4.2, 4.2, 8.9, 7.4, 4.8, 1.1, 3.5, 3.8, 3.4, 5.5, … $ outcome <int> 0, 0, NA, 1, 1, 0, 1, NA, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0,… [1] 600 [1] 564 age_grp n 1 (0,30] 100 2 (30,50] 176 3 (50,70] 202 4 (70,Inf] 86 2026-10-22 cohort_clean: 36 participants with no recorded outcome excluded; 564 remain

The file has 600 participants and six variables; outcome is NA for the 36 participants lost to follow-up. filter(!is.na(outcome)) keeps the rows with a known outcome, mutate() adds one new column (age_grp) and re-encodes smoker in place as a factor with No as the reference. raw is never changed, so the cleaned data can always be rebuilt by running the script again. Keeping only participants with a known outcome is a complete-case decision, so it is recorded in the file log with the number of rows removed and the reason; the date is the day the code is run.

Files for this activity Data Code Answer key

R Reflect on what you just ran

Answer each question from your own output, as on the midterm: identify each role from the direction of the arrows, state the model in one sentence, and give the reason and the consequence for each variable.

1. Based on the causal diagram written in g (Figure 1.1), identify the outcome, the exposure, and the hypothesised confounder(s), collider(s) and mediator(s), justifying each role from the direction of the arrows. Then state in one sentence which variables your regression model should include to estimate the total effect of smoking, and check your sentence against the first line of output.

Model answerThe outcome is the CVD event (cvd), and the exposure is smoking (smoker). Age and sex are hypothesised confounders, because each sends arrows to both smoking and the CVD event, forming the forks smoker ← age → cvd and smoker ← sex → cvd. Blood pressure (bp) is a mediator, because it receives an arrow from smoking and sends one on to the CVD event, forming the chain smoker → bp → cvd. Hospital admission is a collider, because arrows from both smoking and the CVD event point into it (smoker → hospital ← cvd). The model regresses the CVD event on smoking, adjusting for age and sex, and excludes blood pressure and hospital admission (and the sample is not restricted to hospital patients). This matches the first line of output, { age, sex }, the sufficient adjustment set for the total effect.

2. A colleague says, “Put age, sex, blood pressure and hospital admission all in the model, to be safe.” Do you agree? For each variable, explain why it should or should not be included and what the consequence of an incorrect decision to include or exclude it would be, using the second line of the adjustmentSets() output, the inpatient table and the mediation results as evidence.

Model answerNo: adjusting for every variable “to be safe” can add bias rather than remove it. Age and sex are included because they are confounders, common causes of smoking and CVD events; omitting either would leave a back-door path such as smoker ← age → cvd open, so the coefficient for smoking would absorb part of the effect of age or sex and would be biased (confounding). Blood pressure is excluded because it is a mediator; adjusting for it blocks the part of the effect that travels through blood pressure, so the model estimates a smaller direct effect (the set { age, bp, sex } on the second line of output is the set for the direct effect) and answers a different question. The mediation example shows the size of this change: adding income moved the education coefficient from the total effect, 0.647, to the direct effect, 0.351, and R reports the indirect effect through income as 0.297 (a * b, and the ACME in summary(med), 95% CI 0.243 to 0.350), with Prop. Mediated 0.459, about 46% of the total. Hospital admission is excluded because it is a collider; including it, or studying only hospital patients, would condition on the collider, open a non-causal path between smoking and CVD events and induce a spurious association (collider or selection bias) that could hide or exaggerate the true one. The simulation shows this: smoking and diabetes are unrelated in the population (10.0% and 9.8% have diabetes), but among the 1,688 inpatients 44% of non-smokers and 15% of smokers have diabetes, a false protective association created by the sampling alone.

3. Run your own code: at the end of your script, type g2 <- dagitty("dag { age -> activity ; age -> depress ; pain -> activity ; pain -> depress ; activity -> depress ; activity -> clinic ; depress -> clinic ; history -> depress }") and adjustmentSets(g2, exposure = "activity", outcome = "depress"), and run both lines. The diagram is for a study of whether weekly hours of physical activity (activity) affect depressive symptom scores (depress) among adults with arthritis; pain is chronic pain severity, clinic is attendance at a community mental-health clinic and history is a family history of depression. Identify the outcome, the exposure, and the hypothesised confounder(s), collider(s) and mediator(s), noting that not all of these variable types are necessarily present, and state in one sentence which variables the regression model should include.

Model answerThe output is { age, pain }. The outcome is the depressive symptom score (depress), and the exposure is weekly hours of physical activity (activity). Age and chronic pain are hypothesised confounders, because each sends arrows to both activity and the symptom score, forming the forks activity ← age → depress and activity ← pain → depress. Clinic attendance is a hypothesised collider, because arrows from both activity and the symptom score point into it (activity → clinic ← depress). The diagram contains no mediator: no variable lies on a path running from activity to the symptom score, so the correct answer for mediators is none. Family history has an arrow only into the outcome, so it is a cause of the outcome only and not a confounder. The model regresses the depressive symptom score on physical activity, adjusting for age and chronic pain, and excludes clinic attendance, which matches { age, pain }; family history is not needed to remove confounding, although adding it would not bias the estimate.
Saved.

Your output from Activity 1.1 follows the groundwork of this section in one script: the diagram fixes the adjustment set, each wrong decision changes the estimate, and the raw file stays untouched while the cleaned version is rebuilt from it. The key takeaways below draw the section together before its knowledge check.

Key Takeaways

  • 1.1 Good data analysis is a loop rather than a straight line. The structured workflow runs from the causal diagram, through cleaning and verifying the data, to simple unconditional checks, and only then to the multivariable model, and you should expect to back up several steps as you learn more about your data. Jumping straight to the model is tempting, but a model fitted before cleaning inherits every coding error in the file, and a model built without a diagram has no stated estimand (the effect you are trying to estimate) or adjustment set, so its results are often wrong even when they look reasonable. When a question asks what an analyst should do first, or what is wrong with a plan, check the order of the steps: diagram, clean and verify, unconditional checks, model. Be wary of any plan that lets p-values pick the variables, because then the data, rather than the causal question, decide what is adjusted for.
  • 2.1 A causal diagram (a DAG) is your map of how you think the variables are related, and you draw it before you open the data. Each variable’s role comes from the direction of its arrows, never from its name: a confounder sends arrows into both the exposure and the outcome (a fork, X ← C → Y), a mediator or intervening variable receives an arrow from the exposure and sends one on to the outcome (a chain, X → M → Y), and a collider receives arrows from both (X → Z ← Y). When you work through a diagram in an exam, name the outcome and the exposure first, then trace the arrows into and out of every other variable one at a time. Not every role has to be present, so “no mediator” can be the correct answer, and a variable with an arrow only into the outcome is a cause of the outcome, not a confounder. An arrow that points at another arrow marks a moderator (effect modifier), which is none of the three roles and is examined with an interaction term or by reporting the effect in each group.
  • 3.1 Once the diagram is drawn, it tells you what you are estimating (for example, the total effect of smoking on CVD events) and which variables belong on the right-hand side of the model (the adjustment set). The rules are short: adjust for confounders, leave mediators out when you want the total effect, and never adjust for or select the sample on a collider. Each wrong decision has a predictable cost. Leaving out a confounder leaves confounding bias, adding a mediator turns the total effect into a smaller direct effect that answers a different question, and adjusting for or restricting on a collider (for example, studying only hospital patients) creates a false association, as Worked Example 1.3 shows. When a question asks you to justify a model, write one sentence naming what is in and what is out, then give the reason and the consequence of the wrong choice for each variable; you can check your set with adjustmentSets() from the dagitty package, but its answer is only as good as the arrows you type in.
  • 5.1 Before any data reach a computer, look after the original data-collection sheets, because they are the record you will go back to whenever a value is in doubt. Keep the originals in a permanent file and send only photocopies to other locations, record each sheet as it arrives so you know how many are still to come, and scan every sheet for completeness as soon as collection ends. Timing matters here: a gap is far easier to fill by returning to the source soon after collection than weeks or months later. If a question describes forms sent away without copies, or checked only months after collection, those are the faults to name, and the fix is the storage, tracking and early completeness check described in this section.
Knowledge check: this section

1. An analyst at a health authority receives a large file on daily air quality and asthma emergency visits. She plans to fit a multivariable regression model on the first afternoon and to clean the data later if the results look odd. Which response reflects the structured approach taught in this section?

This reverses the workflow: the causal diagram comes first, so the estimand and adjustment set are fixed by the causal question. Drawing it from the significant predictors lets p-values decide what is adjusted for, and a model fitted before cleaning carries every coding error in the file.
Cleaning before modelling is right, but the diagram is not optional or reserved for surprises. It is the first step, because it states what is being estimated and which variables belong in the model before any result is seen.
This follows the structured order: causal diagram, cleaning and verification, unconditional checks, then the multivariable model. Moving back to an earlier step when a problem appears is the expected, iterative pattern, not a failure.
Using the same data does not make the order irrelevant. Each step depends on the one before it: a model needs a stated adjustment set from the diagram and data that have already been cleaned and verified.
The correct answer is (c). The information needed is the order of the workflow in this section (causal diagram, clean and verify, unconditional checks, multivariable model) and the principle that analysis is iterative. A model fitted before cleaning inherits every coding error in the file, and a model built before the diagram has no stated estimand or adjustment set, so its results are likely to be wrong even when they look reasonable; backing up to an earlier step when something new appears is the normal pattern, not a failure. Option (a) is tempting because it seems quick, but letting the significant predictors pick the model lets p-values, rather than the causal question, decide what is adjusted for, and option (b) skips the step that comes first. (Section 1, slide “Analysis is iterative, not linear”, 1.1 Why a Structured, Iterative Workflow and the callout “Key Principle: Analysis Is Iterative”; Objective 1.1)

2. A research team asks whether daily screen time affects sleep duration among adolescents. Its causal diagram (below) has these arrows: age → screen time, age → sleep duration, screen time → evening light exposure → sleep duration, screen time → sleep duration, and caffeine intake → sleep duration. Which classification of the variables is correct?

Causal diagram for screen time and sleep duration Age Caffeine intake Screen time Sleep duration Evening light exposure
Each role is read from the arrow directions: age points into both screen time and sleep (a fork, so a confounder), evening light sits on a chain from screen time to sleep (a mediator), caffeine points only into sleep, and nothing receives arrows from both exposure and outcome.
Age and evening light are classified correctly, but caffeine intake sends an arrow only into sleep duration. A confounder must point into both the exposure and the outcome, so caffeine is a cause of the outcome only.
Two arrows touching a variable do not make it a collider; both must point into it. Evening light receives an arrow from screen time and sends one on to sleep, which is a chain, so it is a mediator.
This mislabels every role. Age points into both screen time and sleep, the fork pattern of a confounder; evening light lies on the chain from exposure to outcome, a mediator; and caffeine sends a single arrow outward, so it cannot be a collider.
The correct answer is (a). The information needed is the direction of every arrow, because each role is read from the arrows, never from the variable’s name. Age sends one arrow into screen time and one into sleep duration (a fork), so it is a confounder; evening light exposure receives an arrow from screen time and sends one on to sleep duration (a chain), so it is a mediator; caffeine intake sends an arrow into sleep duration only, so it is a cause of the outcome only, not a confounder; and no variable receives arrows from both screen time and sleep duration, so there is no collider. Option (c) is tempting because two arrows touch evening light exposure, but a collider needs both arrows pointing into it, and here one of them leaves it. (Section 1, 2.1 Sketching a Causal Diagram, the callout “Recall: DAGs and Their Three Structures” and Worked Example 1.1, “Reading the roles off a diagram”; Objective 2.1)

3. A workplace offers a walking programme, and an analyst wants the total effect of taking part on systolic blood pressure (SBP). The causal diagram (below) has these arrows: age → programme, age → SBP, programme → weight loss → SBP, programme → SBP, and programme → follow-up clinic visit ← SBP. Which model follows from the diagram?

Causal diagram for a walking programme and systolic blood pressure Age Walking programme Systolic BP Weight loss Follow-up clinic visit
The crude model leaves the backdoor path programme ← age → SBP open, so its estimate is confounded by age. Age forms a fork and must be in the adjustment set.
Adding weight loss seems to control for more, but weight loss lies on the chain from programme to SBP. Adjusting for a mediator blocks part of the effect and turns the total effect into a direct effect, a different question.
This adds both the mediator and the collider. Weight loss blocks part of the total effect, and the clinic visit receives arrows from both programme and SBP, so conditioning on it opens a false association.
For the total effect the adjustment set is the confounders only: age closes the backdoor fork, the mediator weight loss is left out so its pathway stays open, and the collider clinic visit is never adjusted for.
The correct answer is (d). The information needed is the role of each variable and the estimand, which here is the total effect. Age forms the fork programme ← age → SBP, so it is a confounder and must be included; weight loss lies on the chain programme → weight loss → SBP, so it is left out when the total effect is wanted; and the follow-up clinic visit receives arrows from both the programme and SBP, so it is a collider and is never adjusted for. Option (b) is the most tempting because it seems to “control for more”, but adding weight loss blocks the part of the programme’s effect that works through weight loss, so the model estimates a direct effect and answers a different question; option (a) leaves the path through age open, so its estimate is confounded. (Section 1, 3.1 Reading the Adjustment Set from the Diagram, 3.1.1 What Adjusting for a Variable Means and Worked Example 1.2; Objective 3.1)

4. A researcher asks whether shift work is associated with migraine and recruits all of her participants from a sleep clinic. Both shift work and migraine make a referral to the sleep clinic more likely. Her model contains only shift work and migraine, and she finds that shift workers are less likely to have migraine. Which statement about the design is correct?

Adjustment is not limited to variables in the model formula. Restricting the sample is itself a form of adjustment, so recruiting only clinic patients conditioned on referral before any model was fitted.
Both conditions point into referral, so referral is a collider, and sampling only referred patients conditions on it. That can create a spurious negative association, as smoking appeared to protect against diabetes among inpatients in the lesson's example.
Referral receives arrows from both shift work and migraine rather than sending arrows into them, so it is a collider, not a confounder. Restricting to it introduces bias instead of removing confounding.
A mediator lies on a chain, receiving an arrow from the exposure and sending one to the outcome. Referral receives arrows from both conditions, so it is a collider, and restricting on it distorts rather than isolates the effect.
The correct answer is (b). The information needed is how each condition relates to referral and the fact that restricting the sample is a form of adjustment. Both arrows point into referral, so referral is a collider; studying only referred patients conditions on it before any model is fitted, and among referred patients a person without migraine is more likely to have been referred because of shift work, so the two conditions appear negatively related even if they are unrelated in the population, just as smoking appeared to protect against diabetes among the inpatients in Worked Example 1.3. Option (a) is tempting because the model formula contains no extra variable, but the sampling decision did the conditioning before the model was fitted. (Section 1, 3.1.1 What Adjusting for a Variable Means, the callout “Restricting the Sample Is a Form of Adjustment” and Worked Example 1.3; Objective 3.1)

5. A community survey team has received 240 completed paper questionnaires. A collaborator at another campus asks to see 30 of them to check the translation, and a first look shows that 12 questionnaires have a blank page. What should the team do?

This breaks two rules for data-collection sheets: originals never leave the file, and every sheet is scanned for completeness at once. Delaying follow-up of blank pages makes it less likely the missing answers can ever be recovered.
The paper originals are the source for checking entries later, including double-entry disagreements, so they are kept, not recycled. Treating blank pages as missing without contacting respondents gives up on data that could still be recovered.
This applies the three rules: protect the originals by sending only copies, track each form as it is filed, and follow up gaps immediately, while returning to the respondent is most likely to succeed.
Tracked delivery still risks a lost original, which cannot be replaced, so only copies should travel. Waiting until all 240 forms are entered also delays the follow-up, which succeeds best soon after collection.
The correct answer is (c). The information needed is the three rules for data-collection sheets: protect the originals, track their arrival, and scan every sheet for completeness before any other step. Originals never leave the file, so only copies travel; a record of each form as it is filed shows how many remain to be collected; and gaps are followed up at once, because returning to the source is far more likely to succeed soon after collection than weeks or months later. Option (d) is tempting because tracked delivery feels safe, but a lost original cannot be replaced, and a follow-up delayed until data entry is finished is less likely to succeed than one made now. (Section 1, slide “Managing data-collection sheets” and 5.1 Storing and Tracking the Original Data; Objective 5.1)

6. An analyst wants the total effect of the weekly hours of unpaid care that employed adults give to a family member (the exposure) on their anxiety score (the outcome). The causal diagram has these arrows: age → caregiving hours and age → anxiety; caregiving hours → leisure time → anxiety; caregiving hours → sick days ← anxiety; and caregiving hours → anxiety. A colleague says, “Put age, leisure time and sick days all in the model, to be safe.” Which reply is correct?

Each role comes from the arrows: age is a fork, so it is a confounder and is adjusted for; leisure time is on the chain and is left out for the total effect; and sick days are a collider, so adjusting for them would create a false association.
Adding variables does not always remove bias. Adjusting for a mediator answers a different question, and adjusting for a collider adds bias, so the habit of including everything to be safe can make the estimate worse.
Being linked to both variables is not enough to be a confounder; the arrow direction matters. Leisure time receives its arrow from caregiving hours, so it lies on the pathway and is a mediator, not a confounder.
The total effect is not the crude estimate when a confounder exists. Age forms a fork with caregiving hours and anxiety, so leaving it out keeps a backdoor path open and leaves confounding bias.
The correct answer is (a). The information needed is the role of each variable, read from the direction of its arrows, and the cost of each wrong decision. Age sends arrows into both caregiving hours and anxiety (a fork), so it is a confounder and is adjusted for; leisure time receives an arrow from caregiving hours and sends one on to anxiety (a chain), so it is a mediator and is left out when the total effect is wanted; and sick days receive arrows from both caregiving hours and anxiety, so they are a collider and are never adjusted for. Option (c) is tempting because leisure time is linked to both variables, but its arrow comes from caregiving hours rather than going into it, so it lies on the pathway; option (b) is the habit of adjusting for everything “to be safe”, which can add bias rather than remove it, and option (d) leaves the path through age open. (Section 1, slide “Three DAG structures”, 2.1 Sketching a Causal Diagram and 3.1.1 What Adjusting for a Variable Means; Objective 3.1)
* Pass the knowledge check (100%) to continue.
Section 2

Data Coding, Entry & File Management

⏱ Estimated time: 20 minutes
Section 2 of 4

Data Coding, Entry & File Management

Encoding responses, organising project files, and building a variable codebook.

Coding conventions

Three rules that prevent cascading errors

Missing values

Assign a code that is impossible as a real response (e.g., −999). Never use a value that could be legitimate data.

Numeric codes

Use numbers for all variables, including those originally recorded as text. In R, the cleaning script then converts each code to a labelled factor.

No compound codes

One variable, one piece of information. A code that bundles sex and ethnicity (1 = male Caucasian, 2 = female Caucasian) breaks the moment you want to analyse either alone.

Data entry

Double-entry and the spreadsheet hazard

Double-entry

Two independent entries, then a programmatic comparison. The most effective way to reduce transcription error.

Spreadsheet caution

Sorting a single column destroys row alignment across the whole dataset. Custom software removes this risk.

Save originals immediately. Store a backup in a second location. Convert to your statistical software's format promptly.

File management

Versioning and the file log

bp01.csv   (28/09) : original; one row per participant; 1092 obs, 8 vars
bp02.csv   (30/09) : 45 missing-value records dropped; 1047 obs, 8 vars
bp03.csv   (02/10) : age_ct, age_ctsq, age_c3, htn added; 1047 obs, 12 vars

Two-digit suffix: 99 versions sort correctly. Never overwrite. The log is your audit trail.

Variable codebook

Naming conventions and the master list

age       Original age in years
age_ct     Age centred by subtracting the mean
age_ctsq   Quadratic term (age_ct squared)
age_c2     Age categorised into 2 groups
age_c3     Age categorised into 3 groups

Group related variables by prefix; shorten long names by removing vowels (wtr_cstrn). One-line description per variable is the minimum standard.

Carry forward

What to take into the next section

  • Coding decisions made in the first hour shape every analysis that follows.
  • Double-entry, versioned files, and a log are the practical backbone of reproducibility.
  • A variable codebook captures what was measured, which is often more useful than the code itself.

Overview

An earlier section set up the conceptual workflow and the discipline of starting from a causal diagram before you touch any data. This section turns to the practical side: how do you encode raw responses, enter them into a workable file, organise files across a project, and keep track of what every variable in your dataset means? These are unglamorous tasks, but they determine whether the analysis you eventually run is reproducible. Organising data so that each variable is a column and each observation a row (the tidy data convention) makes downstream analysis dramatically easier (Wickham, 2014).

Learning Objectives

  • 4.1 Apply coding conventions for missing values, numeric codes, and avoiding compound codes, and identify and correct the coding faults that break them in a raw file.
  • 5.2 Plan a data-entry workflow that minimises transcription error.
  • 5.3 Lay out a project folder structure that distinguishes raw, processed, and analysis files, with versioned file names and a file log.
  • 5.4 Build and maintain a variable codebook that any collaborator could open and use.

4.1 Coding the Data

Before entering data into a computer, careful coding is essential, because good coding practices prevent errors that would otherwise cascade throughout an entire analysis. Coding has two sides: applying a few rules when the data are first coded, and recognising the faults that break those rules when a file arrives from someone else.

4.1.1 Three Rules for Coding

Three rules do most of the work: give missing values a code that can never be a real response, use numeric codes recorded in a master list, and store only one piece of information in each variable. Click each card below to read the detail of each rule and how R handles it.

Missing ValuesClick to explore
Numeric CodesClick to explore
No Compound CodesClick to explore

Worked Example 1.4 shows in R why the rule on missing-value codes matters. Watch the mean age before and after the code is converted to NA.

Worked Example 1.4: How a −999 code distorts a mean, and the fix

The rule about missing-value codes matters because software treats a code such as −999 as a real number until it is told otherwise. The five-person example below shows what happens to the mean age when one person’s unknown age is stored as −999. The function tibble() builds a small table, and c() combines values into a vector. Parts 1 and 2 of Activity 1.2, later in this section, run this code line by line.

Code
library(dplyr) # tibble(), mutate() and na_if(); readr adds read_csv() # Five participants; the third did not report age, coded -999 ages <- tibble(id = 1:5, age = c(34, 51, -999, 47, 62)) mean(ages$age) # -999 is treated as a real age
Console output
[1] -161

R has averaged the code with the real ages and prints a mean age of −161 years, an impossible value. A code of 999 would have produced an equally impossible mean in the other direction, and a code hidden among many real values distorts the mean by less, which makes it harder to notice. Converting the code to NA fixes the problem:

Code
# Fix 1: convert the code to NA, R's missing-value code ages_fixed <- ages |> mutate(age = na_if(age, -999)) ages_fixed$age mean(ages_fixed$age) # NA: R will not average an unknown value mean(ages_fixed$age, na.rm = TRUE) # drop the NA, then average the rest
Console output
[1] 34 51 NA 47 62 [1] NA [1] 48.5

na_if(age, -999) replaces every −999 in age with NA. A plain mean() then returns NA, because the average of a set that includes an unknown value is itself unknown; the argument na.rm = TRUE (“NA remove”) tells R to drop the missing value and average the rest, and R prints 48.5 years, the mean of the four known ages. The same conversion can be made when the file is first read, by listing every value that should count as missing:

Code
# Fix 2: declare the code as missing when the file is read library(readr) demo_file <- tempfile(fileext = ".csv") # a throwaway file for the demo write_csv(ages, demo_file) ages_read <- read_csv(demo_file, na = c("", "NA", "-999"), show_col_types = FALSE) ages_read$age
Console output
[1] 34 51 NA 47 62

In practice. For a real file, the same na argument is added to the call that reads the raw file, in place of demo_file. The empty string "" covers blank cells and "NA" covers cells that already contain the letters NA. Every missing-value code used in the study belongs in this list and in the codebook.

A missing-value code does no harm once it is declared, which is why every code belongs in the codebook and in the call that reads the file. The rule on numeric codes also has a natural form in R, which Worked Example 1.5 shows; notice that the numeric code and its text label are kept side by side.

Worked Example 1.5: Numeric codes and R factors

The rule to use numbers for every variable comes from software in which text values were awkward to analyse. In R, the usual practice is to keep the numeric codes in the raw file, documented in the codebook, and to convert each coded variable into a factor in the cleaning script. A factor is R’s type for a categorical variable: it stores the codes, attaches a text label to each one, and fixes the order of the categories, so that tables and models display Female and Male in place of 1 and 2. A raw file that already stores text, such as the Yes and No values in cohort.csv, is also acceptable provided every value is spelled consistently, and the same factor() step then fixes the order of the categories. Part 4 of Activity 1.2, later in this section, runs this code.

Code
# The raw file stores sex as 1 = Female, 2 = Male (see the codebook) codes <- tibble(id = 1:4, sex = c(1, 2, 2, 1)) codes <- codes |> mutate(sex_f = factor(sex, levels = c(1, 2), labels = c("Female", "Male"))) codes table(codes$sex_f)
Console output
# A tibble: 4 × 3 id sex sex_f <int> <dbl> <fct> 1 1 1 Female 2 2 2 Male 3 3 2 Male 4 4 1 Female Female Male 2 2

Reading the code. In factor(sex, levels = c(1, 2), labels = c("Female", "Male")), levels lists the codes in the order wanted and labels gives the text for each code in the same order. In the printed table, <dbl> marks the original numeric column and <fct> marks the new factor. Keeping both columns side by side makes the conversion easy to check. The first level (here Female) becomes the reference category when the factor is used in a regression model.

The factor keeps the code in the data and the label on the screen, so the coding rule and readable output no longer conflict. The three rules describe a well-coded file; the next task is to recognise a badly coded one.

4.1.2 Spotting Coding Faults in a Raw File

Files received from other people often break these rules, and the first reading of a raw file and its data dictionary is a search for the faults. The five that appear most often, with the correction for each, are listed below.

  • A missing-value code that could be a real value. A code of 99 for “refused” in an age variable cannot be told apart from a real age of 99, and a code of 0 for “not recorded” in cigarettes per day cannot be told apart from a non-smoker. The correction is a code that is impossible as a response, such as −999 (with −998 or −997 if refusals and other reasons need to be kept apart), recorded in the codebook and converted to NA in the cleaning script.
  • Text in a numeric field. An entry such as “none” or “about 5” in a count of hours turns the whole column into text, so R cannot calculate with it. The correction is to record the number the entry means (“none” becomes 0), or the missing-value code if no number can be recovered, and keep the variable numeric.
  • A compound code. A single variable that packs two pieces of information, for example a two-digit code whose first digit is smoking status and whose second digit is the number of alcoholic drinks per week, breaks the rule of one variable for one piece of information. The correction is to split it into two variables, each with its own codes and its own missing-value code. Splitting such a code also splits its missing-value code: a missing code of 99 becomes a 9 in each new variable, which later appears in a frequency table as an unexpected category.
  • Inconsistent formats. Dates written as 05/06/2024, 6 Jun 24 and 2024-06-07 in the same column cannot be sorted or subtracted, and 05/06/2024 is ambiguous (5 June or 6 May). The correction is one unambiguous format for every entry, such as YYYY-MM-DD, stated in the codebook, with the column stored as a date.
  • Inconsistent missing-value conventions. A blank cell in one row, 99 in another and a full stop in a third all mean “missing” but are read differently by the software. The correction is one documented convention for every variable, never a blank; an incomplete form is also a reason to go back to the source while the gap can still be filled.

Every correction is made in the cleaning script, which reads the raw file and writes a new version; the raw file itself is never edited by hand, so each correction can be checked and repeated. Worked Example 1.4 shows the conversion of a −999 code to NA, and Section 3 shows how the same checks are run on a whole file.

Once the coding rules are fixed, the coded data are entered into a computer file.

5.2 Entering the Data

Entering data from paper forms is where many small errors creep in, so the entry step is planned rather than left to chance. Three issues matter most: double data entry, caution with spreadsheets, and saving and backing up the files as soon as entry ends. Expand each panel below for the detail of each.

Double-data entry

Double-data entry, followed by comparison of the 2 files to detect any inconsistencies, is preferable to single-data entry. By this we mean that two people (or the same person on two separate occasions) type in every form independently, producing two files that should be identical. Because two independent typists rarely make the same mistake on the same form, almost every typing error shows up as a disagreement between the files. Each disagreement is then checked against the original paper form and corrected. This dramatically reduces the error rate in your dataset.

Caution with spreadsheets

Spreadsheets are a convenient tool for initial data entry, but they must be used with extreme caution. It is possible to sort individual columns, which could destroy your entire dataset with one inappropriate “sort” command. The danger arises because each row of the dataset belongs to one participant, and the values in that row are linked only by their position. For example, if the age column is sorted from youngest to oldest while the other columns stay where they are, each participant is given someone else’s age, and nothing in the file shows that the link has been broken. Custom data-entry software provides a greater margin of safety.

Save and back up immediately

As soon as the data-entry process has been completed, save the original data files in a safe location. In large, expensive trials, keep a copy of all originals stored in another location. Convert your data to the format your statistical software uses as soon as possible.

Double entry is the strongest of the three defences, and its comparison step is easy to program. Worked Example 1.6 carries out that comparison in R for two independent entries of the same five forms; watch which records each direction of anti_join() returns.

Worked Example 1.6: Comparing two independent entries in R

Double data entry produces two files that should be identical, and the comparison step takes only a few lines of code. In the example below, two people entered the same five forms (sbp is systolic blood pressure in mmHg). The function anti_join(x, y, by = ...) returns the rows of x that have no exact match in y on the listed columns, so running it in both directions lists every record on which the two entries disagree. Part 5 of Activity 1.2, later in this section, runs this code, and its second question asks what to do about each disagreement.

Code
# The same five forms, entered independently by two people entry_a <- tibble(id = 1:5, age = c(34, 51, 29, 47, 62), sbp = c(128, 141, 117, 135, 150)) entry_b <- tibble(id = 1:5, age = c(34, 15, 29, 47, 62), sbp = c(128, 141, 117, 153, 150)) # Rows of entry_a with no exact match in entry_b, then the reverse anti_join(entry_a, entry_b, by = c("id", "age", "sbp")) anti_join(entry_b, entry_a, by = c("id", "age", "sbp"))
Console output
# A tibble: 2 × 3 id age sbp <int> <dbl> <dbl> 1 2 51 141 2 4 47 135 # A tibble: 2 × 3 id age sbp <int> <dbl> <dbl> 1 2 15 141 2 4 47 153

Reading the output. Records 2 and 4 disagree. In record 2 the first person typed an age of 51 and the second typed 15, a pair of swapped digits; in record 4 the systolic pressure is 135 in one entry and 153 in the other. The analyst checks each disagreement against the original paper form and corrects the wrong entry. An error made identically by both people cannot be detected this way, so double entry reduces errors substantially without removing every one.

An entry plan is complete when it has all three elements: double entry with a programmed comparison, care with spreadsheets, and an immediate backup. Each saved file then needs a name and a record of its contents.

5.3 Organising and Versioning Files

It is important to have a system for keeping track of all your files. Key recommendations:

  • Assign a logical name with a 2-digit numerical suffix (e.g., brazil01). A 2-digit suffix allows you to have 99 versions that still sort correctly when listed alphabetically.
  • When data manipulations are carried out, save the file with a new name (the next available number). Do not change data and then overwrite the file.
  • Keep a simple log of files created with information about the contents (e.g., number of observations and variables).

The same system separates the files by their role. The raw files sit in their own folder (for example data/raw) and are treated as read-only, the processed files sit in a separate folder (data/processed) and can be rebuilt at any time from the scripts, and the scripts and the outputs of the analysis have folders of their own. A cleaning error found three steps later can then be corrected by fixing the script and rebuilding the file from the untouched raw version.

A file log records the history of each file. The example below shows the log for a blood pressure study, from the original file as received to the version with derived variables; read each entry for what was done and how many observations and variables the version holds.

Example: File Log for a Blood Pressure Study

bp01.csv (28/09): Original blood pressure study data as received; one record per participant (each participant was measured once). 1092 obs, 8 vars.

bp02.csv (30/09): 45 records with a missing age, systolic, or diastolic value dropped. 1047 obs, 8 vars.

bp03.csv (02/10): Derived variables age_ct, age_ctsq, age_c3, and htn added. 1047 obs, 12 vars.

Dates are written as day/month. The textbook version of this log used .dta files, the format of the Stata statistics package. This course stores every version as .csv (comma-separated values), a plain-text table that R, Stata, SPSS, and spreadsheet programs can all open.

The rule to save a new version rather than overwrite is automatic when the transformations live in a script, and the script can write its own line in the file log. Activity 1.2 brings the section together in R: it runs the code of Worked Examples 1.4 to 1.6, shows two more of the coding faults listed in 4.1.2, and then cleans bp01.csv (the blood pressure study in the file log above: 1092 records, one per participant, 8 variables, 45 records incomplete), saving the result under a new name with a line in the file log. Run each part along with the reading, then answer the questions from your own output.

R Activity 1.2: code, enter and version the data

Text version of the activity: the code of the run-along script, its console output and notes on reading it.

library(dplyr); library(tidyr); library(readr)
ages <- tibble(id = 1:5, age = c(34, 51, -999, 47, 62))   # id 3: not reported
mean(ages$age)                        # -999 is treated as a real age
ages_fixed <- ages |> mutate(age = na_if(age, -999))      # the code becomes NA
ages_fixed$age
mean(ages_fixed$age)                  # NA: R will not average an unknown value
mean(ages_fixed$age, na.rm = TRUE)    # drop the NA, then average the rest
Console output
Attaching package: 'dplyr' The following objects are masked from 'package:stats': filter, lag The following objects are masked from 'package:base': intersect, setdiff, setequal, union [1] -161 [1] 34 51 NA 47 62 [1] NA [1] 48.5

The message printed by library(dplyr) lists base R functions that dplyr replaces; it is not an error. The mean of −161 years is impossible: R has averaged the code −999 with the four real ages as if it were an age. na_if(age, -999) replaces every −999 with NA, R's code for a missing value. A plain mean() then returns NA, because the average of a set that includes an unknown value is itself unknown; na.rm = TRUE drops the missing value and averages the four real ages, which gives 48.5 years.

demo_file <- tempfile(fileext = ".csv")     # a throwaway file for the demo
write_csv(ages, demo_file)
ages_read <- read_csv(demo_file, na = c("", "NA", "-999"),
                      show_col_types = FALSE)
ages_read$age
Console output
[1] 34 51 NA 47 62

The na argument of read_csv() lists every value that should count as missing when the file is read: the empty string "" covers blank cells, "NA" covers cells that already contain the letters NA, and "-999" is the study's missing-value code. Every missing-value code used in a study belongs in this list and in the codebook.

hours <- c("4", "0", "none", "2")     # hours of exercise per week, as entered
mean(as.numeric(hours))               # "none" cannot be read as a number
# smk_drk: first digit smoking (1 no, 2 yes), second digit drinks per week
smk_drk <- c(13, 21, 99, 12)          # 99 = missing
smoker <- smk_drk %/% 10              # split: the first digit
drinks <- smk_drk %% 10               # the second digit
table(smoker)
table(drinks)
Console output
[1] NA Warning message: NAs introduced by coercion smoker 1 2 9 2 1 1 drinks 1 2 3 9 1 1 1 1

The entry "none" turns hours into a text column, so as.numeric() gives NA for it, with a warning, and the mean is NA. smk_drk is a compound code: %/% 10 keeps the first digit (smoking status) and %% 10 the second (drinks per week). Splitting the missing code 99 gives a 9 in each new variable, an unexpected category in both tables that is really a missing value.

codes <- tibble(id = 1:4, sex = c(1, 2, 2, 1))     # 1 = Female, 2 = Male
codes <- codes |>
  mutate(sex_f = factor(sex, levels = c(1, 2), labels = c("Female", "Male")))
codes
table(codes$sex_f)
Console output
# A tibble: 4 × 3 id sex sex_f <int> <dbl> <fct> 1 1 1 Female 2 2 2 Male 3 3 2 Male 4 4 1 Female Female Male 2 2

In factor(sex, levels = c(1, 2), labels = c("Female", "Male")), levels lists the codes in the order wanted and labels gives the text for each code in the same order. <dbl> marks the original numeric column and <fct> the new factor, so the conversion is easy to check. The first level (Female) becomes the reference category in a regression model.

entry_a <- tibble(id = 1:5, age = c(34, 51, 29, 47, 62),
                  sbp = c(128, 141, 117, 135, 150))
entry_b <- tibble(id = 1:5, age = c(34, 15, 29, 47, 62),
                  sbp = c(128, 141, 117, 153, 150))
anti_join(entry_a, entry_b, by = c("id", "age", "sbp"))
anti_join(entry_b, entry_a, by = c("id", "age", "sbp"))
Console output
# A tibble: 2 × 3 id age sbp <int> <dbl> <dbl> 1 2 51 141 2 4 47 135 # A tibble: 2 × 3 id age sbp <int> <dbl> <dbl> 1 2 15 141 2 4 47 153

anti_join(x, y, by = ...) returns the rows of x with no exact match in y on the listed columns, so running it in both directions lists every record on which the two entries disagree: record 2 (age 51 against 15) and record 4 (systolic pressure 135 against 153 mmHg). Each disagreement is checked against the original paper form.

bp_raw <- read.csv("https://heal-learn.ca/r-activities/data/bp01.csv")   # course data, read from the website
bp_clean <- bp_raw |>
  drop_na(systolic, diastolic, age) |>        # drop the incomplete records
  mutate(
    age_ct   = age - mean(age),                # centred
    age_ctsq = age_ct^2,                       # quadratic term
    age_c3   = cut(age, c(0, 35, 55, Inf),
                   labels = c("young", "middle", "older")),
    htn      = factor(systolic >= 140 | diastolic >= 90,
                      levels = c(FALSE, TRUE),
                      labels = c("normotensive", "hypertensive")))
write_csv(bp_clean, "bp02.csv")       # a new version: bp01 is never changed
cat("bp02.csv", format(Sys.Date()), nrow(bp_clean), "obs,",
    ncol(bp_clean), "vars\n", file = "file_log.txt", append = TRUE)
readLines("file_log.txt")             # the file log so far
Console output
[1] "bp02.csv 2026-10-22 1047 obs, 12 vars"

drop_na() removes the 45 records with a missing age, systolic or diastolic value, and mutate() adds four derived variables: centred age (age_ct, so that 0 is a participant of average age), its square (age_ctsq, which lets a later model fit a curve), three age bands (age_c3) and hypertension by the 140/90 rule (htn). The example file log above (“Example: File Log for a Blood Pressure Study”) records these as two steps (bp02.csv and bp03.csv); for brevity the script carries out both in one pass, so the file it saves as bp02.csv already has 12 variables. write_csv() saves the result as a new file, bp02.csv, in RStudio's working folder (shown at the top of the Console pane), and cat(..., append = TRUE) adds one line to file_log.txt, keeping earlier lines; the date is the day you run the script, and running it again adds a second line.

nrow(bp_raw)                          # rows before cleaning
nrow(bp_clean)                        # rows after drop_na()
ncol(bp_clean)                        # 8 original variables + 4 derived
mean(bp_clean$age)                    # the value subtracted to make age_ct
head(bp_clean[, c("id", "age", "age_ct", "age_ctsq", "age_c3")], 3)
count(bp_clean, htn) |> mutate(percent = round(100 * n / sum(n), 1))
Console output
[1] 1092 [1] 1047 [1] 12 [1] 48.04871 id age age_ct age_ctsq age_c3 1 BP0001 49 0.9512894 0.9049515 middle 2 BP0002 43 -5.0487106 25.4894787 middle 3 BP0003 82 33.9512894 1152.6900518 older htn n percent 1 normotensive 734 70.1 2 hypertensive 313 29.9

The counts are the ones in the file log: nrow() gives 1092 rows for bp_raw and 1047 for bp_clean, after the incomplete records were dropped, and ncol() gives 12 variables, the 8 original ones and the 4 derived ones. The mean age is 48.05 years, and head() shows the centred ages: participant BP0001, aged 49, has age_ct = 0.95, and BP0003, aged 82, has age_ct = 33.95 and age_ctsq = 1152.69. Under the 140/90 rule, 313 of the 1047 participants (29.9%) are hypertensive.

Files for this activity Data Code Answer key

R Reflect on what you just ran

Answer each question from your own output, as on the midterm: identify each fault or problem, state how it should be corrected according to best practice, and write the line you would record in the cleaning or file log.

1. Parts 1 to 3 produce three problems: a mean age of −161, a mean of NA with a warning for hours, and a category 9 in table(smoker) and table(drinks). For each, identify the coding fault (or the missing step) responsible, and state how it should be corrected according to best practice.

Model answerThe mean of −161 years is impossible because R treated the missing-value code −999 as a real age; the code itself follows the rule, because −999 can never be a real age, but it must be documented in the codebook and converted to NA in the cleaning script (with na_if() or the na argument of read_csv()), after which mean(..., na.rm = TRUE) gives 48.5 years. A missing-value code that could be a real value, such as 99 for a refused age, would be a worse fault, because a refusal could not be told apart from a true age of 99; it is replaced by a code that is impossible as a response. The entry “none” in hours is text in a numeric field, so the column is stored as text, as.numeric() turns “none” into NA with a warning and the mean is NA; the correction is to record the number the entry means (0) and keep the variable numeric. The two-digit code smk_drk is a compound code that stores two pieces of information (smoking status and drinks per week) in one variable; it is split into two variables, each with its own codes and its own documented missing-value code. Splitting the missing code 99 digit by digit gave a 9 in each new variable, an unexpected category outside the valid codes (1 and 2 for smoking); these values are missing, so they are recoded to NA, the rows are kept, and the rule is recorded in the cleaning log (for example, “smoker = 9 and drinks = 9 (n = 1) come from smk_drk = 99, missing; recoded to NA; rows retained”). Every correction is made in the cleaning script, which reads the raw file and writes a new version; the raw file is never edited by hand.

2. The two anti_join() tables compare two independent entries of the same five forms. Identify the records and values on which the entries disagree, state what you would do about each, and write the line you would record in the cleaning log. A colleague suggests keeping the values in entry_a, because the first person to enter a form is usually the more careful: do you agree?

Model answerIn record 2 the ages are 51 (entry_a) and 15 (entry_b), probably a pair of swapped digits, and in record 4 the systolic pressures are 135 and 153 mmHg. For each disagreement the analyst checks the original paper form and corrects whichever entry is wrong, in the cleaning script; averaging the two values would give a value that nobody recorded. The cleaning log records what was compared, what disagreed and how it was resolved, for example: “entry_a and entry_b compared for 5 forms; 2 disagreements (id 2 age 51 against 15; id 4 sbp 135 against 153) checked against the paper forms and corrected; 5 obs, 3 vars”. I disagree with the colleague: neither entry can be assumed correct, the errors need not all be in one entry, and only the original form can say which value is right. Double entry cannot detect an error that both people made in the same way, so it reduces errors substantially without removing every one; the file still needs the range and consistency checks of Section 3.

3. Run your own code: at the end of the script, type bp_clean |> mutate(htn150 = systolic >= 150 | diastolic >= 90) |> count(htn150) |> mutate(percent = round(100 * n / sum(n), 1)) and run it. Does raising the systolic cut-off to 150 raise or lower the prevalence of hypertension, and what does this tell you about a categorical recode? Then write the entry for bp02.csv that you would record in the file log, and explain why the script saves bp02.csv rather than saving over bp01.csv.

Model answerRaising the cut-off lowers the prevalence, from 313 of 1047 participants (29.9%) under the 140/90 rule to 251 (24%) under a 150/90 rule. A categorical recode depends on its cut-point, so the threshold is defended by reference to a clinical guideline, stated in the codebook, and checked against other cut-points in a sensitivity analysis. File log entry: “bp02.csv (date): records with a missing age, systolic or diastolic value dropped (1092 rows in bp01.csv, 1047 in bp02.csv); age_ct, age_ctsq, age_c3 and htn added; 1047 obs, 12 vars” (the example file log in 5.3 records the two steps as bp02.csv and bp03.csv, and the script does both in one pass). Each count is one that R prints: nrow() gives 1092 and 1047 rows, and ncol() gives 12 variables, the 8 original ones and the 4 derived ones. Saving bp02.csv keeps bp01.csv untouched as the read-only raw file, so a cleaning error found later can be traced and corrected by fixing the script and rerunning it from the raw data, and the two versions can be compared. A point-and-click workflow that saves over the working file destroys its earlier state, so the changed records cannot be identified and the step cannot be rerun from its starting point.
Saved.

Table 1.5 explains each function in Part 6 of Activity 1.2, the part that cleans bp01.csv and writes the file log.

Table 1.5. What each function in Part 6 of Activity 1.2 does.

CodeWhat it does
read.csv("https://heal-learn.ca/r-activities/data/bp01.csv")This call reads the raw file from the course website into R as a table (a data frame) called bp_raw; the file itself is never changed.
drop_na(systolic, diastolic, age)This step removes every row in which any of the three named variables is missing (NA), which drops the 45 incomplete records.
mutate(...)This step creates new columns, or changes existing ones, from calculations on the existing columns.
age - mean(age)This calculation subtracts the mean age (48.05 years among the 1047 participants) from each person’s age, which centres age at zero.
age_ct^2This calculation squares the centred age; the squared term lets a later model fit a curve.
cut(age, c(0, 35, 55, Inf), labels = ...)This call splits age into bands: above 0 and up to 35 (young), above 35 and up to 55 (middle), and above 55 (older). Inf stands for infinity, so the last band has no upper limit.
systolic >= 140 | diastolic >= 90This comparison returns TRUE for each person whose systolic pressure is at least 140 mmHg or (the | symbol) whose diastolic pressure is at least 90 mmHg, and FALSE otherwise.
factor(..., levels = c(FALSE, TRUE), labels = ...)This call turns the TRUE/FALSE result into a factor with readable labels, so FALSE becomes normotensive and TRUE becomes hypertensive.
write_csv(bp_clean, "bp02.csv")This call saves the cleaned table as a new file, the next version of the data; in a project with separate folders for raw and processed data, it would go in the processed folder.
cat(..., file = "file_log.txt", append = TRUE)This call writes one line of text to the log file; append = TRUE adds the line at the end so that earlier entries are kept.
format(Sys.Date())Sys.Date() returns today’s date and format() turns it into text such as 2026-10-01.
nrow(bp_clean)This call counts the rows (participants) in the cleaned table.

What htn means. The variable htn (short for hypertension, or high blood pressure) classifies each participant as hypertensive if the systolic pressure is 140 mmHg or higher or the diastolic pressure is 90 mmHg or higher, and as normotensive (normal blood pressure) otherwise. Systolic pressure is the higher number in a reading such as 140/90 and diastolic the lower. The 140/90 threshold is a widely used clinical definition of hypertension; some guidelines use lower thresholds, which is one reason question 3 of Activity 1.2 asks what happens when the cut-off moves. This simple definition ignores on_treatment, so a participant whose blood pressure is controlled by medication is classified as normotensive. A real analysis would decide how to classify treated participants and record that decision in the codebook.

Reading the checks in Part 7. The 45 incomplete records have gone (nrow() prints 1092 rows before and 1047 after) and four variables have been added (ncol() prints 12), as the file log says. count() counts the rows in each category, and the added percent column gives each count as a percentage of the total: R prints 313 hypertensive participants, 29.9% of the 1047, under the 140/90 rule. Centring is easiest to see in the first rows that R prints. The mean age is 48.05 years, and participant BP0001, aged 49, has an age_ct of 0.95, just above average, while BP0002, aged 43, has −5.05, below average. A centred value of 0 identifies a participant of exactly average age, so in a regression model that uses age_ct, the intercept is the predicted outcome for a participant of average age in this sample. The squared term grows quickly away from the mean: BP0003, aged 82, has an age_ct of 33.95 and an age_ctsq of about 1153. Two of the derived variables are continuous (age_ct and age_ctsq) and two are categorical (age_c3, a three-level factor, and htn, a binary factor). Building them once in the cleaning script means that later analysis code refers to them by name instead of repeating the recoding, and a cut-off such as 140/90 is written in one place, where it can be found, defended and changed.

Figure 1.3 shows why a squared term such as age_ctsq is worth building in advance. A model that contains age on its own can describe only a straight-line relationship, in which the outcome rises or falls by the same amount for every extra year of age. Many health outcomes do not behave this way; an outcome may fall through early adulthood and rise again in later life, for example. Adding the squared term gives the model a second ingredient that grows quickly at both ends of the age range, and by combining the two terms the model can draw a curve that bends. Centring age before squaring it also helps, because it keeps the age term and its square from being almost perfectly correlated with each other.

Two scatterplots of the same simulated data in which the outcome is lowest in middle age. Panel A shows a flat straight line that misses the curve; panel B shows a U-shaped fitted curve that follows the data.
Figure 1.3. Why a squared term is built in advance. Simulated data in which the outcome is lowest in middle age. A. A model with centred age alone can only draw a straight line, which misses the curve. B. Adding the squared term (age_ctsq) lets the fitted line bend, so it follows the data. In bp01.csv the relationship between age and systolic pressure is close to a straight line, so the squared term adds little there; having it ready makes the check quick to run.

SPSS is a statistics package that is often used through menus. A workflow built only by clicking through menus, in SPSS or any similar program, is difficult to re-derive unless the underlying commands are also saved (SPSS calls them syntax). A script can be re-run six months from now, by a colleague, on a different computer.

A versioned and logged set of files records how the dataset has changed. The variables within each file need the same care.

5.4 Naming Variables and Keeping a Codebook

Even a relatively focused study can give rise to a large number of variables once transformed and recoded variables have been created, so the names and their documentation need a system. The first tab below sets out the naming convention and the second applies it to age; open both to see the rule and the example side by side.

Use short but informative names and have all related variables start with the same name. Long names can be shortened by removing vowels (e.g., wtr_cstrn for “water cistern”). If your statistics program is case sensitive, use ONLY lower-case letters. At some point, prepare a master list of all variables.

Table 1.6 applies the naming convention to age: each variable derived from age begins with the same name.

Table 1.6. The original age variable and four variables derived from it, all named to begin with age.

VariableDescription
ageOriginal data (in years)
age_ctAge after centring by subtraction of the mean
age_ctsqQuadratic term (age_ct squared)
age_c2Age categorised into 2 categories (young vs old)
age_c3Age categorised into 3 categories

Because each name derived from age begins with age, the related variables sort together and the link between them is obvious. The names are half of the documentation, and the codebook is the other half.

A codebook (also called a data dictionary) lists every variable with its meaning, type, units, allowed values, and number of missing values, so that a collaborator can use the file without asking the original analyst. Table 1.7 is the codebook for bp01.csv, extended with the four variables derived in the cleaning script. The ranges and missing counts come from the verification checks in the next section.

Table 1.7. Codebook for bp01.csv, extended with the four variables derived in the cleaning script.

VariableDescriptionType and unitsValues or rangeMissing
idParticipant identifiertextBP0001 to BP1092, one per participant0
ageAgenumber, years18 to 8511
sexSextext (factor in analysis)Female, Male0
smokerSmoking statustext (factor in analysis)No, Yes0
bmiBody mass indexnumber, kg/m²16.0 to 41.20
systolicSystolic blood pressurenumber, mmHg85 to 18618
diastolicDiastolic blood pressurenumber, mmHg42 to 11919
on_treatmentTakes blood-pressure medicationtext (factor in analysis)No, Yes0
age_ctAge minus the mean age of 48.05 (derived)number, years−30.05 to 36.950
age_ctsqage_ct squared (derived)number0 to about 13650
age_c3Age band (derived)factoryoung (18 to 35), middle (36 to 55), older (56 to 85)0
htnHypertension by the 140/90 rule (derived)factornormotensive, hypertensive0

The missing counts refer to bp01.csv; in the cleaned file every variable is complete because the 45 incomplete records were removed. A codebook is updated each time a variable is added or redefined, and saving it as a .csv file beside the data (as shown in Activity 1.3 in the next section) keeps the two together.

This section has set out how raw responses are coded and entered, how each version of a file is named, stored and logged, and how variables are named and documented in a codebook. The key takeaways below draw the section together before its knowledge check.

Key Takeaways

  • 4.1 Careful coding prevents errors that would otherwise cascade through the whole analysis, and three rules do most of the work: give missing values a code that can never be a real response (such as −999), use numeric codes listed in a master list or codebook, and store only one piece of information in each variable. When you receive someone else’s file, read it alongside its data dictionary and look for the five common faults: a missing code that could be a real value (99 for a refused age), text in a numeric field (“none” in a count of hours), a compound code, mixed date formats, and missing values recorded in different ways (a blank in one row, 99 in another). For each fault, state both the problem and the correction: an impossible missing code that is converted to NA, the number the text means, two separate variables, one unambiguous date format such as YYYY-MM-DD, and one documented missing-value convention. A common trap is to propose fixing the raw file by hand; every correction belongs in the cleaning script, which reads the raw file and writes a new version. Remember also that splitting a compound code splits its missing code, so a 99 becomes a 9 in each new variable and later shows up as an unexpected category.
  • 5.2 Typing data from paper forms into a computer is where many small errors creep in, so plan the entry step rather than leaving it to chance. The strongest defence is double data entry: two people enter the same forms independently and a program compares the two files, as anti_join() does in Worked Example 1.6, so that each disagreement can be checked against the original form. Double entry cannot catch an error that both people make in the same way, so it reduces errors substantially without removing every one. Use spreadsheets with great caution, because sorting a single column scrambles the rows and can ruin a dataset in one step, and save and back up the original files as soon as entry ends. When you assess an entry plan in a question, look for these three elements (double entry with a programmed comparison, care with spreadsheets, and an immediate backup) and name any that are missing.
  • 5.3 Files multiply quickly during a project, so give them a system from the start. Name each file logically with a two-digit suffix (such as bp01.csv, bp02.csv), save every change as the next version rather than overwriting the last one, keep the raw data read-only in their own folder (data/raw) apart from the processed data (data/processed), and keep a short file log that records what changed and how many observations and variables each version holds. The pay-off is that if you find a cleaning error three steps later, you can rebuild the file from the untouched raw version by rerunning the script. When you read a file log such as the blood pressure example in 5.3, check that each entry says what was done and that its counts match what R reports for that version (nrow() gives 1092 rows for bp01.csv and 1047 for the cleaned file). The fault to look for in a question is a plan that edits and saves over the working file, or that keeps raw and cleaned data in the same place.
  • 5.4 Even a small study ends up with many variables once recoded and derived ones are added, so naming and documentation matter. Use short, informative, lower-case names, and start related variables with the same stem, so that age, age_ct, age_ctsq and age_c3 sort together and their link is obvious. Keep a codebook (data dictionary) that lists every variable with its meaning, type, units, allowed values and number missing, as Table 1.7 does for the blood pressure study, and update it whenever you add or redefine a variable; it is often more useful to a collaborator than the code itself. A good test is whether a collaborator could open the file and the codebook and use the data without asking you a single question. The codebook is also your reference when you check data: a value or category in the file that the codebook does not allow is a fault to explain.
Knowledge check: this section

1. A school board sends you a survey file in which pa_days records the number of days in the past week on which a student was active for at least 60 minutes, so its real values run from 0 to 7. The data dictionary supplied with the file states “0 = question not answered”. Which statement identifies the coding fault and the best-practice correction?

Assuming non-answers were inactive guesses at data that were never given. Because 0 days is a real and common answer, using it as the missing code mixes inactive students with students who skipped the question.
A missing-value code must be impossible as a real response, and 0 is a real answer here. An impossible code such as −999, documented and converted to NA in the script after checking the source, keeps non-answers separate from inactive students.
na.rm = TRUE drops only values already stored as NA. R reads every 0 as a real zero, so the non-answers would stay in the mean and pull it down.
Numeric storage is not the fault; a numeric variable is what allows counts and means. The problem is the missing code, which needs a value that can never be a real answer, not a switch to text labels.
The correct answer is (b). The information needed is the range of real values (0 to 7) and the missing-value code in the dictionary (0). Because 0 days is a real and common answer, every count, percentage or mean of pa_days mixes inactive students with students who skipped the question; the rule is a missing code that can never be a real response, recorded in the codebook and converted to NA in the script, never by hand in the raw file. Option (c) is tempting, but na.rm = TRUE drops only values that are already NA; R treats every 0 as a real zero, so the non-answers would still pull the mean down. (Section 2, slide “Three rules that prevent cascading errors”, 4.1.2 Spotting Coding Faults in a Raw File and Worked Example 1.4; Objective 4.1)

2. A diabetes clinic file stores dtype_ins, a two-digit code whose first digit is the type of diabetes (1 = type 1, 2 = type 2) and whose second digit is insulin use (1 = yes, 2 = no), so 21 means type 2 diabetes treated with insulin; 99 means missing. Which correction follows best practice?

Labels make output readable, but the variable still packs two facts into one value. One variable should hold one piece of information, so diabetes type and insulin use each need their own variable.
Splitting the code is the right idea, but typing by hand into the raw file leaves no record and risks new errors. Corrections belong in a script, with the raw file left unchanged.
Storing the code as text does not separate the two pieces of information, so diabetes type or insulin use still cannot be analysed on its own. The compound code has to be split.
This applies two rules: one variable for one piece of information, and corrections made in a script that leaves the raw file untouched. Giving each new variable its own missing code avoids the stray 9 that a split 99 would create.
The correct answer is (d). The information needed is the rule of one variable for one piece of information and the rule that corrections are made in a script. dtype_ins packs two facts into one value, so any analysis of insulin use alone, or of diabetes type alone, first has to take the variable apart; splitting it gives two variables that can each be tabulated and modelled. The missing code needs care: split digit by digit, 99 becomes a 9 in each new variable, which later shows up as an unexpected category, so each new variable gets its own missing code, converted to NA. Option (a) is tempting because labels make the output readable, but the labelled variable still combines two facts, and option (b) edits the raw file by hand, which leaves no record. (Section 2, slide “Three rules that prevent cascading errors” and 4.1.2 Spotting Coding Faults in a Raw File; Objective 4.1)

3. Two research assistants independently enter the same 200 forms. The comparison step, anti_join() run in both directions, returns the output below: the first table lists the rows of entry_a with no exact match in entry_b, and the second the reverse. What does the output show, and what is the next step?

> anti_join(entry_a, entry_b, by = c("id", "weight_kg", "height_cm"))
# A tibble: 1 × 3
     id weight_kg height_cm
  <int>     <dbl>     <dbl>
1    57        68       172
> anti_join(entry_b, entry_a, by = c("id", "weight_kg", "height_cm"))
# A tibble: 1 × 3
     id weight_kg height_cm
  <int>     <dbl>     <dbl>
1    57        86       172
A record listed in both anti_join() directions is one where the two entries disagree. The swapped digits can only be resolved by going back to the original paper form, which shows which entry is right.
Averaging produces a weight that neither person recorded and that is probably wrong. Disagreements in double entry are resolved against the original form, not by compromise.
Which person entered a form first says nothing about which entry is correct. The lesson resolves each disagreement by checking the paper form.
One disagreement still needs resolving, and double entry cannot catch an error that both people made in the same way. It reduces errors substantially without guaranteeing an error-free file.
The correct answer is (a). The information needed is what anti_join() returns: the rows of one entry with no exact match in the other, so a record listed in both directions is one on which the two entries disagree. Here the two people typed 68 and 86 kg for the same participant, a pair of swapped digits, and only the paper form can say which is right. Option (b) is tempting because averaging seems fair, but 77 kg is a value that nobody recorded; option (d) overlooks that an error made identically by both people produces no disagreement, so double entry reduces errors substantially without removing every one. (Section 2, slide “Double-entry and the spreadsheet hazard”, 5.2 Entering the Data and Worked Example 1.6; Objective 5.2)

4. An analyst cleaning flu03.csv (2,450 observations, 14 variables) corrects 31 impossible ages and saves the result over flu03.csv. Two weeks later a reviewer asks which records were changed, and the analyst cannot say. Which practice would have prevented the problem?

A comment records that a change was made, but once flu03.csv is overwritten its earlier state is gone. The changed records cannot then be identified, and the step cannot be rerun from its starting point.
Overwriting the working copy at every change still destroys each intermediate version, so changes between versions cannot be traced. Each change should be saved under the next numbered name with a log entry.
Saving under the next two-digit suffix keeps the previous version intact for comparison, and the file log records the date, the change and the numbers of observations and variables, so the reviewer's question can be answered.
Raw data are read-only. Placing corrections in the raw folder breaks that rule and erases the record of what was originally received.
The correct answer is (c). The information needed is the file-management rules: a logical name with a two-digit suffix, every change saved under the next number and never over the last version, and a one-line file log for each version. Saving flu04.csv keeps flu03.csv intact, so the two versions can be compared to list the 31 changed records, the log states what changed and when, and the script can rebuild flu04.csv from flu03.csv at any time. Option (a) is tempting because the comment records that a change was made, but once flu03.csv is overwritten its earlier state is gone, so the changed records cannot be identified and the step cannot be rerun from its starting point; option (d) breaks the rule that raw data are read-only. (Section 2, slide “Versioning and the file log”, 5.3 Organising and Versioning Files and Activity 1.2; Objective 5.3)

5. A collaborator receives a file whose age-related variables are named Age, AGE2, agegroup_new and x17, with a codebook that lists only the variable names. Which change best follows the conventions for naming and documenting variables?

This follows the naming convention: short, informative, lower-case names sharing a stem so related variables sort together, plus a codebook giving meaning, type, units, allowed values and number missing.
Short names are easy to type, but v1 to v4 carry no information about content. Names should be short and informative, with a shared stem for related variables.
Full descriptions with spaces are awkward to use in code, and a name cannot hold units, allowed values or missing counts. A codebook is still needed alongside short names.
Mixing capital letters causes errors in a case-sensitive program such as R, where Age and age are different names. The convention is lower case only, with a codebook explaining derived variables.
The correct answer is (a). The information needed is the naming convention (short but informative names, a shared stem for related variables, and only lower-case letters in a case-sensitive program such as R) and what a codebook must contain. Shared stems keep the age family together in any alphabetical list, lower case avoids errors in R, which treats Age and age as different names, and a codebook with meaning, type, units, allowed values and missing count lets a collaborator use the file without asking the original analyst. Option (b) is tempting because the names are short, but v1 to v4 carry no information, and option (c) gives names containing spaces, which are awkward in code and still lack units and allowed values. (Section 2, slide “Naming conventions and the master list”, 5.4 Naming Variables and Keeping a Codebook, Table 1.6 and Table 1.7; Objective 5.4)

6. A youth sports-injury programme sends you its raw file, inj01.csv, with the data dictionary below. Which statement names three coding faults and corrects each according to best practice?

id   sport_sex  prev_inj  inj_date
101  11         2         07/03/2026
102  22         0         2026-03-09
103  99         1         12 Mar 26

Data dictionary as supplied
  sport_sex  first digit = sport (1 soccer, 2 hockey, 3 basketball);
             second digit = sex (1 female, 2 male); 99 = missing
  prev_inj   number of previous injuries; 0 = not reported
  inj_date   date of injury
The dictionary explaining a code does not fix it: sport_sex is still a compound code and prev_inj still uses a real value as its missing code. Retyping dates directly in inj01.csv also edits the raw file.
Documentation cannot separate a real 0 from a missing one or undo a compound code, and R reading several date formats does not resolve an ambiguous date such as 07/03/2026. All three are faults.
Replacing zeros with a mean invents values and treats real zeros as missing, storing sport_sex as text leaves it compound, and saving over inj01.csv destroys the raw file and the record of what changed.
This names each fault and its rule: split the compound code with separate missing codes, give non-reports an impossible code converted to NA, store dates in one unambiguous format, and make every fix in a script that saves the next version.
The correct answer is (d). The information needed is the extract, its dictionary and the five common coding faults. sport_sex packs sport and sex into one value, and its missing code 99 would split into a 9 in each new variable; prev_inj uses 0, a real and common answer, as its missing code, so players with no previous injury cannot be told apart from those whose answer was not reported; and inj_date uses three formats, of which 07/03/2026 is ambiguous (7 March or 3 July). Each fix belongs in the cleaning script, which writes the next version of the file, so the raw file stays as received. Option (b) is tempting because every code is documented, but documentation cannot separate a real 0 from a missing one or undo a compound code; options (a) and (c) edit or overwrite the raw file, which leaves no record of what was changed. (Section 2, 4.1.2 Spotting Coding Faults in a Raw File and 5.3 Organising and Versioning Files; Objectives 4.1 and 5.3)
* Pass the knowledge check (100%) to continue.
Section 3

Program Files, Data Editing & Verification

⏱ Estimated time: 15 minutes
Section 3 of 4

Program Files, Data Editing & Verification

Program-mode scripts, systematic data editing, and verification before analysis.

Program vs. interactive

Why the script is the record

Interactive mode

In RStudio: typing in the Console. Useful for exploration. Produces no durable record of steps. Cannot reconstruct what was done.

Program mode

In RStudio: a saved .R script. Commands compiled into a script that can be saved, re-run, and shared. The audit trail is automatic.

All analyses, including descriptive statistics, should live in the script. Mixed spreadsheet-and-program workflows break the audit trail.

Data editing

Three components of scripted editing

Label variables

Attach a short description to each variable name so output is self-explanatory without the codebook open.

Label categories

Attach text labels to numeric codes: sex coded 0/1 displays as "male" and "female", never bare numbers.

Missing-value codes

Standardise every missing indicator (999, blank, full stop) to the single convention the software recognises as missing (NA in R).

Verification

Check before you trust

Continuous variables

Count valid observations and missing values. Check 5 smallest and 5 largest for implausibility. Plot a histogram to assess distribution shape.

Categorical variables

Count valid observations and missing values. Frequency distribution across all categories. Confirm no unexpected categories have appeared.

Carry forward

What to take into the next section

  • Program mode makes every analytic step reproducible by default.
  • Systematic editing belongs in the cleaning script, not scattered across analysis files.
  • Verification catches data problems that sophisticated modelling would only hide, not fix.

Overview

An earlier section organised your files and variables. This section turns to the analyses themselves: working in program mode rather than interactively (so the work is reproducible), editing data systematically rather than ad hoc, and verifying that the dataset behaves as expected before you draw any inferences from it. The discipline you build here is what separates a defensible analysis from a fragile one, and it has been argued that reproducibility can serve as a minimum standard for evaluating computational research when full independent replication is not possible (Peng, 2011).

Learning Objectives

  • 1.2 Contrast interactive and program-mode workflows and justify why programs are required for reproducible work.
  • 6.1 Edit data through scripted, documented steps (labelling variables and categories and converting every missing-value code to NA) rather than ad hoc fixes to the raw file.
  • 6.2 Run systematic verification checks on ranges, types, and internal consistency, and use their output to identify implausible values and unexpected categories.
  • 6.3 Decide when verification is sufficient to move on to substantive analysis.

1.2 Program Mode and Interactive Mode

Statistical programs can be used in an interactive mode (selecting items from menus or typing in a command) or in program mode (compiling a series of commands into a program and then running it). The difference lies in what is left behind once the work is done. In interactive mode, each command is carried out and then disappears, so the only lasting product is the result on the screen. In program mode, the commands are written into a file that is saved, which means the file itself becomes a complete list of the steps that produced the result. For example, an analyst who recodes age in the Console can see the new variable but has no saved record of the rule used, whereas an analyst who writes the recode in a script can open the file months later and read exactly how it was done.

The two modes suit different jobs, and only one of them leaves a record. Open both tabs below to compare what each mode is good for and why program mode is the one used for the real processing and analysis.

Interactive mode is very useful for exploring your data and trying out analyses. However, it should not be used for any of the “real” processing and/or analysis because it is very difficult to keep a clear record of steps taken. Consequently, it is difficult or impossible to reconstruct the analyses you have completed.

Program mode is the recommended approach. You compile the commands into a program and then run it. These program files can be saved and used to reconstruct any analyses you have carried out. Key tips: name files logically, structure the program to be easy to follow, use sequential indents, and document the file thoroughly with comments.

In RStudio, interactive mode corresponds to typing commands directly into the Console pane, or to clicking through menus such as Import Dataset, and program mode corresponds to writing the commands in a script file (.R) and running it. A practical habit combines the two: commands are tried out in the Console, and each command that is kept is copied into the script with a comment, so that the script alone can recreate every result. In R, a comment is any text that follows a # symbol on a line, and R ignores it when the code runs.

One rule follows directly from this, and it is worth remembering because breaking it is a common source of results that cannot be reproduced.

Do Every Analysis in the Statistical Program

Do all of the analyses in your statistical program. Don’t start doing basic statistics in a spreadsheet. You are going to need the statistical program eventually, and it will be much easier to keep track of all your analyses if they are all done there.

Program mode keeps a record of every step, so a result that cannot be reproduced usually points to a menu or spreadsheet step that was never saved as code. The same discipline applies to editing the data before any analysis begins.

6.1 Editing the Data in a Script

Before beginning any analyses, spend time editing your data. Three edits are the most important: labelling variables, labelling categories, and converting the missing-value codes. Click each card below to read what each edit involves and how it is made in R.

Labelling VariablesClick to explore
Labelling CategoriesClick to explore
Missing Value CodesClick to explore

Each edit is made in the cleaning script rather than by typing into the raw file, so it can be checked and repeated. A codebook table kept beside the data gives each variable its label, factor() attaches a text label to each category code (Worked Example 1.5), and na_if() or the na argument of read_csv() converts every missing-value code to NA (Worked Example 1.4). The last edit has the largest consequences, because until it is made R treats a code such as 999 as a real value. Edited data must still be checked for errors before analysis.

6.2 Verifying the Data

Before you start any analyses, you must verify that your data are correct. By this we mean checking that every value in the file is one that could really have been recorded for that participant, and that the file contains the records it is supposed to contain. Verification matters because a model cannot tell a typing error from a real measurement; an age of 999 or a misspelled category is analysed just as faithfully as a correct value, and the error passes silently into the results. This can be combined with data processing and involves going through all of your variables, one-by-one.

The checks differ for continuous and categorical variables. Expand both panels below to see the checks for each type.

For continuous variables
  • Determine the number of valid observations and the number of missing values
  • Check the maximum and minimum values (or the 5 smallest and 5 largest) to make sure they are reasonable; if they are not, find the error, correct it, and repeat the process
  • Prepare a histogram of the data to get an idea of the distribution and see if it looks reasonable
For categorical variables
  • Determine the number of valid observations and the number of missing values
  • Obtain a frequency distribution to see if the counts in each category look reasonable (and to make sure there are no unexpected categories)

Activity 1.3, at the end of this section, runs these checks in R on the raw blood pressure file, after a codebook that states what each variable should contain, and adds two cross-variable checks: a search for diastolic readings above the systolic reading and a count of the distinct identifiers. Table 1.8 explains how to read each check in its output and what a problem would look like.

Table 1.8. The verification checks in Activity 1.3: what the output shows for bp01.csv and what would signal a problem.

CheckWhat the output shows for bp01.csvWhat would signal a problem
1. summary()Min. and Max. are the smallest and largest values; 1st Qu., Median, and 3rd Qu. split the data into quarters; and NA's counts missing values. Ages run from 18 to 85, BMI from 16.0 to 41.2, systolic pressure from 85 to 186 mmHg, and diastolic pressure from 42 to 119 mmHg, all of which are possible in adults.A minimum or maximum that cannot be true, such as an age of 999 or a blood pressure of 0, or a mean far from the median, would call for a closer look.
2. colSums(is.na())is.na() marks each missing value as TRUE and colSums() counts the TRUE values in each column: 11 ages, 18 systolic values, and 19 diastolic values are missing, and the other variables are complete.Missing counts that differ from the study records, or missing values in a variable that should be complete, would need explaining.
3. head(sort()), tail(sort())sort() puts the values in order, head(..., 5) shows the first five, and tail(..., 5) the last five. The lowest systolic value (85 mmHg) and the highest age (85 years) fill all five places shown. A count with sum(bp_raw$systolic == 85, na.rm = TRUE) finds 15 participants at 85 mmHg, and the same count for age finds 6 participants aged 85.An impossible value would be an error. A pile-up of identical values at the edge, as here, can also mean that values beyond a limit were recorded as the limit, which the study protocol should confirm.
4. hist()The histogram of systolic pressure is a single hump centred near 128 mmHg. The small bars above 180 mmHg hold three high but possible readings (181, 186, and 186 mmHg).An isolated bar far from the rest, or a shape that contradicts what is known about the variable, would need investigation.
5. table(..., useNA = "ifany")Each category is listed with its count. The argument useNA = "ifany" adds an NA column only when values are missing, and none are missing here.An unexpected category, such as a misspelling, or more missing values than expected would need correcting.
6. filter(diastolic > systolic)filter() keeps the rows that meet the condition. The result has 0 rows, so no participant has a diastolic reading above the systolic reading.Any returned row identifies a record to check against the original form.
7. n_distinct(id)n_distinct() counts the different values. There are 1092 identifiers for 1092 rows, which confirms one row per participant.Fewer identifiers than rows would mean duplicated records or repeated measurements that the codebook should explain.

The histogram is often the quickest way to spot a planted code. A histogram divides the range of a variable into equal intervals and draws a bar for the number of values in each, so its horizontal axis must stretch from the smallest value to the largest. If a missing-value code such as 999 has been left in an age variable, the axis has to reach all the way to 999, which squeezes the real ages into a narrow block at one end and leaves a small isolated bar far away from them. The shape is so unusual that the problem is visible at a glance, even when only a few values carry the code. Figure 1.4 shows three histograms of the kind hist() produces: one of plausible values, one with a planted missing-value code, and one with a long right tail.

Three histograms side by side. A: systolic blood pressure with a single hump between 85 and 186 mmHg. B: age on an axis stretching to 1000, with the real ages squeezed at the left and a tiny isolated bar at 999. C: simulated length of stay with most values below 10 days and a long tail to the right.
Figure 1.4. Three histograms of the kind hist() produces. A. Systolic blood pressure in bp01.csv: a single hump of plausible values. B. A copy of bp01.csv in which three missing ages were stored as 999 (planted for illustration): the real ages are squeezed against the left edge, and an isolated bar appears at 999 (highlighted in red here). C. Simulated hospital length of stay: most values are small and a long tail stretches to the right. A skewed shape like C can be correct for the variable; it still matters for later choices, such as the log transformation discussed in the next section.

A check that finds a problem leads to a fix in the cleaning script, after which the check is run again.

Reading the fix in Activity 1.3. The raw file passes every check above, so the activity plants three typical errors on a copy of the data, leaving the raw file untouched, and fixes them in the cleaning script. na_if(age, 999) turns the code into NA. case_when() checks each condition in turn: %in% asks whether the value is one of the listed spellings, and the value after ~ is assigned when it is. Any value that matches no condition becomes NA, so a spelling that the analyst did not anticipate appears as missing in the next frequency table and is never silently relabelled. After the fix, the counts of 554 women and 538 men match the raw file, and age has 14 missing values (11 original and 3 converted); where possible, the three ages are looked up on the original forms and entered through the script. The activity's codebook is a table built with tribble(); saved as a .csv file in the data folder, it stays with the data, any collaborator can open it in R or in a spreadsheet, and it is updated in the same session whenever a variable is added or redefined in the cleaning script.

Each fix is therefore an edit of the kind described in 6.1, found by a check of the kind described here. The remaining question is when the checking can stop.

6.3 Deciding When Verification Is Complete

Verification could go on indefinitely, so the analyst needs a clear point at which to move on to substantive analysis. The checklist below states that point; learn its items, and work through them one at a time whenever you judge whether a dataset is ready.

Verification Checklist: When to Move On

Verification can be considered complete when every statement below is true, and each fix has been made in the cleaning script and recorded in the log.

  • Every variable in the file appears in the codebook, with its meaning, type, units, and allowed values.
  • The number of rows matches the number of participants (or records) expected from the study records, and every identifier is unique.
  • For each continuous variable, the number of missing values is known, and the smallest and largest values are possible.
  • Each continuous variable’s histogram shows no isolated bar at a code such as 999 and no shape that contradicts what is known about the variable.
  • For each categorical variable, the frequency table shows only the categories listed in the codebook.
  • Every missing-value code has been converted to NA.
  • Cross-variable checks, such as a search for diastolic readings above the systolic reading or for pregnancies recorded among male participants, return no rows, or every returned row has been checked against the original form.
  • The cleaning script runs from top to bottom on the raw file and reproduces the cleaned file exactly.

If any item fails, the fix is made in the script and recorded in the log, and the checks are run again, which is the iterative loop of the workflow applied to verification. Activity 1.3 practises the whole sequence on bp01.csv: it builds a codebook, runs the checks of Table 1.8, then plants three typical errors on a copy of the file, finds them with the same checks and fixes them in the cleaning script. Run it along with the reading, then answer the questions from your own output.

R Activity 1.3: verifying, editing and documenting bp01.csv

Text version of the activity: the code of the run-along script, its console output and notes on reading it.

library(dplyr)                    # part of the tidyverse
bp_raw <- read.csv("https://heal-learn.ca/r-activities/data/bp01.csv")   # course data, read from the website
nrow(bp_raw)                      # number of rows
Console output
Attaching package: 'dplyr' The following objects are masked from 'package:stats': filter, lag The following objects are masked from 'package:base': intersect, setdiff, setequal, union [1] 1092

The file is read straight from the course website into bp_raw, which is never changed. It has 1092 rows, which should be one per participant. Line 7 loads dplyr, one of the tidyverse packages, for tribble(), filter(), n_distinct(), mutate(), na_if() and case_when(); its messages about masked objects are normal.

codebook <- tribble(
  ~variable,      ~label,                             ~units_or_codes,
  "id",           "Participant identifier",           "BP0001 to BP1092",
  "age",          "Age",                              "years",
  "sex",          "Sex",                              "Female, Male",
  "smoker",       "Smoking status",                   "No, Yes",
  "bmi",          "Body mass index",                  "kg/m2",
  "systolic",     "Systolic blood pressure",          "mmHg",
  "diastolic",    "Diastolic blood pressure",         "mmHg",
  "on_treatment", "Takes blood-pressure medication",  "No, Yes"
)
codebook
Console output
# A tibble: 8 × 3 variable label units_or_codes <chr> <chr> <chr> 1 id Participant identifier BP0001 to BP1092 2 age Age years 3 sex Sex Female, Male 4 smoker Smoking status No, Yes 5 bmi Body mass index kg/m2 6 systolic Systolic blood pressure mmHg 7 diastolic Diastolic blood pressure mmHg 8 on_treatment Takes blood-pressure medication No, Yes

Labelling variables is the first component of data editing. tribble() builds a small table row by row: the first line names the columns, each name starting with ~, and every following line describes one variable. In a project, the codebook is saved as a .csv file beside the data (for example with write_csv()), where any collaborator can open it, and it is updated in the same session whenever the cleaning script adds or redefines a variable. Category labels are attached separately with factor(), as shown in Worked Example 1.5 (Section 2).

summary(bp_raw[, c("age", "bmi", "systolic", "diastolic")])
colSums(is.na(bp_raw))            # number missing in every variable
head(sort(bp_raw$systolic), 5)    # the five smallest values
tail(sort(bp_raw$systolic), 5)    # the five largest values
head(sort(bp_raw$age), 5)
tail(sort(bp_raw$age), 5)
sum(bp_raw$systolic == 85, na.rm = TRUE)   # how many at the lowest value?
sum(bp_raw$age == 85, na.rm = TRUE)        # how many at the highest age?
hist(bp_raw$systolic, breaks = 30, xlab = "Systolic BP (mmHg)", main = "")
Console output
age bmi systolic diastolic Min. :18.00 Min. :16.00 Min. : 85.0 Min. : 42.00 1st Qu.:38.00 1st Qu.:23.90 1st Qu.:116.0 1st Qu.: 71.00 Median :49.00 Median :27.00 Median :128.0 Median : 80.00 Mean :48.05 Mean :26.84 Mean :127.4 Mean : 79.97 3rd Qu.:58.00 3rd Qu.:29.73 3rd Qu.:139.0 3rd Qu.: 88.00 Max. :85.00 Max. :41.20 Max. :186.0 Max. :119.00 NAs :11 NAs :18 NAs :19 id age sex smoker bmi systolic 0 11 0 0 0 18 diastolic on_treatment 19 0 [1] 85 85 85 85 85 [1] 172 172 181 186 186 [1] 18 18 18 18 18 [1] 85 85 85 85 85 [1] 15 [1] 6
Histogram of systolic blood pressure in bp01.csv: a single hump of plausible values from 85 to 186 mmHg, highest near 125 to 130 mmHg, with a slightly taller first bar at 85 to 90 mmHg and three small bars above 180 mmHg.

Reading the checks. In summary(), Min. and Max. are the smallest and largest values, 1st Qu., Median and 3rd Qu. split the data into quarters, and NAs counts missing values. Ages run from 18 to 85, BMI from 16.0 to 41.2, systolic pressure from 85 to 186 mmHg and diastolic pressure from 42 to 119 mmHg, all possible in adults. colSums(is.na()) counts the missing values: 11 ages, 18 systolic and 19 diastolic values, which are noted and explained rather than treated as errors. sort() puts the values in order and head(..., 5) and tail(..., 5) show the first and last five: the lowest systolic value (85 mmHg, 15 participants) and the highest age (85 years, 6 participants) fill all five places. A pile-up at the edge can mean that values beyond a limit were recorded as the limit, which the study protocol should confirm. The histogram is a single hump centred near 128 mmHg, and the three small bars above 180 mmHg hold high but possible readings (181, 186 and 186 mmHg).

table(bp_raw$sex, useNA = "ifany")
table(bp_raw$smoker, useNA = "ifany")
table(bp_raw$on_treatment, useNA = "ifany")
bp_raw |> filter(diastolic > systolic)    # should return no rows
n_distinct(bp_raw$id)                     # compare with nrow()
Console output
Female Male 554 538 No Yes 844 248 No Yes 759 333 [1] id age sex smoker bmi [6] systolic diastolic on_treatment <0 rows> (or 0-length row.names) [1] 1092

Each frequency table lists only the categories in the codebook; useNA = "ifany" adds an NA column only when values are missing, and none are. filter() keeps the rows that meet the condition: no participant has a diastolic reading above the systolic reading. n_distinct() counts the different identifiers: 1092 for 1092 rows, so there is one row per participant. Fewer identifiers than rows would mean duplicated records or repeated measurements that the codebook should explain.

bp_bad <- bp_raw                       # a copy; bp_raw is untouched
bp_bad$age[c(5, 120, 640)] <- 999      # planted: unknown age typed as 999
bp_bad$sex[c(10, 302)] <- "F"          # planted: abbreviation
bp_bad$sex[450] <- "male"              # planted: lower-case spelling
summary(bp_bad$age)
tail(sort(bp_bad$age), 5)
hist(bp_bad$age, breaks = 30, xlab = "Age (years)", main = "")
table(bp_bad$sex, useNA = "ifany")
Console output
Min. 1st Qu. Median Mean 3rd Qu. Max. NAs 18.00 38.00 49.00 50.69 58.00 999.00 11 [1] 85 85 999 999 999 F Female male Male 2 552 1 537
Histogram of age in bp_bad on an axis stretching to 1000 years: the real ages are squeezed into two bars at the left edge, and a tiny isolated bar sits at 999.

The three planted ages of 999 make the maximum 999 and raise the mean from 48.05 to 50.69 while the median stays at 49; the five largest values and the isolated bar in the histogram show them. The frequency table shows four categories where the codebook allows two: "F" (2) and "male" (1) are other spellings of Female and Male. The 11 missing ages are the original missing values, not a new problem.

bp_fixed <- bp_bad |>
  mutate(age = na_if(age, 999),
         sex = case_when(sex %in% c("Female", "F") ~ "Female",
                         sex %in% c("Male", "male") ~ "Male"))

Reading the fix. na_if(age, 999) turns the code into NA. case_when() checks each condition in turn: %in% asks whether the value is one of the listed spellings, and the value after ~ is assigned when it is. Any value that matches no condition becomes NA, so a spelling that the analyst did not anticipate appears as missing in the next frequency table and is never silently relabelled. After the fix, age has 14 missing values (11 original and 3 converted) and sex has 554 women and 538 men, as in the raw file; where possible, the three ages are looked up on the original forms and entered through the script, and each rule is recorded in the cleaning log.

Files for this activity Data Code Answer key

R Reflect on what you just ran

Answer each question from your own output, as on the midterm: compare each value with what is possible and with the codebook, decide what to do about each problem, and write the rule you would record in the cleaning log.

1. Lines 26 to 41 are a first verification pass of the raw file. Using their output, judge whether bp01.csv is ready for analysis: state whether each minimum and maximum is possible and what the counts of missing values (NAs) mean, explain what the pile-up of values at 85 could mean, and state what the frequency tables, the consistency check (line 40) and the identifier count (line 41) show.

Model answerEvery minimum and maximum is possible for adults: age 18 to 85 years, BMI 16.0 to 41.2, systolic pressure 85 to 186 mmHg and diastolic pressure 42 to 119 mmHg, and each mean lies close to its median. The NAs (11 ages, 18 systolic and 19 diastolic values) are missing values already coded as NA; they are counts to note and explain, not errors. The five smallest systolic values and the five largest ages are all 85 (15 people at 85 mmHg and 6 aged 85); these values are possible, but a pile-up at the edge can mean that values beyond a limit were recorded as the limit, so the study protocol should confirm it. The histogram is a single hump centred near 128 mmHg, and the three readings above 180 mmHg (181, 186 and 186) are high but possible. Each frequency table shows only the codebook categories, with no NA; no participant has a diastolic reading above the systolic one (0 rows); and 1092 identifiers for 1092 rows confirm one row per participant. The file therefore passes this first verification pass, and no value needs to be changed.

2. Suppose you had received bp_bad as the raw file. The output of lines 48 to 51 reveals two problems, one in each of two variables (the 11 missing ages are not one of the problems). Identify the two problems, and for each state whether you would keep, transform, or exclude the affected values, and write the rule you would record in the cleaning log.

Model answerProblem 1 is in age: the maximum is 999.00, the five largest values include three 999s, and the histogram shows an isolated bar at 999, far from the real ages. The three codes pull the mean up from 48.05 to 50.69 while the median stays at 49, so 999 is an undeclared code for an unknown age, not a real measurement. Decision: exclude the values by setting them to NA, keeping the rows, unless the true age can be recovered from the original form, in which case it is corrected. Log rule, for example: age = 999 (n = 3) lies outside the possible range and is the code for an unknown age; values checked against the forms; unverifiable values set to NA; rows retained. Problem 2 is in sex: the frequency table shows four categories where the codebook allows two, and “F” (n = 2) and “male” (n = 1) are other spellings of Female and Male. Decision: keep the values and recode them to the codebook labels, because their meaning is clear; a transformation does not address either problem, because a miscoded value is an error to be corrected. Log rule, for example: sex “F” (n = 2) recoded to Female and “male” (n = 1) to Male, to match the codebook; rows retained.

3. Run your own code: at the end of the script, type summary(bp_fixed$age) and table(bp_fixed$sex, useNA = "ifany") and run them. Use the output to show that the fix worked. A colleague says it would be quicker to open bp01.csv in a spreadsheet and type the corrections there: do you agree? State how the corrections should be made, according to best practice, and what the cleaning log should record.

Model answerAfter the fix, age runs from 18 to 85 with a mean of 48.05, the same as in the raw file, and 14 values are missing: the 11 original NA values plus the 3 converted codes. Sex now has only the two codebook categories, 554 Female and 538 Male, which match the raw file, and no NA, so every spelling was matched by case_when(). I disagree with the colleague: corrections are made in a script that reads the raw file and writes a new version, never by editing the raw file by hand. The raw file then stays untouched, every change is written down where a reviewer can check it, and rerunning the script repeats every step exactly; a hand edit in a spreadsheet leaves no record, so the cleaned file cannot be reproduced. The cleaning log records each rule with its count (age 999 set to NA, n = 3; sex “F” recoded to Female, n = 2, and “male” to Male, n = 1), and where possible the three ages are looked up on the original forms and entered through the script.
Saved.

Your output from Activity 1.3 shows the three habits of this section working together: the checks run as a script, the problems they reveal are fixed by scripted edits, and the checklist decides when the file is ready. The key takeaways below draw the section together before its knowledge check.

Key Takeaways

  • 1.2 R and other statistical programs can be used in two ways. In interactive mode you type commands into the Console or click through menus, which is handy for exploring and trying things out but leaves no reliable record of what you did. In program mode you write the commands in a saved, commented script (an .R file) and run it, so the script becomes the honest record that lets you, or a colleague on another computer six months later, rerun, check and share every step. A practical habit is to try a command in the Console and, once you decide to keep it, copy it into the script with a comment, and to do every analysis, even basic statistics, in the statistical program rather than in a spreadsheet. If a question asks why a result cannot be reproduced, look for a point-and-click or spreadsheet step that was never saved as code.
  • 6.1 Before any analysis the data are edited, and that editing happens in the cleaning script, never by typing into the raw file. Three edits matter most: give every variable a descriptive label (for example, in a codebook table saved beside the data), give every category code a text label (for example, with factor(), so that 1 and 2 print as Female and Male), and convert every missing-value code (999, a blank, a full stop) to the single code the software treats as missing, which is NA in R. The last edit has the biggest consequences, because until it is made R treats a code such as −999 as a real number and averages it with the real values, as Worked Example 1.4 shows. You can make the conversion with na_if() or with the na = argument of read_csv(). When you describe an edit in an answer, say what was changed, which step of the script changed it, and that the raw file was left untouched.
  • 6.2 Verification means going through your variables one by one to confirm that the data are correct before you trust any result. For a continuous variable, check the numbers of valid and missing values, the smallest and largest values (or the five smallest and five largest, with head(sort()) and tail(sort())) and a histogram; for a categorical variable, check the valid and missing counts and a frequency table, such as table(x, useNA = "ifany"). Add cross-variable checks, such as a search for diastolic readings above systolic ones, and confirm that every identifier is unique. When you read summary() or table() output in a question, compare each minimum, maximum and category with what is possible and with what the codebook allows: a maximum age of 999, a mean far from the median, or a category such as “F” or 9 that the codebook does not list is an error to trace. Each problem is corrected in the script and the check is run again, while a count of missing values (NA's) is something to note and explain rather than, by itself, an error.
  • 6.3 It can be hard to know when you have checked enough, so the verification checklist in 6.3 gives you a clear point at which to move on. Verification is complete when every variable is in the codebook, the number of rows matches the study records and every identifier is unique, every minimum, maximum and category is possible, no histogram shows an isolated bar at a code such as 999, every missing code has become NA, cross-variable checks return no unexplained rows, and the cleaning script runs from top to bottom on the raw file and reproduces the cleaned file exactly. If any item fails, fix it in the script, record it in the log and run the checks again. When a question asks whether a dataset is ready for analysis, work through the checklist item by item and name the first item that is not yet met.
Knowledge check: this section

1. Two analysts produce the same table of smoking prevalence by age group. Analyst A typed commands into the RStudio Console and copied the counts into a spreadsheet to work out the percentages. Analyst B wrote a saved, commented .R script that reads the raw file, cleans it and produces the table. A reviewer then finds a duplicated record in the raw file. Which statement is correct?

Reproducibility depends on whether the steps are saved in a script, not on which program ran them. Analyst A's Console commands and spreadsheet arithmetic left no complete record.
Immediate results do not make an analysis reproducible. The Console leaves no list of commands, and splitting work into a spreadsheet breaks the rule that every analysis is done in the statistical program.
This reverses the two modes: interactive mode suits exploration, while program mode, a saved script, is for the real analysis that must be rerun and checked.
A saved, commented script is the record of the analysis, so fixing the duplicate in it and rerunning repeats every step on corrected data. Console commands and spreadsheet steps cannot be rerun this way.
The correct answer is (d). The information needed is the difference between interactive mode (typing in the Console or using menus) and program mode (running a saved script), and the rule that every analysis, including basic statistics, is done in the statistical program. The script is the record: once the duplicate is removed in it, rerunning the script repeats every step on the corrected data, whereas Analyst A has no list of the commands typed and has split the work between two programs. Option (a) is tempting because both analysts used R, but reproducibility depends on whether the steps were saved in a script, not on which program ran them; option (c) reverses the roles of the two modes, since interactive mode suits exploration and program mode suits the real analysis. (Section 3, slide “Why the script is the record”, 1.2 Program Mode and Interactive Mode and the callout “Do Every Analysis in the Statistical Program”; Objective 1.2)

2. The data dictionary for smoke_status lists 1 = never, 2 = former, 3 = current and 999 = missing. The first frequency table from the raw file is shown below; <NA> counts blank cells. Which editing step is correct?

> table(raw$smoke_status, useNA = "ifany")

   .    1    2    3  999 <NA>
  14  402  233   97    9   21 
Following the dictionary literally misses that a full stop is another missing-value convention, not a smoking category. Every form of missing has to become NA.
Scripted editing converts every missing indicator, the 999s and full stops, to NA, labels the real codes with factor(), and reruns the table to confirm that only the expected categories remain.
A blank cell is already read by R as NA, as the output shows, so it is a missing value, not a separate category. Keeping it apart would split missing data into two groups.
Hand-editing the raw file leaves no record of what was changed. Missing indicators are converted in the cleaning script, which leaves the raw file untouched.
The correct answer is (b). The information needed is the list of real codes (1, 2 and 3) and every form that “missing” takes in the output: 14 full stops, 9 codes of 999 and 21 blanks. Scripted editing converts every one of these missing indicators to the single code R recognises, NA, and attaches text labels to the codes so that output reads never, former and current rather than bare numbers. Option (a) is tempting because it follows the dictionary to the letter, but a full stop is another missing-value convention, not a smoking category; option (d) edits the raw file by hand, which leaves no record of the change. (Section 3, slide “Three components of scripted editing” and 6.1 Editing the Data in a Script; Objective 6.1)

3. A school health survey enrolled students aged 12 to 17, and sleep_hrs records each student’s usual hours of sleep per night. The first verification pass gives the output below. Which conclusion follows?

> summary(dat[, c("age", "sleep_hrs")])
      age         sleep_hrs
 Min.   :12.0   Min.   : 5.00
 1st Qu.:13.0   1st Qu.: 7.00
 Median :15.0   Median : 8.00
 Mean   :14.6   Mean   :10.84
 3rd Qu.:16.0   3rd Qu.: 9.00
 Max.   :17.0   Max.   :99.00
                NA's   :6      
No one sleeps 99 hours a night, so the maximum is impossible, and a mean far above the median shows a few large values pulling it up. Listing the extremes, fixing them in the script and rerunning the check is the verification cycle.
Some variables do have a long right tail, but here the cause is an impossible value. An error is corrected at the source, not treated as a shape to be transformed.
A plausible median does not make the maximum acceptable. The range check compares the minimum and maximum with what is possible, and 99 hours of sleep is not; missing values are something to note, not the main fault.
A mean of whole-number ages need not be a whole number, and age runs from 12 to 17 as enrolment requires. The impossible value is in sleep_hrs.
The correct answer is (a). The information needed is the plausible range of each variable (ages 12 to 17; no more than 24 hours of sleep in a day) and the minimum, maximum, quartiles and mean in the output. Age runs from 12 to 17, as enrolment requires, but no one sleeps 99 hours a night, and a mean this far from the median is a sign that a few very large values are dragging it up; the next steps are tail(sort(dat$sleep_hrs), 5) to list the extreme values, a check of each against the source, a fix in the script and a repeat of the check, because where there is one error there are often more. Option (b) is tempting because some variables do have a long right tail (Figure 1.4, panel C), but here the cause is an impossible value, an error to correct rather than a shape to transform. (Section 3, slide “Check before you trust”, 6.2 Verifying the Data and Table 1.8; Objective 6.2)

4. A diabetes clinic file stored a two-digit code (first digit diabetes type, second digit insulin use; 99 = missing), which the cleaning script has split into dtype (1 = type 1, 2 = type 2) and insulin (1 = yes, 2 = no). The frequency table of the new dtype variable is shown below. What does it reveal, and what should the cleaning script do?

> table(clin$dtype, useNA = "ifany")

  1   2   9
 84 391  17 
No third type exists in the codebook; 9 appears because splitting the missing code 99 digit by digit put a 9 in each new variable. Labelling it would treat missing data as a real category.
Missing values are not always shown as NA; here they are hidden behind the number 9. A frequency table is checked for categories the codebook does not allow, and 9 is one.
Code 9 is an artefact of splitting 99, so these are missing values. Recoding dtype and insulin in those rows to NA, keeping the rows and logging the rule handles them in the script.
Merging sparse categories applies to real categories with few people. Code 9 is not a real category but a hidden missing code, so combining it with type 1 would misclassify patients.
The correct answer is (c). The information needed is the list of allowed codes for dtype (1 and 2) and how the variable was made. A frequency table is checked for categories that the codebook does not allow, and 9 is one; splitting 99 digit by digit gives a 9 in each new variable, so these 17 patients are the ones whose original code was missing, and their insulin values will also be 9. Option (b) is tempting because missing values usually appear as NA, but these ones are hidden behind a number, which is why every missing code must be converted; option (d) treats an error as a real but rare category. (Section 3, slide “Check before you trust” and 6.2 Verifying the Data; Section 2, 4.1.2 Spotting Coding Faults in a Raw File; Objective 6.2)

5. An analyst has run summary(), histograms and frequency tables on every variable of a 1,200-row file, and every range and category is possible. Which further finding means that verification is not yet complete?

A long right tail is a plausible shape for income, not an error. It matters for later choices such as a transformation but does not mean verification is incomplete.
The checklist requires every identifier to be unique and the row count to match the expected participants. Fewer distinct identifiers than rows means duplicates or undeclared repeated measures that must be traced first.
Missing values that are already NA and documented in the codebook are what verification aims for. They are noted and reported, not an outstanding fault.
Several ages at the enrolment minimum is exactly what the protocol predicts. A pile-up at a boundary set by design is not a sign of error.
The correct answer is (b). The information needed is the verification checklist in 6.3, which requires every identifier to be unique and the number of rows to match the number of participants expected. With 1,188 distinct identifiers in 1,200 rows, some identifiers appear on more than one row, so duplicated records or undeclared repeated measurements must be traced against the source before analysis. Option (a) is tempting because a long tail looks odd, but a skewed shape can be correct for a variable such as income; it matters for later choices, such as a log transformation, and is not an error, and the pile-up at 18 in option (d) is what the protocol predicts. (Section 3, 6.3 Deciding When Verification Is Complete and Table 1.8; Objective 6.3)

6. A district health office surveyed 500 households about their drinking water. The codebook lists hh_size (number of people in the household; 999 = not answered), kids_u5 (how many of those people are children under 5) and water_src (1 = municipal tap, 2 = private well, 3 = bottled water). The verification output is shown below; its last two lines convert the 999 codes to NA and print the mean household size again. Which reading of the output is correct?

> summary(hh[, c("hh_size", "kids_u5")])
    hh_size          kids_u5
 Min.   :  1.00   Min.   :0.000
 1st Qu.:  2.00   1st Qu.:0.000
 Median :  3.00   Median :0.000
 Mean   : 12.96   Mean   :0.526
 3rd Qu.:  4.00   3rd Qu.:1.000
 Max.   :999.00   Max.   :4.000
> sum(hh$hh_size == 999)
[1] 5
> sum(hh$kids_u5 > hh$hh_size)
[1] 3
> table(hh$water_src, useNA = "ifany")

   1    2    3   22 <NA>
 296  142   49    3   10
> hh <- hh |> mutate(hh_size = na_if(hh_size, 999))
> mean(hh$hh_size, na.rm = TRUE)
[1] 3
The NA values are blank cells to note, not an error to delete, and the mean of 12.96 is inflated by the unconverted 999 codes. That figure should not be reported.
This reads each check correctly: the maximum and the mean far above the median expose the unconverted code, the cross-variable check finds impossible households, and the frequency table shows a category the codebook does not allow. Each is fixed in the script and rechecked.
A household of 999 people is impossible, so those values are the undeclared missing code, not real households. Category 22 is not in the codebook and should be checked against the questionnaire, not relabelled.
The problems are real, but typing over values in the raw spreadsheet and saving under the same name leaves no record. Fixes belong in the cleaning script.
The correct answer is (b). The information needed is what each line of the output checks and what the codebook allows. A household of 999 people is impossible, so the maximum is the undeclared missing code, and the mean far above the median shows the codes pulling the average up; once na_if() turns them into NA, mean(..., na.rm = TRUE) gives 3, the figure to report. The cross-variable check counts three impossible households, and the frequency table shows a category that the codebook does not list. Each problem is checked against the questionnaire, fixed in the script and checked again. Option (a) is tempting because missing values look like a problem, but a count of NA values is something to note and explain, not by itself an error; option (c) treats errors as real values, and option (d) edits the raw file by hand, which leaves no record. (Section 3, slide “Check before you trust”, 6.2 Verifying the Data, Table 1.8 and the verification checklist in 6.3; Objectives 6.1 and 6.2)
* Pass the knowledge check (100%) to continue.
Section 4

Data Processing & Unconditional Associations

⏱ Estimated time: 20 minutes
Section 4 of 4

Data Processing & Unconditional Associations

Processing outcomes and predictors, multilevel structure, and first associations before multivariable modelling.

Outcome processing

Matching the variable to the model class

Categorical

Sparse categories may need collapsing before multinomial regression.

Continuous

Approximately normal? If not, explore transformations. Normality of residuals is what ultimately matters.

Count / rate

Poisson assumption
\[ \color{#0B7B6B}{\mathbb{E}[Y]} \approx \color{#C2410C}{\operatorname{Var}(Y)} \]
E[Y] mean of the count outcome Var(Y) variance of the count outcome

Overdispersion signals negative binomial.

Predictor processing

Missing values, distributions, and sparse categories

Missing values

Drop the predictor, run parallel analyses, or use multiple imputation. The choice must be documented and defended.

Continuous spread

Reasonable variation across the range? If most values cluster near a boundary, consider transformation or categorisation.

Sparse categories

Combine thin cells. Sparse categories distort estimates and cause convergence failures in logistic models.

Multilevel structure

Characterise the hierarchy before modelling

Clinical centres Individuals Individuals Individuals Measurements Measurements Measurements

Ignoring clustering produces standard errors that are too small. Mixed models address this in later lessons.

Unconditional associations

One predictor at a time, before the multivariable model

Variable types Method What to examine
Two continuousCorrelation, scatterplot, simple linear regressionDirection, strength, linearity
Continuous + categoricalOne-way ANOVA, simple linear or logistic regressionGroup differences, effect size
Two categoricalCross-tabulation, chi-squaredCell counts, unexpected categories
Carry forward

The analytic log and the road to modelling

Analyse in blocks

Univariate summaries, then bivariate associations, then multivariable models. Each block in a labelled script section.

Log every decision

Date-stamp outputs. Record what each run produced and what it decided. This log makes the analysis defensible under review.

The structured workflow ends here. The next lesson continues with data cleaning and descriptive analysis, and modelling begins in a later lesson, on a dataset that is clean, documented, and understood.

Overview

Earlier sections produced a verified, well-documented dataset. This section finally takes the step every student is impatient to make: the actual analysis. It begins with the analytic log that records every decision, then shows how to process outcome and predictor variables for analysis, how to recognise a multilevel data structure, and how to run the “unconditional associations” (single-predictor descriptions) that are the necessary first look at any dataset before you touch a multivariable model.

Learning Objectives

  • 5.5 Keep an analytic log that allows you to reconstruct every decision you made.
  • 7.1 Process outcome variables to fit the planned analysis (categorical, continuous, and count or rate).
  • 7.2 Process predictor variables, including decisions about missing values, sparse categories, categorisation, scaling, and recoding.
  • 7.3 Recognise multilevel structure in your dataset and the implications for later modelling.
  • 7.4 Choose, run and interpret the unconditional (single-predictor) association for each pair of variable types as the first analytic step, and explain what it shows about functional form, collinearity and confounders.

5.5 Keeping an Analytic Log

Before starting the more substantial analysis, set up a system for keeping track of your results, because every processing decision in this section will have to be reconstructed later, by you, a co-author or a reviewer. Three habits make up the system: analysing in blocks, keeping log files, and labelling and dating every output. Click each card below to read what each habit involves and how it is done in R.

Analyse in BlocksClick to explore
Keep Log FilesClick to explore
Label & DateClick to explore

The log records what each run produced and also what it decided and why. Each processing step below ends in a decision of that kind, such as excluding participants with missing values or merging a sparse category, and Worked Example 1.9 shows a complete log entry for one of them. A good test of the system is whether you, three months from now, could trace any number in a report back to the script, the file version and the decision that produced it.

7.1 Processing the Outcome Variables

The type of outcome sets the type of model, so each outcome is checked against the analysis that has been planned for it before any model is fitted.

7.1.1 Outcome Types and Their Checks

While verifying data, you can also start processing your outcome variable(s). Review the stated goals of the study to determine the format(s) which best suits the goal(s). The questions to ask depend on the type of outcome; expand each panel below for the questions that apply to a categorical, continuous, and count or rate outcome.

Categorical outcomes

Is the distribution of outcomes across categories acceptable? For example, if you planned a multinomial regression with a 3-category outcome, but very few observations fall in one of the categories, you might want to recode it to a 2-category variable.

Continuous outcomes

Does the variable have the characteristics necessary for the planned analysis? If linear regression is planned, is the distribution approximately normal? If not, explore transformations. Note: It is the normality of the residuals which is ultimately important, but if the original variable is far from normal and there are no strong predictors, the residuals are unlikely to be normal. The reason is that a residual is what is left of each outcome value once the model’s prediction has been subtracted. When the predictors explain a large part of the outcome, the residuals can look quite different from the outcome itself. When the predictors explain little, however, each prediction stays close to the overall mean, so the residuals are little more than the outcome shifted to centre on zero, and they inherit its skew. Checking the outcome at this stage therefore gives an early warning of a problem that the residuals are likely to show later.

Count / rate outcomes

If Poisson regression is planned, are the mean and variance of the distribution approximately equal? The Poisson model assumes they are; when the variance runs well above the mean (a pattern called overdispersion), the fitted standard errors come out too small and the p-values look stronger than the data warrant. This happens because the model calculates its standard errors from the variance it assumes rather than from the variance the data actually show. If the counts are far more spread out than the model expects, the model believes its estimates are more precise than they really are. If that happens, consider negative binomial regression or alternative analytic approaches.

Table 1.9 gathers the outcome types from the slide and the panels above, with an example of each, the model usually used, and the check to make now. The checks in the fourth column belong to this lesson.

Table 1.9. Outcome types, with an example of each, the usual model, and the check to make now.

Outcome typeExampleUsual modelWhat to check now
Binary (two categories)Hypertensive or normotensive (htn)Logistic regressionBoth categories should contain a reasonable number of people, because a rare category gives unstable estimates.
Categorical with three or more categoriesSmoking status recorded as never, former, or currentMultinomial regression (unordered) or ordinal regression (ordered)The count in each category should be checked, and categories that hold only a handful of people can be combined.
ContinuousSystolic blood pressure in mmHgLinear regressionA histogram should show no strong skew; if it does, a transformation such as the logarithm can be tried, and the residuals are checked after the model is fitted.
Count or rateNumber of clinic visits in a yearPoisson regression, or negative binomial regression when the variance is much larger than the meanThe mean and the variance of the counts should be compared, and the check is repeated after the model is fitted, because the Poisson assumption applies to people who share the same values of the predictors.

Three terms used in the panels and in Table 1.9 need plain definitions. Table 1.10 gives the plain-language meaning of variance, residual and overdispersion.

Table 1.10. Plain-language definitions of variance, residual, and overdispersion.

TermPlain-language meaning
VarianceVariance measures spread. It is the average of the squared distances between each value and the mean (R divides by n − 1), and its square root is the standard deviation.
ResidualA residual is the difference between a person’s observed value and the value the model predicts for that person (observed minus predicted). Linear regression assumes that the residuals are roughly bell-shaped around zero.
OverdispersionA count outcome is overdispersed when its variance is clearly larger than its mean. The Poisson model assumes the two are about equal, so overdispersion makes its standard errors too small and its p-values too optimistic.

The definition of variance becomes practical in the check for a Poisson model. Worked Example 1.7 applies it to clinic visits at two clinics; watch whether each printed variance is close to the mean or far above it.

Worked Example 1.7: Comparing the variance with the mean for counts

The check for a Poisson model is to compare the mean of the counts with their variance. The invented example below records clinic visits in one year for ten patients at each of two clinics; var() computes the variance.

Code
# Clinic visits in one year for ten patients at each of two clinics visits_a <- c(0, 2, 4, 3, 2, 6, 1, 3, 2, 3) visits_b <- c(0, 1, 1, 0, 9, 2, 0, 12, 1, 0) mean(visits_a) var(visits_a) mean(visits_b) var(visits_b) var(visits_b) / mean(visits_b)
Console output
[1] 2.6 [1] 2.711111 [1] 2.6 [1] 18.26667 [1] 7.025641

Reading the output. Both clinics average 2.6 visits per patient, and the decision rests on comparing each printed variance with this mean. For clinic A, var() reports 2.71, the spread of the counts measured as in Table 1.10. A variance this close to the mean is what a Poisson model expects. In clinic B, most patients have no visits or one visit and two have 9 and 12, so var() reports 18.27, and the last line, var(visits_b) / mean(visits_b), prints 7.03: the variance is about seven times the mean. That pattern is overdispersion, and a negative binomial model would be the safer starting point for these counts.

The comparison of the variance with the mean is therefore the whole check: clinic A passes it and clinic B does not. For a continuous outcome, the corresponding check concerns the residuals.

7.1.2 Residuals and Transformations

A residual is easiest to understand in a picture. Panel A of Figure 1.5 fits a straight line for systolic pressure against age in the cleaned blood pressure data and draws, for 25 randomly chosen participants, the vertical distance between each observed value and the line. Panel B collects the residuals of all 1047 participants in a histogram; a roughly bell-shaped histogram centred on zero is what linear regression assumes. Residuals are checked after a model is fitted. At the processing stage the analyst looks at the outcome itself, because a strongly skewed outcome rarely produces bell-shaped residuals.

Left: scatterplot of 25 participants with a fitted line and red vertical segments from each point to the line. Right: histogram of 1047 residuals, roughly bell-shaped and centred on zero.
Figure 1.5. Residuals from a linear regression of systolic blood pressure on age (bp01.csv after cleaning). A. Each red segment is one participant’s residual, the observed value minus the value on the fitted line; points above the line have positive residuals. B. Histogram of all 1047 residuals, centred on zero (dashed line) and roughly bell-shaped, which is the pattern linear regression assumes.

When the outcome itself is strongly skewed, a transformation can bring it closer to symmetry. Worked Example 1.8 shows how a log transformation changes a simulated outcome with a long right tail, hospital length of stay; compare the mean and the median before and after the transformation.

Worked Example 1.8: Before and after a log transformation

When an outcome has a long right tail, taking the natural logarithm of each value, log() in R, often makes the distribution close to symmetric, because the logarithm shrinks large values much more than small ones. The simulated example below uses hospital length of stay in days. The function rlnorm() draws right-skewed values for the simulation, and round(..., 1) keeps one decimal place.

Code
# Simulated hospital length of stay (days) for 1000 patients set.seed(4104) los <- round(rlnorm(1000, meanlog = 1.2, sdlog = 0.8), 1) summary(los) # mean well above median: a sign of right skew summary(log(los)) # after the log transformation hist(los, breaks = 40) # before: long right tail hist(log(los), breaks = 30) # after: roughly symmetric
Console output
Min. 1st Qu. Median Mean 3rd Qu. Max. 0.300 1.900 3.400 4.568 5.625 40.700 Min. 1st Qu. Median Mean 3rd Qu. Max. -1.2040 0.6419 1.2238 1.1902 1.7272 3.7062
Left: histogram of length of stay in days with a tall peak near 2 days and a long tail to 40 days. Right: histogram of the logarithm of length of stay, roughly symmetric and bell-shaped.
Figure 1.6. The two histograms produced by the code. A. Length of stay in days (simulated): most stays are short and a long tail stretches to 40 days. B. The same values after log(): the tail is pulled in and the shape is roughly symmetric.

Reading the output. Before the transformation the mean (4.57 days) is well above the median (3.40 days), because the few very long stays pull the mean upwards; this gap is a quick numerical sign of right skew. After the transformation the mean (1.19) and median (1.22) are close, matching the symmetric histogram. In a real analysis the new variable is created in the cleaning script, for example mutate(log_los = log(los)), and the transformation is recorded in the codebook. The logarithm is defined only for values above zero, so an outcome that contains zeros needs adjustment first.

Once each outcome is in a form that suits the planned analysis, the same review is applied to the predictors.

7.2 Processing the Predictor Variables

It is important to go through all predictor variables to determine how they will be handled:

  • Missing values: Are there many? If so, you might need to abandon plans to use that predictor, or conduct 2 analyses (one on the subset where the predictor is present and one on the full dataset ignoring the predictor). A third option, multiple imputation, fills each missing value several times with plausible values predicted from the other variables and combines the results. Whichever option is chosen is recorded in the analysis log with its reason.
  • Distribution: For continuous variables, is there a reasonable representation over the whole range of values? If not, it might be necessary to categorise the variable. By this we mean that a model can describe the relationship only where there are data to support it. For example, if almost every participant smokes between 0 and 5 cigarettes a day and only a handful smoke more than 20, any estimate of what happens at high levels of smoking rests on those few people. Grouping the values into categories such as none, light and heavy keeps each part of the comparison supported by a reasonable number of participants.
  • Categorical variables: Are all categories reasonably well represented? If not, you might have to combine categories.

Worked Example 1.9 applies the first item, missing values: it tabulates the missing values in the raw blood pressure file and records the resulting decision in the analytic log; notice that every number in the log entry is one that R prints.

Worked Example 1.9: Tabulating missing values and logging the decision

The first question about any predictor is how much of it is missing. The two lines below give the count and the percentage of missing values for every variable in the raw blood pressure file. colMeans(is.na()) gives the proportion missing, because R counts each TRUE as 1 and each FALSE as 0.

Code
colSums(is.na(bp_raw)) # number missing per variable round(100 * colMeans(is.na(bp_raw)), 1) # percentage missing per variable
Console output
id age sex smoker bmi systolic 0 11 0 0 0 18 diastolic on_treatment 19 0 id age sex smoker bmi systolic 0.0 1.0 0.0 0.0 0.0 1.6 diastolic on_treatment 1.7 0.0

The decision that follows belongs in the analysis log in plain words. An entry for this file might read as follows.

Example analysis-log entry (written by the analyst)
2026-10-02 bp02.csv Missing data: age 11 (1.0%), systolic 18 (1.6%), diastolic 19 (1.7%); all other variables complete. Decision: complete-case analysis; 45 participants (4.1%) excluded, because each variable is missing for under 2% of participants. Revisit if a later model adds a variable with more missing values.

The analyst writes the log entry in plain words from the counts in the output above. The number of participants excluded and its percentage also come from R: sum(!complete.cases(bp_raw)) returns 45, and round(100 * mean(!complete.cases(bp_raw)), 1) returns 4.1.

The same discipline applies to a category that holds too few people. Worked Example 1.10 combines a sparse age band in the cleaned blood pressure data; watch the count of normotensive participants in the oldest band.

Worked Example 1.10: Combining a sparse category

The cross-tabulation below splits the cleaned blood pressure data into ten-year age bands and counts normotensive and hypertensive participants in each. The breaks c(17, 29, 39, ...) work because ages are whole numbers, so the band above 17 and up to 29 holds ages 18 to 29.

Code
# bp_clean comes from the recoding pipeline in the section on data coding bp_dec <- bp_clean |> mutate(age_dec = cut(age, c(17, 29, 39, 49, 59, 69, 79, Inf), labels = c("18-29", "30-39", "40-49", "50-59", "60-69", "70-79", "80+"))) table(bp_dec$age_dec, bp_dec$htn)
Console output: before
normotensive hypertensive 18-29 110 4 30-39 146 32 40-49 190 68 50-59 187 81 60-69 77 79 70-79 21 35 80+ 3 14
Code
# Merge the two oldest bands into 70+ bp_dec <- bp_dec |> mutate(age_dec6 = cut(age, c(17, 29, 39, 49, 59, 69, Inf), labels = c("18-29", "30-39", "40-49", "50-59", "60-69", "70+"))) table(bp_dec$age_dec6, bp_dec$htn)
Console output: after
normotensive hypertensive 18-29 110 4 30-39 146 32 40-49 190 68 50-59 187 81 60-69 77 79 70+ 24 49

Reading the output. Before merging, the 80+ band holds only 17 participants, and only 3 of them are normotensive. A logistic model that compares each band with a reference band would estimate the 80+ effect from those 3 people, which gives a very wide confidence interval: with 50–59 as the reference band, the odds of hypertension in the 80+ band are estimated at about 11 times the odds in the reference band, with a 95% confidence interval from about 3 to more than 38. A band with no normotensive participants at all would prevent the model from converging (the fitting procedure cannot settle on an answer). Merging 70–79 and 80+ into 70+ gives 24 normotensive and 49 hypertensive participants, enough for a stable estimate. The 18–29 band has only 4 hypertensive participants and might likewise be merged with 30–39. Each merge is a judgement made before the outcome model is fitted, and it is recorded in the log.

Scaling and recoding steps belong to this stage as well. Centring age (age_ct) and grouping it into bands (age_c3), for example, are made in the cleaning script and recorded in the codebook, as in Section 2. A documented choice about a predictor is defensible, whereas a predictor that is dropped, or whose missing values are filled with the mean, without saying so cannot be judged by a reader. Some datasets also hold measurements grouped within people or centres, which raises a different question.

7.3 Recognising Multilevel Data

If your data are multilevel (e.g., blood pressure measurements within individuals within centres), evaluate the hierarchical structure before any modelling. The questions to ask, and the reason they matter, are set out below.

Key Questions for Multilevel Data

What is the average (and range) number of observations at one level in each higher-level unit? Are individuals uniquely identified within a hierarchical level? It is often useful to create one unique identifier for each observation in the dataset.

Why this matters: observations from the same person, or the same centre, resemble one another more than observations drawn at random, so a model that ignores the clustering behaves as if it holds more independent information than it really does. For example, three blood pressure readings from the same person are likely to be similar, because they share that person’s genes, diet and medication; they tell the analyst less than three readings from three different people would. A model that treats every row as an independent person counts the same information several times. The result is standard errors that are too small and confidence intervals that are too narrow. Mixed models are built for exactly this structure.

Worked Example 1.11 stores a small multilevel dataset in long format and answers the first two questions with three counts; watch the number of measurements per person and the number of people per centre.

Worked Example 1.11: A long-format multilevel table in R

Multilevel data are usually stored in long format, with one row per measurement and columns that identify the person and the centre. The small table below, typed into R for illustration with tribble(), holds ten blood pressure measurements on five people in two centres.

Code
# Long format: one row per measurement (illustrative data typed into R) bp_long <- tribble( ~centre, ~person, ~visit, ~sbp, "A", "A01", 1, 132, "A", "A01", 2, 128, "A", "A02", 1, 141, "A", "A02", 2, 139, "A", "A02", 3, 137, "B", "B01", 1, 118, "B", "B01", 2, 121, "B", "B02", 1, 150, "B", "B03", 1, 126, "B", "B03", 2, 124 )

Three counts characterise the hierarchy. count(bp_long, centre, person) counts the rows for each combination of centre and person, which is the number of measurements per person; distinct() keeps one row per person, so counting those rows by centre gives the number of people per centre; and n_distinct() counts the different person identifiers.

Code
count(bp_long, centre, person) # measurements per person bp_long |> distinct(centre, person) |> count(centre) # people per centre n_distinct(bp_long$person) # people overall
Console output
# A tibble: 5 × 3 centre person n <chr> <chr> <int> 1 A A01 2 2 A A02 3 3 B B01 2 4 B B02 1 5 B B03 2 # A tibble: 2 × 2 centre n <chr> <int> 1 A 2 2 B 3 [1] 5

Reading the output. People have between one and three measurements, centre A has two people and centre B three, and there are five people in total. Here every person identifier begins with the centre letter, so each person is uniquely identified across centres. If two centres had both numbered their patients 01, 02, 03, the analyst would build a unique identifier first, for example with mutate(uid = paste(centre, person)), so that no merging step could confuse two people.

With the structure of the data established, the analysis can take its first look at the associations within the data.

7.4 Unconditional Associations

Before proceeding with any multivariable analyses, it is important to evaluate unconditional associations within the data, that is, the crude one-predictor-at-a-time relationships examined with nothing else held constant. By this we mean that each predictor is paired with the outcome on its own, for example smoking with hypertension, or age with systolic pressure, without adjusting for any other variable. These crude associations are not the final answer, because they may still be confounded. They serve, however, as the foundation for building more complex models. They show the shape of each relationship, they reveal predictors that are so strongly related to each other that they carry the same information, and they provide a baseline that the multivariable estimates can later be compared with.

7.4.1 Choosing the Method by Variable Type

The method depends on the types of the two variables. Table 1.11 matches each pair of variable types with an analytical approach.

Table 1.11. Analytical approaches for unconditional associations, by the types of the two variables.

Variable TypesAnalytical Approach
Two continuous variablesCorrelation coefficient, scatterplot, simple linear regression
One continuous + one categoricalOne-way ANOVA, simple linear or logistic regression
Two categorical variablesCross-tabulation and χ² test

In the middle row of Table 1.11, the choice depends on which variable is the outcome. When the continuous variable is the outcome, as when systolic pressure is compared across smoking groups, one-way ANOVA or a simple linear regression compares the group means, and the two methods give the same overall p-value for any number of groups. When the categorical variable is the outcome and has two categories, as when hypertension is related to age, simple logistic regression is used. Worked Examples 1.12, 1.13 and 1.14 run one example of each row on the cleaned blood pressure data (bp_clean from the recoding pipeline in Part 6 of Activity 1.2; in a new R session, that script is run first).

Worked Example 1.12 covers the first row, two continuous variables. Watch how the correlation coefficient and the regression slope describe the same relationship in different units.

Worked Example 1.12: Two continuous variables (age and systolic pressure)

The correlation coefficient r summarises how closely two continuous variables follow a straight line. cor() computes it, and cor.test() adds a 95% confidence interval and a p-value.

Code
cor(bp_clean$age, bp_clean$systolic) # correlation coefficient r cor.test(bp_clean$age, bp_clean$systolic) # adds a 95% CI and a p-value
Console output
[1] 0.4759709 Pearson's product-moment correlation data: bp_clean$age and bp_clean$systolic t = 17.495, df = 1045, p-value < 2.2e-16 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: 0.4277199 0.5215171 sample estimates: cor 0.4759709

The scatterplot shows the shape of the relationship, and a simple linear regression puts a number on the slope. confint() gives 95% confidence intervals for the coefficients.

Code
plot(systolic ~ age, data = bp_clean) # scatterplot fit1 <- lm(systolic ~ age, data = bp_clean) # simple linear regression coef(fit1) confint(fit1) # 95% CIs for the coefficients
Console output
(Intercept) age 99.3509023 0.5851592 2.5 % 97.5 % (Intercept) 96.0561027 102.6457019 age 0.5195291 0.6507894

Reading the output. The correlation is r = 0.48: systolic pressure tends to be higher in older participants, with considerable scatter. As a rough guide, values of |r| below about 0.3 are described as weak, values from about 0.3 to 0.7 as moderate, and values above about 0.7 as strong; the labels are conventions, and the scatterplot should always be inspected as well. In the cor.test() output, the 95% confidence interval for r runs from 0.43 to 0.52, and the p-value (< 2.2e-16) means that a correlation this large would almost never arise by chance if age and systolic pressure were unrelated; t and df are the test statistic and its degrees of freedom. The regression slope for age is 0.585: on average, systolic pressure is 0.59 mmHg higher for each additional year of age (95% CI 0.52 to 0.65). The intercept, 99.4 mmHg, is the predicted value at age 0, which lies far outside the data and has no practical meaning; centred age would give an intercept for a participant of average age.

A model reporting sentence. “Systolic blood pressure was moderately correlated with age (r = 0.48, 95% CI 0.43 to 0.52); in a simple linear regression, each additional year of age was associated with a 0.59 mmHg higher systolic pressure (95% CI 0.52 to 0.65), before adjustment for other variables.”

A correlation coefficient is easier to judge beside a picture. Figure 1.7 sets the moderate correlation for age beside a weak one for BMI and a strong one for diastolic pressure, all in the cleaned blood pressure data.

Three scatterplots of systolic blood pressure. A: against BMI, a wide cloud with a shallow line, r = 0.21. B: against age, an upward trend with much scatter, r = 0.48. C: against diastolic pressure, a tight band around a steep line, r = 0.84.
Figure 1.7. Weak, moderate, and strong correlations in the cleaned blood pressure data, each with its fitted straight line. A. BMI: a wide cloud and a shallow line (r = 0.21). B. Age: a clear upward trend with much scatter (r = 0.48). C. Diastolic pressure: points packed tightly around the line (r = 0.84). A scatterplot also shows whether a relationship is straight or curved, which r alone cannot show.

Worked Example 1.13 covers the middle row, a continuous outcome compared across groups, first for two groups and then for three. Notice that the regression coefficient for smokers equals the difference between the two group means.

Worked Example 1.13: One continuous and one categorical variable (systolic pressure by group)

For two groups, the group means and a simple linear regression answer the question together. tapply(x, group, mean) applies mean() to x separately within each group.

Code
table(bp_clean$smoker) # group sizes tapply(bp_clean$systolic, bp_clean$smoker, mean) # mean systolic by group fit2 <- lm(systolic ~ smoker, data = bp_clean) coef(fit2) confint(fit2)
Console output
No Yes 811 236 No Yes 126.3070 131.4534 (Intercept) smokerYes 126.307028 5.146361 2.5 % 97.5 % (Intercept) 125.082524 127.531532 smokerYes 2.567206 7.725517

For three or more groups, one-way ANOVA tests whether the group means differ. The example uses the three age bands created earlier.

Code
# Three or more groups: one-way ANOVA across the age bands tapply(bp_clean$systolic, bp_clean$age_c3, mean) summary(aov(systolic ~ age_c3, data = bp_clean))
Console output
young middle older 115.5571 126.7930 136.8133 Df Sum Sq Mean Sq F value Pr(>F) age_c3 2 58901 29450 111.4 <2e-16 *** Residuals 1044 275972 264 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Reading the output. Mean systolic pressure is 126.3 mmHg among the 811 non-smokers and 131.5 mmHg among the 236 smokers. In the regression, the intercept (126.3) is the mean for the reference group, non-smokers, because No is the first level of smoker, and the coefficient smokerYes (5.15) is the difference between the two group means, with a 95% CI of 2.57 to 7.73 mmHg that excludes zero. In the ANOVA table, Df gives the degrees of freedom (number of groups minus 1, and number of people minus number of groups), Sum Sq and Mean Sq measure the variation between and within the groups, F value is the ratio of between-group to within-group variation, and Pr(>F) is the p-value. An F of 111.4 with p < 2e-16 indicates that mean systolic pressure differs across the age bands; ANOVA does not identify which bands differ, so the group means (115.6, 126.8, and 136.8 mmHg) are reported alongside it.

A model reporting sentence. “Mean systolic pressure was 131.5 mmHg among smokers and 126.3 mmHg among non-smokers, an unadjusted difference of 5.1 mmHg (95% CI 2.6 to 7.7). Mean systolic pressure also differed across age bands (one-way ANOVA, F = 111.4 on 2 and 1044 degrees of freedom, p < 0.001), rising from 115.6 mmHg in participants aged 35 or younger to 136.8 mmHg in those older than 55.”

Worked Example 1.14 covers the last row, two categorical variables. Check the expected counts as well as the p-value, because they decide whether the chi-squared test can be used.

Worked Example 1.14: Two categorical variables (smoking and hypertension)

A cross-tabulation counts the participants in every combination of two categorical variables, and row percentages make the groups comparable. prop.table(tab, margin = 1) divides each count by its row total.

Code
tab <- table(smoker = bp_clean$smoker, htn = bp_clean$htn) tab # counts round(100 * prop.table(tab, margin = 1), 1) # row percentages
Console output
htn smoker normotensive hypertensive No 581 230 Yes 153 83 htn smoker normotensive hypertensive No 71.6 28.4 Yes 64.8 35.2

The chi-squared test asks whether the pattern of counts differs from what would be expected if the two variables were unrelated. The expected count for a cell is the number of people who would fall in it if smoking made no difference to hypertension. By this we mean that the overall percentage with hypertension is applied to smokers and non-smokers alike, so each group is given the same share of hypertensive participants. The test then adds up how far each observed count lies from its expected count; the further apart they are, the larger the chi-squared statistic and the smaller the p-value. The expected counts are worth printing, because the test relies on an approximation that becomes unreliable when any expected count is small.

Code
chisq.test(tab) # chi-squared test chisq.test(tab)$expected # expected counts if unrelated
Console output
Pearson's Chi-squared test with Yates' continuity correction data: tab X-squared = 3.7261, df = 1, p-value = 0.05357 htn smoker normotensive hypertensive No 568.5521 242.44795 Yes 165.4479 70.55205

Reading the output. Hypertension is more common among smokers (35.2%, 83 of 236) than among non-smokers (28.4%, 230 of 811). The chi-squared statistic is 3.73 on 1 degree of freedom with p = 0.054. R applies a small correction (Yates’ continuity correction) automatically for a table with two rows and two columns. A p-value of 0.054 indicates weak evidence against no association: the data are somewhat unusual under no association, and a difference at least this large would arise in about 5 of every 100 samples of this size if there were no association. The crude difference will also change once age, a likely confounder, is taken into account. All expected counts are far above 5, so the test is appropriate here; when any expected count falls below 5, Fisher’s exact test, fisher.test(tab), is used instead.

A model reporting sentence. “Hypertension was more common among smokers (35.2%) than among non-smokers (28.4%), although the evidence for an unadjusted association was weak (chi-squared test, p = 0.054).”

All three examples report crude estimates, labelled as unadjusted, because nothing else is held constant. Their value lies in what they reveal before the multivariable model is planned.

7.4.2 What the Unconditional Associations Show

When evaluating unconditional associations, pay attention to three kinds of association:

  • Associations between predictors and outcome: Determine if there is any association at all; determine the functional form (is it linear?); get a simple picture of the strength and direction. The functional form is the shape of the relationship. By this we mean whether the outcome changes by the same amount across the whole range of the predictor, as a straight line implies, or whether the relationship curves, levels off or changes direction. For example, a scatterplot may show that an outcome rises steeply at older ages but barely changes among younger adults, which would call for a squared term or a categorised version of the predictor in the later model.
  • Associations between pairs of predictors: Look for potential collinearity problems (highly correlated predictors). Collinearity means that two predictors move together so closely that they carry much the same information. When both are placed in the same model, the model has great difficulty separating the effect of one from the effect of the other, and as a result their coefficients become unstable and their standard errors large. For example, systolic and diastolic pressure are strongly correlated, so a model that includes both as predictors may give an uncertain estimate for each.
  • Confounding variables: Evaluate associations between the confounders identified in the causal diagram and the key predictors of interest and the outcome. These associations show how strongly each confounder could distort the crude comparison; the decision to adjust for a confounder rests on the diagram.

Collinearity between predictors is checked for all the continuous variables at once. Worked Example 1.15 does this with a correlation matrix; look for any pair whose correlation crosses the rule-of-thumb threshold.

Worked Example 1.15: Checking predictors for collinearity with a correlation matrix

A correlation matrix shows the correlation between every pair of continuous variables at once. cor() applied to several columns returns the matrix, and round(..., 2) keeps two decimal places.

Code
round(cor(bp_clean[, c("age", "bmi", "systolic", "diastolic")]), 2)
Console output
age bmi systolic diastolic age 1.00 0.00 0.48 0.41 bmi 0.00 1.00 0.21 0.15 systolic 0.48 0.21 1.00 0.84 diastolic 0.41 0.15 0.84 1.00

Reading the output. Each cell is the correlation between the row variable and the column variable, and the diagonal is always 1. A common rule of thumb flags pairs with |r| above about 0.8 as possible collinearity problems. Systolic and diastolic pressure (r = 0.84) cross that line, which is expected because both measure blood pressure; a model would normally include one of them, or a combined measure, as a predictor. The correlation matrix helps detect collinearity, whereas the choice of confounders to include in a model comes from the causal diagram.

The unconditional analyses feed directly into the plan for the multivariable model. Table 1.12 lists common findings from these analyses and the next step each one calls for.

Table 1.12. Findings from the unconditional analyses and the next step each one calls for in planning the multivariable model.

FindingWhat to do next
A confounder from the DAG shows little crude association with the outcome.The confounder stays in the planned model, because the decision to adjust rests on the causal diagram.
A scatterplot shows a curve.The analyst plans a squared term (such as age_ctsq) or a categorised version (such as age_c3) for that predictor.
Two predictors have |r| above about 0.8.The analyst treats them as candidates for collinearity and keeps the one that best matches the research question, or combines them.
A cross-tabulation has an expected count below 5.The analyst uses Fisher’s exact test or combines sparse categories before modelling.
An association is far stronger or weaker than subject-matter knowledge suggests.The analyst rechecks the coding and verification of both variables before interpreting it.

Activity 1.4 runs the code of Worked Examples 1.7 to 1.15 on the blood pressure data in one narrated walkthrough, with three questions in the form used on the midterm. Run it along with the reading, then answer the questions from your own output.

RActivity 1.4: processing variables and unconditional associations

Text version of the activity: the code of the run-along script, its console output and notes on reading it.

library(dplyr)
library(tidyr)
bp_raw <- read.csv("https://heal-learn.ca/r-activities/data/bp01.csv")   # course data, read from the website
bp_clean <- bp_raw |>
  drop_na(systolic, diastolic, age) |>
  mutate(age_c3 = cut(age, c(0, 35, 55, Inf), labels = c("young", "middle", "older")),
         htn = factor(systolic >= 140 | diastolic >= 90, levels = c(FALSE, TRUE),
                      labels = c("normotensive", "hypertensive")))
c(raw = nrow(bp_raw), clean = nrow(bp_clean))
Console output
Attaching package: 'dplyr' The following objects are masked from 'package:stats': filter, lag The following objects are masked from 'package:base': intersect, setdiff, setequal, union raw clean 1092 1047

The raw file has 1092 participants, and the cleaned file keeps the 1047 with age, systolic and diastolic pressure recorded. It adds the three age bands age_c3 and the hypertension indicator htn (140/90 rule), as the recoding pipeline in Part 6 of Activity 1.2 does.

visits_a <- c(0, 2, 4, 3, 2, 6, 1, 3, 2, 3)    # clinic visits in a year, clinic A
visits_b <- c(0, 1, 1, 0, 9, 2, 0, 12, 1, 0)   # clinic visits in a year, clinic B
c(mean = mean(visits_a), var = var(visits_a))
c(mean = mean(visits_b), var = var(visits_b))
set.seed(4104)
los <- round(rlnorm(1000, meanlog = 1.2, sdlog = 0.8), 1)   # simulated length of stay (days)
summary(los)                     # before the log transformation
summary(log(los))                # after it
par(mfrow = c(1, 2)); hist(los, breaks = 40); hist(log(los), breaks = 30)
Console output
mean var 2.600000 2.711111 mean var 2.60000 18.26667 Min. 1st Qu. Median Mean 3rd Qu. Max. 0.300 1.900 3.400 4.568 5.625 40.700 Min. 1st Qu. Median Mean 3rd Qu. Max. -1.2040 0.6419 1.2238 1.1902 1.7272 3.7062
Left: histogram of simulated length of stay in days with a tall peak near 2 days and a long tail to 40 days. Right: histogram of the logarithm of length of stay, roughly symmetric.

Reading the output. Both clinics average 2.6 visits. In clinic A the variance (2.71) is close to the mean, as a Poisson model expects; in clinic B it is 18.27, far larger than the mean, which is overdispersion, so negative binomial regression is the safer starting point. For the simulated length of stay, the mean (4.57 days) is well above the median (3.40 days), a numerical sign of right skew; after log() the mean (1.19) and median (1.22) are close and the histogram is roughly symmetric. The logarithm is defined only above zero, so an outcome with zeros needs adjustment first, for example log(x + 1).

colSums(is.na(bp_raw))                         # number missing per variable
round(100 * colMeans(is.na(bp_raw)), 1)        # percentage missing per variable
bp_clean <- bp_clean |>
  mutate(age_dec = cut(age, c(17, 29, 39, 49, 59, 69, 79, Inf),
                       labels = c("18-29", "30-39", "40-49", "50-59", "60-69", "70-79", "80+")))
table(bp_clean$age_dec, bp_clean$htn)
bp_clean <- bp_clean |>
  mutate(age_dec6 = cut(age, c(17, 29, 39, 49, 59, 69, Inf),
                        labels = c("18-29", "30-39", "40-49", "50-59", "60-69", "70+")))
table(bp_clean$age_dec6, bp_clean$htn)
Console output
id age sex smoker bmi systolic 0 11 0 0 0 18 diastolic on_treatment 19 0 id age sex smoker bmi systolic 0.0 1.0 0.0 0.0 0.0 1.6 diastolic on_treatment 1.7 0.0 normotensive hypertensive 18-29 110 4 30-39 146 32 40-49 190 68 50-59 187 81 60-69 77 79 70-79 21 35 80+ 3 14 normotensive hypertensive 18-29 110 4 30-39 146 32 40-49 190 68 50-59 187 81 60-69 77 79 70+ 24 49

Reading the output. Age is missing for 11 participants (1.0%), systolic pressure for 18 (1.6%) and diastolic pressure for 19 (1.7%); because some participants miss more than one, the cleaned file (line 15) keeps 1047 of the 1092 participants. In the ten-year bands, 80+ has only 3 normotensive participants (and 14 hypertensive), so a logistic model would estimate that band from 3 people (odds ratio about 11 against 50 to 59, 95% CI from about 3 to more than 38). Merging 70 to 79 and 80+ into 70+ gives 24 normotensive and 49 hypertensive participants. Each decision is recorded in the analysis log with its numbers and its reason.

bp_long <- tribble(
  ~centre, ~person, ~visit, ~sbp,
  "A", "A01", 1, 132,   "A", "A01", 2, 128,   "A", "A02", 1, 141,
  "A", "A02", 2, 139,   "A", "A02", 3, 137,   "B", "B01", 1, 118,
  "B", "B01", 2, 121,   "B", "B02", 1, 150,   "B", "B03", 1, 126,
  "B", "B03", 2, 124)
count(bp_long, centre, person)                          # measurements per person
bp_long |> distinct(centre, person) |> count(centre)    # people per centre
n_distinct(bp_long$person)                              # people overall
Console output
# A tibble: 5 × 3 centre person n <chr> <chr> <int> 1 A A01 2 2 A A02 3 3 B B01 2 4 B B02 1 5 B B03 2 # A tibble: 2 × 2 centre n <chr> <int> 1 A 2 2 B 3 [1] 5

Reading the output. People have between one and three measurements, centre A has two people and centre B three, and there are five people in all. Every identifier begins with the centre letter, so each person is uniquely identified across centres; otherwise a unique identifier is built first, for example with mutate(uid = paste(centre, person)). Observations from the same person or centre resemble one another, so a model that ignores the clustering gives standard errors that are too small.

cor.test(bp_clean$age, bp_clean$systolic)               # two continuous variables
plot(systolic ~ age, data = bp_clean)                   # scatterplot
fit1 <- lm(systolic ~ age, data = bp_clean)
cbind(estimate = coef(fit1), confint(fit1))
tapply(bp_clean$systolic, bp_clean$smoker, mean)        # continuous by two groups
fit2 <- lm(systolic ~ smoker, data = bp_clean)
cbind(estimate = coef(fit2), confint(fit2))
tapply(bp_clean$systolic, bp_clean$age_c3, mean)        # three or more groups
summary(aov(systolic ~ age_c3, data = bp_clean))
tab <- table(smoker = bp_clean$smoker, htn = bp_clean$htn)   # two categorical
tab
round(100 * prop.table(tab, margin = 1), 1)              # row percentages
chisq.test(tab)
chisq.test(tab)$expected                                 # expected counts if unrelated
Console output
Pearson's product-moment correlation data: bp_clean$age and bp_clean$systolic t = 17.495, df = 1045, p-value < 2.2e-16 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: 0.4277199 0.5215171 sample estimates: cor 0.4759709 estimate 2.5 % 97.5 % (Intercept) 99.3509023 96.0561027 102.6457019 age 0.5851592 0.5195291 0.6507894 No Yes 126.3070 131.4534 estimate 2.5 % 97.5 % (Intercept) 126.307028 125.082524 127.531532 smokerYes 5.146361 2.567206 7.725517 young middle older 115.5571 126.7930 136.8133 Df Sum Sq Mean Sq F value Pr(>F) age_c3 2 58901 29450 111.4 <2e-16 *** Residuals 1044 275972 264 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 htn smoker normotensive hypertensive No 581 230 Yes 153 83 htn smoker normotensive hypertensive No 71.6 28.4 Yes 64.8 35.2 Pearson's Chi-squared test with Yates' continuity correction data: tab X-squared = 3.7261, df = 1, p-value = 0.05357 htn smoker normotensive hypertensive No 568.5521 242.44795 Yes 165.4479 70.55205
Three scatterplots of systolic blood pressure: against BMI (r = 0.21), against age (r = 0.48) and against diastolic pressure (r = 0.84).

Reading the output. Age and systolic pressure: r = 0.48 (95% CI 0.43 to 0.52), a moderate correlation, and a slope of 0.59 mmHg per year of age (95% CI 0.52 to 0.65). Smoking: mean systolic pressure 131.5 mmHg in smokers against 126.3 mmHg in non-smokers, a difference of 5.15 mmHg (95% CI 2.57 to 7.73). Age bands: means 115.6, 126.8 and 136.8 mmHg, F = 111.4 on 2 and 1044 degrees of freedom, p < 2e-16. Smoking and hypertension: 35.2% against 28.4%, chi-squared 3.73 on 1 degree of freedom, p = 0.054, with every expected count far above 5 (otherwise fisher.test(tab)). All of these are unadjusted. Question 3 adds round(cor(bp_clean[, c("age", "bmi", "systolic", "diastolic")]), 2), the correlation matrix used to check pairs of predictors for collinearity.

Files for this activity Data Code Answer key

R Reflect on what you just ran

Answer each question from your own output, as on the midterm: justify each choice with evidence from the output, decide what to do about each problem and write the rule you would record in the analysis log, and say whether a variable should stay in the model and why.

1. For each of the three outcomes in part 2 of the script (clinic visits at clinic A, clinic visits at clinic B and hospital length of stay), state the model or processing step you would choose, and justify each choice with evidence from the output. Then state in one sentence what the log transformation did to the shape of the length-of-stay distribution.

Model answerClinic A: a Poisson model suits these counts, because the variance (2.71) is close to the mean (2.6), which is what the Poisson model assumes. Clinic B: the variance (18.27) is far larger than the mean (2.6), which is overdispersion; a Poisson model would give standard errors that are too small and p-values that look stronger than the data warrant, so negative binomial regression is the safer starting point. Length of stay: the mean (4.57 days) is well above the median (3.40 days) and the values run to 40.7 days, so the outcome is right-skewed, with a long right tail, and unlikely to give normal residuals in a linear regression; I would analyse log(los) instead. After the log transformation the mean (1.19) and the median (1.22) are close and the histogram is roughly symmetric, so the logarithm pulled in the long right tail; the new variable is created in the cleaning script and recorded in the codebook, and the residuals are still checked after the model is fitted.

2. The output of lines 29 to 38 reveals two problems in the predictors: one in the missing-value counts and one in the cross-tabulation by ten-year age band. Identify the two problems, state for each whether you would keep, combine or exclude the affected values, and write the entry you would record in the analysis log.

Model answerProblem 1 is missing values: age is missing for 11 participants (1.0%), systolic pressure for 18 (1.6%) and diastolic pressure for 19 (1.7%), and because some participants miss more than one, the cleaned file on line 15 keeps 1047 of the 1092 participants. Decision: exclude the participants with a missing value in a complete-case analysis, because each variable is missing for under 2% of participants; with many more missing values, the options would be to drop the predictor, run two analyses (one with it on the subset where it is present, one without it on the full dataset) or use multiple imputation. Log entry: “2026-10-02, bp02.csv: missing age 11 (1.0%), systolic 18 (1.6%), diastolic 19 (1.7%); complete-case analysis, 1047 of 1092 participants kept, because each variable is missing for under 2%; revisit if a later model adds a variable with more missing values.” Problem 2 is a sparse category: only 3 participants in the 80+ band are normotensive (against 14 hypertensive), so a logistic model would estimate that band from 3 people, with a very wide confidence interval (an odds ratio of about 11, 95% CI from about 3 to more than 38), and a band with no normotensive participants would stop the model from converging. Decision: combine the 70 to 79 and 80+ bands into 70+ (24 normotensive and 49 hypertensive), keeping every participant, before the outcome model is fitted; deleting the 80+ participants would discard data. Log entry: “age_dec: 80+ (3 normotensive, 14 hypertensive) merged with 70 to 79 into 70+ (24 normotensive, 49 hypertensive), because the band was too sparse for a stable estimate; decided before the outcome model was fitted.”

3. Run your own code: at the end of your script, type and run round(cor(bp_clean[, c("age", "bmi", "systolic", "diastolic")]), 2), then tapply(bp_clean$age, bp_clean$smoker, mean), then coef(lm(systolic ~ smoker + age, data = bp_clean)). (a) Identify the problem the correlation matrix reveals for a model that would include these variables as predictors, and state what you would do about it. (b) The causal diagram in Worked Example 1.1 names age as a confounder of the effect of smoking. A colleague notes that smokers and non-smokers differ in mean age by only about two years and proposes to leave age out of the model. Based on these results and the theory driving the analysis, should age be removed? Why or why not?

Model answerThe matrix shows that systolic and diastolic pressure correlate at r = 0.84, above the rule-of-thumb threshold of about 0.8, so they are candidates for collinearity; every other pair is below 0.5 (age with systolic 0.48, age with diastolic 0.41, BMI 0.21 or less). If both were entered as predictors, their standard errors would be inflated and their estimates imprecise, though not biased, so I would keep the one that best matches the research question or combine them into one blood pressure measure. No, age should not be removed: it is a confounder in the causal diagram (a common cause of smoking and blood pressure), and confounders are retained whatever their crude association, because the decision to adjust rests on the diagram, not on the unconditional result. Even a small difference matters here: tapply() shows that smokers are younger on average (46.3 against 48.6 years), and systolic pressure is about 0.59 mmHg higher per year of age, so leaving age out keeps the back-door path through age open and biases the smoking comparison. Adjusting for age moves the smoking difference from 5.15 mmHg (crude) to 6.48 mmHg, so the crude estimate understated it; the results table shows both, with the crude one labelled as unadjusted.
Saved.

Your output from Activity 1.4 takes the blood pressure data from processed variables to the crude associations that the multivariable model will build on, with each decision recorded in the log. The key takeaways below draw the section together before its knowledge check.

Key Takeaways

  • 5.5 Before the serious modelling begins, set up a system for keeping track of what you run, because you will need to reconstruct every result and decision later. Work in blocks (all the single-variable summaries, then the two-variable associations, then the models), keep log files named after the scripts that produced them (for example, a Quarto or R Markdown report, or output saved with sink()), and label and date every output. Record each decision with its reason, as the analysis-log entry in Worked Example 1.9 does for the complete-case decision and the 45 participants it excluded. A good test is whether you, three months from now, could trace any number in your report back to the script, the file version and the decision that produced it.
  • 7.1 Before modelling, check that each outcome suits the analysis you have planned, because the type of outcome sets the type of model. For a categorical outcome, check the count in each category and combine categories that hold only a handful of people, for example before a multinomial model. For a continuous outcome planned for linear regression, look at a histogram, and if it is strongly skewed, try a transformation such as the logarithm (log()); a mean well above the median is a quick numerical clue to right skew, and the residuals are checked later, after the model is fitted. For a count, compare the mean with the variance using mean() and var(): when the variance is well above the mean (overdispersion, as in clinic B of Worked Example 1.7), negative binomial regression is a safer starting point than Poisson regression.
  • 7.2 Predictors get the same careful review, one at a time. First count the missing values, for example with colSums(is.na()); if a predictor has many, you can drop it, run two analyses (one on the subset where it is present and one on the full dataset without it), or use multiple imputation, and whichever you choose goes in the analysis log with its reason. Then check that a continuous predictor is reasonably spread across its range (if it is not, it may need to be categorised) and that every category of a categorical predictor holds enough people, combining sparse categories, as the 70–79 and 80+ age bands were combined in Worked Example 1.10, so that estimates are stable. Scaling and recoding steps, such as centring age to make age_ct, are made in the cleaning script and recorded in the codebook. A common trap is to drop a predictor, or fill its missing values with the mean, without saying so: a documented choice is defensible, and a silent one is not.
  • 7.3 Some datasets have a multilevel structure, with observations grouped inside larger units, such as repeated blood pressure measurements within people, or people within centres. Before modelling, find the average and range of the number of observations per higher-level unit (with count() and n_distinct(), as in Worked Example 1.11) and make sure every individual has a unique identifier; if two centres both number their patients 01, 02, 03, build one first, for example with mutate(uid = paste(centre, person)). This matters because observations from the same person or centre resemble each other, so a model that ignores the clustering behaves as if it holds more independent information than it does, and its standard errors come out too small and its confidence intervals too narrow. Mixed models are built for this structure. In a question, clues such as repeated visits, several clinics, or one identifier appearing on several rows tell you that the data are multilevel.
  • 7.4 Before any multivariable model, look at the unconditional (crude) associations, one predictor at a time with nothing else held constant. The method depends on the two variable types: for two continuous variables, a scatterplot, a correlation coefficient (cor() or cor.test()) and a simple linear regression; for one continuous and one categorical variable, group means with a one-way ANOVA (aov()) or a simple regression, or a simple logistic regression when the categorical variable is a binary outcome; and for two categorical variables, a cross-tabulation with row percentages and a chi-squared test (chisq.test()), with Fisher’s exact test when an expected count is below 5. These first looks show whether each association exists, its direction and rough strength, and whether it is straight or curved; a correlation matrix flags pairs of predictors with |r| above about 0.8 as possible collinearity; and the associations of each confounder with the exposure and the outcome show how much it could distort the crude comparison. A common trap is to drop a confounder from the DAG because its crude association is weak, when the decision to adjust rests on the diagram, not on the unconditional result. When you report one of these results, give the estimate with its 95% confidence interval and say that it is unadjusted, as in “each additional year of age was associated with a 0.59 mmHg higher systolic pressure (95% CI 0.52 to 0.65), before adjustment for other variables”.
Knowledge check: this section

1. An analyst plans a Poisson regression for the number of emergency department visits in a year. For a pilot sample of eight patients, the processing check gives the output below. What does it show?

> ed_visits <- c(0, 1, 0, 2, 0, 9, 1, 3)
> mean(ed_visits)
[1] 2
> var(ed_visits)
[1] 9.142857
> var(ed_visits) / mean(ed_visits)
[1] 4.571429
The check compares the mean and variance with each other, not with some idea of small. Here the variance is several times the mean, which is overdispersion.
Log-transforming counts fails when some counts are 0, as here, because the logarithm of 0 is undefined. A count model that allows extra variance is the better choice.
A Poisson model assumes the mean and variance of the count are about equal. A variance several times the mean is overdispersion, which makes Poisson standard errors too small, so negative binomial regression is the safer start.
A few large counts are not the issue; the issue is whether variance and mean are close. When the variance greatly exceeds the mean, the Poisson assumption fails.
The correct answer is (c). The information needed is the Poisson assumption that the mean and the variance of a count are about equal, and the three numbers R prints. The variance is 9.14 against a mean of 2, and the last line shows that it is about 4.6 times the mean, the pattern called overdispersion, which would make Poisson standard errors too small; as with clinic B in Worked Example 1.7, a negative binomial model is the safer starting point. Option (a) is tempting because both numbers look small, but the check compares them with each other, and here one is more than four times the other; option (b) fails because the logarithm of 0 is undefined and three of the counts are 0. (Section 4, slide “Matching the variable to the model class”, 7.1.1 Outcome Types and Their Checks, Table 1.9 and Worked Example 1.7; Objective 7.1)

2. A logistic regression of diabetes (yes or no) on education is planned. The cross-tabulation of the predictor with the outcome is shown below. What should be done before the model is fitted?

EducationNo diabetesDiabetes
Less than high school92
High school21041
College38852
University30127
A category with so few people gives an unstable estimate and can stop a logistic model from converging. Merging it with the neighbouring category and logging the decision before modelling follows the lesson's approach.
Deleting people discards data and changes who the results describe. The usual remedy for a sparse category is to merge it with a sensible neighbour.
A wide confidence interval signals the problem but does not fix it; the estimate itself stays unreliable, and a sparse cell can stop the model converging.
Merging should combine neighbouring categories that make sense together. Joining the lowest and highest education levels creates a group with no clear meaning.
The correct answer is (a). The information needed is the count in every cell: the lowest education category holds only 9 people without and 2 with diabetes. A thinly populated category gives an unstable estimate with a very wide confidence interval, and a cell of zero can stop a logistic model from converging, so the usual fix is to merge it with a neighbouring category that makes sense, here high school, as the 70–79 and 80+ age bands are merged in Worked Example 1.10, and to log the decision before the outcome model is fitted. Option (c) is tempting because the wide interval does signal the problem, but the estimate itself remains unreliable; option (d) merges categories that are not neighbours, which produces a group with no clear meaning, and option (b) discards data. (Section 4, slide “Missing values, distributions, and sparse categories”, 7.2 Processing the Predictor Variables and Worked Example 1.10; Objective 7.2)

3. A file holds three HbA1c measurements for each of 400 patients, and the patients attend 12 clinics, so the file has 1,200 rows. A colleague proposes to analyse the rows as 1,200 independent observations. What is the likely consequence, and what should be checked first?

Ignoring clustering mainly affects the standard errors, not just the estimates, and the lesson says the hierarchy should be checked before modelling. No check is a mistake.
This reverses the error: treating clustered rows as independent makes standard errors too small, not too large. Dropping the clinic variable also removes information about the hierarchy.
Repeated measurements on one person carry less independent information than separate people. The apparent extra power from 1,200 rows is the false precision that clustering creates.
Measurements within patients and patients within clinics resemble one another, so ignoring this shrinks standard errors and overstates evidence. Counting measurements per patient and patients per clinic, and checking unique identifiers, characterises the hierarchy first.
The correct answer is (d). The information needed is the structure of the file: measurements within patients within clinics. Three measurements on one person carry less independent information than one measurement on each of three people, so a model that ignores the clustering behaves as if it had more information than it does, which shrinks its standard errors; before modelling, the analyst characterises the hierarchy with count() and n_distinct() (here 3 measurements per patient, and the number of patients in each of the 12 clinics) and makes sure that no two patients share an identifier. Option (c) is tempting because more rows usually mean more power, but that apparent extra power is exactly the false precision the clustering creates, and option (b) reverses the direction of the error. (Section 4, slide “Characterise the hierarchy before modelling”, 7.3 Recognising Multilevel Data and Worked Example 1.11; Objective 7.3)

4. Before fitting a multivariable model, an analyst wants the unconditional association for each of three pairs: (i) BMI (kg/m²) and systolic pressure (mmHg); (ii) systolic pressure across three income groups; and (iii) smoking (yes or no) and hypertension (yes or no). Which set of methods matches the variable types?

Every method is mismatched to its variable types: a chi-squared test needs two categorical variables, a correlation needs two continuous ones, and ANOVA compares a continuous variable across groups.
Each method matches the variable types: two continuous variables call for a scatterplot, correlation and simple regression; a continuous variable across groups for group means and one-way ANOVA; and two categorical variables for a cross-tabulation with chi-squared.
Part (i) is right, but a chi-squared test needs two categorical variables, so it cannot compare mean systolic pressure across groups, and two yes or no variables call for a cross-tabulation rather than a correlation of codes.
An ANOVA is for a continuous variable across groups, not two continuous variables, and adjusting for age in (iii) means the result is no longer unconditional, which defeats the purpose of this step.
The correct answer is (b). The information needed is the type of each variable: BMI and systolic pressure are both continuous; systolic pressure is continuous and income group is categorical with three levels; smoking and hypertension are both categorical. Two continuous variables call for a scatterplot (which shows the shape), a correlation and a simple regression; a continuous variable across groups calls for the group means with a one-way ANOVA (or the equivalent simple regression); and two categorical variables call for a cross-tabulation with a chi-squared test, checking that no expected count is below 5. Option (c) is tempting because it gets (i) right, but a chi-squared test needs two categorical variables, so it cannot compare mean systolic pressure across groups; option (d) adjusts for age, so its result is no longer unconditional. (Section 4, slide “One predictor at a time, before the multivariable model”, 7.4.1 Choosing the Method by Variable Type, Table 1.11 and Worked Examples 1.12 to 1.14; Objective 7.4)

5. During the unconditional analyses, an analyst merges the two oldest age bands into one and decides on a complete-case analysis for income, which is missing for 36 of 1,200 participants. Which record of these decisions meets the standard for keeping track of analyses?

The script shows what was done but not why, nor what the counts were when the decision was made. The analytic log adds the reasons and numbers needed to reconstruct each decision later.
Writing from memory after all models are fitted loses the details and dates of each decision. The log is kept as the analysis runs, not reconstructed at the end.
Undated, undescribed output cannot show when a decision was made or what it was based on. Every output is labelled and dated as it is produced.
A dated log entry giving each decision with its numbers and reason lets the analyst, a co-author or a reviewer reconstruct and revisit the choices months later.
The correct answer is (d). The information needed is the rule that analyses are run in blocks, that every output is labelled and dated, and that the log records what each run produced and what it decided. A dated entry with the counts (36 of 1,200 participants, 3.0%) and the reason lets the analyst, a co-author or a reviewer reconstruct every decision months later and see when it should be revisited. Option (a) is tempting because the script is the record of what was done, but the code shows the merge without saying why it was made or what the counts were at the time; options (b) and (c) rely on memory or on undated output, which is exactly what a log replaces. (Section 4, slide “The analytic log and the road to modelling”, 5.5 Keeping an Analytic Log and Worked Example 1.9; Objective 5.5)

6. A school health team will model the number of school days missed by 600 students, with asthma as the exposure and household income and household smoking as confounders in the causal diagram. The missing-value check is shown below. A colleague suggests dropping income from the analysis and not mentioning it in the report. Which response follows the lesson?

> colSums(is.na(sch))
days_missed      asthma      income    hh_smoke
          0           0         162           0
> round(100 * colMeans(is.na(sch)), 1)
days_missed      asthma      income    hh_smoke
          0           0          27           0
> sum(!is.na(sch$income))
[1] 438
No fixed share of missing values makes dropping a confounder automatic, and silent decisions cannot be judged by readers. Income comes from the causal diagram, so leaving it out has a cost that must be stated.
Filling gaps with the mean treats invented values as real, and hiding the replacement conceals the problem. Whatever option is chosen must be recorded and reported.
Income is a confounder from the diagram, so dropping it leaves confounding uncontrolled. The lesson's options are to abandon it and state the cost, run two analyses, or use multiple imputation, with the choice and reason recorded in the log.
Raw files are read-only, and deleting students removes their other valid data. Handling missing income is a documented analysis decision, not a change to the raw file.
The correct answer is (c). The information needed is the count and percentage of missing values in the output (162 students, 27%), the three options for a predictor with many missing values, and the rule that whichever option is chosen is recorded in the analysis log with its reason. Income comes from the causal diagram as a confounder, so leaving it out is a decision with a cost that has to be stated, and comparing the two analyses shows how much that cost matters. Option (b) is tempting because no student is lost, but filling the gaps with the mean without saying so hides the problem; option (a) hides the decision, and option (d) edits the raw file. (Section 4, slide “Missing values, distributions, and sparse categories”, 7.2 Processing the Predictor Variables and Worked Example 1.9; Objectives 7.2 and 5.5)
* Pass the knowledge check (100%) to continue.
Final Assessment

A Structured Approach to Data Analysis: Final Assessment

16 questions • 100% required to pass

Bringing It All Together

This lesson laid out a structured workflow for taking a dataset from collection to first analysis. The first section insisted that you start with a causal diagram and a clear separation of outcomes, predictors, confounders, and intervening variables, before you touch the data. The section on coding, entry, and file management turned that intent into discipline at the level of files: thoughtful coding, a project folder you could hand to a collaborator, and a codebook that captures every variable. The section on program files and verification made the workflow reproducible by moving you out of point-and-click interactive mode into program-mode scripts, with systematic editing and verification. The section on data processing closed the loop by processing outcomes and predictors, surfacing multilevel structure, and producing the unconditional associations that should always precede a multivariable model.

The thread running through all four sections is that an analysis is only as trustworthy as the steps that came before it. Every modelling method, including linear regression, model building, logistic regression, count models, and mixed models, assumes you arrive at the modelling step with a clean, documented, and well-understood dataset. The structured approach is what makes that modelling possible. It also positions you to interpret p-values, confidence intervals, and effect sizes responsibly when you eventually report them (Wasserstein & Lazar, 2016; Greenland et al., 2016), and to avoid the analytic flexibility that drives false-positive results (Simmons, Nelson, & Simonsohn, 2011). A first plain-language reading of a p-value and a confidence interval accompanies the mediation example in the first section, and the worked examples of unconditional associations apply both; the same two quantities are reported with every model. The historical roots of exploratory data analysis trace to John Tukey.

Key Takeaways from this lesson

  1. You can now explain why a trustworthy analysis follows a structured, iterative workflow: causal diagram, cleaning and verification, unconditional checks, and only then the multivariable model, with every step run from a saved script in program mode. Backing up several steps as you learn about the data is normal, and the script is the honest record that lets anyone rerun what you did. When an exam plan jumps straight to a model, lets p-values choose the variables, or relies on unsaved point-and-click steps, name the step that was skipped and explain what could go wrong as a result.
  2. You can read a causal diagram and name the outcome, the exposure, and any confounders (forks), mediators or intervening variables (chains) and colliders, using only the direction of the arrows. The most useful habit is to trace the arrows for every variable one at a time and to say so when a role is absent: “no mediator” can be the right answer, a variable that only causes the outcome is not a confounder, and an arrow pointing at another arrow marks a moderator.
  3. You can read the adjustment set from a diagram (confounders in, mediators out when the total effect is wanted, colliders never) and explain what each wrong decision costs: confounding bias, a direct effect in place of the total effect, or a false association created by adjusting for or selecting on a collider. In an exam answer, state the model in one sentence, then give the reason and the consequence of the wrong choice for each variable in turn.
  4. You can read a raw file against its data dictionary and find the common coding faults: a missing code that could be a real value, text in a numeric field, a compound code, mixed date formats and mixed missing-value conventions. For each one, name the fault and its best-practice correction, and remember that every correction is made in the cleaning script, never by editing the raw file by hand.
  5. You can describe a system that protects the original data and leaves an audit trail: originals kept and checked early for completeness, double data entry with a programmed comparison, raw data kept read-only, versioned file names that are never overwritten, a file log, systematic variable names with a codebook, and a log of analyses run in blocks. The practical test for any part of the system is whether someone else could trace every number back to the original form, the file version and the decision that produced it.
  6. You can describe the scripted editing (variable labels, category labels and every missing code converted to NA) and the verification checks (valid and missing counts, smallest and largest values, histograms, frequency tables and cross-variable checks) made before analysis, and you can use their output to spot implausible values and unexpected categories. When you read summary() or table() output, compare every minimum, maximum and category with what is possible and with what the codebook allows.
  7. You can process outcome and predictor variables for the planned model, recognise multilevel structure, choose the unconditional analysis for each pair of variable types, and explain what those first analyses reveal about functional form, collinearity and confounders. Keep the matching in mind: two continuous variables call for a correlation, scatterplot and simple regression, one continuous and one categorical variable for group means with ANOVA or a simple regression, and two categorical variables for a cross-tabulation with a chi-squared test. Remember too that a weak crude association never removes a confounder from the model, because that decision rests on the diagram.

Final Knowledge Assessment

Final assessment: the 16 questions

1. A colleague says: “Drawing a causal diagram before we look at the data is a formality. We can enter every variable, keep the ones with p < 0.05, and let the model tell us what matters.” Which reply best explains why the lesson puts the causal diagram first?

Journals may expect a diagram, but its purpose is to set the adjustment set from the causal question. Choosing variables by p-values afterwards still lets the data, not the question, decide what is adjusted for.
The diagram fixes the estimand and the adjustment set before results are seen. A p-value says nothing about a variable's causal role, so selecting by significance can add a mediator or collider, or drop a confounder.
The diagram comes before cleaning and verification but does not replace them. Knowing which variables matter does not fix coding errors or impossible values in the file.
The diagram guides every regression model, because it identifies the confounders to include and the mediators and colliders to leave out. It is not limited to mediation analysis.
The correct answer is (b). The information needed is what the diagram settles in the workflow: the estimand (the precise quantity to be estimated) and the adjustment set (the variables on the right-hand side of the model), both decided before any model is fitted. A p-value says nothing about a variable’s causal role, so selecting by significance can keep a mediator, which turns the total effect into a direct effect, keep a collider, which creates a false association, or drop a confounder whose crude association happens to be weak, which leaves confounding. Option (a) is tempting because it sounds like a compromise, but it still lets the data, not the causal question, decide the adjustment set; option (c) is wrong because the diagram comes before cleaning and verification and does not replace them. (Section 1, slide “Three DAG structures”, 1.1 Why a Structured, Iterative Workflow, the callout “Recall: DAGs and Their Three Structures” and Table 1.1; Objective 1.1)

2. A student counts the students with and without asthma in each age group in a spreadsheet, types those counts into the R Console to run a chi-squared test, and saves only the final printed result. Which critique follows the lesson?

Saving only the final printed result keeps no record of how it was obtained. After an error is found, nobody can rerun the counts and the test.
Splitting work between a spreadsheet and R is exactly what the lesson warns against. Every analysis, including basic counts, is done in the statistical program.
Interactive Console work is suited to exploration and leaves no full record; it is not more accurate than a script. The real analysis belongs in a saved script.
A saved, commented script that reads the data and produces the counts and the test is its own audit trail, so the analysis can be rerun, checked and shared.
The correct answer is (d). The information needed is the difference between interactive mode and program mode and the rule that every analysis, including basic statistics, is done in the statistical program. The student’s steps are scattered across two programs and none of them is saved, so nobody, including the student, can rerun them after an error is found, whereas a script that reads the file and produces both the counts and the test is its own audit trail. Option (b) is tempting because spreadsheets handle simple counts well, but splitting the work between programs is exactly what that rule warns against. (Section 3, slide “Why the script is the record”, 1.2 Program Mode and Interactive Mode and the callout “Do Every Analysis in the Statistical Program”; Objective 1.2)

3. A campus health team asks whether food insecurity (the exposure) increases depressive symptoms (the outcome) among university students. Its causal diagram (below) has these arrows: household income → food insecurity, household income → depressive symptoms, food insecurity → poor sleep → depressive symptoms, food insecurity → depressive symptoms, food insecurity → wellness-centre visits ← depressive symptoms, and an arrow from international student status that ends on the arrow from food insecurity to depressive symptoms. Which classification is correct?

Causal diagram for food insecurity and depressive symptoms Household income Food insecurity Depressive symptoms Internationalstudent status Poor sleep Wellness-centre visits
This reverses the roles. Household income points into both the exposure and the outcome, a fork, so it is a confounder, and poor sleep lies on a chain from exposure to outcome, a mediator.
Income is right, but lying between the exposure and outcome is not enough to be a mediator. Wellness-centre visits receive arrows from both food insecurity and depressive symptoms, so they are a collider.
Reading arrow directions: income is a fork into both exposure and outcome (confounder), poor sleep has one arrow in from the exposure and one out to the outcome (mediator), and wellness-centre visits receive both arrows (collider).
Income is right, but the other two are swapped. Poor sleep has one arrow in and one out, a chain, while wellness-centre visits have both arrows pointing in, the collider pattern.
The correct answer is (c). The information needed is the direction of each arrow into and out of each variable. Household income sends arrows into both the exposure and the outcome (a fork), so it is a confounder; poor sleep receives an arrow from food insecurity and sends one on to depressive symptoms (a chain), so it is a mediator; and wellness-centre visits receive arrows from both the exposure and the outcome, so they are a collider. Option (b) is tempting because poor sleep and wellness-centre visits are both drawn between the exposure and the outcome, but only poor sleep has one arrow in and one arrow out; both arrows point into wellness-centre visits. (Section 1, 2.1 Sketching a Causal Diagram, the callout “Recall: DAGs and Their Three Structures” and Worked Example 1.1, “Reading the roles off a diagram”; Objective 2.1)

4. In the diagram in Question 3, the arrow from international student status ends on the middle of the arrow from food insecurity to depressive symptoms, rather than on either variable. How should international student status be classified?

An arrow ending on another arrow marks a change in the strength of that effect, so international student status is a moderator. It is studied with an interaction term or separate estimates by group and is not part of the adjustment set.
A confounder sends arrows into both the exposure and the outcome. International student status sends no arrow into either variable, so it cannot form a fork.
Touching the path is not the same as lying on it. A mediator receives an arrow from the exposure and sends one to the outcome, and international student status does neither.
A collider receives arrows from two variables. International student status receives none; its arrow points at the effect itself, which marks a moderator.
The correct answer is (a). The information needed is where the arrow ends: an arrow that points at another arrow, rather than at a variable, marks a change in the strength of that effect. International student status sends no arrow into food insecurity or into depressive symptoms, so it is not part of a fork, a chain or a collider, and it plays no part in the adjustment set; it is studied by adding an interaction term or by reporting the effect in each group. Option (c) is tempting because the arrow touches the path from the exposure to the outcome, but a mediator must receive an arrow from the exposure and send one to the outcome, and international student status does neither. (Section 1, 2.1 Sketching a Causal Diagram, the paragraph on moderators after Worked Example 1.1; Objective 2.1)

5. A study asks whether neighbourhood green space (the exposure) reduces childhood asthma (the outcome). Its causal diagram (below) has these arrows: green space → asthma, green space → physical activity → asthma, air pollution → asthma, and green space → survey participation ← asthma. Which statement about the diagram, as drawn, is correct?

Causal diagram for green space and childhood asthma Air pollution Green space Childhood asthma Physical activity Survey participation
Physical activity and survey participation are classified correctly, but air pollution sends a single arrow into asthma. A cause of the outcome only is not a confounder; that would need an arrow into green space too.
Physical activity receives an arrow from green space and sends one to asthma, a chain, so it is a mediator. Air pollution sends one arrow out and none in, and survey participation is a collider.
Survey participation receives arrows from both green space and asthma, so it is a collider, while physical activity lies on a chain and is a mediator. Air pollution points only into asthma, so it is not a confounder.
Roles come from the arrows as drawn: physical activity is a chain, survey participation receives both arrows, and no variable points into both green space and asthma, so the diagram has no confounder.
The correct answer is (d). The information needed is the arrows into and out of each variable. Physical activity receives an arrow from green space and sends one to asthma (a chain), survey participation receives arrows from both green space and asthma (a collider), and air pollution sends a single arrow, into asthma; a confounder needs arrows into both the exposure and the outcome, no variable in this diagram has them, and so the correct answer for confounders is “none”. Option (a) is tempting because air pollution is drawn above the other variables and is a well-known cause of asthma, but a cause of the outcome only is not a confounder; if the team believed that green space and air pollution were linked, the diagram would need that arrow. (Section 1, Worked Example 1.1, “Reading the roles off a diagram”; Objective 2.1)

6. For the diagram in Question 3 (household income → food insecurity and household income → depressive symptoms; food insecurity → poor sleep → depressive symptoms; food insecurity → wellness-centre visits ← depressive symptoms; international student status pointing at the arrow from food insecurity to depressive symptoms), which variables must be adjusted for to estimate the total effect of food insecurity on depressive symptoms?

Poor sleep is related to both variables, but it lies on the chain from food insecurity to depressive symptoms. Adjusting for it blocks the part of the effect that passes through sleep, so the total effect is lost.
For the total effect, only confounders are adjusted for. Household income closes the backdoor fork, the mediator stays out, the collider is never included, and the moderator is not needed to remove confounding.
This adds the mediator, which blocks part of the effect, and the collider, which opens a false association. Neither belongs in the model for the total effect.
International student status is a moderator; its arrow points at the effect, not into food insecurity. Being linked to the exposure in some way does not make a variable a confounder.
The correct answer is (b). The information needed is the role of each variable and the estimand, the total effect. Household income is a confounder, so it is adjusted for to close the path food insecurity ← household income → depressive symptoms; poor sleep is a mediator, so it is left out when the total effect is wanted; wellness-centre visits are a collider, so they are never adjusted for; and international student status, a moderator, is not needed to remove confounding. Option (a) is tempting because sleep is clearly related to both variables, but adjusting for it blocks the part of the effect that travels through sleep; option (c) adds the collider as well, which would create a false association. (Section 1, 3.1 Reading the Adjustment Set from the Diagram, 3.1.1 What Adjusting for a Variable Means and Worked Example 1.2; Objective 3.1)

7. Using the diagram in Question 3, a model of the depressive symptom score (0 to 27 points) on food insecurity, adjusted for household income, gives a food-insecurity coefficient of 3.2 points. A colleague then adds poor sleep to the model “to be safe”, and the coefficient falls to 1.9 points. Which interpretation is correct?

A change in a coefficient does not decide a variable's role; the arrows do. Poor sleep receives an arrow from food insecurity, so it is a mediator, and the smaller coefficient is a direct effect, not a less biased total effect.
Collider bias arises from conditioning on a variable that receives arrows from both exposure and outcome. Poor sleep has one arrow in and one out, so the fall reflects mediation, not collider bias.
Adding a mediator changes the question: the first coefficient is the total effect, and the second is the direct effect that does not pass through sleep. The difference reflects the part carried through sleep, if the diagram is correct.
The two models answer different questions, so they do not both estimate the total effect. Being smaller does not make the direct effect a more conservative version of the total effect.
The correct answer is (c). The information needed is the role of poor sleep (a chain: food insecurity → poor sleep → depressive symptoms) and what a coefficient means once a mediator is added. Adding a mediator changes the question the model answers: the coefficient becomes the direct effect, c′, the part of the effect that does not pass through sleep, and the drop from the total effect, c, is the part carried through sleep (the indirect effect), if the diagram is correct. Option (a) is tempting because a coefficient that changes when a variable is added often signals confounding, but the arrows, not the change in the estimate, decide a variable’s role, and sleep has an arrow coming from food insecurity. (Section 1, 3.1.1 What Adjusting for a Variable Means, 3.1.4 Adjusting for a Mediator: A Mediation Example, Figure 1.2 and Table 1.3; Objective 3.1)

8. The first three rows of a raw smoking-cessation file are shown below; the data dictionary describes visit_date as “date of clinic visit” and cigs_day as “cigarettes smoked per day”. Which pair of faults and corrections is correct?

id    visit_date   cigs_day
K01   2024-11-02   12
K02   02/11/2024   ten
K03   3 Nov 24     0
Dates need one consistent, unambiguous format, converted in the script and stated in the codebook, and a numeric variable holds only numbers. Recording the word as the number it means keeps cigs_day usable for calculation.
R reading several formats does not remove the ambiguity of a date such as 02/11/2024, and mixed formats cannot be sorted or subtracted reliably. Converting cigs_day to text would stop R calculating with it.
Inconsistent date formats are corrected, not deleted with the rows. The 0 in cigs_day is a real answer from a participant who does not smoke and must not be recoded as missing.
One format is right in principle, but DD/MM/YYYY is itself the ambiguous form. Leaving the word in cigs_day also keeps the whole column as text, so R cannot calculate with it.
The correct answer is (a). The information needed is the rule of one consistent, unambiguous format for each variable and the rule that a numeric variable holds only numbers. Three date formats in one column cannot be sorted or subtracted, and 02/11/2024 is ambiguous, so the script converts the dates to one stated format; a single word such as “ten” turns the whole cigs_day column into text, so R cannot calculate with it, and the number the word means is recorded instead. Option (d) is tempting because it chooses one format, but DD/MM/YYYY is itself the ambiguous form, and it leaves the text in place; the 0 in option (c) is the real answer of a participant who does not smoke and must not become missing. (Section 2, 4.1.2 Spotting Coding Faults in a Raw File; Objective 4.1)

9. A perinatal file records bwt_g, birth weight in grams, and its data dictionary states “999 = not recorded”. The file includes infants born very preterm, some of whom weigh less than 1,000 g at birth. What is the fault, and how should it be corrected?

Being far from the typical weight makes 999 g unusual, not impossible. In a file with very preterm infants it is a real weight, so the code cannot be told apart from a real value.
A missing code must be impossible as a real value, and 999 g is possible for a very preterm infant. A code such as −999, documented and converted to NA in the script after checking the source, keeps real weights and missing records apart.
Replacing missing values with the mean invents birth weights, and it does not solve the fault, because real 999 g infants would be replaced too.
Documenting the code does not let anyone tell a real 999 g infant from a missing record. The code itself has to be one that no real weight can take.
The correct answer is (b). The information needed is the range of real values, which in a file with very preterm births includes weights below 1,000 g, and the rule that a missing-value code must be impossible as a real response. Because 999 g is a real weight for some infants, every count of very low birth weight infants and every mean would mix real infants with missing records, so the code is changed and converted to NA in the script. Option (d) is tempting because the code is documented, but documentation cannot tell a real 999 g infant from a missing record; option (a) mistakes an unusual value for an impossible one. (Section 2, slide “Three rules that prevent cascading errors”, 4.1.2 Spotting Coding Faults in a Raw File and Worked Example 1.4; Objective 4.1)

10. Four entries from a draft data dictionary are listed below. Which entry follows the coding rules taught in this lesson?

The income codes are numeric and clear, but missing values are recorded in two different ways, a blank and a full stop. The lesson calls for one documented convention with codes that cannot be real values.
This is a compound code that packs two facts into one variable. One variable should hold one piece of information, so births and breastfeeding need separate variables.
A BMI can never be negative, so the codes are impossible as real values, and the two codes keep the reasons for missingness apart. The variable is numeric and holds one piece of information.
Free text with inconsistent spellings breaks the rule of numeric codes, and the variants would appear as separate categories. Responses should be coded with documented numeric values.
The correct answer is (c). The information needed is the three coding rules (missing-value codes that can never be real values, numeric codes, and one piece of information per variable) and the rule of one documented convention for missing values. A BMI can never be negative, so −999 and −998 are impossible as real values and also keep the two reasons for missingness apart; entry (a) uses blanks and full stops, two conventions that the software reads differently, (b) is a compound code that packs two facts into one variable, and (d) uses free text with inconsistent spellings instead of numeric codes. Option (a) is tempting because its income codes are numeric and clear, but its missing values are recorded in two different ways, one of them a blank. (Section 2, 4.1.1 Three Rules for Coding (cards) and 4.1.2 Spotting Coding Faults in a Raw File; Objective 4.1)

11. A research coordinator proposes four practices for a new influenza study. Which one would put the data at risk and break the audit trail?

Sorting one column on its own moves the ages without the rest of each row, so each person's age is matched with someone else's other values. Saving the file makes the damage permanent and invisible.
This is a recommended practice: read-only raw files with every processed file regenerated by numbered scripts protect the data and keep the audit trail.
Double entry with a programmed comparison, resolved against the paper forms, is the lesson's strongest defence against transcription errors. It protects the data rather than putting it at risk.
A one-line file log for each version is part of the file-management rules. It strengthens the audit trail by recording what changed in each version.
The correct answer is (a). The information needed is the system that protects the data: read-only raw files with every other file regenerated by scripts, double entry with a programmed comparison, versioned files with a log, and caution with spreadsheets. Sorting a single column in a spreadsheet moves the ages without moving the rest of each row, so one person’s age is matched with another person’s other values, and saving the file makes the damage permanent and invisible; the other three proposals are practices this lesson recommends. Option (c) can look like unnecessary extra work, but double entry with a programmed comparison is the strongest defence against transcription error. (Section 2, slide “Double-entry and the spreadsheet hazard”, 5.2 Entering the Data and 5.3 Organising and Versioning Files; Objectives 5.2 and 5.3)

12. An adult nutrition survey records weight_kg. The verification check of the extreme values gives the output below. Which values should be investigated?

> head(sort(dat$weight_kg), 5)
[1]  0.0 38.2 41.5 43.0 44.8
> tail(sort(dat$weight_kg), 5)
[1] 131.0 136.4 142.9 680.0 999.0
The familiar code is only one of the impossible values. No adult weighs 0 kg, and 680 kg is implausible, likely a misplaced decimal point, so both need investigating as well.
The values near the ends of the list are unusual but possible for adults, while 0, 680 and 999 are impossible. Each impossible value is traced to the source, fixed or set to NA in the script, and the check is rerun.
Listing the extremes is for inspection, not deletion. Unusual but possible weights are kept; only values that cannot be real need correcting.
Keeping possible extreme values is sound, but impossible values are errors, not genuine extremes. They must be corrected or set to NA, not kept.
The correct answer is (b). The information needed is the plausible range of adult weight and the five smallest and five largest values in the output. Weights of 38.2 to 44.8 kg and of 131.0 to 142.9 kg are unusual but possible, whereas 0.0, 680.0 and 999.0 are impossible; each impossible value is traced to the original form and fixed in the script (680.0 becomes 68.0 if the form confirms it, and 999.0 becomes NA if it is a missing code), and the check is repeated, because where there is one error there are often more. Option (a) is tempting because 999 is the familiar code, but 0.0 and 680.0 are just as impossible; option (c) misreads the purpose of the check, which is to inspect the extremes, not to delete them. (Section 3, slide “Check before you trust”, 6.2 Verifying the Data and Table 1.8; Objective 6.2)

13. The codebook allows three values for vacc_status: Complete, Partial and None. The first frequency table is shown below. What should the cleaning script do?

> table(dat$vacc_status, useNA = "ifany")

complete Complete     none     None  Partial     <NA>
       6      412        3       58      133       11 
Treating each spelling as a separate category means the model sees five groups where the codebook allows three. The variants must be recoded so each answer falls in its intended category.
Deleting rows discards answers whose meaning is clear, and editing the raw file leaves no record. Recoding happens in the cleaning script.
The lower-case variants have a clear meaning, so the script maps them to the codebook values with case_when(), sends any unexpected spelling to NA, stores a factor, and reruns the table to confirm.
Setting clear answers to NA throws away usable information and inflates the missing values. Spelling variants whose meaning is obvious should be recoded, not discarded.
The correct answer is (c). The information needed is the list of allowed categories and the count for every category in the output. The table shows five categories where the codebook allows three, and the two extra ones are spelling variants whose meaning is clear, so the script maps “complete” to Complete and “none” to None, leaves Partial unchanged and keeps the 11 true missing values as NA; the rerun table then lists only the codebook categories. Option (d) is tempting because it removes the unexpected categories, but it throws away answers whose meaning is clear and adds to the missing values; option (b) also edits the raw file by hand. (Section 3, slide “Three components of scripted editing”, 6.2 Verifying the Data and “Reading the fix in Activity 1.3”; Objective 6.2)

14. Before fitting a model for the effect of physical activity on systolic pressure, the unconditional analyses show that BMI and waist circumference are highly correlated (r = 0.88), and that age, a confounder in the causal diagram, has only a weak crude association with systolic pressure (r = 0.08). Which plan follows from these findings?

A correlation above about 0.8 signals collinearity, so one variable is kept to match the question or the two are combined. Age stays because the decision to adjust rests on the causal diagram, not on a crude association.
A weak crude association does not remove a confounding path in the diagram, so age stays. A high correlation between two predictors signals collinearity, not that both are strong predictors.
Correlated predictors are not banned outright; the usual remedy is to keep the one that best fits the question or combine them. Dropping both throws away useful information.
Unconditional analyses do inform the model: they reveal problems such as collinearity before fitting. Here the very high correlation between BMI and waist circumference needs a decision.
The correct answer is (a). The information needed is the rule of thumb for collinearity (|r| above about 0.8 between two predictors) and the rule that confounders come from the diagram, not from crude associations. Two predictors with r = 0.88 carry nearly the same information, so a model that includes both cannot separate their effects; age stays because the diagram says it causes both the exposure and the outcome, and a weak crude association does not remove that path. Option (b) is tempting because dropping weak variables looks tidy, but it lets a crude association overrule the diagram, and it keeps the collinear pair. (Section 4, 7.4.2 What the Unconditional Associations Show, Worked Example 1.15 and Table 1.12; Objective 7.4)

15. The planned outcome for a linear regression is the cost of a hospital stay, in dollars, for 900 patients. Its summary is shown below. What does the processing step suggest?

> summary(dat$cost)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
     90     900    2020    3361    3982   43780 
Costs above the third quartile are a quarter of the data, all real values. Being large in a skewed distribution does not make a value an error to delete.
Residuals are what ultimately matter, but the lesson treats a strongly skewed outcome as an early warning worth acting on in processing. The residuals are still checked after fitting.
A continuous outcome does not need to be normal before any analysis, and grouping it throws away information. A transformation such as the log is the step the lesson suggests for strong right skew.
A mean well above the median and a long upper tail show strong right skew. Because every cost is positive, a log transformation can be created in the script and recorded, with the residuals checked after fitting.
The correct answer is (d). The information needed is the gap between the mean and the median, the long distance from the third quartile to the maximum, and the minimum (90), which shows that every value is above zero. A mean this far above the median is a quick sign that a few very expensive stays pull the mean up, and a strongly skewed outcome rarely gives the bell-shaped residuals that linear regression assumes, so a transformation such as the logarithm is explored now and the residuals are still checked later. Option (b) is tempting because the lesson says that the normality of the residuals is what ultimately matters, but it also treats a far-from-normal outcome as a fair early warning; option (a) deletes a quarter of the data, all of them real costs. (Section 4, 7.1 Processing the Outcome Variables, Table 1.9 and Worked Example 1.8; Objective 7.1)

16. A regional health authority gives you walk01.csv, the raw file of a survey of 1,800 adults aged 50 and over, to study whether owning a dog (the exposure) is associated with weekly minutes of walking (the outcome). A colleague proposes to correct odd-looking values by typing over them in a spreadsheet and saving the file under the same name, and then to enter all 40 variables, including the number of dog walks per day, into one model and keep those with p < 0.05. Which revised plan follows the structured approach of this lesson?

Drawing the diagram after the model is fitted is too late to guide the adjustment set, and the plan still overwrites the raw file and keeps the mediator dog walks per day.
A diagram drawn first is right, but choosing variables by p-values still overrides it, and typing over values in a spreadsheet saved under the same name leaves no record. Two of the three problems remain.
Scripted cleaning fixes one problem, but entering all 40 variables still adjusts for dog walks per day, a mediator, and possibly colliders, so the estimate would no longer be the total effect.
This follows the workflow in order: diagram and adjustment set first, raw file kept unchanged with scripted corrections and a file log, editing and verification, processing and unconditional analyses, and only then the model.
The correct answer is (d). The information needed is the order of the structured workflow (causal diagram; coding and files; editing and verification in program mode; processing and unconditional analyses; then the multivariable model) and the rule for each step. The colleague’s plan skips the diagram, so p-values rather than the causal question choose the variables, and it keeps the number of dog walks per day, a mediator on the path dog ownership → dog walks → minutes of walking, which would turn the total effect into a smaller direct effect; it also overwrites the raw file, which leaves no record of what was changed. Option (c) is tempting because it fixes the cleaning, but entering every variable still adjusts for the mediator and for any collider; option (b) keeps two of the three problems, and option (a) draws the diagram too late to guide the model. (Section 1, slide “Analysis is iterative, not linear”, 1.1 Why a Structured, Iterative Workflow, 2.1 Sketching a Causal Diagram and Worked Example 1.1; Section 2, 5.3 Organising and Versioning Files; Section 3, 6.2 Verifying the Data; Objectives 1.1 and 3.1)

✦ Before submitting: pass every section knowledge check (100%).

Lesson Complete!

You have successfully completed A Structured Approach to Data Analysis. Your responses have been downloaded.

With data collected and organized, the work that follows is detecting outliers, addressing missing values, and producing the descriptive summaries (Table 1) that every analysis report eventually needs. The structured workflow you built here makes that work tractable.