3 #' Plot a range of curves in data
5 #' @param data Object of class Data
6 #' @param indices Range of indices (integers or dates)
9 plotCurves <- function(data, indices=seq_len(data$getSize()))
11 series = data$getSeries(indices)
12 yrange = quantile(series, probs=c(0.025,0.975), na.rm=TRUE)
13 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
14 for (i in seq_along(indices))
16 plot(series[,i], type="l", ylim=yrange,
17 xlab=ifelse(i==1,"Temps (en heures)",""), ylab=ifelse(i==1,"PM10",""))
18 if (i < length(indices))
25 #' Draw error graphs, potentially from several runs of \code{computeForecast}
27 #' @param err Error as returned by \code{computeError}
28 #' @param cols Colors for each error (default: 1,2,3,...)
30 #' @seealso \code{\link{plotCurves}}, \code{\link{plotPredReal}},
31 #' \code{\link{plotSimils}}, \code{\link{plotFbox}},
32 #' \code{\link{computeFilaments}, }\code{\link{plotFilamentsBox}}, \code{\link{plotRelVar}}
35 plotError <- function(err, cols=seq_along(err))
37 if (!is.null(err$abs))
39 par(mfrow=c(2,2), mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=2)
41 yrange = range( sapply(1:L, function(i) ( err[[i]]$abs$day ) ), na.rm=TRUE )
44 plot(err[[i]]$abs$day, type="l", xlab=ifelse(i==1,"Temps (heures)",""),
45 ylab=ifelse(i==1,"Moyenne |y - y_hat|",""), ylim=yrange, col=cols[i])
49 yrange = range( sapply(1:L, function(i) ( err[[i]]$abs$indices ) ), na.rm=TRUE )
52 plot(err[[i]]$abs$indices, type="l", xlab=ifelse(i==1,"Temps (jours)",""),
53 ylab=ifelse(i==1,"Moyenne |y - y_hat|",""), ylim=yrange, col=cols[i])
57 yrange = range( sapply(1:L, function(i) ( err[[i]]$MAPE$day ) ), na.rm=TRUE )
60 plot(err[[i]]$MAPE$day, type="l", xlab=ifelse(i==1,"Temps (heures)",""),
61 ylab=ifelse(i==1,"MAPE moyen",""), ylim=yrange, col=cols[i])
65 yrange = range( sapply(1:L, function(i) ( err[[i]]$MAPE$indices ) ), na.rm=TRUE )
68 plot(err[[i]]$MAPE$indices, type="l", xlab=ifelse(i==1,"Temps (jours)",""),
69 ylab=ifelse(i==1,"MAPE moyen",""), ylim=yrange, col=cols[i])
75 #' Plot measured / predicted
77 #' Plot measured curve (in black) and predicted curve (in blue)
79 #' @param data Object return by \code{getData}
80 #' @param pred Object as returned by \code{computeForecast}
81 #' @param index Index in forecasts (integer or date)
84 plotPredReal <- function(data, pred, index)
86 horizon = length(pred$getSerie(1))
87 measure = data$getSerie( pred$getIndexInData(index)+1 )[1:horizon]
88 prediction = pred$getSerie(index)
89 yrange = range(measure, prediction)
90 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=3)
91 plot(measure, type="l", ylim=yrange, xlab="Temps (en heures)", ylab="PM10")
93 plot(prediction, type="l", col="#0000FF", ylim=yrange, xlab="", ylab="")
98 #' Plot histogram of similarities (weights)
100 #' @param pred Object as returned by \code{computeForecast}
101 #' @param index Index in forecasts (integer or date)
104 plotSimils <- function(pred, index)
106 weights = pred$getParams(index)$weights
107 if (is.null(weights))
108 stop("plotSimils only works on 'Neighbors' forecasts")
109 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
110 hist(pred$getParams(index)$weights, nclass=20, xlab="Poids", ylab="Effectif")
113 #' Functional boxplot
115 #' Draw the functional boxplot on the left, and bivariate plot on the right
117 #' @param data Object return by \code{getData}
118 #' @param indices integer or date indices to process
119 #' @param plot_bivariate Should the bivariate plot appear?
122 plotFbox <- function(data, indices=seq_len(data$getSize()))
124 if (!requireNamespace("rainbow", quietly=TRUE))
125 stop("Functional boxplot requires the rainbow package")
127 series_matrix = data$getSeries(indices)
128 # Remove series with NAs
129 no_NAs_indices = sapply( 1:ncol(series_matrix),
130 function(i) all(!is.na(series_matrix[,i])) )
131 series_matrix = series_matrix[,no_NAs_indices]
133 series_fds = rainbow::fds(seq_len(nrow(series_matrix)), series_matrix)
134 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
135 rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Temps (heures)", ylab="PM10",
136 plotlegend=FALSE, lwd=2)
137 rainbow::fboxplot(series_fds, "bivariate", "hdr", plotlegend=FALSE)
142 #' Get similar days in the past, as black as distances are small
144 #' @param data Object as returned by \code{getData}
145 #' @param index Index in data (integer or date)
146 #' @param limit Number of neighbors to consider
147 #' @param plot Should the result be plotted?
149 #' @return A list with
151 #' \item index : index of the current serie ('today')
152 #' \item neighb_indices : indices of its neighbors
153 #' \item colors : colors of neighbors curves (shades of gray)
157 computeFilaments <- function(data, index, limit=60, plot=TRUE)
159 ref_serie = data$getCenteredSerie(index)
160 if (any(is.na(ref_serie)))
161 stop("computeFilaments requires a serie without NAs")
163 # Determine indices of no-NAs days followed by no-NAs tomorrows
164 fdays = getNoNA2(data, 1, dateIndexToInteger(index,data)-1)
165 # Series + tomorrows in columns, ref_serie first
166 centered_series = data$getCenteredSeries(fdays)
168 # Obtain neighbors (closest for euclidian norm)
169 L = length(ref_serie)
170 distances = sqrt( colSums( (centered_series - ref_serie)^2 / L ) )
171 sorted_distances = sort(distances, index.return=TRUE)
173 # Compute colors for each neighbor (from darkest to lightest)
174 nn = min(limit, length(distances))
175 min_dist = min(sorted_distances$x[1:nn])
176 max_dist = max(sorted_distances$x[1:nn])
177 color_values = floor( 19.5 * (sorted_distances$x[1:nn]-min_dist) / (max_dist-min_dist) ) + 1
178 colors = gray.colors(20,0.1,0.9)[color_values] #TODO: 20 == magic number
182 # Complete series with (past and present) tomorrows
183 ref_serie = c(ref_serie,data$getCenteredSerie(index+1))
184 centered_series = rbind( centered_series, data$getCenteredSeries(fdays+1) )
185 yrange = quantile(cbind(ref_serie,centered_series), probs=c(0.025,0.975), na.rm=TRUE)
186 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=2)
189 plot(centered_series[,sorted_distances$ix[i]], ylim=yrange, type="l", col=colors[i],
190 xlab=ifelse(i==nn,"Temps (en heures)",""), ylab=ifelse(i==nn,"PM10 centré",""))
193 # Also plot ref curve, in red
194 plot(ref_serie, ylim=yrange, type="l", col="#FF0000", xlab="", ylab="")
195 abline(v=24, lty=2, col=colors()[56], lwd=1)
198 list("index"=index,"neighb_indices"=fdays[sorted_distances$ix[1:nn]],"colors"=colors)
201 #' Functional boxplot on filaments
203 #' Draw the functional boxplot on filaments obtained by \code{computeFilaments}
205 #' @param data Object return by \code{getData}
206 #' @param fil Output of \code{computeFilaments}
209 plotFilamentsBox = function(data, fil, ...)
211 if (!requireNamespace("rainbow", quietly=TRUE))
212 stop("Functional boxplot requires the rainbow package")
214 series_matrix = rbind(
215 data$getSeries(fil$neighb_indices), data$getSeries(fil$neighb_indices+1) )
216 series_fds = rainbow::fds(seq_len(nrow(series_matrix)), series_matrix)
217 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
218 rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Temps (heures)", ylab="PM10",
219 plotlegend=FALSE, lwd=2)
221 # "Magic" found at http://stackoverflow.com/questions/13842560/get-xlim-from-a-plot-in-r
223 yr <- (usr[4] - usr[3]) / 27
225 plot(c(data$getSerie(fil$index),data$getSerie(fil$index+1)), type="l", lwd=2, lty=2,
226 ylim=c(usr[3] + yr, usr[4] - yr), xlab="", ylab="")
227 abline(v=24, lty=2, col=colors()[56])
230 #' Plot relative conditional variability / absolute variability
232 #' Draw the relative conditional variability / absolute variability based on filaments
233 #' obtained by \code{computeFilaments}
235 #' @param data Object return by \code{getData}
236 #' @param fil Output of \code{computeFilaments}
239 plotRelVar = function(data, fil, ...)
241 ref_var = c( apply(data$getSeries(fil$neighb_indices),1,sd),
242 apply(data$getSeries(fil$neighb_indices+1),1,sd) )
243 fdays = getNoNA2(data, 1, fil$index-1)
244 global_var = c( apply(data$getSeries(fdays),1,sd), apply(data$getSeries(fdays+1),1,sd) )
246 yrange = range(ref_var, global_var)
247 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
248 plot(ref_var, type="l", col=1, lwd=3, ylim=yrange,
249 xlab="Temps (heures)", ylab="Écart-type")
251 plot(global_var, type="l", col=2, lwd=3, ylim=yrange, xlab="", ylab="")
252 abline(v=24, lty=2, col=colors()[56])