Commit | Line | Data |
---|---|---|
64cceb2e | 1 | utils::globalVariables(c("Var1","Var2","X1","X2","value")) #, package="valse") |
0ba1b11c | 2 | #' Plot |
3453829e BA |
3 | #' |
4 | #' It is a function which plots relevant parameters | |
5 | #' | |
6 | #' @param X matrix of covariates (of size n*p) | |
7 | #' @param Y matrix of responses (of size n*m) | |
8 | #' @param model the model constructed by valse procedure | |
1196a43d BA |
9 | #' @param comp TRUE to enable pairwise clusters comparison |
10 | #' @param k1 index of the first cluster to be compared | |
11 | #' @param k2 index of the second cluster to be compared | |
12 | #' | |
64cceb2e | 13 | #' @importFrom ggplot2 ggplot aes ggtitle geom_tile geom_line scale_fill_gradient2 geom_boxplot theme |
1196a43d BA |
14 | #' @importFrom cowplot background_grid |
15 | #' @importFrom reshape2 melt | |
3453829e | 16 | #' |
3453829e | 17 | #' @export |
3921ba9b | 18 | plot_valse <- function(X, Y, model, comp = FALSE, k1 = NA, k2 = NA) |
3453829e | 19 | { |
3921ba9b | 20 | n <- nrow(X) |
3453829e BA |
21 | K <- length(model$pi) |
22 | ## regression matrices | |
23 | gReg <- list() | |
64cceb2e BA |
24 | for (r in 1:K) { |
25 | Melt <- reshape2::melt(t((model$phi[, , r]))) | |
26 | gReg[[r]] <- ggplot2::ggplot(data = Melt, ggplot2::aes(x = Var1, y = Var2, fill = value)) + | |
27 | ggplot2::geom_tile() + ggplot2::scale_fill_gradient2(low = "blue", high = "red", mid = "white", | |
28 | midpoint = 0, space = "Lab") + ggplot2::ggtitle(paste("Regression matrices in cluster", r)) | |
3453829e BA |
29 | } |
30 | print(gReg) | |
31 | ||
32 | ## Differences between two clusters | |
64cceb2e | 33 | if (comp) { |
1196a43d | 34 | if (is.na(k1) || is.na(k2)) |
3453829e | 35 | print("k1 and k2 must be integers, representing the clusters you want to compare") |
64cceb2e BA |
36 | Melt <- reshape2::melt(t(model$phi[, , k1] - model$phi[, , k2])) |
37 | gDiff <- ggplot2::ggplot(data = Melt, ggplot2::aes(x = Var1, y = Var2, fill = value)) + | |
38 | ggplot2::geom_tile() + ggplot2::scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, | |
39 | space = "Lab") + ggplot2::ggtitle(paste("Difference between regression matrices in cluster", | |
3453829e BA |
40 | k1, "and", k2)) |
41 | print(gDiff) | |
42 | } | |
43 | ||
44 | ### Covariance matrices | |
45 | matCov <- matrix(NA, nrow = dim(model$rho[, , 1])[1], ncol = K) | |
46 | for (r in 1:K) | |
47 | matCov[, r] <- diag(model$rho[, , r]) | |
64cceb2e BA |
48 | MeltCov <- reshape2::melt(matCov) |
49 | gCov <- ggplot2::ggplot(data = MeltCov, ggplot2::aes(x = Var1, y = Var2, fill = value)) + ggplot2::geom_tile() + | |
50 | ggplot2::scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, | |
51 | space = "Lab") + ggplot2::ggtitle("Covariance matrices (diag., one row per cluster)") | |
3453829e BA |
52 | print(gCov) |
53 | ||
54 | ### Proportions | |
64cceb2e | 55 | gam2 <- matrix(NA, ncol = 2, nrow = n) |
3453829e BA |
56 | for (i in 1:n) |
57 | gam2[i, ] <- c(model$proba[i, model$affec[i]], model$affec[i]) | |
58 | ||
64cceb2e BA |
59 | bp <- ggplot2::ggplot(data.frame(gam2), ggplot2::aes(x = X2, y = X1, color = X2, group = X2)) + ggplot2::geom_boxplot() + |
60 | ggplot2::theme(legend.position = "none") + cowplot::background_grid(major = "xy", minor = "none") + | |
61 | ggplot2::ggtitle("Assignment boxplot per cluster") | |
3453829e | 62 | print(bp) |
3453829e | 63 | } |