Commit | Line | Data |
---|---|---|
ffdf9447 | 1 | #' Plot |
4c9cc558 | 2 | #' |
3 | #' It is a function which plots relevant parameters | |
4 | #' | |
5965d116 | 5 | #' @param X matrix of covariates (of size n*p) |
6 | #' @param Y matrix of responses (of size n*m) | |
a6b60f91 | 7 | #' @param model the model constructed by valse procedure |
8 | #' @param n sample size | |
4c9cc558 | 9 | #' @return several plots |
10 | #' | |
11 | #' @examples TODO | |
12 | #' | |
13 | #' @export | |
14 | #' | |
ffdf9447 BA |
15 | plot_valse <- function(X, Y, model, n, comp = FALSE, k1 = NA, k2 = NA) |
16 | { | |
4c9cc558 | 17 | require("gridExtra") |
18 | require("ggplot2") | |
19 | require("reshape2") | |
a6b60f91 | 20 | require("cowplot") |
1b698c16 | 21 | |
ffdf9447 | 22 | K <- length(model$pi) |
4c9cc558 | 23 | ## regression matrices |
ffdf9447 BA |
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", | |
1b698c16 | 30 | midpoint = 0, space = "Lab") + ggtitle(paste("Regression matrices in cluster", r)) |
4c9cc558 | 31 | } |
32 | print(gReg) | |
1b698c16 | 33 | |
4c9cc558 | 34 | ## Differences between two clusters |
ffdf9447 BA |
35 | if (comp) |
36 | { | |
37 | if (is.na(k1) || is.na(k)) | |
ffdf9447 | 38 | print("k1 and k2 must be integers, representing the clusters you want to compare") |
ffdf9447 | 39 | Melt <- melt(t(model$phi[, , k1] - model$phi[, , k2])) |
1b698c16 BA |
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)) | |
fb6e49cb | 46 | print(gDiff) |
fb6e49cb | 47 | } |
1b698c16 | 48 | |
4c9cc558 | 49 | ### Covariance matrices |
ffdf9447 BA |
50 | matCov <- matrix(NA, nrow = dim(model$rho[, , 1])[1], ncol = K) |
51 | for (r in 1:K) | |
ffdf9447 | 52 | matCov[, r] <- diag(model$rho[, , r]) |
ffdf9447 | 53 | MeltCov <- melt(matCov) |
1b698c16 BA |
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") | |
ffdf9447 | 58 | print(gCov) |
1b698c16 | 59 | |
fb6e49cb | 60 | ### Proportions |
ffdf9447 BA |
61 | gam2 <- matrix(NA, ncol = K, nrow = n) |
62 | for (i in 1:n) | |
ffdf9447 | 63 | gam2[i, ] <- c(model$proba[i, model$affec[i]], model$affec[i]) |
1b698c16 BA |
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") | |
fb6e49cb | 69 | print(bp) |
1b698c16 | 70 | |
4c9cc558 | 71 | ### Mean in each cluster |
ffdf9447 BA |
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, ] | |
1b698c16 | 78 | if (sum(model$affec == r) == 1) { |
ffdf9447 | 79 | meanPerClass[, r] <- XY_class[[r]] |
1b698c16 | 80 | } else { |
ffdf9447 | 81 | meanPerClass[, r] <- apply(XY_class[[r]], 2, mean) |
fb6e49cb | 82 | } |
4c9cc558 | 83 | } |
1b698c16 BA |
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)) | |
ffdf9447 | 86 | g <- ggplot(data, aes(x = time, y = mean, group = cluster, color = cluster)) |
1b698c16 BA |
87 | print(g + geom_line(aes(linetype = cluster, color = cluster)) |
88 | + geom_point(aes(color = cluster)) + ggtitle("Mean per cluster")) | |
ffdf9447 | 89 | } |