| 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("~/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 <- delta <- length(dates) |
| 27 | |
| 28 | load("~/tmp/2009_synchros200RND") |
| 29 | synchros09 <- synchros[[1]] |
| 30 | #synchros09 <- as.matrix(read.table("~/tmp/2009_synchros200RANDOM.txt")) |
| 31 | #synchros09 <- t(as.matrix(read.table("~/tmp/2009_synchros200RANDOM.txt"))) |
| 32 | nas <- which(is.na(synchros09)[, 1]) # some 1/1/2009 are missing |
| 33 | synchros09[nas, 1] <- rowMeans(synchros09[nas, 2:4]) |
| 34 | |
| 35 | imput09 <- synchros09[, 4180:4181] %*% matrix(c(2/3, 1/3, 1/3, 2/3), 2) |
| 36 | synchros09 <- cbind(synchros09[, 1:4180], imput09, synchros09[, 4181:17518]) |
| 37 | |
| 38 | conso <- (synchros09)[-201, ]; # series must be on rows |
| 39 | n <- nrow(conso) |
| 40 | delta <- ncol(conso) |
| 41 | |
| 42 | rm(synchros09, nas) |
| 43 | |
| 44 | ## 2. Compute WER distance matrix #### |
| 45 | |
| 46 | ## _.a CWT -- Filtering the lowest freqs (>6m) #### |
| 47 | nvoice <- 4 |
| 48 | # noctave4 = 2^13 = 8192 half hours ~ 180 days |
| 49 | noctave4 <- adjust.noctave(N = delta, dt = 1, s0 = 2, |
| 50 | tw = 0, noctave = 13) |
| 51 | # 4 here represent 2^5 = 32 half-hours ~ 1 day |
| 52 | scalevector4 <- 2^(4:(noctave4 * nvoice) / nvoice) * 2 |
| 53 | lscvect4 <- length(scalevector4) |
| 54 | lscvect <- lscvect4 # i should clean my code: werFam demands a lscvect |
| 55 | Xcwt4 <- toCWT(conso, noctave = noctave4, dt = 1, |
| 56 | scalevector = scalevector4, |
| 57 | lt = delta, smooth = FALSE, |
| 58 | nvoice = nvoice) # observations node with CWT |
| 59 | |
| 60 | Xcwt2 <- matrix(0.0, nrow= n, ncol= 2 + delta * lscvect) |
| 61 | Xcwt2 <- matrix(NA_complex_, nrow= n, ncol= 2 + length((c(Xcwt4[,,1])))) |
| 62 | |
| 63 | for(i in 1:n) |
| 64 | Xcwt2[i,] <- c(delta, lscvect, Xcwt4[,,i] / max(Mod(Xcwt4[,,i])) ) |
| 65 | |
| 66 | rm(conso, Xcwt4); gc() |
| 67 | |
| 68 | ## _.b WER^2 distances ######## |
| 69 | Xwer_dist <- matrix(0.0, n, n) |
| 70 | for(i in 1:(n - 1)){ |
| 71 | cat(sprintf('\nIter: , %i', i)) |
| 72 | mat1 <- vect2mat(Xcwt2[i,]) |
| 73 | for(j in (i + 1):n){ |
| 74 | mat2 <- vect2mat(Xcwt2[j,]) |
| 75 | num <- Mod(mat1 * Conj(mat2)) |
| 76 | WX <- Mod(mat1 * Conj(mat1)) |
| 77 | WY <- Mod(mat2 * Conj(mat2)) |
| 78 | smsmnum <- smCWT(num, scalevector = scalevector4) |
| 79 | smsmWX <- smCWT(WX, scalevector = scalevector4) |
| 80 | smsmWY <- smCWT(WY, scalevector = scalevector4) |
| 81 | wer2 <- sum(colSums(smsmnum)^2) / |
| 82 | sum( sum(colSums(smsmWX) * colSums(smsmWY)) ) |
| 83 | Xwer_dist[i, j] <- sqrt(delta * lscvect * (1 - wer2)) |
| 84 | Xwer_dist[j, i] <- Xwer_dist[i, j] |
| 85 | } |
| 86 | } |
| 87 | diag(Xwer_dist) <- numeric(n) |
| 88 | |
| 89 | save(Xwer_dist, file = "../res/2009_synchros200RANDOM-WER.Rdata") |
| 90 | |
| 91 | #load("../res/2009_synchros200WER.Rdata") |
| 92 | |
| 93 | |
| 94 | ## 3. Cluster using WER distance matrix #### |
| 95 | |
| 96 | #hc <- hclust(as.dist(Xwer_dist), method = "ward.D") |
| 97 | #plot(hc) |
| 98 | # |
| 99 | # #clust <- cutree(hc, 2) |
| 100 | # |
| 101 | for(K in 2:30){ |
| 102 | #K <- 3 |
| 103 | #pamfit <- pam(tdata[-201, ci$selectv], k = K) |
| 104 | pamfit <- pam(as.dist(Xwer_dist), k = K, diss = TRUE) |
| 105 | |
| 106 | #table(pamfit$clustering) |
| 107 | |
| 108 | SC <- matrix(0, ncol = p, nrow = K) |
| 109 | |
| 110 | clustfactor <- pamfit$clustering |
| 111 | # for(k in 1:K){ |
| 112 | # clustk <- which(clustfactor == k) |
| 113 | # if(length(clustk) > 0) { |
| 114 | # if(length(clustk) > 1) { |
| 115 | # SCk <- colSums(synchros09[which(clustfactor == k), ]) |
| 116 | # } else { |
| 117 | # SCk <- synchros09[which(clustfactor == k), ] |
| 118 | # } |
| 119 | # SC[k, ] <- SC[k, ] + SCk |
| 120 | # rm(SCk) |
| 121 | # } |
| 122 | #} |
| 123 | |
| 124 | # #write.table(clustfactor, file = paste0("~/tmp/clustfactorRC", K, ".txt")) |
| 125 | # #write.table(clustfactor, file = "~/tmp/clustfactor3.txt") |
| 126 | write.table(clustfactor, file = paste0("~/tmp/clustfactorRANDOM", K, ".txt")) |
| 127 | } |
| 128 | # |
| 129 | # # Plots |
| 130 | # layout(1) |
| 131 | # matplot(t(SC)[48*10 + 1:(48*30), ], type = 'l', ylab = '',col = 1:3, lty = 1) |
| 132 | # matplot(t(SC)[48*100 + 1:(48*30), ], type = 'l', ylab = '', col = 1:3, lty = 1) |
| 133 | # |
| 134 | # |
| 135 | # |