commit last state
[ppam-mpi.git] / script_clustering_by_pam.r
CommitLineData
81923e5c
BA
1##################################################################
2## File: script_clustering_by_pam.r
3##
4## Description: Using PAM to clustering conso using pam
5## Last modified: jan 2012 by JC
6##
7##################################################################
8
9## Function: Converts a matrix of curves to the discret wavelet domain
10toDWT <- function(x, filter.number = 6, family = "DaubLeAsymm"){
11 x2 <- spline(x, n = 2^ceiling( log(length(x), 2) ),
12 method = 'natural')$y
13 Dx2 <- wd(x2, family = family, filter.number = filter.number)$D
14 return(Dx2)
15}
16
17## Function: Computes the absolute contribution of the wavelet's scale
18## to the total total of the curve.
19contrib <- function(x) {
20 J <- log( length(x)+1, 2)
21 nrj <- numeric(J)
22 t0 <- 1
23 t1 <- 0
24 for( j in 1:J ) {
25 t1 <- t1 + 2^(J-j)
26 nrj[j] <- sqrt( sum( x[t0:t1]^2 ) )
27 t0 <- t1 + 1
28 }
29 return(nrj)
30}
31
32## 1. Load libraries & data ####
33library(wavethresh)
34library(cluster)
35
36
37# powerload is a matrix that contains on each line one observation
38# of length delta
39
40## 2. DWT ##################
41delta <- ncol(powerload)
42n <- nrow(powerload)
43Xdwt <- t(apply(powerload, 1, toDWT)) # DWT over the lines of powerload.
44Xnrj <- t(apply(Xdwt, 1, contrib)) # Absolute contribution to the energy.
45
46## 3. Cluster ##############
47K <- 8 # Number of clusters
48Xnrj_dist <- dist(Xnrj)
49Xnrj_pam <- pam(as.dist(Xnrj_dist), K)