X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=pkg%2FR%2Fplot.R;h=2488d81548202fa9af8e8e3e2a1f23f006914694;hb=4f3fdbb8e2ac4bd57a4e27539a58ef0e7ec2304c;hp=f4fc3bfac02a7f703c493aa29c16bab51c2dc6a1;hpb=1e20780ee1505fac6c7ed68d340892c497524561;p=talweg.git diff --git a/pkg/R/plot.R b/pkg/R/plot.R index f4fc3bf..2488d81 100644 --- a/pkg/R/plot.R +++ b/pkg/R/plot.R @@ -1,235 +1,246 @@ -#' @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( range( 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)) + matplot(series, type="l", ylim=yrange, xlab="Time (hours)", ylab="PM10") +} + +#' Plot error +#' +#' Draw error graphs, potentially from several runs of \code{computeForecast()}. +#' +#' @param err Error as returned by \code{computeError()} +#' @param cols Colors for each error (default: 1,2,3,...) +#' +#' @seealso \code{\link{plotCurves}}, \code{\link{plotPredReal}}, +#' \code{\link{plotSimils}}, \code{\link{plotFbox}}, \code{\link{computeFilaments}}, +#' \code{\link{plotFilamentsBox}}, \code{\link{plotRelVar}} +#' +#' @export +plotError <- function(err, cols=seq_along(err)) +{ + 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(i) ( err[[i]]$abs$day ) ), na.rm=TRUE ) + for (i in seq_len(L)) + { + plot(err[[i]]$abs$day, type="l", xlab=ifelse(i==1,"Time (hours)",""), + ylab=ifelse(i==1,"Mean |y - y_hat|",""), ylim=yrange, col=cols[i]) + if (i < L) + par(new=TRUE) + } + yrange = range( sapply(1:L, function(i) ( err[[i]]$abs$indices ) ), na.rm=TRUE ) + for (i in seq_len(L)) + { + plot(err[[i]]$abs$indices, type="l", xlab=ifelse(i==1,"Time (days)",""), + ylab=ifelse(i==1,"Mean |y - y_hat|",""), ylim=yrange, col=cols[i]) + if (i < L) + par(new=TRUE) + } + yrange = range( sapply(1:L, function(i) ( err[[i]]$MAPE$day ) ), na.rm=TRUE ) + for (i in seq_len(L)) { - 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)) + plot(err[[i]]$MAPE$day, type="l", xlab=ifelse(i==1,"Time (hours)",""), + ylab=ifelse(i==1,"Mean MAPE",""), ylim=yrange, col=cols[i]) + if (i < L) + par(new=TRUE) + } + yrange = range( sapply(1:L, function(i) ( err[[i]]$MAPE$indices ) ), na.rm=TRUE ) + for (i in seq_len(L)) + { + plot(err[[i]]$MAPE$indices, type="l", xlab=ifelse(i==1,"Time (days)",""), + ylab=ifelse(i==1,"Mean MAPE",""), ylim=yrange, col=cols[i]) + if (i < L) par(new=TRUE) } } -#' @title plot measured / predicted +#' Plot measured / predicted #' -#' @description Plot measured curve (in black) and predicted curve (in red) +#' Plot measured curve (in black) and predicted curve (in blue). #' -#' @param data Object return by \code{getData} -#' @param pred Object as returned by \code{getForecast} -#' @param index Index in forecasts +#' @inheritParams computeError +#' @param index Index in forecasts (integer or date) #' #' @export plotPredReal <- function(data, pred, index) { - horizon = length(pred$getSerie(1)) - measure = data$getSerie(pred$getIndexInData(index)+1)[1:horizon] - yrange = range( pred$getSerie(index), measure ) + prediction = pred$getForecast(index) + measure = data$getSerie( pred$getIndexInData(index) )[1:length(prediction)] + 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="Temps (en heures)", ylab="PM10") + plot(measure, type="l", ylim=yrange, xlab="Time (hours)", ylab="PM10") par(new=TRUE) - plot(pred$getSerie(index), type="l", col="#0000FF", ylim=yrange, xlab="", ylab="") + plot(prediction, type="l", col="#0000FF", ylim=yrange, xlab="", ylab="") } -#' @title Compute filaments +#' Plot similarities #' -#' @description Get similar days in the past + "past tomorrow", as black as distances are small +#' Plot histogram of similarities (weights), for 'Neighbors' method. #' -#' @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) +plotSimils <- function(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) - first_day = ifelse(length(data$getCenteredSerie(1)