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