LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 25 lessons

Lesson 3 of 7

Module 2 · Statistics

Counts and proportions

Counts and proportions

Counts and proportions

Many clinical outcomes are yes or no: low birth weight, response to treatment, a mutation present or absent. This lesson compares proportions and introduces the odds ratio.

⏱ 12 min readModule 2 · StatisticsFree
Lesson at a glance3 steps · about 12 min
  1. A two-by-two table
  2. Testing the difference
  3. Logistic regression: adjusting for other factors

A two-by-two table

When both variables are yes-or-no, the data reduce to four counts, one for each combination: smokers with and without a low-weight baby, and non-smokers with and without one. This is a two-by-two table, also called a contingency table. In Describing data the outcome was a measurement in grams; here it is a category, low, which is "low" when the baby weighed under 2,500 g. The questions are the same, how big is the difference and could it be chance, but the tools change.

Describing data built the bw table from MASS::birthwt. The code below assumes that bw already exists. If you start a fresh R session, run that lesson's first code block again before this one, or R will report that object bw is not found.

R
tab <- table(bw$smoke, bw$low)
tab
round(prop.table(tab, margin = 1), 3)      # proportions within each row

What this does

  • table(bw$smoke, bw$low) counts the births in each combination; the first variable gives the rows and the second the columns.
  • prop.table(tab, margin = 1) divides every count by the total of its row, so each row adds up to 1. With margin = 2 it would divide by column totals instead, which answers a different question.
  • round(…, 3) keeps three decimal places.

Reading the output. Of 115 non-smokers (86 + 29), 29 had a low-weight baby; of 74 smokers (44 + 30), 30 did. In the second table each row sums to 1: 0.252 of non-smokers' babies and 0.405 of smokers' babies were low weight. Row proportions are the right choice here because the question is "among smokers, how many…?"; smoking is the exposure and low weight the outcome.

The row and column order come from the factor levels. In Describing data you turned low and smoke into factors with "normal" and "non-smoker" first, so the table reads in that order and no counts are hidden behind codes such as 0 and 1. addmargins(tab) adds the row and column totals, which is a quick way to check that the four cells add up to 189.

Choosing the wrong margin is an easy mistake. With margin = 2, the low-weight column would show that 30 of the 59 low-weight babies, 51%, had mothers who smoked. That is a true statement, but it answers "among low-weight babies, how many had smoking mothers?", not "does smoking raise the risk?". It only looks like the second question when the percentages are read carelessly, so always say what each percentage is a share of.

normal low non-smoker 86 29 smoker 44 30 normal low non-smoker 0.748 0.252 smoker 0.595 0.405

Worked example · risk and odds

risk of low birth weight: smokers 30 ÷ 74 = 40.5% · non-smokers 29 ÷ 115 = 25.2%
risk difference = 15.3 points · risk ratio = 0.405 ÷ 0.252 = 1.61
odds: smokers 30 ÷ 44 = 0.68 · non-smokers 29 ÷ 86 = 0.34 · odds ratio = 0.68 ÷ 0.34 = 2.02

Risk and odds are different scales. When an outcome is common, as here, the odds ratio (2.02) looks much bigger than the risk ratio (1.61). Say which one you report.

Step by step. Risk is the share of a group with the outcome: 30 ÷ 74 = 0.405 for smokers and 29 ÷ 115 = 0.252 for non-smokers. The risk difference subtracts them: 0.405 − 0.252 = 0.153, or 15.3 percentage points. The risk ratio divides them: 0.405 ÷ 0.252 = 1.61. Odds compare those with the outcome to those without, in the same group: 30 ÷ 44 = 0.68 for smokers and 29 ÷ 86 = 0.34 for non-smokers. The odds ratio divides the two odds: 0.68 ÷ 0.34 = 2.02 (using the unrounded odds, 0.682 ÷ 0.337).

What the answer means. Babies of smokers were 1.61 times as likely to be low weight, an increase of 61% in relative terms or 15.3 points in absolute terms. Both are correct and they sound very different, so always give the absolute risks as well. When an outcome is rare, say under about 10% in every group, odds and risks are almost the same and the odds ratio is close to the risk ratio; as the outcome becomes more common, the odds ratio moves further from 1 than the risk ratio does.

Counts and proportions · LSMA free course Two bars, one for 74 smokers split into 30 low and 44 normal birth weights, and one for 115 non-smokers split into 29 low and 86 normal. Risk divides low by the whole bar, odds divide low by normal. The risk ratio is 1.61 and the odds ratio 2.02. 1 · SMOKERS · 74 BABIES 30 low 44 normal risk = 30 ÷ 74 = 0.405 odds = 30 ÷ 44 = 0.68 2 · NON-SMOKERS · 115 BABIES 29 low 86 normal risk = 29 ÷ 115 = 0.252 odds = 29 ÷ 86 = 0.34 Risk ratio · low ÷ whole group 0.405 ÷ 0.252 = 1.61 Odds ratio · low ÷ normal 0.68 ÷ 0.34 = 2.02
Bar lengths are drawn to scale, four pixels per baby. Risk compares the vermilion part with the whole bar; odds compare it with the navy part only. Because low birth weight is common here, the two ratios differ noticeably.

