Update plot_valse and add it to main.R
[valse.git] / pkg / R / plot.R
CommitLineData
4c9cc558 1#' Plot
2#'
3#' It is a function which plots relevant parameters
4#'
5#'
6#' @return several plots
7#'
8#' @examples TODO
9#'
10#' @export
11#'
12plot_valse = function(){
13 require("gridExtra")
14 require("ggplot2")
15 require("reshape2")
16
17 ## regression matrices
18 gReg = list()
19 for (r in 1:K){
20 Melt = melt(t((model$phi[,,r])))
21 gReg[[r]] = ggplot(data = Melt, aes(x=Var1, y=Var2, fill=value)) + geom_tile() +
22 scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, space = "Lab") +
23 ggtitle(paste("Regression matrices in cluster",r))
24 }
25 gReg
26
27 ## Differences between two clusters
28 k1 = 1
29 k2 = 2
30 Melt = melt(t(model$phi[,,k1]-model$phi[,,k2]))
31 gDiff = ggplot(data = Melt, aes(x=Var1, y=Var2, fill=value)) + geom_tile() +
32 scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, space = "Lab") +
33 ggtitle(paste("Difference between regression matrices in cluster",k1, "and", k2))
34 gDiff
35
36 ### Covariance matrices
37 matCov = matrix(NA, nrow = dim(model$rho[,,1])[1], ncol = K)
38 for (r in 1:K){
39 matCov[,r] = diag(model$rho[,,r])
40 }
41 MeltCov = melt(matCov)
42 gCov = ggplot(data =MeltCov, aes(x=Var1, y=Var2, fill=value)) + geom_tile() +
43 scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0, space = "Lab") +
44 ggtitle(paste("Regression matrices in cluster",r))
45 gCov
46
47 ### proportions
48 Gam = matrix(0, ncol = K, nrow = n)
49 gam = Gam
50 for (i in 1:n){
51 for (r in 1:K){
52 sqNorm2 = sum( (Y[i,]%*%model$rho[,,r]-X[i,]%*%model$phi[,,r])^2 )
53 Gam[i,r] = model$pi[r] * exp(-0.5*sqNorm2)* det(model$rho[,,r])
54 }
55 gam[i,] = Gam[i,] / sum(Gam[i,])
56 }
57 affec = apply(gam, 1,which.max)
58 gam2 = matrix(NA, ncol = K, nrow = n)
59 for (i in 1:n){
60 gam2[i, ] = c(gam[i, affec[i]], affec[i])
61 }
62 bp <- ggplot(data.frame(gam2), aes(x=X2, y=X1, color=X2, group = X2)) +
63 geom_boxplot() + theme(legend.position = "none")
64 bp + background_grid(major = "xy", minor = "none")
65
66 ### Mean in each cluster
67 XY = cbind(X,Y)
68 XY_class= list()
69 meanPerClass= matrix(0, ncol = K, nrow = dim(XY)[2])
70 for (r in 1:K){
71 XY_class[[r]] = XY[affec == r, ]
72 meanPerClass[,r] = apply(XY_class[[r]], 2, mean)
73 }
74 data = data.frame(mean = as.vector(meanPerClass), cluster = as.character(rep(1:K, each = dim(XY)[2])), time = rep(1:dim(XY)[2],K))
75 g = ggplot(data, aes(x=time, y = mean, group = cluster, color = cluster))
76 g + geom_line(aes(linetype=cluster, color=cluster))+ geom_point(aes(color=cluster))
77
78}