3 #' Plot a range of curves in data.
5 #' @inheritParams computeError
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 matplot(series, type="l", ylim=yrange, xlab="Time (hours)", ylab="PM10")
19 #' Draw error graphs, potentially from several runs of \code{computeForecast()}.
21 #' @param err Error as returned by \code{computeError()}
22 #' @param cols Colors for each error (default: 1,2,3,...)
24 #' @seealso \code{\link{plotCurves}}, \code{\link{plotPredReal}},
25 #' \code{\link{plotSimils}}, \code{\link{plotFbox}}, \code{\link{computeFilaments}},
26 #' \code{\link{plotFilamentsBox}}, \code{\link{plotRelVar}}
29 plotError <- function(err, cols=seq_along(err))
31 if (!is.null(err$abs))
33 par(mfrow=c(2,2), mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=2)
35 yrange = range( sapply(1:L, function(i) ( err[[i]]$abs$day ) ), na.rm=TRUE )
38 plot(err[[i]]$abs$day, type="l", xlab=ifelse(i==1,"Time (hours)",""),
39 ylab=ifelse(i==1,"Mean |y - y_hat|",""), ylim=yrange, col=cols[i])
43 yrange = range( sapply(1:L, function(i) ( err[[i]]$abs$indices ) ), na.rm=TRUE )
46 plot(err[[i]]$abs$indices, type="l", xlab=ifelse(i==1,"Time (days)",""),
47 ylab=ifelse(i==1,"Mean |y - y_hat|",""), ylim=yrange, col=cols[i])
51 yrange = range( sapply(1:L, function(i) ( err[[i]]$MAPE$day ) ), na.rm=TRUE )
54 plot(err[[i]]$MAPE$day, type="l", xlab=ifelse(i==1,"Time (hours)",""),
55 ylab=ifelse(i==1,"Mean MAPE",""), ylim=yrange, col=cols[i])
59 yrange = range( sapply(1:L, function(i) ( err[[i]]$MAPE$indices ) ), na.rm=TRUE )
62 plot(err[[i]]$MAPE$indices, type="l", xlab=ifelse(i==1,"Time (days)",""),
63 ylab=ifelse(i==1,"Mean MAPE",""), ylim=yrange, col=cols[i])
69 #' Plot measured / predicted
71 #' Plot measured curve (in black) and predicted curve (in blue).
73 #' @inheritParams computeError
74 #' @param index Index in forecasts (integer or date)
77 plotPredReal <- function(data, pred, index)
79 prediction = pred$getForecast(index)
80 measure = data$getSerie( pred$getIndexInData(index) )[1:length(prediction)]
81 yrange = range(measure, prediction)
82 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=3)
83 plot(measure, type="l", ylim=yrange, xlab="Time (hours)", ylab="PM10")
85 plot(prediction, type="l", col="#0000FF", ylim=yrange, xlab="", ylab="")
90 #' Plot histogram of similarities (weights), for 'Neighbors' method.
92 #' @inheritParams computeError
93 #' @param index Index in forecasts (integer or date)
96 plotSimils <- function(pred, index)
98 weights = pred$getParams(index)$weights
100 stop("plotSimils only works on 'Neighbors' forecasts")
101 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
102 hist(pred$getParams(index)$weights, nclass=20, main="", xlab="Weight", ylab="Count")
105 #' Functional boxplot
107 #' Draw the functional boxplot on the left, and bivariate plot on the right.
109 #' @inheritParams computeError
110 #' @inheritParams plotCurves
113 plotFbox <- function(data, indices=seq_len(data$getSize()))
115 if (!requireNamespace("rainbow", quietly=TRUE))
116 stop("Functional boxplot requires the rainbow package")
118 series_matrix = data$getSeries(indices)
119 # Remove series with NAs
120 no_NAs_indices = sapply( 1:ncol(series_matrix),
121 function(i) all(!is.na(series_matrix[,i])) )
122 series_matrix = series_matrix[,no_NAs_indices]
124 series_fds = rainbow::fds(seq_len(nrow(series_matrix)), series_matrix)
125 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
126 rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Time (hours)", ylab="PM10",
127 plotlegend=FALSE, lwd=2)
128 rainbow::fboxplot(series_fds, "bivariate", "hdr", plotlegend=FALSE)
133 #' Obtain similar days in the past, and (optionally) plot them -- as black as distances
136 #' @inheritParams computeError
137 #' @param index Index in forecast (integer or date)
138 #' @param limit Number of neighbors to consider
139 #' @param plot Should the result be plotted?
140 #' @param predict_from First prediction instant
142 #' @return A list with
144 #' \item index : index of the current serie ('today')
145 #' \item neighb_indices : indices of its neighbors
146 #' \item colors : colors of neighbors curves (shades of gray)
150 computeFilaments <- function(data, pred, index, predict_from, limit=60, plot=TRUE)
152 if (is.null(pred$getParams(index)$weights) || is.na(pred$getParams(index)$weights[1]))
153 stop("computeFilaments requires a serie without NAs")
155 # Compute colors for each neighbor (from darkest to lightest)
156 sorted_dists = sort(-log(pred$getParams(index)$weights), index.return=TRUE)
157 nn = min(limit, length(sorted_dists$x))
158 min_dist = min(sorted_dists$x[1:nn])
159 max_dist = max(sorted_dists$x[1:nn])
160 color_values = floor(19.5*(sorted_dists$x[1:nn]-min_dist)/(max_dist-min_dist)) + 1
161 colors = gray.colors(20,0.1,0.9)[color_values] #TODO: 20 == magic number
165 # Complete series with (past and present) tomorrows
166 ref_serie = c( data$getCenteredSerie( pred$getIndexInData(index)-1 ),
167 data$getCenteredSerie( pred$getIndexInData(index) ) )
168 centered_series = rbind(
169 data$getCenteredSeries( pred$getParams(index)$indices-1 ),
170 data$getCenteredSeries( pred$getParams(index)$indices ) )
171 yrange = range( ref_serie,
172 quantile(centered_series, probs=c(0.025,0.975), na.rm=TRUE) )
173 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=2)
176 plot(centered_series[,sorted_dists$ix[i]], ylim=yrange, type="l", col=colors[i],
177 xlab=ifelse(i==1,"Time (hours)",""), ylab=ifelse(i==1,"Centered PM10",""))
180 # Also plot ref curve, in red
181 plot(ref_serie, ylim=yrange, type="l", col="#FF0000", xlab="", ylab="")
182 abline(v=24+predict_from-0.5, lty=2, col=colors()[56], lwd=1)
186 "index"=pred$getIndexInData(index),
187 "neighb_indices"=pred$getParams(index)$indices[sorted_dists$ix[1:nn]],
191 #' Functional boxplot on filaments
193 #' Draw the functional boxplot on filaments obtained by \code{computeFilaments()}.
195 #' @inheritParams computeError
196 #' @param fil Output of \code{computeFilaments}
199 plotFilamentsBox = function(data, fil, predict_from)
201 if (!requireNamespace("rainbow", quietly=TRUE))
202 stop("Functional boxplot requires the rainbow package")
204 series_matrix = rbind(
205 data$getSeries(fil$neighb_indices-1), data$getSeries(fil$neighb_indices) )
206 series_fds = rainbow::fds(seq_len(nrow(series_matrix)), series_matrix)
208 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
209 rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Time (hours)", ylab="PM10",
210 plotlegend=FALSE, lwd=2)
212 # "Magic": http://stackoverflow.com/questions/13842560/get-xlim-from-a-plot-in-r
214 yr <- (usr[4] - usr[3]) / 27
216 plot(c(data$getSerie(fil$index),data$getSerie(fil$index+1)), type="l", lwd=2, lty=2,
217 ylim=c(usr[3] + yr, usr[4] - yr), xlab="", ylab="")
218 abline(v=24+predict_from-0.5, lty=2, col=colors()[56])
221 #' Plot relative conditional variability / absolute variability
223 #' Draw the relative conditional variability / absolute variability based on filaments
224 #' obtained by \code{computeFilaments()}.
226 #' @inheritParams computeError
227 #' @inheritParams plotFilamentsBox
230 plotRelVar = function(data, fil, predict_from)
232 ref_var = c( apply(data$getSeries(fil$neighb_indices-1),1,sd),
233 apply(data$getSeries(fil$neighb_indices),1,sd) )
234 tdays = .getNoNA2(data, 2, fil$index)
236 apply(data$getSeries(tdays-1),1,sd),
237 apply(data$getSeries(tdays),1,sd) )
239 yrange = range(ref_var, global_var)
240 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
241 plot(ref_var, type="l", col=1, lwd=3, ylim=yrange,
242 xlab="Time (hours)", ylab="Standard deviation")
244 plot(global_var, type="l", col=2, lwd=3, ylim=yrange, xlab="", ylab="")
245 abline(v=24+predict_from-0.5, lty=2, col=colors()[56])