When an outcome is common, as low birth weight is here (59 of 189 births, 31%), the odds ratio lies further from 1 than the risk ratio. Zhang and Yu (1998, JAMA, "What's the relative risk?") proposed a widely cited formula for converting one into the other, but it is approximate and less reliable for adjusted odds ratios, so report odds ratios as odds ratios, with the absolute risks beside them. The logistic models in this lesson follow Hosmer and Lemeshow's Applied Logistic Regression (Wiley), now in its third edition (2013, with Sturdivant), the source of the birthwt data.

Testing the difference

Both tests below ask the same question: if smoking and low birth weight were unrelated, how surprising would this table be? They differ in how they calculate the answer.

The null hypothesis here is independence: knowing whether a mother smoked tells you nothing about whether her baby was low weight. Both tests assume that each birth is counted once and that births are independent of each other. They work on the counts themselves, never on percentages: a table of percentages has lost the sample size, and a test run on it would give a meaningless answer.

R
chisq.test(tab)
fisher.test(tab)
Pearson's Chi-squared test with Yates' continuity correction data: tab X-squared = 4.2359, df = 1, p-value = 0.03958 Fisher's Exact Test for Count Data data: tab p-value = 0.03618 alternative hypothesis: true odds ratio is not equal to 1 95 percent confidence interval: 1.028780 3.964904 sample estimates: odds ratio 2.014137

Reading the output

  • The chi-squared test compares each observed count with the count you would expect if the two variables were unrelated: row total × column total ÷ grand total. For smokers with a low-weight baby that is 74 × 59 ÷ 189 = 23.1, but 30 were observed. X-squared = 4.2359 sums up how far all four cells are from their expected counts. df = 1 because, once the row and column totals are fixed, one cell determines the other three. Yates' continuity correction is a small adjustment used for two-by-two tables that makes the test slightly more cautious.
  • Fisher's exact test calculates the probability of the table directly, without the approximation the chi-squared test relies on. Its output adds an odds ratio and a 95% confidence interval. The odds ratio, 2.014, differs very slightly from the 2.02 worked out by hand because Fisher's test uses a different estimation method (a conditional estimate); the difference does not matter in practice.

Both tests agree (p ≈ 0.04). Fisher's exact test is the safer choice when any expected count is small (below about 5), and it also gives the odds ratio with a confidence interval: 2.0 (1.0 to 4.0).

Here the smallest expected count is the 23.1 worked out above, well above 5, so both tests are reasonable, and they agree. In a small study, for example 12 patients with a rare mutation, some expected counts would fall below 5 and only Fisher's test would be appropriate. Notice also that the interval for the odds ratio, 1.03 to 3.96, barely excludes 1, which is consistent with a p-value barely below 0.05.

To see where X-squared comes from: for each cell, take the observed count minus the expected count, square it, divide by the expected count, and add the four results. Yates' correction subtracts 0.5 from each absolute difference before squaring, which shrinks the statistic and raises the p-value a little. chisq.test(tab, correct = FALSE) leaves it out; whichever you use, say so, because the two p-values differ slightly.

Compare these p-values, about 0.04, with the t-test on the same babies in Comparing two groups, p = 0.007. The weaker evidence is not a contradiction. Turning birth weight into "low" or "normal" throws information away: a baby of 2,490 g and one of 1,200 g count the same, and one of 2,510 g counts as normal. Cutting a continuous measurement into categories costs power, so keep the measurement when the question allows it, and use a categorical outcome when the category itself matters clinically, as the 2,500 g threshold does.

Go deeper: an odds ratio interval by hand

Fisher's test gave an exact interval for the odds ratio. A quick approximate one, called the Woolf or Wald interval, needs only the four counts. Work on the log scale. The log odds ratio is ln(2.02) = 0.704, and its standard error is the square root of 1/30 + 1/44 + 1/29 + 1/86 = 0.102, which is 0.320. The 95% interval is 0.704 ± 1.96 × 0.320, from 0.077 to 1.331, and exp() turns these back into 1.08 to 3.78.

That is close to Fisher's 1.03 to 3.96, a little narrower; the two differ more when a count is small, and the formula fails if any cell is 0. The interval is not symmetric around 2.02, because it was symmetric on the log scale. In R: exp(log(2.02) + c(-1, 1) * 1.96 * sqrt(1/30 + 1/44 + 1/29 + 1/86)).

Logistic regression: adjusting for other factors

