2488d81548202fa9af8e8e3e2a1f23f006914694
[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, lwd=2)
34 L = length(err)
35 yrange = range( sapply(1:L, function(i) ( err[[i]]$abs$day ) ), na.rm=TRUE )
36 for (i in seq_len(L))
37 {
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])
40 if (i < L)
41 par(new=TRUE)
42 }
43 yrange = range( sapply(1:L, function(i) ( err[[i]]$abs$indices ) ), na.rm=TRUE )
44 for (i in seq_len(L))
45 {
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])
48 if (i < L)
49 par(new=TRUE)
50 }
51 yrange = range( sapply(1:L, function(i) ( err[[i]]$MAPE$day ) ), na.rm=TRUE )
52 for (i in seq_len(L))
53 {
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])
56 if (i < L)
57 par(new=TRUE)
58 }
59 yrange = range( sapply(1:L, function(i) ( err[[i]]$MAPE$indices ) ), na.rm=TRUE )
60 for (i in seq_len(L))
61 {
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])
64 if (i < L)
65 par(new=TRUE)
66 }
67 }
68
69 #' Plot measured / predicted
70 #'
71 #' Plot measured curve (in black) and predicted curve (in blue).
72 #'
73 #' @inheritParams computeError
74 #' @param index Index in forecasts (integer or date)
75 #'
76 #' @export
77 plotPredReal <- function(data, pred, index)
78 {
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")
84 par(new=TRUE)
85 plot(prediction, type="l", col="#0000FF", ylim=yrange, xlab="", ylab="")
86 }
87
88 #' Plot similarities
89 #'
90 #' Plot histogram of similarities (weights), for 'Neighbors' method.
91 #'
92 #' @inheritParams computeError
93 #' @param index Index in forecasts (integer or date)
94 #'
95 #' @export
96 plotSimils <- function(pred, index)
97 {
98 weights = pred$getParams(index)$weights
99 if (is.null(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")
103 }
104
105 #' Functional boxplot
106 #'
107 #' Draw the functional boxplot on the left, and bivariate plot on the right.
108 #'
109 #' @inheritParams computeError
110 #' @inheritParams plotCurves
111 #'
112 #' @export
113 plotFbox <- function(data, indices=seq_len(data$getSize()))
114 {
115 if (!requireNamespace("rainbow", quietly=TRUE))
116 stop("Functional boxplot requires the rainbow package")
117
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]
123
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)
129 }
130
131 #' Compute filaments
132 #'
133 #' Obtain similar days in the past, and (optionally) plot them -- as black as distances
134 #' are small.
135 #'
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
141 #'
142 #' @return A list with
143 #' \itemize{
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)
147 #' }
148 #'
149 #' @export
150 computeFilaments <- function(data, pred, index, predict_from, limit=60, plot=TRUE)
151 {
152 if (is.null(pred$getParams(index)$weights) || is.na(pred$getParams(index)$weights[1]))
153 stop("computeFilaments requires a serie without NAs")
154
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
162
163 if (plot)
164 {
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)
174 for (i in nn:1)
175 {
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",""))
178 par(new=TRUE)
179 }
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)
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 #'
198 #' @export
199 plotFilamentsBox = function(data, fil, predict_from)
200 {
201 if (!requireNamespace("rainbow", quietly=TRUE))
202 stop("Functional boxplot requires the rainbow package")
203
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)
207
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)
211
212 # "Magic": http://stackoverflow.com/questions/13842560/get-xlim-from-a-plot-in-r
213 usr <- par("usr")
214 yr <- (usr[4] - usr[3]) / 27
215 par(new=TRUE)
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])
219 }
220
221 #' Plot relative conditional variability / absolute variability
222 #'
223 #' Draw the relative conditional variability / absolute variability based on filaments
224 #' obtained by \code{computeFilaments()}.
225 #'
226 #' @inheritParams computeError
227 #' @inheritParams plotFilamentsBox
228 #'
229 #' @export
230 plotRelVar = function(data, fil, predict_from)
231 {
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)
235 global_var = c(
236 apply(data$getSeries(tdays-1),1,sd),
237 apply(data$getSeries(tdays),1,sd) )
238
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")
243 par(new=TRUE)
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])
246 }