11  Generalised Linear Models: Binary Data

Week 11: Logistic regression for presence/absence, alive/dead and other yes/no responses

Many ecological questions have a yes/no answer. Is the species present at this site? Did the seedling survive? Did the coral bleach? Did the bird nest successfully? This week you will model these binary responses with logistic regression, a GLM with a binomial distribution and a logit link. It works the same way as the count GLMs from Week 8. Workshop materials are available in the GitHub repository ECS200.

TipLearning outcomes

By the end of this workshop you should be able to:

  1. Explain why a binary response needs logistic regression, and what probability, odds and the logit link are.
  2. Fit and check a logistic regression in R.
  3. Turn coefficients into odds ratios and predicted probabilities.
  4. Write up the results of a logistic regression.

Before we start

frog_ponds.csv

Code
# Week 11 workshop - GLMs for binary data
# Written by Nicholas Wu, 1234578, 06/05/2027

library(tidyverse)   # data import, wrangling and plotting
library(performance) # checking model assumptions
library(see)         # draws the plots for performance
library(ggeffects)   # predictions from models for plotting

# setwd("C:/Users/YOUR-NAME/Documents/ECS200")

Study context

Farm dams can be important breeding habitat for frogs, but only if they are suitable. Fringing vegetation provides shelter and calling sites, while introduced fish such as mosquitofish (Gambusia holbrooki) eat eggs and tadpoles.

In spring, 90 farm dams were surveyed once at night for a calling tree frog. For each dam, the survey recorded whether the frog was detected (1) or not (0), the percentage of the shoreline with fringing vegetation, and whether fish were present.

Question: Is frog occurrence related to fringing vegetation and the presence of fish?

A green tree frog (Litoria caerulea).
Code
frog_data <- readr::read_csv("data/frog_ponds.csv") %>%
  dplyr::mutate(fish = factor(fish, levels = c("absent", "present")))

dplyr::glimpse(frog_data)
Rows: 90
Columns: 4
$ pond_id      <chr> "D01", "D02", "D03", "D04", "D05", "D06", "D07", "D08", "…
$ veg_cover    <dbl> 45, 39, 48, 92, 84, 52, 44, 34, 2, 12, 69, 26, 23, 34, 78…
$ fish         <fct> present, absent, absent, absent, absent, absent, present,…
$ frog_present <dbl> 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, …
Code
# How many dams had frogs?
frog_data %>%
  dplyr::count(frog_present)
# A tibble: 2 × 2
  frog_present     n
         <dbl> <int>
1            0    47
2            1    43

1. Background

Probability and odds

For a binary response we model the probability (p) that the outcome is 1 (e.g. that the frog is present). Probabilities are stuck between 0 and 1, so a straight line does not work: it will eventually predict probabilities below 0 or above 1. Instead, logistic regression models the log-odds:

\[ \text{odds} = \frac{p}{1-p} \qquad\qquad \log\left(\frac{p_i}{1-p_i}\right) = \beta_0 + \beta_1 x_i \]

  • Odds compare the chance of “yes” with the chance of “no”. p = 0.5 gives odds of 1 (1:1), p = 0.8 gives odds of 4 (4:1).
  • The logit link, log(odds), turns a probability between 0 and 1 into a number between −∞ and +∞, so a straight line is fine on that scale.
  • On the probability scale the line becomes an S-shaped curve that never goes below 0 or above 1.
  • \(e^{\beta_1}\) is the odds ratio: how much the odds are multiplied for every one-unit increase in x. An odds ratio > 1 means p increases with x, < 1 means it decreases, and 1 means no effect.

The response follows a binomial distribution (for 0/1 data, also called the Bernoulli distribution), so in R we use glm(..., family = binomial).

Use the app to see how the intercept and slope control the S-curve, and why a straight line (lm()) fails for binary data.

App: the logistic curve

2. Visualise

Binary data are hard to see on a scatterplot because every point sits at 0 or 1. Two useful plots are the raw 0/1 points with a fitted logistic curve on top, and the proportion of 1s in each group.

Code
# Raw 0/1 data (jittered vertically a little) with a logistic curve for each fish group
ggplot2::ggplot(frog_data, ggplot2::aes(x = veg_cover, y = frog_present, colour = fish)) +
  ggplot2::geom_jitter(height = 0.03, width = 0, alpha = 0.6) +
  ggplot2::geom_smooth(method = "glm", method.args = list(family = binomial), se = FALSE) +
  ggplot2::scale_colour_manual(values = c("absent" = "grey40", "present" = "#e12744")) +
  ggplot2::labs(x = "Fringing vegetation (% of shoreline)", y = "Frog detected (1) or not (0)",
                colour = "Fish") +
  ggplot2::theme_classic()

