X-Git-Url: https://git.auder.net/?p=valse.git;a=blobdiff_plain;f=pkg%2FR%2Fplot_valse.R;h=0a6fa9e7bd2f57dc606cc3adb2e4b4b5cbd361ef;hp=120196db2a523c720c0a2c5a313e0a344f200385;hb=4d9db27f0d1749e5577038dedbc5f4d0826f2772;hpb=9fadef2bff80d4b0371962dea4b6de24086f230b diff --git a/pkg/R/plot_valse.R b/pkg/R/plot_valse.R index 120196d..0a6fa9e 100644 --- a/pkg/R/plot_valse.R +++ b/pkg/R/plot_valse.R @@ -2,6 +2,8 @@ #' #' It is a function which plots relevant parameters #' +#' @param X matrix of covariates (of size n*p) +#' @param Y matrix of responses (of size n*m) #' @param model the model constructed by valse procedure #' @param n sample size #' @return several plots @@ -10,7 +12,7 @@ #' #' @export #' -plot_valse = function(model,n){ +plot_valse = function(X,Y,model,n, comp = FALSE, k1 = NA, k2 = NA){ require("gridExtra") require("ggplot2") require("reshape2") @@ -28,13 +30,15 @@ plot_valse = function(model,n){ print(gReg) ## Differences between two clusters - k1 = 1 - k2 = 2 - Melt = melt(t(model$phi[,,k1]-model$phi[,,k2])) - gDiff = ggplot(data = Melt, aes(x=Var1, y=Var2, fill=value)) + geom_tile() + - scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, space = "Lab") + - ggtitle(paste("Difference between regression matrices in cluster",k1, "and", k2)) - print(gDiff) + if (comp){ + if (is.na(k1) || is.na(k)){print('k1 and k2 must be integers, representing the clusters you want to compare')} + Melt = melt(t(model$phi[,,k1]-model$phi[,,k2])) + gDiff = ggplot(data = Melt, aes(x=Var1, y=Var2, fill=value)) + geom_tile() + + scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, space = "Lab") + + ggtitle(paste("Difference between regression matrices in cluster",k1, "and", k2)) + print(gDiff) + + } ### Covariance matrices matCov = matrix(NA, nrow = dim(model$rho[,,1])[1], ncol = K) @@ -47,23 +51,27 @@ plot_valse = function(model,n){ ggtitle("Covariance matrices") print(gCov ) - ### proportions + ### Proportions gam2 = matrix(NA, ncol = K, nrow = n) for (i in 1:n){ - gam2[i, ] = c(model$Gam[i, model$affec[i]], model$affec[i]) + gam2[i, ] = c(model$proba[i, model$affec[i]], model$affec[i]) } bp <- ggplot(data.frame(gam2), aes(x=X2, y=X1, color=X2, group = X2)) + geom_boxplot() + theme(legend.position = "none")+ background_grid(major = "xy", minor = "none") - print(bp ) + print(bp) ### Mean in each cluster XY = cbind(X,Y) XY_class= list() meanPerClass= matrix(0, ncol = K, nrow = dim(XY)[2]) for (r in 1:K){ - XY_class[[r]] = XY[affec == r, ] - meanPerClass[,r] = apply(XY_class[[r]], 2, mean) + XY_class[[r]] = XY[model$affec == r, ] + if (sum(model$affec==r) == 1){ + meanPerClass[,r] = XY_class[[r]] + } else { + meanPerClass[,r] = apply(XY_class[[r]], 2, mean) + } } data = data.frame(mean = as.vector(meanPerClass), cluster = as.character(rep(1:K, each = dim(XY)[2])), time = rep(1:dim(XY)[2],K)) g = ggplot(data, aes(x=time, y = mean, group = cluster, color = cluster))