'update'
[talweg.git] / pkg / R / plot.R
index 5cb55b0..0f895bd 100644 (file)
-#' @title plot curves
+#' Plot curves
 #'
-#' @description Plot a range of curves in data
+#' Plot a range of curves in data.
 #'
-#' @param data Object of class Data
+#' @inheritParams computeError
 #' @param indices Range of indices (integers or dates)
 #'
 #' @export
 plotCurves <- function(data, indices=seq_len(data$getSize()))
 {
-       yrange = quantile( sapply( indices, function(i) {
-               serie = c(data$getCenteredSerie(i))
-               if (!all(is.na(serie)))
-                       range(serie, na.rm=TRUE)
-               c()
-       }), probs=c(0.05,0.95) )
+       series = data$getSeries(indices)
+       yrange = quantile(series, probs=c(0.025,0.975), na.rm=TRUE)
        par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
-       for (i in seq_along(indices))
-       {
-               plot(data$getSerie(indices[i]), type="l", ylim=yrange,
-                       xlab=ifelse(i==1,"Temps (en heures)",""), ylab=ifelse(i==1,"PM10",""))
-               if (i < length(indices))
-                       par(new=TRUE)
-       }
+       matplot(series, type="l", ylim=yrange, xlab="Time (hours)", ylab="PM10")
 }
 
-#' @title plot measured / predicted
+#' Plot error
 #'
-#' @description Plot measured curve (in black) and predicted curve (in red)
+#' Draw error graphs, potentially from several runs of \code{computeForecast()}.
 #'
-#' @param data Object return by \code{getData}
-#' @param pred Object as returned by \code{computeForecast}
-#' @param index Index in forecasts
+#' @param err Error as returned by \code{computeError()}
+#' @param cols Colors for each error (default: 1,2,3,...)
+#' @param agg Aggregation level ("day", "week" or "month")
+#'
+#' @seealso \code{\link{plotCurves}}, \code{\link{plotPredReal}},
+#'   \code{\link{plotSimils}}, \code{\link{plotFbox}}, \code{\link{computeFilaments}},
+#'   \code{\link{plotFilamentsBox}}, \code{\link{plotRelVar}}
 #'
 #' @export
-plotPredReal <- function(data, pred, index)
+plotError <- function(err, cols=seq_along(err), agg="day")
 {
-       horizon = length(pred$getSerie(1))
-       measure = data$getSerie(pred$getIndexInData(index)+1)[1:horizon]
-       yrange = range( pred$getSerie(index), measure )
-       par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=3)
-       plot(measure, type="l", ylim=yrange, xlab="Temps (en heures)", ylab="PM10")
-       par(new=TRUE)
-       plot(pred$getSerie(index), type="l", col="#0000FF", ylim=yrange, xlab="", ylab="")
+       if (!is.null(err$abs))
+               err = list(err)
+       par(mfrow=c(2,2), mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
+       L = length(err)
+
+       yrange = range( sapply(1:L, function(i) err[[i]]$abs$day), na.rm=TRUE )
+       matplot(sapply( seq_len(L), function(i) err[[i]]$abs$day ), type="l",
+               xlab="Time (hours)", ylab="Mean |y - y_hat|", ylim=yrange, col=cols, lwd=2, lty=1)
+
+       agg_curves <- sapply( seq_len(L), function(i) {
+               curve <- err[[i]]$abs$indices
+               delta <- if (agg=="day") 1 else if (agg=="week") 7 else if (agg=="month") 30
+               vapply( seq(1,length(curve),delta), function(i) {
+                       mean(curve[i:(i+delta-1)], na.rm=TRUE)
+               }, vector("double",1), USE.NAMES=FALSE )
+       })
+       yrange = range(agg_curves, na.rm=TRUE)
+       matplot(agg_curves, type="l", xlab=paste("Time (",agg,"s)", sep=""),
+               ylab="Mean |y - y_hat|", ylim=yrange, col=cols, lwd=2, lty=1)
+
+       yrange = range( sapply(1:L, function(i) err[[i]]$MAPE$day), na.rm=TRUE )
+       matplot(sapply( seq_len(L), function(i) err[[i]]$MAPE$day ), type="l",
+               xlab="Time (hours)", ylab="Mean MAPE", ylim=yrange, col=cols, lwd=2, lty=1)
+
+       agg_curves <- sapply( seq_len(L), function(i) {
+               curve <- err[[i]]$MAPE$indices
+               delta <- if (agg=="day") 1 else if (agg=="week") 7 else if (agg=="month") 30
+               vapply( seq(1,length(curve),delta), function(i) {
+                       mean(curve[i:(i+delta-1)], na.rm=TRUE)
+               }, vector("double",1), USE.NAMES=FALSE )
+       })
+       yrange = range(agg_curves, na.rm=TRUE)
+       matplot(agg_curves, type="l", xlab=paste("Time (",agg,"s)", sep=""),
+               ylab="Mean MAPE", ylim=yrange, col=cols, lwd=2, lty=1)
 }
 
