# Build the GxE matrices: no need to change or look at any code in here #---- Manual construction if(nEnv == 2){ cov12 <- cor12*sqrt(Venv1*Venv2) # Calculate genetic covariance between environment 1 & 2 Ge <- matrix(data = c(Venv1,cov12,cov12,Venv2), nrow = nEnv, ncol = nEnv) Ce <- cov2cor(Ge) # Genetic correlation matrix De <- diag(diag(Ge)) # Genetic variance matrix k = nEnv # Number of covariates } #---- FieldSimR construction if(nEnv > 2){ # 1. Simulate a between-environment genetic correlation matrix, Ce, for all environments # in the TPE using FieldSimR's function struc_cor_mat(). Ce <- struc_cor_mat(n = nEnv, base.cor = rho, rank = k, skew = gamma) plot_hist(df = Ce[upper.tri(Ce)]) # distribution of genetic correlations mean(Ce[upper.tri(Ce)]) # Mean genetic correlation #2. Simulate genetic variances. # If you want equal varinces in environments, we use the correlation matrix as our GxE matrix # If you want heterogenous variances, sample the variances with a gamma distrbution if(equal_vars == TRUE){ Ge <- Ce De <- diag(1, nrow = nEnv, ncol = nEnv) } else { De <- skew_diag_mat(n = nEnv, shape = alpha, scale = beta) plot_hist(df = diag(De)) # distrbution of genetic variances mean(diag(De)) # mean genetic variance within environments # Construct the between-environment genetic variance matrix, Ge. Ge <- sqrt(De) %*% Ce %*% sqrt(De) } }