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