#############
# Example 1 #
#############
data(Russett)
X_agric =as.matrix(Russett[,c("gini","farm","rent")])
X_ind = as.matrix(Russett[,c("gnpr","labo")])
X_polit = as.matrix(Russett[ , c("demostab", "dictatur")])
A = list(X_agric, X_ind, X_polit)
#Define the design matrix (output = C)
C = matrix(c(0, 0, 1, 0, 0, 1, 1, 1, 0), 3, 3)
result.rgcca = rgcca(A, C, tau = c(1, 1, 1), scheme = "factorial", scale = TRUE)
lab = as.vector(apply(Russett[, 10:12], 1, which.max))
plot(result.rgcca$Y[[1]], result.rgcca$Y[[2]], col = "white",
xlab = "Y1 (Agric. inequality)", ylab = "Y2 (Industrial Development)")
text(result.rgcca$Y[[1]], result.rgcca$Y[[2]], Russett[, 1], col = lab, cex = .7)
############################################
# Example 2: RGCCA and mutliple components #
############################################
############################
# plot(y1, y2) for (RGCCA) #
############################
result.rgcca = rgcca(A, C, tau = rep(1, 3), ncomp = c(2, 2, 1),
scheme = "factorial", verbose = TRUE)
plot(result.rgcca$Y[[1]][, 1], result.rgcca$Y[[2]][, 1], col = "white", xlab = "Y1 (GE)",
ylab = "Y2 (CGH)", main = "Factorial plan of RGCCA")
text(result.rgcca$Y[[1]][, 1], result.rgcca$Y[[2]][, 1], Russett[, 1], col = lab, cex = .6)
plot(result.rgcca$Y[[1]][, 1], result.rgcca$Y[[1]][, 2], col = "white", xlab = "Y1 (GE)",
ylab = "Y2 (GE)", main = "Factorial plan of RGCCA")
text(result.rgcca$Y[[1]][, 1], result.rgcca$Y[[1]][, 2], Russett[, 1], col = lab, cex = .6)
######################################
# example 3: RGCCA and leave one out #
######################################
Ytest = matrix(0, 47, 3)
result.rgcca = rgcca(A, C, tau = rep(1, 3), ncomp = rep(1, 3),
scheme = "factorial", verbose = TRUE)
for (i in 1:nrow(Russett)){
B = lapply(A, function(x) x[-i, ])
B = lapply(B, scale2)
resB = rgcca(B, C, tau = rep(1, 3), scheme = "factorial", scale = FALSE, verbose = FALSE)
# look for potential conflicting sign among components within the loo loop.
for (k in 1:length(B)){
if (cor(result.rgcca$a[[k]], resB$a[[k]]) >= 0)
resB$a[[k]] = resB$a[[k]] else resB$a[[k]] = -resB$a[[k]]
}
Btest =lapply(A, function(x) x[i, ])
Btest[[1]]=(Btest[[1]]-attr(B[[1]],"scaled:center"))/(attr(B[[1]],"scaled:scale"))
Btest[[2]]=(Btest[[2]]-attr(B[[2]],"scaled:center"))/(attr(B[[2]],"scaled:scale"))
Btest[[3]]=(Btest[[3]]-attr(B[[3]],"scaled:center"))/(attr(B[[3]],"scaled:scale"))
Ytest[i, 1] = Btest[[1]]%*%resB$a[[1]]
Ytest[i, 2] = Btest[[2]]%*%resB$a[[2]]
Ytest[i, 3] = Btest[[3]]%*%resB$a[[3]]
}
lab = apply(Russett[, 10:12], 1, which.max)
plot(result.rgcca$Y[[1]], result.rgcca$Y[[2]], col = "white",
xlab = "Y1 (Agric. inequality)", ylab = "Y2 (Ind. Development)")
text(result.rgcca$Y[[1]], result.rgcca$Y[[2]], Russett[, 1], col = lab)
text(Ytest[, 1], Ytest[, 2], substr(Russett[, 1], 1, 1), col = lab)
Run the code above in your browser using DataLab