---
title: "07-01 · Choose, fit and explain a GLM"
description: "Work through pen-study examples, interpret output and check another group’s answers"
order: 701
lang: en
format:
  html:
    code-line-numbers: false
    theme: cosmo
    embed-resources: true
    code-overflow: wrap
    toc: true
    code-copy: true
execute:
  eval: false
---

## Open the class project

**Wednesday 21 October 2026, 12:30–16:00.** Open the Week 7 project provided by your instructor in **Posit Cloud**. Open the script for your group: `group-1-student.R`, `group-2-student.R`, `group-3-student.R` or `group-4-student.R`.

All required scripts and data are already there. Read `START-HERE.md` for a description of the files. If you cannot run R, use the tables and plots in `reference/` to answer the same questions. The [recap](recap.html) explains the statistical terms used below.

Work in groups of three. One person runs R, another explains the result, and the third checks the explanation against the study description. Change jobs at 13:55 and 15:10 so that everyone takes part. Write your answers in a document beside your script; save both regularly.

No new preparation is required. If you have your Week 6 notes, bring them, but the opening teaching reviews what you need.

## Review the pen experiment — 12:30–12:40

We are comparing Control and High fibre diets. There are **64 pens in four rooms**, with six pigs in every pen. Within each room, eight pens receive each diet, assigned at random. The whole pen receives the assigned diet. We follow every pen for 28 days and record one outcome per pen.

The four group datasets describe this same study design, but contain different outcomes. All are simulated teaching data. They do not provide evidence about an actual diet's effects.

Discuss these questions after reading the description:

1. Which unit is independently assigned a diet: a pig, a pen or a room? Explain your choice.
2. What does one row of this dataset describe, and how many rows should there be?
3. What is measured in your group's data? Use the table below if you need a reminder.
4. Why might pens in different rooms have different average outcomes, even under the same diet?

| Group | What the `response` column records for each pen | Family and link supplied for this first model |
|---|---|---|
| 1 | Average daily weight gain, in g/day | Gaussian family, identity link |
| 2 | 0 if no pig needed treatment; 1 if at least one did | Binomial family, logit link |
| 3 | Number of treatment events during 28 days; a pig can contribute more than one event | Poisson family, log link |
| 4 | Feed conversion ratio: kg of feed consumed per kg of weight gained | Gamma family, log link |

## Follow the worked example — 12:40–13:10

Use the [class teaching slides](07-01-glm-foundations.html). The instructor will explain the response, formula, family and link, then fit the daily-gain model and read its output with you.

In your notes, write what each part of `response ~ diet + room` refers to. Add the family and link used by your group. The **family** describes the distribution of the measured outcome after allowing for the explanatory variables. The **link** connects the expected outcome to the sum of the model terms, called the linear predictor.

The worked gain model uses an identity link: the linear predictor is already expected gain in g/day. Control and Room 1 are its **reference categories**, so the intercept describes Control pens in Room 1. The diet coefficient describes how much the expected gain changes for High fibre, comparing pens in the same room.

## Choose models for the short studies — 13:10–13:40

Spend 10 minutes on Study A, 10 minutes on Study B, and 7 minutes discussing Study C. Use the final 3 minutes to compare answers and identify anything you want the instructor to explain.

For A and B, write a formula and state the family and link. For C, begin with the treatment combinations and the question of interest; we will explain its interaction formula after the break. These are written exercises, not new datasets you need to analyse in R.

### Study A: two diets without room blocks

A researcher randomly assigns Control or High fibre to 24 pens. Each pen contains six pigs, and all pens have the same follow-up period. There are no room blocks in this study. The outcome `gain` is average daily weight gain per pen, in g/day. A Gaussian family with an identity link is supplied as a reasonable first model.

| Question | Your answer |
|---|---|
| What difference between the diets does the researcher want to estimate? | |
| What does one row describe? Which unit receives the randomly assigned diet? | |
| What is measured, and can its values be continuous or must they be counts? | |
| What formula would you write using `gain` and `diet`? | |
| Which family and link are being used? | |
| With Control as the reference diet, what would the diet coefficient mean, including its units? | |

Use the worked gain example as a guide. Include only variables described in this study; no room variable is needed here.

### Study B: counts from pens in four rooms

A researcher randomly assigns diets to 48 pens in four rooms, with equal numbers on each diet within every room. Every pen contains six pigs and is followed for 28 days. The outcome `events` is the number of treatment events per pen. A pig may be treated more than once. Start with a Poisson family and log link.

| Question | Your answer |
|---|---|
| What does one row describe, and which unit receives diet? | |
| What values can `events` take? | |
| What formula would you write using `events`, `diet` and `room`? | |
| Why should the comparison account for room? | |
| Which family and link are supplied? | |
| If the High fibre / Control expected-count ratio were 0.60, what would that mean for pens in the same room over 28 days? | |

For the last question, suppose the expected Control count were 5 events per pen. Multiplying 5 by 0.60 gives the corresponding High fibre expected count. You are practising an interpretation, not calculating a fitted result for this written study.

The Poisson model also makes assumptions about how counts vary around their expected values. We will examine these assumptions in Week 9; you do not need to diagnose this study now.

### Study C: diets in two housing conditions

A separate study uses 48 pens in four rooms. Pens receive one of two diets, Control or High fibre, and one of two housing conditions, Standard or Enriched. Every room contains all four combinations, with three pens per combination. Both treatments are assigned to pens. The outcome is average daily gain per pen, in g/day.

