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