library(ASRgenomics)
library(asreml)

#### Load phenotype data (stage II) ####
stgII_data <- readRDS(file = "...")

#### Load marker data ####
Marker.PP <- readRDS(file = "...")

#### Round marker data and make the type as integer to be able to be forwarded to G.matrix function ####
Marker.PP.1 <- round(Marker.PP,2)
mode(Marker.PP.1) <- "integer"

#### Kinship matrix score using vanRaden #####
K.m <- G.matrix(M = Marker.PP.1, 
                method = "VanRaden", na.string = NA)$G

#### Check whether markers and the entrynames in the phenotype data match #####
match.Entry.K <- match.kinship2pheno(K = K.m, pheno.data = stgII_data.II,
                                     indiv = "EntryName", clean = FALSE, mism = TRUE)

#### put 0.01 in the diagonal part to avoid ill-conditioned of the kinship matrix #####
G <- K.m + diag(0.01, nrow(K.m))

##### Get the inverse of the K matrix and make it as a sparse form #####
Ginv.sparse <- G.inverse(G = G, sparseform = TRUE)$Ginv

##### Fit GCA1 MY model #######
asreml.options(trace = T,maxit = 100, workspace = "8gb", pworkspace = "8gb")
options(scipen = 999, digits = 5)

fit1.gblup.1 <- asreml(fixed   = Yield_LSM~1,
                       random  = ~vm(EntryName,Ginv.sparse) + idv(Year) + 
                                  idv(Year):vm(EntryName,Ginv.sparse),
                       weights = smith.w,
                       family  = asr_gaussian(dispersion = 1.0),
                       data    = stgII_data.II)

##### Get the summary of the fitted model ######
summary(fit1.gblup.1)

##### Compute rho #####
rho <- vpredict(fit1.gblup.1, rho ~V2 / (V2 + V3))