The researcher wants to know whether the High fibre minus Control gain difference is the same under both housing conditions.

Before the break:

1. List the four diet-and-housing combinations.
2. Explain why we need to compare diets within Standard housing and within Enriched housing, rather than only comparing one overall average for each diet.
3. Choose one room in which to compare the four predicted averages.

Keep these answers. After the break, add the model formula and explain what an interaction allows it to describe.

## Break — 13:40–13:55

## Does the diet difference change with housing? — 13:55–14:10

Follow the [workshop slides](07-02-glm-workshop.html). An **additive model** requires the same High fibre minus Control gain difference in both housing conditions. An **interaction model** allows that difference to change.

For this Gaussian identity-link example:

- `gain ~ diet + housing + room` describes the additive model.
- `gain ~ diet * housing + room` includes both treatment variables and their interaction, as well as room.

Use the four predicted gains shown in class. Subtract Control from High fibre in Standard housing, then do the same in Enriched housing. How much larger or smaller is the second difference? Give the answer in g/day and explain which two comparisons you made.

You can use `reference/factorial-predictions.csv` and its plot. `factorial-student.R` reproduces the example if you want to run it later. The `housing` variable belongs only to this separate study; do not add it to your group's diet-only data.

## Fit your group's model — 14:10–14:55

Run your assigned group script in order. Allow about 10 minutes to read the data and fit the model, 15 minutes to examine predictions, 10 minutes to write an explanation, and 10 minutes to discuss it with another group.

### 1. Check what the data describe

The first output shows example rows and a table counting pens by diet and room. Check that every room has eight pens on each diet. Write what your group's `response` measures and its units.

### 2. Read the fitted coefficients

The script fits `response ~ diet + room` using your group's supplied family and link. `Estimate` contains the fitted coefficient; `Std. Error` describes uncertainty in that estimate. The displayed intercept refers to Control in Room 1. The diet row compares High fibre with Control in the same room.

For gain with an identity link, the diet estimate is a difference in g/day. For log-link counts or feed conversion ratio, exponentiating it gives the High fibre / Control ratio of expected outcomes. For the binary outcome, use the predicted probabilities below first; the exponentiated coefficient is an odds ratio, as explained in the recap.

Keep any warning messages and ask the instructor about them. A model returning numbers does not, by itself, show that it describes the data well.

### 3. Compare the diets in Room 1

Use the Room 1 rows in the prediction output. `expected` is the model's estimated average outcome, or probability for Group 2. `lower` and `upper` are approximate 95% confidence limits for that average or probability. They show estimation uncertainty under the model assumptions, not a range containing individual pen outcomes.

Record:

| Result for Room 1 | Your value, with units |
|---|---|
| Expected Control outcome | |
| Expected High fibre outcome | |
| High fibre minus Control difference | |
| Confidence interval for each expected outcome | |
| Model's diet difference, expected-outcome ratio or odds ratio, and its interval | |

For Group 2, say **probability that at least one pig in a pen needed treatment**. Do not describe it as the probability for an individual pig. The pen's 0/1 observation cannot tell us how many of its pigs were treated.

### 4. Explain the result to another group

Write a short paragraph stating what you measured and how diet was assigned, which model you used, and what it predicts for the two diets in Room 1. Include units and the intervals showing uncertainty. Then name an assumption you would want to check before relying on the comparison, such as whether the chosen distribution describes the variation adequately.

Ask another group to read it. Can they identify the measured outcome, understand the diet comparison, and distinguish an estimated average from an individual observation? Revise any sentence that leaves those points unclear.

## Break — 14:55–15:10

## Apply the same steps to a calf study — 15:10–15:30

A new experiment uses 40 calf pens in five barns, with eight pens per barn and four calves per pen. In each barn, four pens are randomly assigned Standard feed and four Supplemented feed. Every pen is followed for 21 days.

The outcome `any_treated` is 1 if at least one calf in that pen needed treatment, and 0 otherwise. The researcher wants to compare the two feeds while allowing for differences between barns. Use the column names `feed` and `barn`. No data are supplied, so write an answer without fitting a model.

Spend **10 minutes writing, 5 minutes checking another group's answer, and 5 minutes revising**.

| What to explain | Your answer |
|---|---|
| Which unit receives feed, and what does one row describe? | |
| What does 0 or 1 mean for the outcome? | |
| What formula, family and link would you use? | |
| Why does the formula include barn? | |
| In one chosen barn, which two feed conditions would you ask R to predict? | |
| Why would the result describe a pen-level event rather than an individual calf's chance of treatment? | |

Use these same questions to check another group's answer. Give a reason for any suggested correction; do not merely mark an answer wrong.

## Discuss your group project — 15:30–15:45

Bring out your short description from the week 42 task on the course website. Tell the class what question you want to answer, what you will measure, and which groups or conditions you will compare. Say which dataset you plan to use and whether you can access it.

Write down what you still need to find out about treatment assignment, sampling or measurements. If your preferred data are unavailable, retain one of the supplied project datasets so you can continue working.

## Before you leave — 15:45–16:00

Individually, explain why `response ~ diet + room` does not tell another analyst everything needed to fit your model: what must they also know about the response, family and link?

Use your output to write one comparison of the two diets in Room 1, with units and a statement about uncertainty. Save your script, written answers and prediction plot. Complete the [after-class work](after-class.html) before Week 8 on **28 October**.