-#' @title Compute filaments
+#' Plot measured / predicted
 #'
-#' @description Get similar days in the past + "past tomorrow", as black as distances are small
+#' Plot measured curve (in black) and predicted curve (in blue).
 #'
-#' @param data Object as returned by \code{getData}
-#' @param index Index in data
-#' @param limit Number of neighbors to consider
-#' @param plot Should the result be plotted?
+#' @inheritParams computeError
+#' @param index Index in forecasts (integer or date)
 #'
 #' @export
-computeFilaments <- function(data, index, limit=60, plot=TRUE)
+plotPredReal <- function(data, pred, index)
 {
-       index = dateIndexToInteger(index, data)
-       ref_serie = data$getCenteredSerie(index)
-       if (any(is.na(ref_serie)))
-               stop("computeFilaments requires a serie without NAs")
-       L = length(ref_serie)
+       prediction = pred$getForecast(index)
+       measure = data$getSerie( pred$getIndexInData(index) )[1:length(pred$getForecast(1))]
 
-       # Determine indices of no-NAs days followed by no-NAs tomorrows
-       first_day = ifelse(length(data$getCenteredSerie(1))<L, 2, 1)
-       fdays_indices = c()
-       for (i in first_day:(index-1))
-       {
-               if ( !any(is.na(data$getSerie(i)) | is.na(data$getSerie(i+1))) )
-                       fdays_indices = c(fdays_indices, i)
-       }
+       # Remove the common part, where prediction == measure
+       dot_mark <- ifelse(prediction[1]==measure[1],
+               which.max(seq_along(prediction)[prediction==measure]), 0)
+       prediction = prediction[(dot_mark+1):length(prediction)]
+       measure = measure[(dot_mark+1):length(measure)]
 
-       distances = sapply(fdays_indices, function(i) {
-               sqrt( sum( (ref_serie - data$getCenteredSerie(i))^2 ) / L )
-       })
-       indices = sort(distances, index.return=TRUE)$ix[1:min(limit,length(distances))]
-       yrange = quantile( c(ref_serie, sapply( indices, function(i) {
-               ii = fdays_indices[i]
-               serie = c(data$getCenteredSerie(ii), data$getCenteredSerie(ii+1))
-               if (!all(is.na(serie)))
-                       return (range(serie, na.rm=TRUE))
-               c()
-       }) ), probs=c(0.05,0.95) )
-       grays = gray.colors(20, 0.1, 0.9) #TODO: 20 == magic number
-       min_dist = min(distances[indices])
-       max_dist = max(distances[indices])
-       color_values = floor( 19.5 * (distances[indices]-min_dist) / (max_dist-min_dist) ) + 1
-       plot_order = sort(color_values, index.return=TRUE, decreasing=TRUE)$ix
-       colors = c(grays[ color_values[plot_order] ], "#FF0000")
-       if (plot)
-       {
-               par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=2)
-               for ( i in seq_len(length(indices)+1) )
-               {
-                       ii = ifelse(i<=length(indices), fdays_indices[ indices[plot_order[i]] ], index)
-                       plot(c(data$getCenteredSerie(ii),data$getCenteredSerie(ii+1)),
-                               ylim=yrange, type="l", col=colors[i],
-                               xlab=ifelse(i==1,"Temps (en heures)",""), ylab=ifelse(i==1,"PM10 centrĂ©",""))
-                       if (i <= length(indices))
-                               par(new=TRUE)
-               }
-               abline(v=24, lty=2, col=colors()[56])
-       }
-       list("indices"=c(fdays_indices[ indices[plot_order] ],index), "colors"=colors)
+       yrange = range(measure, prediction)
+       par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=3)
+       plot(measure, type="l", ylim=yrange, xlab="Time (hours)", ylab="PM10")
+       par(new=TRUE)
+       plot(prediction, type="l", col="#0000FF", ylim=yrange, xlab="", ylab="")
 }
 
