remove logging
[talweg.git] / pkg / R / plot.R
CommitLineData
98e958ca 1#' Plot curves
e030a6e3 2#'
af3b84f4 3#' Plot a range of curves in data
e030a6e3
BA
4#'
5#' @param data Object of class Data
6#' @param indices Range of indices (integers or dates)
7#'
8#' @export
1e20780e 9plotCurves <- 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
BA
13 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
14 for (i in seq_along(indices))
15 {
98e958ca 16 plot(series[,i], type="l", ylim=yrange,
e030a6e3 17 xlab=ifelse(i==1,"Temps (en heures)",""), ylab=ifelse(i==1,"PM10",""))
1e20780e 18 if (i < length(indices))
e030a6e3
BA
19 par(new=TRUE)
20 }
21}
22
af3b84f4 23#' Plot error
3d69ff21 24#'
af3b84f4 25#' Draw error graphs, potentially from several runs of \code{computeForecast}
3d69ff21 26#'
99f83c9a 27#' @param err Error as returned by \code{computeError}
09cf9c19 28#' @param cols Colors for each error (default: 1,2,3,...)
3d69ff21 29#'
98e958ca
BA
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}}
3d69ff21
BA
33#'
34#' @export
09cf9c19 35plotError <- function(err, cols=seq_along(err))
3d69ff21 36{
09cf9c19
BA
37 if (!is.null(err$abs))
38 err = list(err)
4e95ec8f 39 par(mfrow=c(2,2), mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=2)
3d69ff21 40 L = length(err)
98e958ca 41 yrange = range( sapply(1:L, function(i) ( err[[i]]$abs$day ) ), na.rm=TRUE )
3d69ff21
BA
42 for (i in seq_len(L))
43 {
44 plot(err[[i]]$abs$day, type="l", xlab=ifelse(i==1,"Temps (heures)",""),
09cf9c19 45 ylab=ifelse(i==1,"Moyenne |y - y_hat|",""), ylim=yrange, col=cols[i])
3d69ff21
BA
46 if (i < L)
47 par(new=TRUE)
48 }
98e958ca 49 yrange = range( sapply(1:L, function(i) ( err[[i]]$abs$indices ) ), na.rm=TRUE )
3d69ff21
BA
50 for (i in seq_len(L))
51 {
52 plot(err[[i]]$abs$indices, type="l", xlab=ifelse(i==1,"Temps (jours)",""),
09cf9c19 53 ylab=ifelse(i==1,"Moyenne |y - y_hat|",""), ylim=yrange, col=cols[i])
3d69ff21
BA
54 if (i < L)
55 par(new=TRUE)
56 }
98e958ca 57 yrange = range( sapply(1:L, function(i) ( err[[i]]$MAPE$day ) ), na.rm=TRUE )
3d69ff21
BA
58 for (i in seq_len(L))
59 {
60 plot(err[[i]]$MAPE$day, type="l", xlab=ifelse(i==1,"Temps (heures)",""),
09cf9c19 61 ylab=ifelse(i==1,"MAPE moyen",""), ylim=yrange, col=cols[i])
3d69ff21
BA
62 if (i < L)
63 par(new=TRUE)
64 }
98e958ca 65 yrange = range( sapply(1:L, function(i) ( err[[i]]$MAPE$indices ) ), na.rm=TRUE )
3d69ff21
BA
66 for (i in seq_len(L))
67 {
68 plot(err[[i]]$MAPE$indices, type="l", xlab=ifelse(i==1,"Temps (jours)",""),
09cf9c19 69 ylab=ifelse(i==1,"MAPE moyen",""), ylim=yrange, col=cols[i])
3d69ff21
BA
70 if (i < L)
71 par(new=TRUE)
72 }
73}
74
98e958ca
BA
75#' Plot measured / predicted
76#'
77#' Plot measured curve (in black) and predicted curve (in blue)
78#'
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)
82#'
83#' @export
84plotPredReal <- function(data, pred, index)
85{
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")
92 par(new=TRUE)
93 plot(prediction, type="l", col="#0000FF", ylim=yrange, xlab="", ylab="")
94}
95
96#' Plot similarities
97#'
98#' Plot histogram of similarities (weights)
99#'
100#' @param pred Object as returned by \code{computeForecast}
101#' @param index Index in forecasts (integer or date)
102#'
103#' @export
104plotSimils <- function(pred, index)
105{
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")
111}
112
af3b84f4 113#' Functional boxplot
3d69ff21 114#'
af3b84f4 115#' Draw the functional boxplot on the left, and bivariate plot on the right
3d69ff21
BA
116#'
117#' @param data Object return by \code{getData}
98e958ca 118#' @param indices integer or date indices to process
fa8078f9 119#' @param plot_bivariate Should the bivariate plot appear?
3d69ff21
BA
120#'
121#' @export
98e958ca 122plotFbox <- 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)
3d69ff21
BA
135 rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Temps (heures)", ylab="PM10",
136 plotlegend=FALSE, lwd=2)
98e958ca
BA
137 rainbow::fboxplot(series_fds, "bivariate", "hdr", plotlegend=FALSE)
138}
139
140#' Compute filaments
141#'
142#' Get similar days in the past, as black as distances are small
143#'
144#' @param data Object as returned by \code{getData}
8f84543c
BA
145#' @param pred Object of class Forecast
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
8f84543c 158computeFilaments <- function(data, pred, index, limit=60, plot=TRUE)
98e958ca 159{
8f84543c 160 ref_serie = data$getCenteredSerie( pred$getIndexInData(index) )
98e958ca
BA
161 if (any(is.na(ref_serie)))
162 stop("computeFilaments requires a serie without NAs")
163
98e958ca 164 # Compute colors for each neighbor (from darkest to lightest)
8f84543c
BA
165 sorted_dists = sort(-log(pred$getParams(index)$weights), index.return=TRUE)
166 nn = min(limit, length(sorted_dists$x))
167 min_dist = min(sorted_dists$x[1:nn])
168 max_dist = max(sorted_dists$x[1:nn])
169 color_values = floor(19.5*(sorted_dists$x[1:nn]-min_dist)/(max_dist-min_dist)) + 1
98e958ca
BA
170 colors = gray.colors(20,0.1,0.9)[color_values] #TODO: 20 == magic number
171
172 if (plot)
173 {
174 # Complete series with (past and present) tomorrows
8f84543c
BA
175 ref_serie = c(ref_serie, data$getCenteredSerie( pred$getIndexInData(index)+1 ))
176 centered_series = rbind(
177 data$getCenteredSeries( pred$getParams(index)$indices ),
178 data$getCenteredSeries( pred$getParams(index)$indices+1 ) )
179 yrange = range( ref_serie, quantile(centered_series, probs=c(0.025,0.975), na.rm=TRUE) )
98e958ca
BA
180 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=2)
181 for (i in nn:1)
182 {
8f84543c
BA
183 plot(centered_series[,sorted_dists$ix[i]], ylim=yrange, type="l", col=colors[i],
184 xlab=ifelse(i==1,"Temps (en heures)",""), ylab=ifelse(i==1,"PM10 centré",""))
98e958ca
BA
185 par(new=TRUE)
186 }
187 # Also plot ref curve, in red
188 plot(ref_serie, ylim=yrange, type="l", col="#FF0000", xlab="", ylab="")
6d50a76f 189 abline(v=24, lty=2, col=colors()[56], lwd=1)
98e958ca
BA
190 }
191
8f84543c
BA
192 list(
193 "index"=pred$getIndexInData(index),
194 "neighb_indices"=pred$getParams(index)$indices[sorted_dists$ix[1:nn]],
195 "colors"=colors)
fa8078f9
BA
196}
197
af3b84f4 198#' Functional boxplot on filaments
fa8078f9 199#'
af3b84f4 200#' Draw the functional boxplot on filaments obtained by \code{computeFilaments}
fa8078f9
BA
201#'
202#' @param data Object return by \code{getData}
98e958ca 203#' @param fil Output of \code{computeFilaments}
fa8078f9
BA
204#'
205#' @export
98e958ca 206plotFilamentsBox = function(data, fil, ...)
fa8078f9 207{
98e958ca
BA
208 if (!requireNamespace("rainbow", quietly=TRUE))
209 stop("Functional boxplot requires the rainbow package")
210
211 series_matrix = rbind(
212 data$getSeries(fil$neighb_indices), data$getSeries(fil$neighb_indices+1) )
213 series_fds = rainbow::fds(seq_len(nrow(series_matrix)), series_matrix)
214 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
215 rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Temps (heures)", ylab="PM10",
216 plotlegend=FALSE, lwd=2)
217
fa8078f9
BA
218 # "Magic" found at http://stackoverflow.com/questions/13842560/get-xlim-from-a-plot-in-r
219 usr <- par("usr")
220 yr <- (usr[4] - usr[3]) / 27
98e958ca 221 par(new=TRUE)
a5a3a294 222 plot(c(data$getSerie(fil$index),data$getSerie(fil$index+1)), type="l", lwd=2, lty=2,
fa8078f9 223 ylim=c(usr[3] + yr, usr[4] - yr), xlab="", ylab="")
d5fe1f30 224 abline(v=24, lty=2, col=colors()[56])
3d69ff21 225}
16b1c049 226
af3b84f4 227#' Plot relative conditional variability / absolute variability
16b1c049 228#'
af3b84f4
BA
229#' Draw the relative conditional variability / absolute variability based on filaments
230#' obtained by \code{computeFilaments}
16b1c049
BA
231#'
232#' @param data Object return by \code{getData}
98e958ca 233#' @param fil Output of \code{computeFilaments}
16b1c049
BA
234#'
235#' @export
98e958ca 236plotRelVar = function(data, fil, ...)
16b1c049 237{
98e958ca
BA
238 ref_var = c( apply(data$getSeries(fil$neighb_indices),1,sd),
239 apply(data$getSeries(fil$neighb_indices+1),1,sd) )
240 fdays = getNoNA2(data, 1, fil$index-1)
241 global_var = c( apply(data$getSeries(fdays),1,sd), apply(data$getSeries(fdays+1),1,sd) )
16b1c049 242
af3b84f4 243 yrange = range(ref_var, global_var)
16b1c049 244 par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
af3b84f4
BA
245 plot(ref_var, type="l", col=1, lwd=3, ylim=yrange,
246 xlab="Temps (heures)", ylab="Écart-type")
16b1c049 247 par(new=TRUE)
98e958ca 248 plot(global_var, type="l", col=2, lwd=3, ylim=yrange, xlab="", ylab="")
16b1c049
BA
249 abline(v=24, lty=2, col=colors()[56])
250}