# ============================================================ # Exercise 2: Should we select for stability? # # This simulation compares breeding programs that place # different emphasis on mean performance # versus the stability of their performance across environments. # # Work through the script following the questions in # the worksheet # # ============================================================ rm(list = ls()) # Install packages if required # install.packages("FieldSimR") # install.packages("AlphaSimR") # Load packages library(AlphaSimR) library(FieldSimR) library(data.table) library(dplyr) library(ggplot2) # ============================================================ # Step 1: Define the GxE structure of your TPE # ============================================================ # Choose the number of environments in your TPE. # For two environments, use the manual parameters below. # For more than two, use the FieldSimR parameters. nEnv <- 2 # --- Two environments: manual construction # Change these values to explore different GxE scenarios. Venv1 <- 1 # Genetic variance in environment 1 Venv2 <- 1 # Genetic variance in environment 2 cor12 <- 0.4 # Genetic correlation between environments # --- More than two environments: FieldSimR # Change these parameters if nEnv > 2. # Genetic correlations rho <- 0.6 # Baseline genetic correlation k <- nEnv # Rank of correlation matrix (max = nEnv) gamma <- -0.4 # Skewness of correlation distribution # Genetic variances equal_vars <- TRUE # TRUE = equal variances across environments # FALSE = sample variances from a gamma distribution alpha <- 1.5 # Gamma distribution shape parameter beta <- 1 # Gamma distribution parameter # --- Build the GxE matrices # Run the script below to construct the matrices # using the parameters specified above. source("BuildGeMatrices.R") # --- Check the GxE structure # Plot the genetic correlations plot_matrix(Ce) # Summarise the correlations summary(Ce[upper.tri(Ce)]) # Plot the distribution of genetic variances hist(diag(Ge), ylab = "Number of environments", xlab = "Genetic variance", main = "Distribution of genetic variances") # ============================================================ # Step 2: Create the founding population # ============================================================ # Load the simulation parameters source("GlobalParameters.R") # Create the founding doubled haploid population source("CreateFoundingDHs.R") # Inspect the founding population FoundingDH # --- Check the simulated genetic values # Calculate genetic values for each individual # in each environment env_gv <- FoundingDH@gv[, -1, drop = FALSE] %*% t(covs_tpe) # Compare the simulated covariance matrix with # the target GxE matrix. # Points should be close to the 1:1 line. plot(var(env_gv), Ge, xlim = c(-2,2), ylim = c(-2,2));abline(0, 1) # ============================================================ # Step 3: Run the breeding program # ============================================================ # Define the selection strategies. # # The index is: # Index = Mean GV - lambda * SD(GV) # # lambda = 0: selection for mean performance only # Higher lambda: greater penalty on instability # # Try changing the lambda values, but don't rename/delete scenarios lambda_scenarios <- list( zero = 0, low = 0.3, med = 0.7, high = 1 ) # Run the breeding program for each selection strategy. source("RunBreedingProgram.R") # ============================================================ # Step 4: Compare responses to selection (population level) # ============================================================ # Generate the plots and summary tables source("PlotGain.R") # --- Genetic gain # Mean genetic gain across selection cycles gain_cycles # Genetic gain at the end of the breeding program gain_final # --- Stability # Changes in stability across selection cycles stability_cycles # Stability at the end of the breeding program stability_final #---- Gain in indexes index_responses # ============================================================================== # Step 5: Compare the best genotypes (individual level) # ============================================================================== # Identify the best genotypes under each selection # strategy and compare their performance. source("CompareBestGenotypes.R") # --- Compare index values # Compare the best genotypes when evaluated using # the different selection indices. best_genotype_index # --- Compare performance across environments # Plot the genetic values of the best genotypes by index # across environments, averaged over reps best_genotype_envs