X-Git-Url: https://git.auder.net/?p=talweg.git;a=blobdiff_plain;f=pkg%2FR%2Fplot.R;h=eb0c81afe7c49a2772c35404ef7108e87dadde91;hp=372abf0a99e086cceb9fec1dc5f9da9c5907da90;hb=8f84543c2d469933d0fc122543d3eeb824970011;hpb=360a5d41bee019d4c0e39f0f9b26be4f35b243ad diff --git a/pkg/R/plot.R b/pkg/R/plot.R index 372abf0..eb0c81a 100644 --- a/pkg/R/plot.R +++ b/pkg/R/plot.R @@ -142,7 +142,8 @@ plotFbox <- function(data, indices=seq_len(data$getSize())) #' Get similar days in the past, as black as distances are small #' #' @param data Object as returned by \code{getData} -#' @param index Index in data (integer or date) +#' @param pred Object of class Forecast +#' @param index Index in forecast (integer or date) #' @param limit Number of neighbors to consider #' @param plot Should the result be plotted? #' @@ -154,40 +155,33 @@ plotFbox <- function(data, indices=seq_len(data$getSize())) #' } #' #' @export -computeFilaments <- function(data, index, limit=60, plot=TRUE) +computeFilaments <- function(data, pred, index, limit=60, plot=TRUE) { - ref_serie = data$getCenteredSerie(index) + ref_serie = data$getCenteredSerie( pred$getIndexInData(index) ) if (any(is.na(ref_serie))) stop("computeFilaments requires a serie without NAs") - # Determine indices of no-NAs days followed by no-NAs tomorrows - fdays = getNoNA2(data, 1, dateIndexToInteger(index,data)-1) - # Series + tomorrows in columns, ref_serie first - centered_series = data$getCenteredSeries(fdays) - - # Obtain neighbors (closest for euclidian norm) - L = length(ref_serie) - distances = sqrt( colSums( (centered_series - ref_serie)^2 / L ) ) - sorted_distances = sort(distances, index.return=TRUE) - # Compute colors for each neighbor (from darkest to lightest) - nn = min(limit, length(distances)) - min_dist = min(sorted_distances$x[1:nn]) - max_dist = max(sorted_distances$x[1:nn]) - color_values = floor( 19.5 * (sorted_distances$x[1:nn]-min_dist) / (max_dist-min_dist) ) + 1 + sorted_dists = sort(-log(pred$getParams(index)$weights), index.return=TRUE) + nn = min(limit, length(sorted_dists$x)) + 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 if (plot) { # Complete series with (past and present) tomorrows - ref_serie = c(ref_serie,data$getCenteredSerie(index+1)) - centered_series = rbind( centered_series, data$getCenteredSeries(fdays+1) ) - yrange = quantile(cbind(ref_serie,centered_series), probs=c(0.025,0.975), na.rm=TRUE) + ref_serie = c(ref_serie, data$getCenteredSerie( pred$getIndexInData(index)+1 )) + centered_series = rbind( + data$getCenteredSeries( pred$getParams(index)$indices ), + data$getCenteredSeries( pred$getParams(index)$indices+1 ) ) + 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_distances$ix[i]], ylim=yrange, type="l", col=colors[i], - xlab=ifelse(i==nn,"Temps (en heures)",""), ylab=ifelse(i==nn,"PM10 centré","")) + plot(centered_series[,sorted_dists$ix[i]], ylim=yrange, type="l", col=colors[i], + xlab=ifelse(i==1,"Temps (en heures)",""), ylab=ifelse(i==1,"PM10 centré","")) par(new=TRUE) } # Also plot ref curve, in red @@ -195,7 +189,10 @@ computeFilaments <- function(data, index, limit=60, plot=TRUE) abline(v=24, lty=2, col=colors()[56], lwd=1) } - list("index"=index,"neighb_indices"=fdays[sorted_distances$ix[1:nn]],"colors"=colors) + list( + "index"=pred$getIndexInData(index), + "neighb_indices"=pred$getParams(index)$indices[sorted_dists$ix[1:nn]], + "colors"=colors) } #' Functional boxplot on filaments