-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEx_A4.Multivariate_Continous.R
94 lines (76 loc) · 2.4 KB
/
Ex_A4.Multivariate_Continous.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Import SKM library
library(SKM)
# Load the dataset
load("EYT_1.RData", verbose = TRUE)
# Oder Pheno by Env and Lines
Pheno <- Pheno[order(Pheno$Env, Pheno$Line), ]
geno_order <- sort(rownames(Geno))
# Order Geno by Line
Geno <- Geno[geno_order, geno_order]
# Design matrices
Z_g<- model.matrix(~ 0 + Line, data = Pheno)
K_g <- Z_g %*% Geno %*% t(Z_g) # Linear kernel
# Env design matrix without the first column
X_E<- model.matrix(~ 0 + Env, data = Pheno)[, -1]
K_e <- X_E %*% t(X_E) / ncol(X_E)
X_g <- SKM::cholesky(K_g)
K_ge <- K_e * K_g
# Interaction matrix
X_ge<- SKM::cholesky(K_ge)
# Put the matrices in the expected format with the model
# to use with each one
X <- list(
list(x =X_E, model = "FIXED"),
list(x =K_g, model = "BRR"),
list(x =K_ge, model = "BRR")
)
# Retrieve the two continuos response variables
y <- SKM::to_matrix(Pheno[, c("DTHD", "DTMT")])
# Folds generation for random cross validation
folds <- SKM::cv_random(
records_number = nrow(Pheno),
folds_number = 5,
testing_proportion = 0.25
)
# List for storing the individual predictions at each fold
Predictions <- list(DTHD = data.frame(), DTMT = data.frame())
for (i in seq_along(folds)) {
fold <- folds[[i]]
# Set testing indices to NA in the response variables
y_na <- y
y_na[fold$testing, ] <- NA
# *********** MODEL EVALUATION AND HYPERPARAMETERS TUNING WITH SKM ***********
model <- SKM::bayesian_model(
X,
y_na,
iterations_number = 10000,
burn_in = 5000
)
# Predict over testing set
predictions <- predict(model)
for (trait in names(Predictions)) {
# Save the predictions along with environment and line information
Predictions[[trait]] <- rbind(
Predictions[[trait]],
data.frame(
Fold = i,
Line = Pheno$Line[fold$testing],
Env = Pheno$Env[fold$testing],
Observed = y[fold$testing, trait],
Predicted = predictions[[trait]]$predicted
)
)
}
}
# Errors metrics for prediction performance
DTHDSummaries <- SKM::gs_summaries(Predictions$DTHD)
# Print summaries by line, environment and fold
DTHDSummaries$line
DTHDSummaries$env
DTHDSummaries$fold
# Errors metrics for prediction performance
DTMTSummaries <- SKM::gs_summaries(Predictions$DTMT)
# Print summaries by line, environment and fold
DTMTSummaries$line
DTMTSummaries$env
DTMTSummaries$fold