1 # Original R script (not required by the C program)
4 ##################################################################
5 ## File: script_clustering_by_pam.r
7 ## Description: Using PAM to clustering conso using pam
8 ## Last modified: jan 2012 by JC
10 ##################################################################
12 ## Function: Converts a matrix of curves to the discret wavelet domain
13 toDWT <- function(x, filter.number = 6, family = "DaubLeAsymm"){
14 x2 <- spline(x, n = 2^ceiling( log(length(x), 2) ),
16 Dx2 <- wd(x2, family = family, filter.number = filter.number)$D
20 ## Function: Computes the absolute contribution of the wavelet's scale
21 ## to the total total of the curve.
22 contrib <- function(x) {
23 J <- log( length(x)+1, 2)
29 nrj[j] <- sqrt( sum( x[t0:t1]^2 ) )
35 ## 1. Load libraries & data ####
40 # powerload is a matrix that contains on each line one observation
43 ## 2. DWT ##################
44 delta <- ncol(powerload)
46 Xdwt <- t(apply(powerload, 1, toDWT)) # DWT over the lines of powerload.
47 Xnrj <- t(apply(Xdwt, 1, contrib)) # Absolute contribution to the energy.
49 ## 3. Cluster ##############
50 K <- 8 # Number of clusters
51 Xnrj_dist <- dist(Xnrj)
52 Xnrj_pam <- pam(as.dist(Xnrj_dist), K)