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