## File : 05_cluster2stepWER.r ## Description : rm(list = ls()) if(Sys.info()[4] == "mojarrita"){ setwd("~/Documents/projects/2014_EDF-Orsay-Lyon2/codes/") } else { setwd("~/ownCloud/projects/2014_EDF-Orsay-Lyon2/codes/") } library(Rwave) # CWT library(cluster) # pam #library(flexclust) # kcca source("aux.r") # auxiliary clustering functions source("sowas-superseded.r") # auxiliary CWT functions ## 1. Read auxiliar data files #### identifiants <- read.table("identifs.txt")[ ,1] dates0 <- read.table("datesall.txt")[, 1] dates <- as.character(dates0[grep("2009", dates0)]) rm(dates0) #n <- length(identifiants) #p <- length(dates) load('~/tmp/2009synchrosdf200WER') #load('../res/2009_synchros200WER.Rdata') synchros09 <- synchros load('~/tmp/2010synchrosdf200WER') synchros10 <- synchros; rm(synchros) conso <- lapply(synchros09, function(ll) { nas <- which(is.na(ll)[, 1]) # some 1/1/2009 are missing ll[nas, 1] <- rowMeans(ll[nas, 2:4]) imput <- ll[, 4180:4181] %*% matrix(c(2/3, 1/3, 1/3, 2/3), 2) cbind(ll[, 1:4180], imput, ll[, 4181:17518]) } ) n <- nrow(conso[[1]]) delta <- ncol(conso[[1]]) rm(synchros09) ## 2. Compute WER distance matrix #### ## _.a CWT -- Filtering the lowest freqs (>6m) #### nvoice <- 4 # noctave4 = 2^13 = 8192 half hours ~ 180 days noctave4 <- adjust.noctave(N = delta, dt = 1, s0 = 2, tw = 0, noctave = 13) # 4 here represent 2^5 = 32 half-hours ~ 1 day scalevector4 <- 2^(4:(noctave4 * nvoice) / nvoice) * 2 lscvect4 <- length(scalevector4) lscvect <- lscvect4 # i should clean my code: werFam demands a lscvect Xcwt4 <- lapply(conso, function(ll) toCWT(ll, noctave = noctave4, dt = 1, scalevector = scalevector4, lt = delta, smooth = FALSE, nvoice = nvoice) # observations node with CWT ) rm(conso) Xcwt4 <- lapply(conso, function(ll) { aux <- toCWT(ll, noctave = noctave4, dt = 1, scalevector = scalevector4, lt = delta, smooth = FALSE, nvoice = nvoice) # observations node with CWT res <- matrix(NA_complex_, nrow = n, ncol= 2 + length((c(aux[,,1])))) for(i in 1:n) res[i, ] <- c(delta, lscvect, res[,,i] / max(Mod(res[,,i])) ) res }) rm(conso) #Xcwt2 <- matrix(0.0, nrow= n, ncol= 2 + delta * lscvect) #Xcwt2 <- matrix(NA_complex_, nrow= n, ncol= 2 + length((c(Xcwt4[,,1])))) #for(i in 1:n) # Xcwt2[i,] <- c(delta, lscvect, Xcwt4[,,i] / max(Mod(Xcwt4[,,i])) ) #rm(conso, Xcwt4); gc() ## _.b WER^2 distances ######## #Xwer_dist <- matrix(0.0, n, n) #for(i in 1:(n - 1)){ # mat1 <- vect2mat(Xcwt2[i,]) # for(j in (i + 1):n){ # mat2 <- vect2mat(Xcwt2[j,]) # num <- Mod(mat1 * Conj(mat2)) # WX <- Mod(mat1 * Conj(mat1)) # WY <- Mod(mat2 * Conj(mat2)) # smsmnum <- smCWT(num, scalevector = scalevector4) # smsmWX <- smCWT(WX, scalevector = scalevector4) # smsmWY <- smCWT(WY, scalevector = scalevector4) # wer2 <- sum(colSums(smsmnum)^2) / # sum( sum(colSums(smsmWX) * colSums(smsmWY)) ) # Xwer_dist[i, j] <- sqrt(delta * lscvect * (1 - wer2)) # Xwer_dist[j, i] <- Xwer_dist[i, j] # } # } # diag(Xwer_dist) <- numeric(n) # # save(Xwer_dist, file = "../res/2009_synchros200WER.Rdata") load("../res/2009_synchros200WER.Rdata") ## 3. Cluster using WER distance matrix #### #hc <- hclust(as.dist(Xwer_dist), method = "ward.D") #plot(hc) # # #clust <- cutree(hc, 2) # for(K in 2:30){ #K <- 3 #pamfit <- pam(tdata[-201, ci$selectv], k = K) pamfit <- pam(as.dist(Xwer_dist), k = K, diss = TRUE) #table(pamfit$clustering) SC <- matrix(0, ncol = p, nrow = K) clustfactor <- pamfit$clustering # for(k in 1:K){ # clustk <- which(clustfactor == k) # if(length(clustk) > 0) { # if(length(clustk) > 1) { # SCk <- colSums(synchros09[which(clustfactor == k), ]) # } else { # SCk <- synchros09[which(clustfactor == k), ] # } # SC[k, ] <- SC[k, ] + SCk # rm(SCk) # } #} #write.table(clustfactor, file = paste0("~/tmp/clustfactorRC", K, ".txt")) #write.table(clustfactor, file = "~/tmp/clustfactor3.txt") write.table(clustfactor, file = paste0("~/tmp/clustfactorWER", K, ".txt")) } # # # Plots # layout(1) # matplot(t(SC)[48*10 + 1:(48*30), ], type = 'l', ylab = '',col = 1:3, lty = 1) # matplot(t(SC)[48*100 + 1:(48*30), ], type = 'l', ylab = '', col = 1:3, lty = 1) # # #