--- title: "Getting started with bayesammi" author: "Muhammad Yaseen" date: last-modified format: html: self-contained: true number-sections: true toc: true toc-depth: 3 highlight-style: pygments code-tools: true html-math-method: mathjax vignette: > %\VignetteIndexEntry{Getting started with bayesammi} %\VignetteEngine{quarto::html} %\VignetteEncoding{UTF-8} execute: echo: true warning: false message: false --- {{< include _bayesammi-macros.txt >}} ```{r setup, include = FALSE} # HTML figures are drawn as PNG and kept as WebP: lossless (the same pixels in about half the space of PNG), or # lossy at quality 90 when that is less than half the size, as for traces and densities of thousands of draws. The # PDF figures stay vector graphics. Any failure of magick (not installed, no WebP, or a broken installation whose # system libraries are missing) keeps the PNG webp <- function(path) { if (knitr::is_latex_output() || !grepl(pattern = "[.]png$", x = path)) return(path) tryCatch( { if (!requireNamespace("magick", quietly = TRUE) || !isTRUE(magick::magick_config()$webp)) return(path) img <- magick::image_read(path) out <- sub(pattern = "[.]png$", replacement = ".webp", x = path) tmp <- tempfile(fileext = ".webp") magick::image_write( image = img , path = out , format = "webp" , quality = 100 , defines = c(`webp:lossless` = "true") ) magick::image_write(image = img, path = tmp, format = "webp", quality = 90) if (file.size(tmp) < file.size(out) / 2) file.copy(from = tmp, to = out, overwrite = TRUE) unlink(c(path, tmp)) out } , error = function(e) path ) } knitr::opts_chunk$set(collapse = FALSE, comment = "", fig.width = 7, fig.height = 4.2, dev = if (knitr::is_latex_output()) "cairo_pdf" else "ragg_png", dpi = 96, fig.process = webp) if (requireNamespace("bayesammi", quietly = TRUE)) library(bayesammi) else pkgload::load_all(export_all = FALSE, helpers = FALSE, quiet = TRUE) ``` # Abstract **bayesammi** fits the AMMI model of a multi-environment trial by Bayesian methods (Crossa et al. 2011). This vignette goes from the plot data of the maize trial `Maiz` to posterior summaries, traces, the biplot with highest posterior density (HPD) regions and a written interpretation. Each later vignette takes one part further. # The data `Maiz` holds the grain yield (kg/ha) of 9 maize genotypes in 20 sites, a randomized complete block design with 4 replicates in every site, one row per plot. ```{r data} data(Maiz) str(Maiz) m <- ge_mean( .data = Maiz , .y = y , .gen = entry , .env = site ) round(m$ge_means[, 1:6]) m$grand_mean ``` # The least-squares AMMI fit The classical AMMI analysis decomposes the interaction effects by their singular values. `bayes_ammi()` starts its chains there. ```{r ls} ls_fit <- ge_ammi( .data = Maiz , .y = y , .gen = entry , .env = site , .rep = rep ) round(ls_fit$lambdas) ls_fit$sigma2 ``` # The Bayesian fit Two chains of 3000 kept iterations after a burn-in of 500 take a few seconds. The first two bilinear terms are fitted, as in Crossa et al. (2011). ```{r fit} fit <- bayes_ammi( .data = Maiz , .y = y , .gen = entry , .env = site , .rep = rep , .nIter = 3000 , .k = 2 , .burnin = 500 , .thin = 1 , .chains = 2 , .priors = bam_priors( mu_mean = 0 , mu_var = 1e+8 , tau_mean = 0 , tau_var = 1e+8 , delta_mean = 0 , delta_var = 1e+8 , lambda_mean = 0 , lambda_var = 1e+8 , nu = 1 , s2 = NULL ) , .level = 0.95 , .seed = 1 , .align = TRUE , .model = "AMMI" , .het = FALSE , .waic = TRUE ) fit ``` Each row gives the posterior mean, the posterior standard deviation and the 95% HPD interval of one parameter. `ess` is the effective sample size and `rhat` the potential scale reduction factor. Values of `rhat` near 1 show that the two chains agree. The scores of the bilinear terms are in `fit$summary` too: ```{r scores} head(x = fit$summary[block == "alpha"], n = 9) ``` # Traces and the biplot ```{r trace, fig.height = 5.5} plot(x = fit, which = "trace") ``` ```{r biplot, fig.height = 6} plot(x = fit, which = "biplot") ``` The biplot of the posterior means looks like a classical AMMI biplot. `biplots()` adds a region around every point that holds 95% (and 99%) of its posterior. A region that contains the origin marks a genotype or environment whose interaction is not different from zero. ```{r hpd, fig.width = 9, fig.height = 5.5} bp <- biplots( model = fit , burnin = 0 , thin = 1 , pb = c(0.05, 0.01) , plot_stable = TRUE , plot_unstable = TRUE , ncolors = 5 , .which = "both" , .grid = 100 ) bp$biplot bp$biplot_data[byplot == "Genotype"] ``` # Interpretation ```{r int} bam_interpret( x = fit , higher_better = TRUE , top = 5 ) ``` # Where to go next * Vignette 2 derives the model, the priors and the sampler. * Vignette 3 reproduces the published analysis of these data. * Vignettes 4 to 6 cover the biplot regions, convergence, and the choice of priors and number of terms. * Vignette 7 fits unbalanced trials and environment-specific error variances. * Vignette 8 fits the GGE (sites regression) model and finds which genotype wins where. * Vignette 9 turns the posterior into decisions: probabilities of superior performance, ranks, stability, predictions, and the choice and checking of models. * Vignette 10 fits the Bayesian Eberhart-Russell, Finlay-Wilkinson and Shukla stability models. * Vignette 11 gives the posterior distributions of classical and AMMI-based stability indices. * Vignette 12 describes the application started by `bam_app()`, which runs all of this without code. * Vignette 13 lists the corrections to version 0.3.0 and the checks of the sampler. * Vignette 14 explains the interpretation. * Vignette 15 lists the references of every method; `citation("bayesammi")` and `bam_cite()` give them too. # References Crossa, J., Perez-Elizalde, S., Jarquin, D., Cotes, J. M., Viele, K., Liu, G. and Cornelius, P. L. (2011). Bayesian estimation of the additive main effects and multiplicative interaction model. *Crop Science*, 51(4), 1458--1469. doi:10.2135/cropsci2010.06.0343