Section B: Split data into training and testing lots

Before splitting the dataset, we truncate it to make it divisible by 5

wheat <- wheat[1:(dim(wheat)[1]-2),]

Getting the lot indices for training and testing lots.

s <- list()
wheatls <- list()
lotsize <- trunc(nrow(wheat)/5)
indices <- 1:nrow(wheat)
for (i in 1:5){
s[[i]] <- sample(indices,lotsize)
indices <- indices[!(indices %in% s[[i]])]
wheatls[[i]] <- wheat[s[[i]],]
}

Section C: Getting Yhat from testing sets and computing the overall correlation between Yhat and Y.

Fit data excluding ith lot ( Training sets )

fm <- list()
wheat_exclude <- list()
for (j in 1:5){
i <- c(1,2,3,4,5)
isel <- i[!(i %in% j)]
wheat_exclude[[j]] <- do.call("rbind",wheatls[isel])
fm[[j]] <- pedigreemm(sdGY~env+(1|gen1),data=wheat_exclude[[j]],pedigree=list(gen1=wheat_ped_final))
}

Get Yhat on ith lot (using BetaHat computed on fit excluding ith lot) and put them together. ( Testing sets )

predict <- vector()
for (i in 1:5){
predict <- append(predict,predict(fm[[i]],newdata=wheatls[[i]]))
}

Sort the results and compute the correlation with original dataset

predict_sorted <- predict[order(as.integer(names(predict)))]
result <- cor(predict_sorted,wheat$sdGY)