-#' @title Plot similarities
+#' Plot similarities
 #'
-#' @description Plot histogram of similarities (weights)
+#' Plot histogram of similarities (weights), for 'Neighbors' method.
 #'
-#' @param pred Object as returned by \code{computeForecast}
-#' @param index Index in forecasts (not in data)
+#' @inheritParams computeError
+#' @param index Index in forecasts (integer or date)
 #'
 #' @export
 plotSimils <- function(pred, index)
@@ -118,168 +104,160 @@ plotSimils <- function(pred, index)
        weights = pred$getParams(index)$weights
        if (is.null(weights))
                stop("plotSimils only works on 'Neighbors' forecasts")
+       par(mfrow=c(1,2), mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
+       small_weights = weights[ weights < 1/length(weights) ]
+       large_weights = weights[ weights >= 1/length(weights) ]
+       hist(small_weights, nclass=25, main="", xlab="Weight < 1/N", ylab="Count")
+       hist(large_weights, nclass=25, main="", xlab="Weight >= 1/N", ylab="Count")
+}
+
+#' Functional boxplot
+#'
+#' Draw the functional boxplot on the left, and bivariate plot on the right.
+#'
+#' @inheritParams computeError
+#' @inheritParams plotCurves
+#'
+#' @export
+plotFbox <- function(data, indices=seq_len(data$getSize()))
+{
+       if (!requireNamespace("rainbow", quietly=TRUE))
+               stop("Functional boxplot requires the rainbow package")
+
+       series_matrix = data$getSeries(indices)
+       # Remove series with NAs
+       no_NAs_indices = sapply( 1:ncol(series_matrix),
+               function(i) all(!is.na(series_matrix[,i])) )
+       series_matrix = series_matrix[,no_NAs_indices]
+
+       series_fds = rainbow::fds(seq_len(nrow(series_matrix)), series_matrix)
        par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
-       hist(pred$getParams(index)$weights, nclass=20, xlab="Poids", ylab="Effectif")
+       rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Time (hours)", ylab="PM10",
+               plotlegend=FALSE, lwd=2)
+       rainbow::fboxplot(series_fds, "bivariate", "hdr", plotlegend=FALSE)
 }
 
-#' @title Plot error
+#' Compute filaments
 #'
-#' @description Draw error graphs, potentially from several runs of \code{computeForecast}
+#' Obtain similar days in the past, and (optionally) plot them -- as black as distances
+#' are small.
 #'
-#' @param err Error as returned by \code{computeError}
-#' @param cols Colors for each error (default: 1,2,3,...)
+#' @inheritParams computeError
+#' @param index Index in forecast (integer or date)
+#' @param limit Number of neighbors to consider
+#' @param plot Should the result be plotted?
 #'
