Commit | Line | Data |
---|---|---|
98e958ca | 1 | #' Plot curves |
e030a6e3 | 2 | #' |
102bcfda | 3 | #' Plot a range of curves in data. |
e030a6e3 | 4 | #' |
102bcfda | 5 | #' @inheritParams computeError |
e030a6e3 BA |
6 | #' @param indices Range of indices (integers or dates) |
7 | #' | |
8 | #' @export | |
1e20780e | 9 | plotCurves <- function(data, indices=seq_len(data$getSize())) |
e030a6e3 | 10 | { |
98e958ca | 11 | series = data$getSeries(indices) |
a5a3a294 | 12 | yrange = quantile(series, probs=c(0.025,0.975), na.rm=TRUE) |
e030a6e3 | 13 | par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5) |
4f3fdbb8 | 14 | matplot(series, type="l", ylim=yrange, xlab="Time (hours)", ylab="PM10") |
e030a6e3 BA |
15 | } |
16 | ||
af3b84f4 | 17 | #' Plot error |
3d69ff21 | 18 | #' |
102bcfda | 19 | #' Draw error graphs, potentially from several runs of \code{computeForecast()}. |
3d69ff21 | 20 | #' |
102bcfda | 21 | #' @param err Error as returned by \code{computeError()} |
09cf9c19 | 22 | #' @param cols Colors for each error (default: 1,2,3,...) |
3a38473a | 23 | #' @param agg Aggregation level ("day", "week" or "month") |
3d69ff21 | 24 | #' |
98e958ca | 25 | #' @seealso \code{\link{plotCurves}}, \code{\link{plotPredReal}}, |
72b9c501 BA |
26 | #' \code{\link{plotSimils}}, \code{\link{plotFbox}}, \code{\link{computeFilaments}}, |
27 | #' \code{\link{plotFilamentsBox}}, \code{\link{plotRelVar}} | |
3d69ff21 BA |
28 | #' |
29 | #' @export | |
aa5397f1 | 30 | plotError <- function(err, cols=seq_along(err), agg="day") |
3d69ff21 | 31 | { |
09cf9c19 BA |
32 | if (!is.null(err$abs)) |
33 | err = list(err) | |
10886062 | 34 | par(mfrow=c(2,2), mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5) |
3d69ff21 | 35 | L = length(err) |
9b9bb2d4 | 36 | |
aa5397f1 BA |
37 | yrange = range( sapply(1:L, function(i) err[[i]]$abs$day), na.rm=TRUE ) |
38 | matplot(sapply( seq_len(L), function(i) err[[i]]$abs$day ), type="l", | |
39 | xlab="Time (hours)", ylab="Mean |y - y_hat|", ylim=yrange, col=cols, lwd=2, lty=1) | |
9b9bb2d4 | 40 | |
aa5397f1 BA |
41 | agg_curves <- sapply( seq_len(L), function(i) { |
42 | curve <- err[[i]]$abs$indices | |
43 | delta <- if (agg=="day") 1 else if (agg=="week") 7 else if (agg=="month") 30 | |
44 | vapply( seq(1,length(curve),delta), function(i) { | |
45 | mean(curve[i:(i+delta-1)], na.rm=TRUE) | |
46 | }, vector("double",1), USE.NAMES=FALSE ) | |
47 | }) | |
48 | yrange = range(agg_curves, na.rm=TRUE) | |
49 | matplot(agg_curves, type="l", xlab=paste("Time (",agg,"s)", sep=""), | |
50 | ylab="Mean |y - y_hat|", ylim=yrange, col=cols, lwd=2, lty=1) | |
9b9bb2d4 | 51 | |
aa5397f1 BA |
52 | yrange = range( sapply(1:L, function(i) err[[i]]$MAPE$day), na.rm=TRUE ) |
53 | matplot(sapply( seq_len(L), function(i) err[[i]]$MAPE$day ), type="l", | |
54 | xlab="Time (hours)", ylab="Mean MAPE", ylim=yrange, col=cols, lwd=2, lty=1) | |
9b9bb2d4 | 55 | |
aa5397f1 BA |
56 | agg_curves <- sapply( seq_len(L), function(i) { |
57 | curve <- err[[i]]$MAPE$indices | |
58 | delta <- if (agg=="day") 1 else if (agg=="week") 7 else if (agg=="month") 30 | |
59 | vapply( seq(1,length(curve),delta), function(i) { | |
60 | mean(curve[i:(i+delta-1)], na.rm=TRUE) | |
61 | }, vector("double",1), USE.NAMES=FALSE ) | |
62 | }) | |
63 | yrange = range(agg_curves, na.rm=TRUE) | |
64 | matplot(agg_curves, type="l", xlab=paste("Time (",agg,"s)", sep=""), | |
65 | ylab="Mean MAPE", ylim=yrange, col=cols, lwd=2, lty=1) | |
3d69ff21 BA |
66 | } |
67 | ||
98e958ca BA |
68 | #' Plot measured / predicted |
69 | #' | |
102bcfda | 70 | #' Plot measured curve (in black) and predicted curve (in blue). |
98e958ca | 71 | #' |
102bcfda | 72 | #' @inheritParams computeError |
98e958ca BA |
73 | #' @param index Index in forecasts (integer or date) |
74 | #' | |
75 | #' @export | |
76 | plotPredReal <- function(data, pred, index) | |
77 | { | |
72b9c501 | 78 | prediction = pred$getForecast(index) |
9b9bb2d4 BA |
79 | measure = data$getSerie( pred$getIndexInData(index) )[1:length(pred$getForecast(1))] |
80 | ||
81 | # Remove the common part, where prediction == measure | |
2e0ef04b BA |
82 | dot_mark <- ifelse(prediction[1]==measure[1], |
83 | which.max(seq_along(prediction)[prediction==measure]), 0) | |
9b9bb2d4 BA |
84 | prediction = prediction[(dot_mark+1):length(prediction)] |
85 | measure = measure[(dot_mark+1):length(measure)] | |
86 | ||
98e958ca BA |
87 | yrange = range(measure, prediction) |
88 | par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=3) | |
4e25de2c | 89 | plot(measure, type="l", ylim=yrange, xlab="Time (hours)", ylab="PM10") |
98e958ca BA |
90 | par(new=TRUE) |
91 | plot(prediction, type="l", col="#0000FF", ylim=yrange, xlab="", ylab="") | |
92 | } | |
93 | ||
94 | #' Plot similarities | |
95 | #' | |
102bcfda | 96 | #' Plot histogram of similarities (weights), for 'Neighbors' method. |
98e958ca | 97 | #' |
102bcfda | 98 | #' @inheritParams computeError |
98e958ca BA |
99 | #' @param index Index in forecasts (integer or date) |
100 | #' | |
101 | #' @export | |
102 | plotSimils <- function(pred, index) | |
103 | { | |
104 | weights = pred$getParams(index)$weights | |
105 | if (is.null(weights)) | |
106 | stop("plotSimils only works on 'Neighbors' forecasts") | |
9b9bb2d4 BA |
107 | par(mfrow=c(1,2), mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5) |
108 | small_weights = weights[ weights < 1/length(weights) ] | |
109 | large_weights = weights[ weights >= 1/length(weights) ] | |
110 | hist(small_weights, nclass=25, main="", xlab="Weight < 1/N", ylab="Count") | |
111 | hist(large_weights, nclass=25, main="", xlab="Weight >= 1/N", ylab="Count") | |
98e958ca BA |
112 | } |
113 | ||
af3b84f4 | 114 | #' Functional boxplot |
3d69ff21 | 115 | #' |
102bcfda | 116 | #' Draw the functional boxplot on the left, and bivariate plot on the right. |
3d69ff21 | 117 | #' |
102bcfda BA |
118 | #' @inheritParams computeError |
119 | #' @inheritParams plotCurves | |
3d69ff21 BA |
120 | #' |
121 | #' @export | |
98e958ca | 122 | plotFbox <- function(data, indices=seq_len(data$getSize())) |
3d69ff21 BA |
123 | { |
124 | if (!requireNamespace("rainbow", quietly=TRUE)) | |
125 | stop("Functional boxplot requires the rainbow package") | |
126 | ||
98e958ca BA |
127 | series_matrix = data$getSeries(indices) |
128 | # Remove series with NAs | |
af3b84f4 BA |
129 | no_NAs_indices = sapply( 1:ncol(series_matrix), |
130 | function(i) all(!is.na(series_matrix[,i])) ) | |
99f83c9a | 131 | series_matrix = series_matrix[,no_NAs_indices] |
3d69ff21 BA |
132 | |
133 | series_fds = rainbow::fds(seq_len(nrow(series_matrix)), series_matrix) | |
fa8078f9 | 134 | par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5) |
4e25de2c | 135 | rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Time (hours)", ylab="PM10", |
3d69ff21 | 136 | plotlegend=FALSE, lwd=2) |
98e958ca BA |
137 | rainbow::fboxplot(series_fds, "bivariate", "hdr", plotlegend=FALSE) |
138 | } | |
139 | ||
140 | #' Compute filaments | |
141 | #' | |
102bcfda BA |
142 | #' Obtain similar days in the past, and (optionally) plot them -- as black as distances |
143 | #' are small. | |
98e958ca | 144 | #' |
102bcfda | 145 | #' @inheritParams computeError |
8f84543c | 146 | #' @param index Index in forecast (integer or date) |
98e958ca BA |
147 | #' @param limit Number of neighbors to consider |
148 | #' @param plot Should the result be plotted? | |
149 | #' | |
150 | #' @return A list with | |
151 | #' \itemize{ | |
152 | #' \item index : index of the current serie ('today') | |
153 | #' \item neighb_indices : indices of its neighbors | |
154 | #' \item colors : colors of neighbors curves (shades of gray) | |
155 | #' } | |
156 | #' | |
157 | #' @export | |
9b9bb2d4 | 158 | computeFilaments <- function(data, pred, index, limit=60, plot=TRUE) |
98e958ca | 159 | { |
9b9bb2d4 BA |
160 | weights <- pred$getParams(index)$weights |
161 | if (is.null(weights) || is.na(pred$getParams(index)$weights[1])) | |
98e958ca BA |
162 | stop("computeFilaments requires a serie without NAs") |
163 | ||
9b9bb2d4 BA |
164 | nn <- min(limit, length(weights)) |
165 | sorted_dists = sort(-log(weights), index.return=TRUE) | |
166 | # Compute colors for each neighbor (from darkest to lightest), if weights differ | |
167 | if ( any( weights != weights[1] ) ) | |
168 | { | |
169 | min_dist = min(sorted_dists$x[1:nn]) | |
170 | max_dist = max(sorted_dists$x[1:nn]) | |
171 | color_values = floor(19.5*(sorted_dists$x[1:nn]-min_dist)/(max_dist-min_dist)) + 1 | |
172 | colors = gray.colors(20,0.1,0.9)[color_values] #TODO: 20 == magic number | |
173 | } | |
174 | else | |
175 | colors <- rep(colors()[17], length(weights)) | |
98e958ca BA |
176 | |
177 | if (plot) | |
178 | { | |
179 | # Complete series with (past and present) tomorrows | |
d2ab47a7 BA |
180 | ref_serie = c( data$getCenteredSerie( pred$getIndexInData(index)-1 ), |
181 | data$getCenteredSerie( pred$getIndexInData(index) ) ) | |
8f84543c | 182 | centered_series = rbind( |
9e0f25f6 BA |
183 | data$getCenteredSeries( pred$getParams(index)$indices-1 ), |
184 | data$getCenteredSeries( pred$getParams(index)$indices ) ) | |
72b9c501 BA |
185 | yrange = range( ref_serie, |
186 | quantile(centered_series, probs=c(0.025,0.975), na.rm=TRUE) ) | |
98e958ca BA |
187 | par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=2) |
188 | for (i in nn:1) | |
189 | { | |
8f84543c | 190 | plot(centered_series[,sorted_dists$ix[i]], ylim=yrange, type="l", col=colors[i], |
4e25de2c | 191 | xlab=ifelse(i==1,"Time (hours)",""), ylab=ifelse(i==1,"Centered PM10","")) |
98e958ca BA |
192 | par(new=TRUE) |
193 | } | |
194 | # Also plot ref curve, in red | |
195 | plot(ref_serie, ylim=yrange, type="l", col="#FF0000", xlab="", ylab="") | |
9b9bb2d4 BA |
196 | dot_mark <- 0.5 + which.max( pred$getForecast(1) == |
197 | data$getSerie( pred$getIndexInData(1) )[1:length(pred$getForecast(1))] ) | |
198 | abline(v=24+dot_mark, lty=2, col=colors()[56], lwd=1) | |
98e958ca BA |
199 | } |
200 | ||
8f84543c BA |
201 | list( |
202 | "index"=pred$getIndexInData(index), | |
203 | "neighb_indices"=pred$getParams(index)$indices[sorted_dists$ix[1:nn]], | |
204 | "colors"=colors) | |
fa8078f9 BA |
205 | } |
206 | ||
af3b84f4 | 207 | #' Functional boxplot on filaments |
fa8078f9 | 208 | #' |
102bcfda | 209 | #' Draw the functional boxplot on filaments obtained by \code{computeFilaments()}. |
fa8078f9 | 210 | #' |
102bcfda | 211 | #' @inheritParams computeError |
98e958ca | 212 | #' @param fil Output of \code{computeFilaments} |
9b9bb2d4 | 213 | #' @param predict_from First predicted time step |
fa8078f9 BA |
214 | #' |
215 | #' @export | |
d2ab47a7 | 216 | plotFilamentsBox = function(data, fil, predict_from) |
fa8078f9 | 217 | { |
98e958ca BA |
218 | if (!requireNamespace("rainbow", quietly=TRUE)) |
219 | stop("Functional boxplot requires the rainbow package") | |
220 | ||
221 | series_matrix = rbind( | |
3fd7377d | 222 | data$getSeries(fil$neighb_indices-1), data$getSeries(fil$neighb_indices) ) |
98e958ca | 223 | series_fds = rainbow::fds(seq_len(nrow(series_matrix)), series_matrix) |
3fd7377d | 224 | |
98e958ca | 225 | par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5) |
4e25de2c | 226 | rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Time (hours)", ylab="PM10", |
98e958ca BA |
227 | plotlegend=FALSE, lwd=2) |
228 | ||
72b9c501 | 229 | # "Magic": http://stackoverflow.com/questions/13842560/get-xlim-from-a-plot-in-r |
fa8078f9 BA |
230 | usr <- par("usr") |
231 | yr <- (usr[4] - usr[3]) / 27 | |
98e958ca | 232 | par(new=TRUE) |
9b9bb2d4 | 233 | plot(c(data$getSerie(fil$index-1),data$getSerie(fil$index)), type="l", lwd=2, lty=2, |
fa8078f9 | 234 | ylim=c(usr[3] + yr, usr[4] - yr), xlab="", ylab="") |
d2ab47a7 | 235 | abline(v=24+predict_from-0.5, lty=2, col=colors()[56]) |
3d69ff21 | 236 | } |
16b1c049 | 237 | |
af3b84f4 | 238 | #' Plot relative conditional variability / absolute variability |
16b1c049 | 239 | #' |
af3b84f4 | 240 | #' Draw the relative conditional variability / absolute variability based on filaments |
102bcfda | 241 | #' obtained by \code{computeFilaments()}. |
16b1c049 | 242 | #' |
102bcfda BA |
243 | #' @inheritParams computeError |
244 | #' @inheritParams plotFilamentsBox | |
16b1c049 BA |
245 | #' |
246 | #' @export | |
d2ab47a7 | 247 | plotRelVar = function(data, fil, predict_from) |
16b1c049 | 248 | { |
3fd7377d BA |
249 | ref_var = c( apply(data$getSeries(fil$neighb_indices-1),1,sd), |
250 | apply(data$getSeries(fil$neighb_indices),1,sd) ) | |
cf3bb001 | 251 | tdays = .getNoNA2(data, 2, fil$index) |
72b9c501 | 252 | global_var = c( |
cf3bb001 BA |
253 | apply(data$getSeries(tdays-1),1,sd), |
254 | apply(data$getSeries(tdays),1,sd) ) | |
16b1c049 | 255 | |
af3b84f4 | 256 | yrange = range(ref_var, global_var) |
16b1c049 | 257 | par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5) |
af3b84f4 | 258 | plot(ref_var, type="l", col=1, lwd=3, ylim=yrange, |
4e25de2c | 259 | xlab="Time (hours)", ylab="Standard deviation") |
16b1c049 | 260 | par(new=TRUE) |
98e958ca | 261 | plot(global_var, type="l", col=2, lwd=3, ylim=yrange, xlab="", ylab="") |
d2ab47a7 | 262 | abline(v=24+predict_from-0.5, lty=2, col=colors()[56]) |
16b1c049 | 263 | } |