1 ## File : 06_predictions.r
2 ## Description : Calculer les synchrones pour chaque groupe
3 ## obtenu par le clustering.
7 if(Sys.info()[4] == "mojarrita"){
8 setwd("~/recherche/03_projects/2014_EDF-Orsay-Lyon2/codes/")
10 setwd("~/2014_EDF-Orsay-Lyon2/codes/")
13 ## 1. Read auxiliar data files ####
15 identifiants <- read.table("identifs.txt")[ ,1]
16 dates0 <- read.table("datesall.txt")[, 1]
17 dates09 <- as.character(dates0[grep("2009", dates0)])
18 dates10 <- as.character(dates0[grep("2010", dates0)])
19 dates <- c(dates09, dates10)
20 rm(dates0, dates09, dates10)
22 n <- length(identifiants)
25 # groups for the predictor
26 gr_calen <- read.table("calendar_transition_groups-1996-2011.txt")
27 i_start_calen <- which(rownames(gr_calen) == "2009-01-01")
28 i_end_calen <- which(rownames(gr_calen) == "2010-12-31")
29 gr <- gr_calen$gr[i_start_calen:i_end_calen]
30 gr <- gsub("Thu", "TWT", gr)
31 gr <- gsub("Wed", "TWT", gr)
32 gr <- gsub("Tue", "TWT", gr)
34 days <- seq.Date(from = as.Date("2009/01/01"),
35 to = as.Date("2010/12/31"),
38 #synchros09 <- as.matrix(read.table("~/tmp/2009_synchros200.txt"))
39 #synchros10 <- as.matrix(read.table("~/tmp/2010_synchros200.txt"))
40 #synchros09 <- as.matrix(read.table("~/tmp/2009_synchros200RC.txt"))
41 #synchros10 <- as.matrix(read.table("~/tmp/2010_synchros200RC.txt"))
42 synchros09 <- as.matrix(read.table("~/tmp/2009_synchros200RC.txt"))
43 synchros10 <- as.matrix(read.table("~/tmp/2010_synchros200RC.txt"))
46 synchros09 <- t(synchros09) # series must be on rows
47 synchros10 <- t(synchros10) # series must be on rows
49 ## 2. Imputation of missing values ####
50 # (done by linear interpolation)
51 nas <- which(is.na(synchros09)[, 1]) # some 1/1/2009 are missing
52 synchros09[nas, 1] <- rowMeans(synchros09[nas, 2:4])
54 imput09 <- synchros09[, 4180:4181] %*% matrix(c(2/3, 1/3, 1/3, 2/3), 2)
55 imput10 <- synchros10[, 4132:4133] %*% matrix(c(2/3, 1/3, 1/3, 2/3), 2)
57 synchros09 <- cbind(synchros09[, 1:4180], imput09, synchros09[, 4181:17518])
58 synchros10 <- cbind(synchros10[, 1:4132], imput10, synchros10[, 4133:17518])
61 rm(imput09, imput10, nas)
62 rm(gr_calen, i_start_calen, i_end_calen)
64 # Set start and end timepoints for the prediction
65 i_00 <- which(days == "2009-01-01")
66 n_00 <- which(days == "2009-12-31")
67 n_01 <- which(days == "2010-12-31")
70 ## 3. Construct predictors ####
72 synchros <- (cbind( synchros09, synchros10 ))[-201, ]
74 #clustfactors <- dir("~/tmp/", pattern = 'clustfactorAC')
75 #clustfactors <- dir("~/tmp/", pattern = 'clustfactorRC')
76 #clustfactors <- dir("~/tmp/", pattern = 'clustfactorWER')
77 clustfactors <- dir("~/tmp/", pattern = 'clustfactorRANDOM')
79 maxK <- length(clustfactors) - 1
81 performK <- data.frame(K = rep(NA_real_, maxK),
82 mape = rep(NA_real_, maxK),
83 rmse = rep(NA_real_, maxK))
86 for(cf in seq_along(clustfactors)) {
87 #clustfactor <- read.table(file = "~/tmp/clustfactor3.txt")
88 clustfactor <- read.table(file = clustfactors[cf])
90 K <- nrow(unique(clustfactor))
94 SC <- matrix(0, ncol = ncol(synchros), nrow = K)
96 clustk <- which(clustfactor == k)
97 if(length(clustk) > 0) {
98 if(length(clustk) > 1) {
99 SCk <- colSums(synchros[which(clustfactor == k), ])
101 SCk <- synchros[which(clustfactor == k), ]
103 SC[k, ] <- SC[k, ] + SCk
108 mat_Load <- matrix(colSums(SC), ncol = 48, byrow = TRUE)
109 mat_ks <- lapply(1:K, function(k) matrix(SC[k, ], ncol = 48, byrow = TRUE))
112 ## 2. Transform to wkdata (performs DWT) ##
114 wk_Load <- wkdata(X = c(t(mat_Load)),
115 gr = gr[1:nrow(mat_Load)],
118 wks <- lapply(mat_ks, function(mat_k)
119 wkdata(X = c(t(mat_k)), gr = gr[1:nrow(mat_Load)], p = 48))
121 ## 3. Begin of big loop over ##
122 grid <- n_00:(n_01 - 1) # these are the reference days
123 #h_Load <- h_k1 <- h_k2 <- h_k3 <- double(length(grid))
125 # output matrix of predicted days
126 # names of the predicted days (reference days + 1)
127 pred_Load <- matrix(NA_real_, ncol = 48, nrow = length(grid))
128 preds <- lapply(1:K, function(k) pred_Load)
131 for(q in seq_along(grid)[-c(121, 122, 227, 228)]) { #seq_along(grid)[-c(46:47)]
132 if((q %/% 10) == (q / 10)) cat(paste(" iteration: ", q, "\n"))
135 select_Load <- select.wkdata(wk_Load, i_00:Q)
136 aux_Load <- wavkerfun(obj = select_Load, kerneltype = "gaussian",
137 EPS = .Machine$double.eps)
138 pred_Load[q, ] <- predict.wavkerfun(obj = aux_Load, cent = "DIFF")$X
140 selects <- lapply(wks, function(k) select.wkdata(k, i_00:Q))
141 auxs <- lapply(selects,
142 function(k) wavkerfun(obj = k,
143 kerneltype = "gaussian",
144 EPS = .Machine$double.eps))
146 preds[[k]][q, ] <- predict.wavkerfun(obj = auxs[[k]], cent = "DIFF")$X
151 #rm(wks, selects, auxs, aux_Load, mat_ks, q, Q, wk_Load)
153 # Compute the prediction of synchrone by the sum of individuals
154 pred_Load_sum <- Reduce('+', preds)
156 ## 4. Analisis of the predictions ####
157 resids_Load <- pred_Load - mat_Load[grid + 1, ] # i.e. reference days + 1
158 resids_Load_sum <- pred_Load_sum - mat_Load[grid + 1, ] # i.e. reference days + 1
160 mape_Load <- 100 * rowMeans(abs(resids_Load / mat_Load[grid + 1, ]))
161 rmse_Load <- sqrt(rowMeans(resids_Load^2))
163 mape_Load_sum <- 100 * rowMeans(abs(resids_Load_sum / mat_Load[grid + 1, ]))
164 rmse_Load_sum <- sqrt(rowMeans(resids_Load_sum^2))
166 performK[cf, 2] <- mean(mape_Load, na.rm = TRUE)
167 performK[cf, 3] <- mean(mape_Load_sum, na.rm = TRUE)
168 #print(mean(mape_Load, na.rm = TRUE))
169 #print(mean(mape_Load_sum, na.rm = TRUE))
174 performKwer <- performK
176 #performKrc <- read.table("~/Documents/projects/2014_EDF-Orsay-Lyon2/res/perform200RC.txt")
177 #write.table(file = '~/Documents/projects/2014_EDF-Orsay-Lyon2/res/perform200RC.txt',
179 #write.table(file = '~/ownCloud/projects/2014_EDF-Orsay-Lyon2/res/perform200RANDOM.txt',
182 #performK <- data.frame(K = performKrc$K,
183 # rc = performKrc$rmse,
184 # wer = performKwer$rmse)
187 setwd("~/recherche/03_projects/2014_EDF-Orsay-Lyon2/res/")
188 performKWER <- read.table("perform200WER.txt")
189 performKRND <- read.table("perform200RANDOM.txt")
190 performKrnd <- read.table("perform200-random.txt")
192 performKWER <- performKWER[order(performKWER$K), ]
193 performKRND <- performKRND[order(performKRND$K), ]
194 performKrnd <- performKrnd[order(performKrnd$K), ]
196 perform <- data.frame(k = performKRND$K,
197 k_check = performKWER$K,
198 random1 = performKrnd$rmse,
199 random2 = performKRND$rmse,
200 wer = performKWER$rmse,
201 agg = performKWER$mape)
203 pdf("~/perf.pdf", width = 10)
204 plot(perform$k, perform$wer, type = 'l', ylim = c(1.7,2.2),
205 lwd = 2, col = "darkgrey", xlab = "Nb Cluster",
206 ylab = "MAPE (in %)")
207 lines(perform$k, perform$random2, lty = 2, lwd = 2)
208 lines(perform$k, perform$random1, lty = 3, lwd = 2)
209 abline(h = mean(perform$agg), lwd = 2)
210 legend("topright", c('AGG', "Random > WER", "RC > Random", "RC > WER"),
211 lwd = 2, lty = c(1, 3, 2, 1),
212 col = c(1, 1, 1, "darkgrey"), bg = "white")