#' 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?
#'
#' }
#'
#' @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
plot(ref_serie, ylim=yrange, type="l", col="#FF0000", xlab="", ylab="")
- abline(v=24, lty=2, col=colors()[56])
+ 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