-#' @seealso \code{\link{plotPredReal}}, \code{\link{plotFilaments}}, \code{\link{plotSimils}}
-#'   \code{\link{plotFbox}}
+#' @return A list with
+#' \itemize{
+#'   \item index : index of the current serie ('today')
+#'   \item neighb_indices : indices of its neighbors
+#'   \item colors : colors of neighbors curves (shades of gray)
+#' }
 #'
 #' @export
-plotError <- function(err, cols=seq_along(err))
+computeFilaments <- function(data, pred, index, limit=60, plot=TRUE)
 {
-       if (!is.null(err$abs))
-               err = list(err)
-       par(mfrow=c(2,2), mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=2)
-       L = length(err)
-       yrange = range( sapply(1:L, function(index) ( err[[index]]$abs$day ) ), na.rm=TRUE )
-       for (i in seq_len(L))
-       {
-               plot(err[[i]]$abs$day, type="l", xlab=ifelse(i==1,"Temps (heures)",""),
-                       ylab=ifelse(i==1,"Moyenne |y - y_hat|",""), ylim=yrange, col=cols[i])
-               if (i < L)
-                       par(new=TRUE)
-       }
-       yrange = range( sapply(1:L, function(index) ( err[[index]]$abs$indices ) ), na.rm=TRUE )
-       for (i in seq_len(L))
-       {
-               plot(err[[i]]$abs$indices, type="l", xlab=ifelse(i==1,"Temps (jours)",""),
-                       ylab=ifelse(i==1,"Moyenne |y - y_hat|",""), ylim=yrange, col=cols[i])
-               if (i < L)
-                       par(new=TRUE)
-       }
-       yrange = range( sapply(1:L, function(index) ( err[[index]]$MAPE$day ) ), na.rm=TRUE )
-       for (i in seq_len(L))
+       weights <- pred$getParams(index)$weights
+       if (is.null(weights) || is.na(pred$getParams(index)$weights[1]))
+               stop("computeFilaments requires a serie without NAs")
+
+       nn <- min(limit, length(weights))
+       sorted_dists = sort(-log(weights), index.return=TRUE)
+       # Compute colors for each neighbor (from darkest to lightest), if weights differ
+       if ( any( weights != weights[1] ) )
        {
-               plot(err[[i]]$MAPE$day, type="l", xlab=ifelse(i==1,"Temps (heures)",""),
-                       ylab=ifelse(i==1,"MAPE moyen",""), ylim=yrange, col=cols[i])
-               if (i < L)
-                       par(new=TRUE)
+               min_dist = min(sorted_dists$x[1:nn])
+               max_dist = max(sorted_dists$x[1:nn])
+               color_values = floor(19.5*(sorted_dists$x[1:nn]-min_dist)/(max_dist-min_dist)) + 1
+               colors = gray.colors(20,0.1,0.9)[color_values] #TODO: 20 == magic number
        }
-       yrange = range( sapply(1:L, function(index) ( err[[index]]$MAPE$indices ) ), na.rm=TRUE )
-       for (i in seq_len(L))
+       else
+               colors <- rep(colors()[17], length(weights))
+
+       if (plot)
        {
-               plot(err[[i]]$MAPE$indices, type="l", xlab=ifelse(i==1,"Temps (jours)",""),
-                       ylab=ifelse(i==1,"MAPE moyen",""), ylim=yrange, col=cols[i])
-               if (i < L)
+               # Complete series with (past and present) tomorrows
+               ref_serie = c( data$getCenteredSerie( pred$getIndexInData(index)-1 ),
+                       data$getCenteredSerie( pred$getIndexInData(index) ) )
+               centered_series = rbind(
+                       data$getCenteredSeries( pred$getParams(index)$indices-1 ),
+                       data$getCenteredSeries( pred$getParams(index)$indices ) )
+               yrange = range( ref_serie,
+                       quantile(centered_series, probs=c(0.025,0.975), na.rm=TRUE) )
+               par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5, lwd=2)
+               for (i in nn:1)
+               {
+                       plot(centered_series[,sorted_dists$ix[i]], ylim=yrange, type="l", col=colors[i],
+                               xlab=ifelse(i==1,"Time (hours)",""), ylab=ifelse(i==1,"Centered PM10",""))
                        par(new=TRUE)
+               }
+               # Also plot ref curve, in red
+               plot(ref_serie, ylim=yrange, type="l", col="#FF0000", xlab="", ylab="")
+               dot_mark <- 0.5 + which.max( pred$getForecast(1) ==
+                       data$getSerie( pred$getIndexInData(1) )[1:length(pred$getForecast(1))] )
+               abline(v=24+dot_mark, lty=2, col=colors()[56], lwd=1)
        }
