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