Refresh package, suppress what we won't do right now. Focus on doc + debug
[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 n sample size
9 #' @return several plots
10 #'
11 #' @examples TODO
12 #'
13 #' @export
14 #'
15 plot_valse <- function(X, Y, model, n, comp = FALSE, k1 = NA, k2 = NA)
16 {
17 require("gridExtra")
18 require("ggplot2")
19 require("reshape2")
20 require("cowplot")
21
22 K <- length(model$pi)
23 ## regression matrices
24 gReg <- list()
25 for (r in 1:K)
26 {
27 Melt <- melt(t((model$phi[, , r])))
28 gReg[[r]] <- ggplot(data = Melt, aes(x = Var1, y = Var2, fill = value)) +
29 geom_tile() + scale_fill_gradient2(low = "blue", high = "red", mid = "white",
30 midpoint = 0, space = "Lab") + ggtitle(paste("Regression matrices in cluster", r))
31 }
32 print(gReg)
33
34 ## Differences between two clusters
35 if (comp)
36 {
37 if (is.na(k1) || is.na(k))
38 print("k1 and k2 must be integers, representing the clusters you want to compare")
39 Melt <- melt(t(model$phi[, , k1] - model$phi[, , k2]))
40 gDiff <- ggplot(data = Melt, aes(x = Var1, y = Var2, fill = value))
41 + geom_tile()
42 + scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0,
43 space = "Lab")
44 + ggtitle(paste("Difference between regression matrices in cluster",
45 k1, "and", k2))
46 print(gDiff)
47 }
48
49 ### Covariance matrices
50 matCov <- matrix(NA, nrow = dim(model$rho[, , 1])[1], ncol = K)
51 for (r in 1:K)
52 matCov[, r] <- diag(model$rho[, , r])
53 MeltCov <- melt(matCov)
54 gCov <- ggplot(data = MeltCov, aes(x = Var1, y = Var2, fill = value)) + geom_tile()
55 + scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0,
56 space = "Lab")
57 + ggtitle("Covariance matrices")
58 print(gCov)
59
60 ### Proportions
61 gam2 <- matrix(NA, ncol = K, nrow = n)
62 for (i in 1:n)
63 gam2[i, ] <- c(model$proba[i, model$affec[i]], model$affec[i])
64
65 bp <- ggplot(data.frame(gam2), aes(x = X2, y = X1, color = X2, group = X2))
66 + geom_boxplot()
67 + theme(legend.position = "none")
68 + background_grid(major = "xy", minor = "none")
69 print(bp)
70
71 ### Mean in each cluster
72 XY <- cbind(X, Y)
73 XY_class <- list()
74 meanPerClass <- matrix(0, ncol = K, nrow = dim(XY)[2])
75 for (r in 1:K)
76 {
77 XY_class[[r]] <- XY[model$affec == r, ]
78 if (sum(model$affec == r) == 1) {
79 meanPerClass[, r] <- XY_class[[r]]
80 } else {
81 meanPerClass[, r] <- apply(XY_class[[r]], 2, mean)
82 }
83 }
84 data <- data.frame(mean = as.vector(meanPerClass),
85 cluster = as.character(rep(1:K, each = dim(XY)[2])), time = rep(1:dim(XY)[2], K))
86 g <- ggplot(data, aes(x = time, y = mean, group = cluster, color = cluster))
87 print(g + geom_line(aes(linetype = cluster, color = cluster))
88 + geom_point(aes(color = cluster)) + ggtitle("Mean per cluster"))
89 }