+
+       list(
+               "index"=pred$getIndexInData(index),
+               "neighb_indices"=pred$getParams(index)$indices[sorted_dists$ix[1:nn]],
+               "colors"=colors)
 }
 
-#' @title Functional boxplot
+#' Functional boxplot on filaments
 #'
-#' @description Draw the functional boxplot on the left, and bivariate plot on the right
+#' Draw the functional boxplot on filaments obtained by \code{computeFilaments()}.
 #'
-#' @param data Object return by \code{getData}
-#' @param fiter Optional filter: return TRUE on indices to process
-#' @param plot_bivariate Should the bivariate plot appear?
+#' @inheritParams computeError
+#' @param fil Output of \code{computeFilaments}
+#' @param predict_from First predicted time step
 #'
 #' @export
-plotFbox <- function(data, filter=function(index) TRUE, plot_bivariate=TRUE)
+plotFilamentsBox = function(data, fil, predict_from)
 {
        if (!requireNamespace("rainbow", quietly=TRUE))
                stop("Functional boxplot requires the rainbow package")
 
-       start_index = 1
-       end_index = data$getSize()
-       if (length(data$getCenteredSerie(1)) < length(data$getCenteredSerie(2)))
-       {
-               # Shifted start (7am, or 1pm, or...)
-               start_index = 2
-               end_index = data$getSize() - 1
-       }
-
-       L = length(data$getCenteredSerie(2))
-       series_matrix = sapply(start_index:end_index, function(index) {
-               if (filter(index))
-                       as.matrix(data$getSerie(index))
-               else
-                       rep(NA,L)
-       })
-       # TODO: merge with previous step: only one pass should be required
-       no_NAs_indices = sapply( 1:ncol(series_matrix), function(i) all(!is.na(series_matrix[,i])) )
-       series_matrix = series_matrix[,no_NAs_indices]
-
+       series_matrix = rbind(
+               data$getSeries(fil$neighb_indices-1), data$getSeries(fil$neighb_indices) )
        series_fds = rainbow::fds(seq_len(nrow(series_matrix)), series_matrix)
-       if (plot_bivariate)
-               par(mfrow=c(1,2))
+
        par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
-       rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Temps (heures)", ylab="PM10",
+       rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Time (hours)", ylab="PM10",
                plotlegend=FALSE, lwd=2)
-       if (plot_bivariate)
-               rainbow::fboxplot(series_fds, "bivariate", "hdr", plotlegend=FALSE)
-}
 
