# More visits had long waits. What changed?

In a fictional outpatient clinic, 28% of visits had a wait longer than 30 minutes before a scheduling change. Afterward, 52% did. The clinic saw more walk-ins in the second period. Within each visit type, the share waiting more than 30 minutes stayed the same.

The crude proportion describes the visits the clinic actually saw. To compare periods with the same mix, calculate the proportion within each visit type and apply the same weights to both periods. Here the reference gives scheduled visits and walk-ins equal weight. In a study, choose a reference population that fits the question and report which one you used.

Save this as `wait_times.R` and run `Rscript wait_times.R`. It uses base R.

```r
visits <- data.frame(
  period = rep(c("Before", "After"), each = 2),
  type = rep(c("scheduled", "walk_in"), times = 2),
  total = c(80, 20, 20, 80),
  over_30 = c(16, 12, 4, 48)
)
reference <- c(scheduled = 0.5, walk_in = 0.5)

summarize_period <- function(group) {
  rate <- group$over_30 / group$total
  names(rate) <- group$type
  c(
    crude = sum(group$over_30) / sum(group$total),
    standardized = sum(rate[names(reference)] * reference)
  )
}

result <- rbind(
  Before = summarize_period(subset(visits, period == "Before")),
  After = summarize_period(subset(visits, period == "After"))
)

stopifnot(
  isTRUE(all.equal(unname(result[, "crude"]), c(0.28, 0.52))),
  isTRUE(all.equal(unname(result[, "standardized"]), c(0.40, 0.40)))
)
print(100 * result)
```

```text
       crude standardized
Before    28           40
After     52           40
```

Both columns show percentages. The observed share rose by 24 percentage points. With the same mix of visits, the estimate is 40% in each period. For staffing, the observed change can still matter. For a comparison that holds the visit mix fixed, use the standardized column. Neither estimate establishes the effect of changing the schedule. This example also leaves out sampling uncertainty and assumes both visit types appear in both periods.

This exercise uses invented data. It was developed at Lumina, with AI assistance in drafting, and tested in R.

## Continue learning with Lumina

This comparison is one part of a health analysis. You also need to define the research question, check the records, choose a method and explain the uncertainty.

I created **Applied Statistics for Health Research** at Lumina for health professionals, researchers and students who are new to statistics. The paid individual self-study course has **13 core modules**, with guided examples and decision exercises you can complete without coding. **14 optional Python and base R labs** add practice with executable code. The lessons and labs are available in English and Portuguese.

[Explore the course and read the free first lesson](https://lumytics.com/courses/applied-health-statistics/?utm_source=hashnode&utm_medium=community&utm_campaign=lumina_cursos_health_2026_11&utm_content=r_wait_mix_tutorial). Start by writing an analysis plan before choosing a statistical test. The course page explains the full syllabus, resources and enrollment.