Code
# Proportion of dams with frogs, with and without fish
frog_data %>%
  dplyr::group_by(fish) %>%
  dplyr::summarise(n_dams      = dplyr::n(),
                   n_frogs     = sum(frog_present),
                   prop_frogs  = mean(frog_present)) # mean of 0/1 values = proportion of 1s
# A tibble: 2 × 4
  fish    n_dams n_frogs prop_frogs
  <fct>    <int>   <dbl>      <dbl>
1 absent      55      33      0.6  
2 present     35      10      0.286

Frogs were more often detected at dams with more fringing vegetation, and less often at dams with fish.

3. Check assumptions

Residuals from binary data are always strange-looking (every residual comes from a 0 or a 1), so the usual residual plots are not useful. Instead:

Assumption How to check
Independent observations Study design: one survey per dam ✅. Repeated surveys of the same dam would need a mixed-effects model (Week 12)
The relationship is linear on the logit scale Binned residuals: group observations by their fitted value and average the residuals in each bin. Most bins should fall inside the error bounds
Enough data A rough rule is at least 10 observations of the rarer outcome (0 or 1) for every predictor. Here both outcomes have more than 40 dams, and there are only 2 predictors ✅
Predictors are not strongly correlated performance::check_collinearity()

Overdispersion cannot happen with 0/1 data, so there is nothing to check for it.

Code
frog_glm <- glm(frog_present ~ veg_cover + fish,
                family = binomial,
                data = frog_data)

# Binned residuals: points should mostly fall inside the grey bounds
plot(performance::binned_residuals(frog_glm))

Code
# All diagnostics together
performance::check_model(frog_glm)

The binned residuals fall inside the bounds and the collinearity is low, so the model is appropriate.

Situation What to do
Imperfect detection: a single survey can miss a species that is really there (a false absence) Survey each site several times and use an occupancy model, which estimates detection and occupancy separately (Specht et al., 2017). Logistic regression then models “detected”, not “present”
Successes out of a known number of trials (e.g. 7 of 20 eggs hatched) Binomial GLM with cbind(successes, failures) ~ x as the response
A continuous proportion (e.g. % cover) Beta regression (e.g. glmmTMB package with family = beta_family())
Repeated surveys at the same sites, or sites grouped in regions Mixed model, lme4::glmer(..., family = binomial) (Week 12)
Warning “fitted probabilities numerically 0 or 1 occurred” Complete separation: a predictor perfectly predicts the outcome (e.g. all dams with fish have no frogs). Estimates are unreliable, so collect more data or simplify the model
A time until the event happens (e.g. days until death) Survival analysis (Week 13)

4. Model

Code
summary(frog_glm)

Call:
glm(formula = frog_present ~ veg_cover + fish, family = binomial, 
    data = frog_data)

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept) -1.53941    0.56548  -2.722  0.00648 ** 
veg_cover    0.03970    0.01015   3.910 9.21e-05 ***
fishpresent -1.70076    0.54490  -3.121  0.00180 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 124.589  on 89  degrees of freedom
Residual deviance:  96.466  on 87  degrees of freedom
AIC: 102.47

Number of Fisher Scoring iterations: 4

The output gives everything you need:

  • Coefficients: one row per term.
    • Estimate: the effect on the log-odds scale. (Intercept) is the log-odds of detecting frogs at a dam with 0% vegetation and no fish (the reference level). veg_cover is the change in log-odds for each 1% more vegetated shoreline. fishpresent is the difference in log-odds between dams with and without fish. A positive estimate means frogs are more likely to be detected, a negative one less likely.
    • Std. Error: the uncertainty in each estimate.
    • z value: the estimate divided by its standard error.
    • Pr(>|z|): the p-value, the probability of an effect this large if the true effect were zero.
  • Dispersion parameter for binomial family taken to be 1: fixed at 1 for 0/1 data, which is why there is no overdispersion to check.
  • Null deviance and Residual deviance: how badly a model with no predictors and our model fit the data. The bigger the drop, the more the predictors explain.
  • AIC: compares models fitted to the same data (lower is better).
  • Number of Fisher Scoring iterations: how many steps R took to fit the model. You can ignore it unless it is very large (over about 25), which can be a sign of complete separation.

As in Week 8, the coefficients are on the link scale (here log-odds). Back-transform them with exp() to get odds ratios.

Code
exp(coef(frog_glm))     # odds ratios
(Intercept)   veg_cover fishpresent 
  0.2145073   1.0404985   0.1825454 
Code
exp(confint(frog_glm))  # 95% confidence intervals for the odds ratios
                 2.5 %    97.5 %
