h2.Y <- VarGY/VarPY # Heritability of Y
Rg <- cor(G.X, G.Y) # Genetic correlation
Rp <- cor(P.X, P.Y) # Phenotypic correlation
# Store results
Output[Igen,1:9] <- c(Igen, VarGX, VarGY, mean(P.X), mean(P.Y),
h2.X, h2.Y, Rg, Rp)
# Apply Selection. Note that selection here is only a function of X
ParentX <- SELECTION(P.X, G.Xmatrix, T0[Igen])
ParentY <- SELECTION(P.X, G.Ymatrix, T0[Igen])
ParentC <- SELECTION(P.X, G.Cmatrix, T0[Igen])
# Form Gamete pool
GameteX <- apply(ParentX, 1, GAMETE, X.loci)
GameteX <- t(GameteX) # Convert to proper matrix
GameteY <- apply(ParentY, 1, GAMETE, Y.loci)
GameteY <- t(GameteY) # Convert to proper matrix
GameteC <- apply(ParentC, 1, GAMETE, C.loci)
GameteC <- t(GameteC) # Convert to proper matrix
N.Parents <- nrow(ParentX) # Number of available parents
n <- seq(1, N.Parents) # sequence 1 to N.Parents
# Get 2*N.Pop random indices with replacement
G.Index <- sample(x¼n, size¼2*N.Pop, replace¼TRUE)
# Get gametes from gamete pool
S.GameteX <- GameteX[G.Index,]
S.GameteY <- GameteY[G.Index,]
S.GameteC <- GameteC[G.Index,]
# Form next generation
n1 <- N.Popþ1
n2 <- 2*N.Pop
G.Xmatrix <- cbind(S.GameteX[1:N.Pop,], S.GameteX[n1:n2,])
G.Ymatrix <- cbind(S.GameteY[1:N.Pop,], S.GameteY[n1:n2,])
G.Cmatrix <- cbind(S.GameteC[1:N.Pop,], S.GameteC[n1:n2,])
# Mutations
Pmut <- 0.0001 # Mutation probability
G.Xmatrix <- MUTATION(G.Xmatrix, Pmut, X.loci, N.Pop)
G.Ymatrix <- MUTATION(G.Ymatrix, Pmut, Y.loci, N.Pop)
G.Cmatrix <- MUTATION(G.Cmatrix, Pmut, C.loci, N.Pop)
} # Next generation
par(mfrow¼c(2,2))
#
Plot phenotypic value on generation
ymin <- min(Output[,4:5]); ymax <- max(Output[,4:5]) # Limits on y
plot( Output[,1], Output[,4], xlab¼‘Generation’, ylab¼‘Pheno-
types’, type¼‘l’, ylim¼c(ymin,ymax))
lines(Output[,1], Output[,5], lty¼2)
# Plot genetic variances on generation
ymin <- min(Output[,2:3]); ymax <- max(Output[,2:3]) # Limits on y
266 MODELING EVOLUTION