## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = NA, warning = FALSE, message = FALSE, fig.align = "center", fig.width = 7, fig.height = 4.5, out.width = "85%", dpi = 150 ) ## ----setup-------------------------------------------------------------------- library(FielDHub) library(ggplot2) theme_set(theme_minimal(base_size = 12) + theme(plot.title = element_text(face = "bold"))) ## ----------------------------------------------------------------------------- rcbd <- RCBD(t = 10, reps = 4, nrows = 5, seed = 2026) sim <- simulate(rcbd, seed = 1, mean = 5, residual_sd = 0.5) sim ## ----------------------------------------------------------------------------- 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()) ## ----------------------------------------------------------------------------- book <- sim$field_book truth <- tapply(sim$effects[[1]]$treatment, book$TREATMENT, mean) estimate <- tapply(book$YIELD, book$TREATMENT, mean) cor(estimate, truth) ## ----fig.width = 5.5, fig.height = 5, out.width = "65%"----------------------- 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)") ## ----------------------------------------------------------------------------- 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)) ## ----fig.width = 8, fig.height = 4.5, out.width = "90%"----------------------- 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)") ## ----------------------------------------------------------------------------- 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 ## ----fig.width = 6.5, fig.height = 5, out.width = "70%"----------------------- 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()) ## ----eval = FALSE------------------------------------------------------------- # rc <- row_column(t = 30, nrows = 5, reps = 3, seed = 3) # rc$fieldBook <- field_layout(rc) # simulate(rc, spatial = TRUE) ## ----------------------------------------------------------------------------- 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