Six people, two groups, and a visit log with repeat visits. Build an endpoint, compare risks, then audit three proposed pieces of code.
Inspect the source tables
Day 0 is enrollment. Follow-up is the number of days with complete capture of return visits. The visit log can include records outside the endpoint window.
cohort
id
arm
followup
1
A
30
2
A
30
3
A
12
4
B
30
5
B
30
6
B
30
visits
id
day
1
8
1
12
2
31
4
30
6
0
SQL workbench
Return six rows ordered by id, with columns id and event. Count a visit on days 1 through 30 inclusive. A person with no qualifying visit needs 30 days of follow-up to be classified as event-free. Otherwise the outcome is unknown.
Explain the group comparison
With 12 days observed, person 3 has an unknown outcome. Group A has one event among two observed people (50%). With 30 days and no visit, that person becomes an observed zero and group A falls to one in three. Group B stays at one in three. The change comes entirely from outcome ascertainment.
Code review desk
risk <- sum(event, na.rm = TRUE) / length(event)
Which issue or review action matters here?
Define one row
Project brief. Create a person-level summary for a synthetic cohort. Each row represents one person, event is 1, 0, or missing, and the target quantity is the event proportion among people whose outcome was observed. Before coding, write down the row unit, numerator, denominator, and missing-outcome rule. Run these blocks in order in a clean R session.
Select the observed outcomes
Validate the data contract before calculating. A unique ID check prevents duplicated records from silently changing the denominator. Explicitly check observed values so imported character or unexpected codes cannot pass unnoticed.
Keep counts alongside the estimate. Naming each intermediate quantity makes the denominator visible during review. The result is 2 events among 4 observed outcomes. The missing fifth outcome is not silently classified as a non-event.
Change the last outcome from 1L to NA_integer_. Predict the result and rerun both blocks. The original assertion should fail. Verify one event among three observed participants by hand before updating the expected values.
Make the run reproducible
Save the script in a project with a README that states the R version, how to run the analysis, the expected output, and the outcome definition. Start a clean session and run the script from the project root. For a package-based analysis, record package versions with a lockfile. Commit the script and lockfile, then inspect git diff before sharing. A seed is relevant only when the analysis uses randomness.
Discussion
Questions, précisions et applications de cette ressource.