Lab · Programming · 5 sections

R for statistical analysis

Use a base R data frame to estimate an observed event proportion and check its denominator.

No account needed to read online.

What it covers

  • Define one row
  • Select the observed outcomes
  • Keep the counts with the estimate
  • Practice with a missing outcome

STATISTICAL PROGRAMMING LAB

Who had a return visit within 30 days?

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

idarmfollowup
1A30
2A30
3A12
4B30
5B30
6B30

visits

idday
18
112
231
430
60

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?

Read online

R for statistical analysis

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.

outcomes <- data.frame(
  id = 1:5,
  event = c(1L, 0L, NA_integer_, 0L, 1L)
)
stopifnot(nrow(outcomes) == 5L)
stopifnot(!anyNA(outcomes$id))
stopifnot(anyDuplicated(outcomes$id) == 0L)
observed <- outcomes$event[!is.na(outcomes$event)]
stopifnot(all(observed %in% c(0L, 1L)))

Keep the counts with the estimate

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.

summarise_risk <- function(data) {
  stopifnot(!anyNA(data$id))
  stopifnot(anyDuplicated(data$id) == 0L)
  observed <- data$event[!is.na(data$event)]
  stopifnot(all(observed %in% c(0L, 1L)))
  n_observed <- length(observed)
  events <- sum(observed)
  data.frame(
    events = events,
    observed = n_observed,
    missing = sum(is.na(data$event)),
    risk = if (n_observed > 0L) events / n_observed else NA_real_
  )
}
result <- summarise_risk(outcomes)
print(result)
stopifnot(result$events == 2L, result$observed == 4L, result$risk == 0.5)

Practice with a missing outcome

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, clarifications and applications of this resource.

Loading discussion…