Before we begin
Study designs; sample proportions; risk difference and relative risk.
We will run a few chunks at a time as we reach each topic in class.
Open BTE3207_Advanced_Biostatistics.Rproj, then open this
week’s .Rmd file. Run the chunks from the top, or click
Knit to make the whole document.
Study designs
This week, we are going to work with binary outcomes. Before calculating anything, let’s think about how the data were collected.
In a randomized trial, we assign subjects to groups. In an observational cohort study, we observe their exposure and follow their outcomes. In a case-control study, we select cases and controls first, and then compare their previous exposure.
Random assignment
Let’s say we have 12 subjects. This is a small example for practicing R, not the actual vaccine trial data.
set.seed(320702)
subject <- 1:12
assigned_group <- sample(rep(c("Treatment", "Control"), each = 6))
trial_example <- data.frame(subject, assigned_group)
trial_example
table(trial_example$assigned_group)
##
## Control Treatment
## 6 6
rep() repeats values. sample() randomly
rearranges them here. We have six subjects in each group because we
specified that number in advance.
Random assignment helps reduce systematic differences between groups. It does not guarantee that every characteristic is balanced in a small sample.
Question
If smokers and non-smokers volunteered for different groups, would this be random assignment? What if smoking status was related to the outcome as well?
Remember the smoking, alcohol, and cold example from the lecture. A calculation alone does not remove confounding.
Sample proportion
Response to ART was measured in 1,000 HIV-positive patients. Among them, 206 responded to therapy.
Let’s give a value of 1 to a response and 0
to no response.
response <- c(rep(1, 206), rep(0, 794))
length(response)
## [1] 1000
table(response)
## response
## 0 1
## 794 206
The sample proportion, p-hat, is the number of responses divided by the total number of observations.
p_hat <- sum(response) / length(response)
p_hat
## [1] 0.206
mean(response)
## [1] 0.206
Voila! The mean of a 0/1 variable is its sample proportion. Here, 20.6% of the patients responded.
SD of binary data
sd(response)
## [1] 0.4046328
sqrt(p_hat * (1 - p_hat))
## [1] 0.4044305
The SD of a Bernoulli variable with probability p is
sqrt(p * (1 - p)). Here we have replaced p
with our sample estimate. R’s sd() uses the sample
denominator, n - 1, so the two results are slightly
different.
n <- length(response)
sqrt(n / (n - 1) * p_hat * (1 - p_hat))
## [1] 0.4046328
This matches sd(response). For binary data, p-hat is
usually easier to interpret than the SD or percentiles.
Comparing sample proportions
What if the response differs by group?
From the same example, 127 of 503 patients with CD4 < 250 responded. Among the 497 patients with CD4 >= 250, 79 responded.
art <- data.frame(
group = c("CD4 < 250", "CD4 >= 250"),
responded = c(127, 79),
total = c(503, 497)
)
art$not_responded <- art$total - art$responded
art$p_hat <- art$responded / art$total
art
Each row is one group. As before, $ selects one variable
from a data frame. We can also select a particular element of a vector
with [ ].
p_low_cd4 <- art$p_hat[1]
p_high_cd4 <- art$p_hat[2]
p_low_cd4
## [1] 0.2524851
p_high_cd4
## [1] 0.1589537
Risk difference
The risk difference is the difference between two proportions. Here, we compare CD4 < 250 with CD4 >= 250.
rd_art <- p_low_cd4 - p_high_cd4
rd_art
## [1] 0.09353137
rd_art * 100
## [1] 9.353137
The response proportion in the CD4 < 250 group was 9.35 percentage points higher. This is the absolute difference.
Even though we call it “risk”, the event can be a good outcome, such
as responding to therapy. Always check which outcome is coded as
1.
Relative risk
For relative risk, we divide one proportion by the other.
rr_art <- p_low_cd4 / p_high_cd4
rr_art
## [1] 1.588419
(rr_art - 1) * 100
## [1] 58.84189
The response proportion in the CD4 < 250 group was 1.59 times that in the CD4 >= 250 group, or 58.8% higher in relative terms.
Both results use the same two proportions. The percentage-point difference and relative percentage increase answer different questions.
Question
What happens if we change the reference group? Try the calculation first.
p_high_cd4 - p_low_cd4
## [1] -0.09353137
p_high_cd4 / p_low_cd4
## [1] 0.6295569
The sign of the risk difference changes. The relative risk becomes the reciprocal of the original result.
Maternal/infant HIV transmission
Let’s use the other example from the lecture. There were 13 HIV-positive infants among 180 in the AZT group, and 40 among 183 in the placebo group.
hiv <- data.frame(
group = c("AZT", "Placebo"),
infected = c(13, 40),
total = c(180, 183)
)
hiv$not_infected <- hiv$total - hiv$infected
hiv$p_hat <- hiv$infected / hiv$total
hiv
Question: RD and RR
Compare AZT with placebo. Which proportion should be in the numerator? Which group is the reference?
p_azt <- hiv$p_hat[1]
p_placebo <- hiv$p_hat[2]
rd_hiv <- p_azt - p_placebo
rr_hiv <- p_azt / p_placebo
c(RD = rd_hiv, RR = rr_hiv)
## RD RR
## -0.1463570 0.3304167
The observed transmission proportion was 14.64 percentage points lower in the AZT group. Its relative risk was 0.33, or about 67% lower relative to placebo.
We calculated these values directly from the counts. Keep the unrounded proportions for calculations, and round the final answer when reporting it.
Absolute and relative differences
What does this difference mean for 1,000 people?
people <- 1000
expected_azt <- people * p_azt
expected_placebo <- people * p_placebo
c(AZT = expected_azt,
Placebo = expected_placebo,
Difference = expected_azt - expected_placebo)
## AZT Placebo Difference
## 72.22222 218.57923 -146.35701
If these group proportions applied to 1,000 people in each group, the difference would be about 146 transmissions. This is an illustration using the observed proportions, not a count of additional patients in this study.
Same relative risk, different risk difference
Can a large relative change have a small absolute difference?
comparison <- data.frame(
example = c("A", "B"),
p_treatment = c(0.01, 0.20),
p_reference = c(0.02, 0.40)
)
comparison$RD <- comparison$p_treatment - comparison$p_reference
comparison$RR <- comparison$p_treatment / comparison$p_reference
comparison
These are simple practice values. Both examples have a relative risk of 0.5, but their absolute differences are quite different. We need the group proportions to understand the size of the difference.
For more than two groups, we can choose one reference group and repeat these calculations for each of the other groups.
Before moving on
These calculations describe the groups we observed. Whether the difference can be interpreted as an effect of treatment also depends on the study design.
In a case-control study, we chose the numbers of cases and controls ourselves. Dividing those sampled case counts by exposure-group totals does not generally estimate the population risk. We will use odds ratios for that comparison next week.
Bibliography
R Core Team (2024). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria. https://www.R-project.org/.
Xie Y (2025). knitr: A General-Purpose Package for Dynamic Report Generation in R. R package version 1.50, https://yihui.org/knitr/.
Xie Y (2015). Dynamic Documents with R and knitr, 2nd edition. Chapman and Hall/CRC, Boca Raton, Florida. ISBN 978-1498716963, https://yihui.org/knitr/.
Xie Y (2014). “knitr: A Comprehensive Tool for Reproducible Research in R.” In Stodden V, Leisch F, Peng RD (eds.), Implementing Reproducible Computational Research. Chapman and Hall/CRC. ISBN 978-1466561595.
Allaire J, Xie Y, Dervieux C, McPherson J, Luraschi J, Ushey K, Atkins A, Wickham H, Cheng J, Chang W, Iannone R (2025). rmarkdown: Dynamic Documents for R. R package version 2.30, https://github.com/rstudio/rmarkdown.
Xie Y, Allaire J, Grolemund G (2018). R Markdown: The Definitive Guide. Chapman and Hall/CRC, Boca Raton, Florida. ISBN 9781138359338, https://bookdown.org/yihui/rmarkdown.
Xie Y, Dervieux C, Riederer E (2020). R Markdown Cookbook. Chapman and Hall/CRC, Boca Raton, Florida. ISBN 9780367563837, https://bookdown.org/yihui/rmarkdown-cookbook.
Barnier J (2022). rmdformats: HTML Output Formats and Templates for ‘rmarkdown’ Documents. R package version 1.0.4, https://CRAN.R-project.org/package=rmdformats.