Initial commit
[talweg.git] / R / plot.R
CommitLineData
3d69ff21
BA
1#' @title plot measured / predicted
2#'
3#' @description Plot measured curve (in black) and predicted curve (in red)
4#'
5#' @param data Object return by \code{getData}
6#' @param pred Object as returned by \code{getForecast}
7#' @param index Index in forecasts
8#'
9#' @export
10plotPredReal <- function(data, pred, index)
11{
12 horizon = length(pred$getSerie(1))
13 par(mar=c(4.7,5,1,1), cex.axis=2, cex.lab=2, lwd=2)
14 measure = data$getSerie(pred$getIndexInData(index)+1)[1:horizon]
15 yrange = range( pred$getSerie(index), measure )
16 plot(measure, type="l", ylim=yrange, lwd=3)
17 par(new=TRUE)
18 plot(pred$getSerie(index), type="l", col=2, ylim=yrange, lwd=3)
19}
20
21#' @title Plot filaments
22#'
23#' @description Plot similar days in the past + "past tomorrow", as black as distances are small
24#'
25#' @param data Object as returned by \code{getData}
26#' @param index Index in data
27#' @param limit Number of neighbors to consider
28#'
29#' @export
30plotFilaments <- function(data, index, limit=60)
31{
32 index = dateIndexToInteger(index, data)
33 ref_serie = data$getCenteredSerie(index)
34 if (any(is.na(ref_serie)))
35 stop("plotFilaments requires a serie without NAs")
36 L = length(ref_serie)
37 first_day = ifelse(length(data$getCenteredSerie(1)<L), 2, 1)
38 distances = sapply(first_day:(index-1), function(i) {
39 sqrt( sum( (ref_serie - data$getCenteredSerie(i))^2 ) / L )
40 })
41 # HACK to suppress NA effect while keeping indexation
42 distances[is.na(distances)] = max(distances,na.rm=TRUE) + 1
43 indices = sort(distances, index.return=TRUE)$ix[1:min(limit,index-first_day)]
44 yrange = range( ref_serie, sapply( indices, function(i) {
45 index = i - first_day + 1
46 serie = c(data$getCenteredSerie(index), data$getCenteredSerie(index+1))
47 if (!all(is.na(serie)))
48 return ( range(serie, na.rm=TRUE) )
49 return (0)
50 }) )
51 grays = gray.colors(20, 0.1, 0.9) #TODO: 20 == magic number
52 colors = c(
53 grays[ floor( 20.5 * distances[indices] / (1+max(distances[indices])) ) ], "#FF0000")
54 par(mar=c(4.7,5,1,1), cex.axis=2, cex.lab=2, lwd=2)
55 for (i in seq_len(length(indices)+1))
56 {
57 ind = ifelse(i<=length(indices), indices[i] - first_day + 1, index)
58 plot(c(data$getCenteredSerie(ind),data$getCenteredSerie(ind+1)),
59 ylim=yrange, type="l", col=colors[i],
60 xlab=ifelse(i==1,"Temps (en heures)",""), ylab=ifelse(i==1,"PM10 centré",""))
61 if (i <= length(indices))
62 par(new=TRUE)
63 }
64}
65
66#' @title Plot similarities
67#'
68#' @description Plot histogram of similarities (weights)
69#'
70#' @param pred Object as returned by \code{getForecast}
71#' @param index Index in forecasts (not in data)
72#'
73#' @export
74plotSimils <- function(pred, index)
75{
76 weights = pred$getParams(index)$weights
77 if (is.null(weights))
78 stop("plotSimils only works on 'Neighbors' forecasts")
79 par(mar=c(4.7,5,1,1))
80 hist(pred$getParams(index)$weights, nclass=20, xlab="Weight", ylab="Frequency")
81}
82
83#' @title Plot error
84#'
85#' @description Draw error graphs, potentially from several runs of \code{getForecast}
86#'
87#' @param err Error as returned by \code{getError}
88#'
89#' @seealso \code{\link{plotPredReal}}, \code{\link{plotFilaments}}, \code{\link{plotSimils}}
90#' \code{\link{plotFbox}}
91#'
92#' @export
93plotError <- function(err)
94{
95 par(mfrow=c(2,2), mar=c(4.7,5,1,1), cex.axis=2, cex.lab=2, lwd=2)
96 L = length(err)
97 yrange = range( sapply(1:L, function(index) ( err[[index]]$abs$day ) ), na.rm=TRUE )
98 for (i in seq_len(L))
99 {
100 plot(err[[i]]$abs$day, type="l", xlab=ifelse(i==1,"Temps (heures)",""),
101 ylab=ifelse(i==1,"Moyenne |y - y_hat|",""), ylim=yrange, col=i)
102 if (i < L)
103 par(new=TRUE)
104 }
105 yrange = range( sapply(1:L, function(index) ( err[[index]]$abs$indices ) ), na.rm=TRUE )
106 for (i in seq_len(L))
107 {
108 plot(err[[i]]$abs$indices, type="l", xlab=ifelse(i==1,"Temps (jours)",""),
109 ylab=ifelse(i==1,"Moyenne |y - y_hat|",""), ylim=yrange, col=i)
110 if (i < L)
111 par(new=TRUE)
112 }
113 yrange = range( sapply(1:L, function(index) ( err[[index]]$MAPE$day ) ), na.rm=TRUE )
114 for (i in seq_len(L))
115 {
116 plot(err[[i]]$MAPE$day, type="l", xlab=ifelse(i==1,"Temps (heures)",""),
117 ylab=ifelse(i==1,"MAPE moyen",""), ylim=yrange, col=i)
118 if (i < L)
119 par(new=TRUE)
120 }
121 yrange = range( sapply(1:L, function(index) ( err[[index]]$MAPE$indices ) ), na.rm=TRUE )
122 for (i in seq_len(L))
123 {
124 plot(err[[i]]$MAPE$indices, type="l", xlab=ifelse(i==1,"Temps (jours)",""),
125 ylab=ifelse(i==1,"MAPE moyen",""), ylim=yrange, col=i)
126 if (i < L)
127 par(new=TRUE)
128 }
129}
130
131#' @title Functional boxplot
132#'
133#' @description Draw the functional boxplot on the left, and bivariate plot on the right
134#'
135#' @param data Object return by \code{getData}
136#' @param fiter Optional filter: return TRUE on indices to process
137#'
138#' @export
139plotFbox <- function(data, filter=function(index) (TRUE))
140{
141 if (!requireNamespace("rainbow", quietly=TRUE))
142 stop("Functional boxplot requires the rainbow package")
143
144 start_index = 1
145 end_index = data$getSize()
146 if (length(data$getCenteredSerie(1)) < length(data$getCenteredSerie(2)))
147 {
148 # Shifted start (7am, or 1pm, or...)
149 start_index = 2
150 end_index = data$getSize() - 1
151 }
152
153 series_matrix = sapply(start_index:end_index, function(index) {
154 as.matrix(data$getSerie(index))
155 })
156 # Remove NAs. + filter TODO: merge with previous step: only one pass required...
157 nas_indices = seq_len(ncol(series_matrix))[ sapply( 1:ncol(series_matrix),
158 function(index) ( !filter(index) || any(is.na(series_matrix[,index])) ) ) ]
159 series_matrix = series_matrix[,-nas_indices]
160
161 series_fds = rainbow::fds(seq_len(nrow(series_matrix)), series_matrix)
162 par(mfrow=c(1,2), mar=c(4.7,5,1,1), cex.axis=2, cex.lab=2)
163 rainbow::fboxplot(series_fds, "functional", "hdr", xlab="Temps (heures)", ylab="PM10",
164 plotlegend=FALSE, lwd=2)
165 rainbow::fboxplot(series_fds, "bivariate", "hdr", plotlegend=FALSE)
166}