Randomisation
Foundations and R refresher
Gavin Simpson
Aarhus University
Wednesday, 21 October 2026
Today
12:30–12:40
Retrieval questions
12:40–13:15
Randomisation teaching
13:15–13:40
R refresher and demonstration
13:40–13:55
Break
13:55–16:00
Workshop, second break, debrief and exit
No preparation was required. Explain that the first hour restores the design ideas and R vocabulary needed for the workshop. The two breaks are 13:40–13:55 and 14:55–15:10.
Retrieve
For a feed trial in which every pig in a pen receives the same diet:
What is the experimental unit?
What is a treatment level?
What counts as replication of a diet?
When would diet be confounded with room?
Give students two minutes alone, then two minutes with a neighbour. Expected: the pen; one named diet condition; independently treated pens; diet is confounded with room if the diets and rooms cannot be separated, for example if each room receives only one diet.
Source: Ruxton, Graeme D., and Nick Colegrave. 2016. Experimental Design for the Life Sciences . 4th ed. Oxford: Oxford University Press. Chapter 7, sections 7.1–7.4, pp. 101–108. https://www.oup.com.au/books/higher-education/biological-sciences/9780198717355
Two meanings of random
Selects units from a population
Assigns available units to treatments
Supports generalisation to the sampled population
Supports a fair treatment comparison
Concerns who or what enters the study
Concerns which treatment each unit receives
Today we randomise treatment allocation. We do not claim the 24 pens are a random sample of all pig pens.
Keep sampling and allocation separate. Random allocation does not by itself make the animals representative of a wider population. The scope of inference still depends on how the animals, pens and conditions entered the study.
Source: Ruxton, Graeme D., and Nick Colegrave. 2016. Experimental Design for the Life Sciences . 4th ed. Oxford: Oxford University Press. Chapter 7, sections 7.1–7.4, pp. 101–108. ISBN 978-0-19-871735-5. https://www.oup.com.au/books/higher-education/biological-sciences/9780198717355
What is randomised?
Ask what differs between the two halves. The units are alike, but only the right-hand process prevents the researcher from choosing assignments. Point out that the treatment is attached to the pen, so the pen must appear once in the allocation list.
Illustration created for this course. Source for the design principles: Ruxton, Graeme D., and Nick Colegrave. 2016. Experimental Design for the Life Sciences . 4th ed. Oxford: Oxford University Press. Chapter 7, sections 7.1–7.4, pp. 101–108. ISBN 978-0-19-871735-5. https://www.oup.com.au/books/higher-education/biological-sciences/9780198717355
The randomisation unit
The experimental unit is the smallest unit that can receive a treatment independently of other units.
A diet delivered to a whole pen randomises pens
Measuring six pigs in a pen does not create six allocations
Recording one row per pen keeps the design visible
If individual pigs cannot receive different diets independently, pigs must not be shuffled as though they could.
Connect directly to Classes 1 and 2. The allocation table is a practical test of whether students have identified the experimental unit correctly.
Source: Ruxton, Graeme D., and Nick Colegrave. 2016. Experimental Design for the Life Sciences . 4th ed. Oxford: Oxford University Press. Chapter 7, section 7.2, pp. 102–105. ISBN 978-0-19-871735-5. https://www.oup.com.au/books/higher-education/biological-sciences/9780198717355
Same question, different units
Reuse this earlier course illustration to retrieve the scale of treatment application. Individual animal, pen and whole house can each be the experimental unit in a different feasible design. The randomisation list must match the actual scale at which treatments can differ independently.
Source: Ruxton, Graeme D., and Nick Colegrave. 2016. Experimental Design for the Life Sciences . 4th ed. Oxford: Oxford University Press. Chapter 7, section 7.2, pp. 102–105. ISBN 978-0-19-871735-5. https://www.oup.com.au/books/higher-education/biological-sciences/9780198717355
A complete design
A completely randomised single-factor design has:
one experimental factor
two or more treatment levels
independent experimental units
every permitted allocation determined by a random process
In our case, the factor is diet , with four levels assigned to 24 pens.
“Completely” describes the absence of a blocking restriction. It does not mean careless or undocumented. The fixed numbers of treatment labels give balance overall; their ordering among pens is random.
Source: Ruxton, Graeme D., and Nick Colegrave. 2016. Experimental Design for the Life Sciences . 4th ed. Oxford: Oxford University Press. Chapter 7, sections 7.1–7.4, pp. 101–108. ISBN 978-0-19-871735-5. https://www.oup.com.au/books/higher-education/biological-sciences/9780198717355
Balance is different
Balance means equal replication of treatment levels.
Randomisation means chance determines which unit receives which label.
Six labels per diet, shuffled
Yes
Yes
Control, A, B, C repeated
Yes
No
24 independent draws with replacement
Not guaranteed
Yes
Students often treat balance as evidence of randomisation. The fixed repeating sequence is predictable and may coincide with position or time. Independent draws with replacement are random but may produce unequal treatment counts; that is not the design wanted here.
Source: Ruxton, Graeme D., and Nick Colegrave. 2016. Experimental Design for the Life Sciences . 4th ed. Oxford: Oxford University Press. Chapter 7, sections 7.1–7.4, pp. 101–108. ISBN 978-0-19-871735-5. https://www.oup.com.au/books/higher-education/biological-sciences/9780198717355
Not random allocation
Alternation: first unit Control, next unit Diet A
Baseline choice: give the lightest animals the experimental diet
Repeating pattern: Control, A, B, C down every room
Convenience: use whichever treatment is easiest to deliver next
These rules are predictable or depend on a unit characteristic. None uses a random process.
Ask students to name the risk in each rule. Alternation and repeating patterns allow foreknowledge and may align with time or position. Baseline choice makes treatment groups differ by construction. Convenience permits researcher choice.
Source: Ruxton, Graeme D., and Nick Colegrave. 2016. Experimental Design for the Life Sciences . 4th ed. Oxford: Oxford University Press. Chapter 7, sections 7.1–7.4, pp. 101–108. ISBN 978-0-19-871735-5. https://www.oup.com.au/books/higher-education/biological-sciences/9780198717355
Confounded by construction
Reuse this earlier course illustration. If every blue treatment unit is in one house and every orange unit in another, treatment is confounded with house. A random draw at the wrong scale cannot repair a design with one house per treatment.
Source: Ruxton, Graeme D., and Nick Colegrave. 2016. Experimental Design for the Life Sciences . 4th ed. Oxford: Oxford University Press. Chapter 7, sections 7.1–7.4, pp. 101–108. ISBN 978-0-19-871735-5. https://www.oup.com.au/books/higher-education/biological-sciences/9780198717355
Why randomise?
Random allocation:
removes deliberate or unconscious choice from assignment
gives uncontrolled unit differences a chance to be spread among treatments
provides the basis for probability-based analysis
makes the allocation procedure auditable when it is recorded
Randomisation protects the allocation process; it does not certify that the realised groups are identical.
Use “chance to be spread” rather than claiming randomisation balances every characteristic. In a finite experiment, chance imbalance remains possible.
Source: Ruxton, Graeme D., and Nick Colegrave. 2016. Experimental Design for the Life Sciences . 4th ed. Oxford: Oxford University Press. Chapter 7, sections 7.1–7.4, pp. 101–108. ISBN 978-0-19-871735-5. https://www.oup.com.au/books/higher-education/biological-sciences/9780198717355
What it cannot promise
One random allocation may still produce:
more heavy animals in one treatment
more of one treatment in a particular room
a visually surprising run of neighbouring assignments
It also cannot repair:
too few independent experimental units
poor measurement
treatment contamination or interference
biased selection into the study
This is the key conceptual preparation for the audit. A surprising allocation may be genuine. The response is to inspect the design against pre-specified constraints, not to repeatedly change the seed until the table looks attractive.
Source: Ruxton, Graeme D., and Nick Colegrave. 2016. Experimental Design for the Life Sciences . 4th ed. Oxford: Oxford University Press. Chapter 7, sections 7.1–7.4, pp. 101–108. ISBN 978-0-19-871735-5. https://www.oup.com.au/books/higher-education/biological-sciences/9780198717355
Keep the record
An allocation is not just an R object visible on one laptop. Retain the seed, the exact code, the ordered unit list and the final assignment. The final record matters because operational changes can occur after code is run.
The unit table
library ("tibble" )
library ("dplyr" )
library ("ggplot2" )
pens <- tibble (
pen_id = sprintf ("P%02d" , 1 : 24 ),
room = rep (c ("North" , "Central" , "South" ), each = 8 ),
position = rep (1 : 8 , times = 3 )
)
pens
Recap tibble() and rep(). sprintf() is provided rather than required knowledge; it creates stable IDs P01 to P24. One row represents one independently treated pen. Preserve this row order as part of the record.
Treatment labels
treatments <- rep (
c ("Control" , "Diet A" , "Diet B" , "Diet C" ),
each = 6
)
tibble (diet = treatments) |>
count (diet)
# A tibble: 4 × 2
diet n
<chr> <int>
1 Control 6
2 Diet A 6
3 Diet B 6
4 Diet C 6
The vector contains exactly six copies of each treatment label.
rep(..., each = 6) creates the planned overall replication before any shuffling. This is how the example combines balance with randomisation.
Draw once
set.seed (123 )
allocation <- pens |>
mutate (diet = sample (treatments))
allocation
set.seed() fixes the random-number stream
sample() permutes the 24 labels
mutate() attaches one label to each pen
Base R performs the random operation. Tidyverse creates and modifies the table. Run the chunk twice to demonstrate reproducibility, then change the seed and show that the allocation changes. The seed is not evidence that the allocation was good; it makes the process reproducible.
Check the total
allocation |>
count (diet)
# A tibble: 4 × 2
diet n
<chr> <int>
1 Control 6
2 Diet A 6
3 Diet B 6
4 Diet C 6
Checking is part of producing the allocation—not an optional extra.
Ask what this check can establish and what it cannot. It confirms treatment totals, but not that all pen IDs are unique, that the correct units were randomised, or that the distribution within rooms is acceptable.
Check the rooms
allocation |>
count (room, diet)
# A tibble: 12 × 3
room diet n
<chr> <chr> <int>
1 Central Control 2
2 Central Diet A 1
3 Central Diet B 1
4 Central Diet C 4
5 North Control 2
6 North Diet A 2
7 North Diet B 3
8 North Diet C 1
9 South Control 2
10 South Diet A 3
11 South Diet B 2
12 South Diet C 1
What does overall balance conceal?
Seed 123 gives: Central A 1, B 1, C 4, Control 2; North A 2, B 3, C 1, Control 2; South A 3, B 2, C 1, Control 2. Let students describe the result before judging it. This allocation is valid for the completely randomised design but noticeably imbalanced within rooms.
See the layout
The plot preserves room and position. A bar chart of total counts would hide the spatial pattern. The figure is descriptive: it helps audit the realised design without turning randomisation into a search for the prettiest layout.
Grouped operations
allocation |>
group_by (room) |>
count (diet) |>
ungroup ()
group_by() makes later operations act within each room
ungroup() removes that instruction when it is no longer needed
This is a syntax rehearsal for the restricted allocation later. Emphasise that grouping changes the meaning of later verbs. Always ungroup when the grouped structure should not persist.
Before the break
You should now be able to:
identify the unit being randomised
distinguish random allocation from random sampling
distinguish balance from randomisation
generate and audit an allocation in R
explain why a valid randomisation can look uneven
Take questions, then start the 13:40–13:55 break. Ask students to return with RStudio open and the student worksheet available.
Sources
Ruxton, Graeme D., and Nick Colegrave. 2016. Experimental Design for the Life Sciences . 4th ed. Oxford: Oxford University Press. Chapter 7, sections 7.1–7.4, pp. 101–108. ISBN 978-0-19-871735-5. Publisher page .
R Core Team. 2026. R: A Language and Environment for Statistical Computing . Vienna: R Foundation for Statistical Computing. R project .
Wickham, Hadley, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. doi:10.21105/joss.01686 .
Post-class reading: Ruxton and Colegrave, Chapter 7, sections 7.1–7.4, pp. 101–108. Chapter 9 is deliberately deferred until the Week 4 materials.