Simulating Trait Data on a Field Design¶
library(FielDHub)
library(ggplot2)
theme_set(theme_minimal(base_size = 12) +
theme(plot.title = element_text(face = "bold")))
Why simulate?¶
A design tells you which treatment goes on which plot. Simulation adds a trait value to every plot, so you can see what data from that design could look like before anything is planted.
FielDHub keeps the true effects behind every simulated value. That makes simulation useful for:
- Teaching: analyse data whose answer is known, and see why blocking matters.
- Planning: compare designs, replicates or block sizes before a trial.
- Testing: check an analysis script or model on data with a known truth.
- Demonstrations: realistic field books, heatmaps and examples.
Each simulated value is the trait mean plus a treatment effect, an effect for each block (or replicate, row, column, whole plot) of the design, an optional spatial trend, and a plot error. Values are Gaussian, so they are not bounded, and a treatment has the same effect at every location.
In the Shiny app¶
Generate a design and click Simulate!. Choose the trait, its mean and the plot-error (residual) SD, and optionally tick Add spatial correlation. The simulated trait appears in the field book, the heatmap and the download.
Example 1: simulate a trait¶
rcbd <- RCBD(t = 10, reps = 4, nrows = 5, seed = 2026)
sim <- simulate(rcbd, seed = 1, mean = 5, residual_sd = 0.5)
sim
FielDHub trait simulation (design_gaussian)
Plots: 40 Locations: 1 Draws: 1 Seed: 1
Trait: YIELD
Mean: 5 Residual SD: 0.5
Treatment SD: 0.625
Design effects: block = 0.25
Spatial: none
Effects behind each draw: x$effects[[i]] (location, treatment, block, residual)
ID LOCATION PLOT ROW COLUMN REP TREATMENT YIELD
1 1 loc1 101 1 1 1 T9 5.749543
2 2 loc1 102 1 2 1 T1 4.963945
3 3 loc1 206 2 1 1 T6 5.674493
4 4 loc1 205 2 2 1 T5 5.337071
5 5 loc1 207 3 1 1 T3 6.785606
6 6 loc1 208 3 2 1 T4 5.880838
... x$field_book has 40 rows and 1 draw
The simulated field book is sim$field_book. Each plot has a ROW and COLUMN, so the trait can be drawn on the field:
ggplot(sim$field_book, aes(COLUMN, ROW, fill = YIELD)) +
geom_tile(colour = "white") +
geom_text(aes(label = TREATMENT), size = 3, colour = "white") +
scale_y_reverse(breaks = 1:5) +
scale_x_continuous(breaks = 1:8) +
scale_fill_viridis_c() +
coord_equal() +
labs(title = "Simulated yield on the RCBD field", x = "Column", y = "Row",
fill = "Yield") +
theme(panel.grid = element_blank())

Example 2: compare an analysis with the truth¶
The true treatment effects are stored in sim$effects. We compare them with the treatment means of the simulated data.
book <- sim$field_book
truth <- tapply(sim$effects[[1]]$treatment, book$TREATMENT, mean)
estimate <- tapply(book$YIELD, book$TREATMENT, mean)
cor(estimate, truth)
[1] 0.9299297
recovered <- data.frame(truth = truth - mean(truth),
estimate = estimate - mean(estimate),
treatment = names(truth))
ggplot(recovered, aes(truth, estimate)) +
geom_abline(linetype = "dashed", colour = "grey50") +
geom_point(size = 3, colour = "#2C7FB8") +
geom_text(aes(label = treatment), vjust = -1, size = 3) +
coord_equal() +
labs(title = "Estimated versus true treatment effects",
x = "True effect (centred)", y = "Estimated effect (centred)")

Points close to the dashed line mean the trial recovered the treatments well. Try fewer replicates or a larger residual_sd and watch them spread.
Example 3: why blocking matters¶
An alpha lattice splits each replicate into small incomplete blocks. We simulate a field with strong differences between blocks (design_sd), then analyse the trial twice: ignoring the blocks, and including them.
alpha <- alpha_lattice(t = 30, k = 5, reps = 3, seed = 11)
sim_alpha <- simulate(alpha, seed = 3, residual_sd = 4,
design_sd = c(incomplete_block = 8))
book <- sim_alpha$field_book
book$BLOCK <- factor(paste(book$REP, book$IBLOCK))
truth <- tapply(sim_alpha$effects[[1]]$treatment, book$TREATMENT, mean)
ignoring_blocks <- tapply(book$YIELD, book$TREATMENT, mean)
fit <- lm(YIELD ~ 0 + TREATMENT + BLOCK, data = book)
with_blocks <- coef(fit)[paste0("TREATMENT", names(truth))]
c(ignoring_blocks = cor(ignoring_blocks, truth),
with_blocks = cor(with_blocks, truth))
ignoring_blocks with_blocks
0.7088849 0.8276272
compared <- rbind(
data.frame(analysis = "Ignoring blocks", truth = truth - mean(truth),
estimate = ignoring_blocks - mean(ignoring_blocks)),
data.frame(analysis = "Including blocks", truth = truth - mean(truth),
estimate = with_blocks - mean(with_blocks))
)
ggplot(compared, aes(truth, estimate)) +
geom_abline(linetype = "dashed", colour = "grey50") +
geom_point(size = 2.5, colour = "#2C7FB8") +
facet_wrap(~ analysis) +
theme(aspect.ratio = 1) +
labs(title = "Recovering treatment effects in an alpha lattice",
x = "True effect (centred)", y = "Estimated effect (centred)")

Including the blocks brings the estimates much closer to the truth. Set incomplete_block = 0 and the gain disappears, which is a good discussion point for a class.
Example 4: add a spatial trend¶
spatial adds a smooth trend over the field, so neighbouring plots are more alike. rho_row is the correlation between plots in neighbouring rows, and rho_col between plots in neighbouring columns.
prep <- partially_replicated(nrows = 10, ncols = 12, repGens = c(80, 20),
repUnits = c(1, 2), seed = 7)
sim_prep <- simulate(prep, seed = 4,
spatial = list(sd = 8, rho_row = 0.9, rho_col = 0.9))
book <- sim_prep$field_book
book$SPATIAL <- sim_prep$effects[[1]]$spatial
ggplot(book, aes(COLUMN, ROW, fill = SPATIAL)) +
geom_tile(colour = "white") +
scale_y_reverse(breaks = 1:10) +
scale_x_continuous(breaks = 1:12) +
scale_fill_distiller(palette = "RdBu", direction = 1) +
coord_equal() +
labs(title = "Simulated spatial trend", x = "Column", y = "Row",
fill = "Effect") +
theme(panel.grid = element_blank())

For Latin square and row-column designs, first use the physical layout:
rc <- row_column(t = 30, nrows = 5, reps = 3, seed = 3)
rc$fieldBook <- field_layout(rc)
simulate(rc, spatial = TRUE)
More options¶
- Several draws:
nsim = 50givesYIELD_1, …,YIELD_50. - Several locations: give a value per location, such as
mean = c(LOC1 = 6, LOC2 = 4). - Scales:
treatment_sd,design_sdandlocation_sddefault to fixed multiples ofresidual_sd, so changing the residual SD rescales everything. - Reproduce:
reproduce_simulation(sim)rebuilds the same values.
See ?simulate.FielDHub for all the arguments.