-#' @title Functional boxplot on filaments
-#'
-#' @description Draw the functional boxplot on filaments obtained by \code{computeFilaments}
-#'
-#' @param data Object return by \code{getData}
-#' @param indices Indices as output by \code{computeFilaments}
-#'
-#' @export
-plotFilamentsBox = function(data, indices, ...)
-{
-       past_neighbs_indices = head(indices,-1)
-       plotFbox(data, function(i) i %in% past_neighbs_indices, plot_bivariate=FALSE)
-       par(new=TRUE)
-       # "Magic" found at http://stackoverflow.com/questions/13842560/get-xlim-from-a-plot-in-r
+       # "Magic": http://stackoverflow.com/questions/13842560/get-xlim-from-a-plot-in-r
        usr <- par("usr")
        yr <- (usr[4] - usr[3]) / 27
-       plot(data$getSerie(tail(indices,1)), type="l", lwd=2, lty=2,
+       par(new=TRUE)
+       plot(c(data$getSerie(fil$index-1),data$getSerie(fil$index)), type="l", lwd=2, lty=2,
                ylim=c(usr[3] + yr, usr[4] - yr), xlab="", ylab="")
+       abline(v=24+predict_from-0.5, lty=2, col=colors()[56])
 }
 
-#' @title Plot relative conditional variability / absolute variability
+#' Plot relative conditional variability / absolute variability
 #'
-#' @description Draw the relative conditional variability / absolute variability based on on
-#'   filaments obtained by \code{computeFilaments}
+#' Draw the relative conditional variability / absolute variability based on filaments
+#' obtained by \code{computeFilaments()}.
 #'
-#' @param data Object return by \code{getData}
-#' @param indices Indices as output by \code{computeFilaments}
+#' @inheritParams computeError
+#' @inheritParams plotFilamentsBox
 #'
 #' @export
-plotRelativeVariability = function(data, indices, ...)
+plotRelVar = function(data, fil, predict_from)
 {
-       #plot left / right separated by vertical line brown dotted
-       #median of 3 runs for random length(indices) series
-       ref_series = t( sapply(indices, function(i) {
-               c( data$getSerie(i), data$getSerie(i+1) )
-       }) )
-       ref_var = apply(ref_series, 2, sd)
-
-       # Determine indices of no-NAs days followed by no-NAs tomorrows
-       first_day = ifelse(length(data$getCenteredSerie(1))<length(ref_series[1,]), 2, 1)
-       fdays_indices = c()
-       for (i in first_day:(tail(indices,1)-1))
-       {
-               if ( !any(is.na(data$getSerie(i)) | is.na(data$getSerie(i+1))) )
-                       fdays_indices = c(fdays_indices, i)
-       }
-
-       # TODO: 3 == magic number
-       random_var = matrix(nrow=3, ncol=48)
-       for (mc in seq_len(nrow(random_var)))
-       {
-               random_indices = sample(fdays_indices, length(indices))
-               random_series = t( sapply(random_indices, function(i) {
-                       c( data$getSerie(i), data$getSerie(i+1) )
-               }) )
-               random_var[mc,] = apply(random_series, 2, sd)
-       }
-       random_var = apply(random_var, 2, median)
+       ref_var = c( apply(data$getSeries(fil$neighb_indices-1),1,sd),
+               apply(data$getSeries(fil$neighb_indices),1,sd) )
+       tdays = .getNoNA2(data, 2, fil$index)
+       global_var = c(
+               apply(data$getSeries(tdays-1),1,sd),
+               apply(data$getSeries(tdays),1,sd) )
 
-       yrange = range(ref_var, random_var)
+       yrange = range(ref_var, global_var)
        par(mar=c(4.7,5,1,1), cex.axis=1.5, cex.lab=1.5)
-       plot(ref_var, type="l", col=1, lwd=3, ylim=yrange, xlab="Temps (heures)", ylab="Écart-type")
+       plot(ref_var, type="l", col=1, lwd=3, ylim=yrange,
+               xlab="Time (hours)", ylab="Standard deviation")
        par(new=TRUE)
-       plot(random_var, type="l", col=2, lwd=3, ylim=yrange, xlab="", ylab="")
-       abline(v=24, lty=2, col=colors()[56])
+       plot(global_var, type="l", col=2, lwd=3, ylim=yrange, xlab="", ylab="")
+       abline(v=24+predict_from-0.5, lty=2, col=colors()[56])
 }