Smoking might be linked to other factors that also affect birth weight. Logistic regression estimates the odds ratio for smoking while holding the others constant. exp() turns its coefficients into odds ratios.

Logistic regression is a model for a yes-or-no outcome. It describes the log odds of the outcome (the natural logarithm of the odds) as a baseline value plus an amount for each factor. Working on the log scale keeps predicted probabilities between 0 and 1. Each coefficient is therefore a log odds ratio, and exp(), the opposite of the natural log, turns it back into an odds ratio. A factor that tends to appear together with the exposure and also affects the outcome is called a confounder; including it in the model is what "adjusting for" means.

A model needs enough outcomes to estimate its terms. A common rough guide is at least about 10 outcome events for each coefficient estimated. Here there are 59 low-weight births and four coefficients besides the intercept (smoker, two race groups and mother's weight), about 15 per coefficient, which is adequate. Adding many more factors to the same 189 births would make the estimates unstable and their intervals very wide.

R
fit <- glm(low ~ smoke + race + lwt, data = bw, family = binomial)
round(exp(cbind(OR = coef(fit), confint(fit))), 2)

What this does

  • glm() fits a generalised linear model. The formula low ~ smoke + race + lwt says "low birth weight explained by smoking, race and mother's weight". family = binomial makes it a logistic regression, for a two-category outcome. R models the chance of the second level, "low".
  • coef(fit) gives the coefficients on the log odds scale, and confint(fit) their 95% confidence intervals.
  • cbind(OR = …, …) binds them side by side as columns, exp() turns every number into an odds ratio, and round(…, 2) keeps two decimals.

Reading the output. "Waiting for profiling to be done…" is a progress message from confint(), not an error. Each row is one term, with its odds ratio and the lower (2.5 %) and upper (97.5 %) ends of its 95% interval. Categorical factors are compared with their first level: smokesmoker compares smokers with non-smokers, and raceblack and raceother compare those groups with the "white" group. The intercept is the odds for the reference groups at a mother's weight of 0 lb, which is impossible, so it has no useful meaning here. An odds ratio above 1 means higher odds of low birth weight; below 1 means lower odds; an interval that includes 1 is compatible with no effect.

Waiting for profiling to be done... OR 2.5 % 97.5 % (Intercept) 0.90 0.17 5.40 smokesmoker 2.89 1.39 6.20 raceblack 3.63 1.34 10.08 raceother 2.64 1.19 6.06 lwt 0.99 0.97 1.00

After adjusting for recorded race and the mother's weight, the odds ratio for smoking rises from 2.0 to 2.9 (95% CI 1.4 to 6.2): the unadjusted figure was hiding part of the effect. The differences by recorded race reflect social and economic circumstances in this 1986 population, not biology; interpret such variables with care. Each extra pound of the mother's weight slightly lowers the odds (0.99 per pound).

Why did the odds ratio rise after adjustment? If smoking was more common among mothers whose other characteristics were linked to lower risk, the simple comparison would mix the harm of smoking with that lower background risk and understate it. Adjustment separates the two. Adjustment can also move an estimate the other way, towards 1, when a confounder made the exposure look worse than it is. Note too that the adjusted interval, 1.4 to 6.2, is wide: it rests on 189 births and five estimated terms.

One property of odds ratios is surprising. Unlike risk differences, an odds ratio can change when a factor is added to the model even if that factor is unrelated to the exposure, as long as it affects the outcome. Statisticians call this non-collapsibility. So a change from 2.0 to 2.9 is not by itself proof of confounding; it shows that the unadjusted and adjusted odds ratios answer slightly different questions, and the adjusted one is the comparison of mothers alike on the other factors.

The mother's weight is a continuous factor, so its odds ratio is per pound, which makes it look tiny. To express it per 10 lb, multiply its coefficient by 10 before taking exp(), as in exp(10 * coef(fit)["lwt"]). Look at more decimal places too, with round(…, 3): an upper limit printed as 1.00 has been rounded, and the extra digits show whether the interval really stops short of 1.

Key point

An observational association, even after adjustment, is not proof of cause. You can only adjust for what was measured.

A classic example: people with nicotine-stained fingers have higher rates of lung cancer. The stains do not cause cancer; both are caused by smoking. If smoking had not been recorded, no amount of adjustment could reveal that, and the stains would look like a risk factor. In this dataset, diet, income and alcohol use were not measured, so they cannot be ruled out.

Further reading · Module 2Greenland et al. 2016: 25 common misreadings of p-values and confidence intervals. · All reading for this course

Check your understanding

1. 20 of 100 treated and 10 of 100 untreated patients relapse. What is the risk ratio?

2. When should you prefer Fisher's exact test to chi-squared?

3. A logistic regression coefficient is 0.69. What odds ratio is that?

Up next

Lesson 4

Correlation and linear regression

Ask BioTutor

Create a free website with Framer, the website builder loved by startups, designers and agencies.