3 #' It is a function which plots relevant parameters
5 #' @param model the model constructed by valse procedure
6 #' @param n sample size
7 #' @return several plots
13 plot_valse = function(model,n){
20 ## regression matrices
23 Melt = melt(t((model$phi[,,r])))
24 gReg[[r]] = ggplot(data = Melt, aes(x=Var1, y=Var2, fill=value)) + geom_tile() +
25 scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, space = "Lab") +
26 ggtitle(paste("Regression matrices in cluster",r))
30 ## Differences between two clusters
33 Melt = melt(t(model$phi[,,k1]-model$phi[,,k2]))
34 gDiff = ggplot(data = Melt, aes(x=Var1, y=Var2, fill=value)) + geom_tile() +
35 scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, space = "Lab") +
36 ggtitle(paste("Difference between regression matrices in cluster",k1, "and", k2))
39 ### Covariance matrices
40 matCov = matrix(NA, nrow = dim(model$rho[,,1])[1], ncol = K)
42 matCov[,r] = diag(model$rho[,,r])
44 MeltCov = melt(matCov)
45 gCov = ggplot(data =MeltCov, aes(x=Var1, y=Var2, fill=value)) + geom_tile() +
46 scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, space = "Lab") +
47 ggtitle("Covariance matrices")
51 Gam = matrix(0, ncol = K, nrow = n)
55 sqNorm2 = sum( (Y[i,]%*%model$rho[,,r]-X[i,]%*%model$phi[,,r])^2 )
56 Gam[i,r] = model$pi[r] * exp(-0.5*sqNorm2)* det(model$rho[,,r])
58 gam[i,] = Gam[i,] / sum(Gam[i,])
60 affec = apply(gam, 1,which.max)
61 gam2 = matrix(NA, ncol = K, nrow = n)
63 gam2[i, ] = c(gam[i, affec[i]], affec[i])
65 bp <- ggplot(data.frame(gam2), aes(x=X2, y=X1, color=X2, group = X2)) +
66 geom_boxplot() + theme(legend.position = "none")+ background_grid(major = "xy", minor = "none")
69 ### Mean in each cluster
72 meanPerClass= matrix(0, ncol = K, nrow = dim(XY)[2])
74 XY_class[[r]] = XY[affec == r, ]
75 meanPerClass[,r] = apply(XY_class[[r]], 2, mean)
77 data = data.frame(mean = as.vector(meanPerClass), cluster = as.character(rep(1:K, each = dim(XY)[2])), time = rep(1:dim(XY)[2],K))
78 g = ggplot(data, aes(x=time, y = mean, group = cluster, color = cluster))
79 print(g + geom_line(aes(linetype=cluster, color=cluster))+ geom_point(aes(color=cluster)) + ggtitle('Mean per cluster'))