(Intercept) 0.06618091 0.6201455
veg_cover   1.02124027 1.0630939
fishpresent 0.05866736 0.5068468

Odds ratios are still a bit abstract, so it helps to show predicted probabilities.

Code
frog_pred <- ggeffects::predict_response(frog_glm, terms = c("veg_cover [all]", "fish"))

plot(frog_pred, show_data = TRUE, jitter = 0.03) +
  ggplot2::scale_colour_manual(values = c("grey40", "#e12744")) +
  ggplot2::scale_fill_manual(values = c("grey40", "#e12744")) +
  ggplot2::labs(title = NULL, x = "Fringing vegetation (% of shoreline)",
                y = "Probability of detecting frogs", colour = "Fish", fill = "Fish")

Code
# Predicted probability at 10% and 90% vegetation cover, with and without fish
ggeffects::predict_response(frog_glm, terms = c("veg_cover [10, 90]", "fish"))
# Predicted probabilities of frog_present

fish: absent

veg_cover | Predicted |     95% CI
----------------------------------
       10 |      0.24 | 0.11, 0.45
       90 |      0.88 | 0.73, 0.96

fish: present

veg_cover | Predicted |     95% CI
----------------------------------
       10 |      0.06 | 0.02, 0.18
       90 |      0.58 | 0.35, 0.79

5. Interpret

Term Odds ratio 95% CI Meaning
veg_cover 1.04 1.021–1.063 Each extra 1% of vegetated shoreline multiplies the odds of detecting frogs by 1.04. For a 10% increase: ×1.49
fishpresent 0.18 0.06–0.51 With fish present, the odds of detecting frogs are 0.18 times the odds without fish, i.e. the odds are reduced by 82%
ImportantOdds are not probabilities

An odds ratio of 0.2 does not mean the probability is 80% lower. The change in probability depends on where you are on the S-curve. That is why it is good practice to also report predicted probabilities, as in the results sentence below.

Methods sentence: Frog detection (detected/not detected) was modelled with a binomial generalised linear model (logistic regression) using the glm() function in R, with fringing vegetation cover and fish presence as predictors. Model fit was assessed with binned residuals using the performance::binned_residuals() function. Significance was set at α = 0.05.

Results sentence:

The probability of detecting frogs increased with fringing vegetation (odds ratio = 1.04 per 1% cover, 95% CI 1.021–1.063, p < 0.001) and was lower at dams with fish (odds ratio = 0.18, 95% CI 0.06–0.51, p < 0.001; Fig. 1). At dams without fish, the predicted probability of detecting frogs rose from 0.24 at 10% vegetation cover to 0.88 at 90% cover, whereas at dams with fish it rose from 0.06 to 0.58. Because each dam was surveyed once, these are probabilities of detection, which may underestimate true occupancy.

Assessment

Task to complete before the end of the workshop.

week11_assessment.R coral_bleaching.csv

Coral bleaching after a marine heatwave. Heat stress on coral reefs is measured as degree heating weeks (DHW): how much, and for how long, the water was warmer than the usual summer maximum. NOAA warns that significant bleaching is likely above 4 DHW and severe bleaching above 8 DHW. After a marine heatwave, divers recorded whether 150 tagged coral colonies were bleached (1) or not (0), together with the heat stress at each colony’s location (dhw) and the colony’s depth (depth_m).

  1. Import the data and check its structure. How many colonies bleached?
  2. Plot the bleaching data against DHW and against depth. Describe the patterns.
  3. Fit a logistic regression with dhw and depth_m as predictors.
  4. Check the model (binned residuals and collinearity). Is the model appropriate?
  5. Calculate the odds ratios and 95% CIs. In words, how do the odds of bleaching change with each extra DHW and with each extra metre of depth?
  6. Use ggeffects::predict_response() to find the predicted probability of bleaching for a colony at 5 m depth at 4 DHW and at 8 DHW, and plot the predicted probability against DHW.
  7. Write a results sentence.
  8. The study recorded each colony once, a few weeks after the heatwave. Suggest one reason why a colony recorded as “not bleached” might be misclassified, and how the design could be improved.

Submit one R script (.R) before the end of the class. Write your answers to written questions as # comments next to the code that produced them.

Score What it looks like
0 No submission or no attempt.
0.25 Submitted, but a very poor attempt (e.g. incomplete, or lots of gaps).
0.5 Most questions answered, or all answered but most annotations missing.
0.75 All questions answered, but some annotations missing or some answers incorrect.
1 Everything completed, with annotations on all code so the marker can follow it.

Tip: an annotation is a short # comment that says what the line does and why. For example: # remove the one plant with a stem dry mass of 7.4 g (data entry error).

Additional resources