library(FielDHub)
library(ggplot2)
theme_set(theme_minimal(base_size = 12) +
theme(plot.title = element_text(face = "bold")))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:
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.
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.
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 drawThe 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())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.9299297recovered <- 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.
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.
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]]$spatialggplot(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)You rarely know how strongly neighbouring plots are correlated. Give
a range instead of a value, and every simulated trial gets its own
correlations, drawn uniformly from that range. With several locations,
each location gets its own pair. The true values are returned in
spatial_draws:
sim_many <- simulate(prep, nsim = 4, seed = 4,
spatial = list(sd = 8, rho_row = c(0.3, 0.9), rho_col = c(0.1, 0.6)))
sim_many$spatial_draws
draw LOCATION rho_row rho_col
1 1 LOC1 0.6514802 0.1044729
2 2 LOC1 0.4762438 0.2386875
3 3 LOC1 0.7881445 0.2302139
4 4 LOC1 0.7346435 0.5530461A correlation you leave out is drawn from 0.15 to 0.9, and so are
both with spatial = TRUE. A single number, such as
rho_row = 0.6, stays fixed. This is useful when you test an
analysis or a design: it should work over a range of fields, not only
the one you guessed.
nsim = 50 gives
YIELD_1, …, YIELD_50.mean = c(LOC1 = 6, LOC2 = 4).treatment_sd,
design_sd and location_sd default to fixed
multiples of residual_sd, so changing the residual SD
rescales everything.reproduce_simulation(sim)
rebuilds the same values.See ?simulate.FielDHub for all the arguments.