Commit | Line | Data |
---|---|---|
ad642dc6 BA |
1 | ## File : 05_cluster2stepWER.r |
2 | ## Description : | |
3 | ||
4 | rm(list = ls()) | |
5 | ||
6 | if(Sys.info()[4] == "mojarrita"){ | |
7 | setwd("~/Documents/projects/2014_EDF-Orsay-Lyon2/codes/") | |
8 | } else { | |
9 | setwd("~/ownCloud/projects/2014_EDF-Orsay-Lyon2/codes/") | |
10 | } | |
11 | ||
12 | library(Rwave) # CWT | |
13 | library(cluster) # pam | |
14 | #library(flexclust) # kcca | |
15 | source("aux.r") # auxiliary clustering functions | |
16 | source("sowas-superseded.r") # auxiliary CWT functions | |
17 | ||
18 | ## 1. Read auxiliar data files #### | |
19 | ||
20 | identifiants <- read.table("identifs.txt")[ ,1] | |
21 | dates0 <- read.table("datesall.txt")[, 1] | |
22 | dates <- as.character(dates0[grep("2009", dates0)]) | |
23 | rm(dates0) | |
24 | ||
25 | #n <- length(identifiants) | |
26 | #p <- length(dates) | |
27 | ||
28 | load('~/tmp/2009synchrosdf200WER') | |
29 | #load('../res/2009_synchros200WER.Rdata') | |
30 | synchros09 <- synchros | |
31 | load('~/tmp/2010synchrosdf200WER') | |
32 | synchros10 <- synchros; rm(synchros) | |
33 | ||
34 | conso <- lapply(synchros09, function(ll) { | |
35 | nas <- which(is.na(ll)[, 1]) # some 1/1/2009 are missing | |
36 | ll[nas, 1] <- rowMeans(ll[nas, 2:4]) | |
37 | imput <- ll[, 4180:4181] %*% matrix(c(2/3, 1/3, 1/3, 2/3), 2) | |
38 | cbind(ll[, 1:4180], imput, ll[, 4181:17518]) } ) | |
39 | ||
40 | n <- nrow(conso[[1]]) | |
41 | delta <- ncol(conso[[1]]) | |
42 | rm(synchros09) | |
43 | ||
44 | ||
45 | ## 2. Compute WER distance matrix #### | |
46 | ||
47 | ## _.a CWT -- Filtering the lowest freqs (>6m) #### | |
48 | nvoice <- 4 | |
49 | # noctave4 = 2^13 = 8192 half hours ~ 180 days | |
50 | noctave4 <- adjust.noctave(N = delta, dt = 1, s0 = 2, | |
51 | tw = 0, noctave = 13) | |
52 | # 4 here represent 2^5 = 32 half-hours ~ 1 day | |
53 | scalevector4 <- 2^(4:(noctave4 * nvoice) / nvoice) * 2 | |
54 | lscvect4 <- length(scalevector4) | |
55 | lscvect <- lscvect4 # i should clean my code: werFam demands a lscvect | |
56 | ||
57 | Xcwt4 <- lapply(conso, function(ll) | |
58 | toCWT(ll, noctave = noctave4, dt = 1, | |
59 | scalevector = scalevector4, | |
60 | lt = delta, smooth = FALSE, | |
61 | nvoice = nvoice) # observations node with CWT | |
62 | ) | |
63 | ||
64 | rm(conso) | |
65 | ||
66 | Xcwt4 <- lapply(conso, function(ll) { | |
67 | aux <- toCWT(ll, noctave = noctave4, dt = 1, | |
68 | scalevector = scalevector4, | |
69 | lt = delta, smooth = FALSE, | |
70 | nvoice = nvoice) # observations node with CWT | |
71 | res <- matrix(NA_complex_, nrow = n, ncol= 2 + length((c(aux[,,1])))) | |
72 | for(i in 1:n) | |
73 | res[i, ] <- c(delta, lscvect, res[,,i] / max(Mod(res[,,i])) ) | |
74 | res }) | |
75 | ||
76 | rm(conso) | |
77 | ||
78 | ||
79 | #Xcwt2 <- matrix(0.0, nrow= n, ncol= 2 + delta * lscvect) | |
80 | #Xcwt2 <- matrix(NA_complex_, nrow= n, ncol= 2 + length((c(Xcwt4[,,1])))) | |
81 | ||
82 | #for(i in 1:n) | |
83 | # Xcwt2[i,] <- c(delta, lscvect, Xcwt4[,,i] / max(Mod(Xcwt4[,,i])) ) | |
84 | ||
85 | #rm(conso, Xcwt4); gc() | |
86 | ||
87 | ## _.b WER^2 distances ######## | |
88 | #Xwer_dist <- matrix(0.0, n, n) | |
89 | #for(i in 1:(n - 1)){ | |
90 | # mat1 <- vect2mat(Xcwt2[i,]) | |
91 | # for(j in (i + 1):n){ | |
92 | # mat2 <- vect2mat(Xcwt2[j,]) | |
93 | # num <- Mod(mat1 * Conj(mat2)) | |
94 | # WX <- Mod(mat1 * Conj(mat1)) | |
95 | # WY <- Mod(mat2 * Conj(mat2)) | |
96 | # smsmnum <- smCWT(num, scalevector = scalevector4) | |
97 | # smsmWX <- smCWT(WX, scalevector = scalevector4) | |
98 | # smsmWY <- smCWT(WY, scalevector = scalevector4) | |
99 | # wer2 <- sum(colSums(smsmnum)^2) / | |
100 | # sum( sum(colSums(smsmWX) * colSums(smsmWY)) ) | |
101 | # Xwer_dist[i, j] <- sqrt(delta * lscvect * (1 - wer2)) | |
102 | # Xwer_dist[j, i] <- Xwer_dist[i, j] | |
103 | # } | |
104 | # } | |
105 | # diag(Xwer_dist) <- numeric(n) | |
106 | # | |
107 | # save(Xwer_dist, file = "../res/2009_synchros200WER.Rdata") | |
108 | ||
109 | load("../res/2009_synchros200WER.Rdata") | |
110 | ||
111 | ||
112 | ## 3. Cluster using WER distance matrix #### | |
113 | ||
114 | #hc <- hclust(as.dist(Xwer_dist), method = "ward.D") | |
115 | #plot(hc) | |
116 | # | |
117 | # #clust <- cutree(hc, 2) | |
118 | # | |
119 | for(K in 2:30){ | |
120 | #K <- 3 | |
121 | #pamfit <- pam(tdata[-201, ci$selectv], k = K) | |
122 | pamfit <- pam(as.dist(Xwer_dist), k = K, diss = TRUE) | |
123 | ||
124 | #table(pamfit$clustering) | |
125 | ||
126 | SC <- matrix(0, ncol = p, nrow = K) | |
127 | ||
128 | clustfactor <- pamfit$clustering | |
129 | # for(k in 1:K){ | |
130 | # clustk <- which(clustfactor == k) | |
131 | # if(length(clustk) > 0) { | |
132 | # if(length(clustk) > 1) { | |
133 | # SCk <- colSums(synchros09[which(clustfactor == k), ]) | |
134 | # } else { | |
135 | # SCk <- synchros09[which(clustfactor == k), ] | |
136 | # } | |
137 | # SC[k, ] <- SC[k, ] + SCk | |
138 | # rm(SCk) | |
139 | # } | |
140 | #} | |
141 | ||
142 | #write.table(clustfactor, file = paste0("~/tmp/clustfactorRC", K, ".txt")) | |
143 | #write.table(clustfactor, file = "~/tmp/clustfactor3.txt") | |
144 | write.table(clustfactor, file = paste0("~/tmp/clustfactorWER", K, ".txt")) | |
145 | } | |
146 | # | |
147 | # # Plots | |
148 | # layout(1) | |
149 | # matplot(t(SC)[48*10 + 1:(48*30), ], type = 'l', ylab = '',col = 1:3, lty = 1) | |
150 | # matplot(t(SC)[48*100 + 1:(48*30), ], type = 'l', ylab = '', col = 1:3, lty = 1) | |
151 | # | |
152 | # | |
153 | # |