|
1 | | -#' Plot multivariate time series forecast |
| 1 | +#' Plot multivariate time series forecast or residuals |
2 | 2 | #' |
3 | | -#' @param x result from varf or ridge2f (multivariate time series forecast) |
| 3 | +#' @param x result from \code{basicf}, \code{ridge2f} or \code{varf} (multivariate time series forecast) |
4 | 4 | #' @param selected_series name of the time series selected for plotting |
5 | | -#' @param type_graph "pi": basic prediction intervals; "dist": a distribution of predictions; "sims": the simulations |
| 5 | +#' @param type_graph "pi": basic prediction intervals; |
| 6 | +#' "dist": a distribution of predictions; "sims": the simulations |
6 | 7 | #' @param level confidence levels for prediction intervals |
7 | 8 | #' @param ... additional parameters to be passed to \code{plot} or \code{matplot} |
8 | 9 | #' |
|
41 | 42 | #' |
42 | 43 | #' |
43 | 44 | plot.mtsforecast <- function(x, selected_series, |
44 | | - type_graph = c("pi", "dist", "sims"), |
45 | | - level = 95, |
46 | | - ...) |
| 45 | + type_graph = c("pi", |
| 46 | + "dist", |
| 47 | + "sims"), |
| 48 | + level = 95, ...) |
47 | 49 | { |
48 | 50 | type_graph <- match.arg(type_graph) |
49 | 51 |
|
@@ -73,6 +75,8 @@ plot.mtsforecast <- function(x, selected_series, |
73 | 75 | lines(y_upper, col="gray60") |
74 | 76 | lines(y_lower, col="gray60") |
75 | 77 | lines(y_mean) |
| 78 | + |
| 79 | + return() |
76 | 80 | } |
77 | 81 |
|
78 | 82 | if (type_graph == "dist") |
@@ -107,6 +111,8 @@ plot.mtsforecast <- function(x, selected_series, |
107 | 111 | bands_add(abs, y_mean, col = color[3], ci_upper = x_summary[2, ], |
108 | 112 | ci_lower = x_summary[4, ]) |
109 | 113 |
|
| 114 | + return() |
| 115 | + |
110 | 116 | } |
111 | 117 |
|
112 | 118 | if (type_graph == "sims") |
@@ -149,15 +155,93 @@ plot.mtsforecast <- function(x, selected_series, |
149 | 155 | ... |
150 | 156 | ) |
151 | 157 | lines(x = time_inputs, y = input_series, lwd = 2) |
152 | | - } |
153 | | - |
154 | 158 |
|
| 159 | + return() |
| 160 | + } |
155 | 161 | } else { |
156 | | - stop("Method not implemented for this type of object") |
| 162 | + stop("Method not implemented for this type of object") |
157 | 163 | } |
158 | 164 | } |
159 | 165 |
|
160 | 166 |
|
| 167 | +#' Plot bivariate distributions |
| 168 | +#' |
| 169 | +#' @param x bivariate matrix or time series |
| 170 | +#' @param y additional series (default is \code{NULL}) |
| 171 | +#' @export |
| 172 | +#' |
| 173 | +#' @examples |
| 174 | +#' |
| 175 | +#' obj <- ahead::ridge2f(fpp::insurance, h = 10, type_pi = "blockbootstrap", |
| 176 | +#' block_length=5, B = 10) |
| 177 | +#' |
| 178 | +#' \dontrun{ |
| 179 | +#' plot(obj$residuals, type_graph = "pairs") |
| 180 | +#' } |
| 181 | +#' |
| 182 | +#' |
| 183 | +plot.pairs <- function(x, y = NULL) |
| 184 | +{ |
| 185 | + x <- matrix(unlist(x), ncol = 2) |
| 186 | + if (!is.null(y)) |
| 187 | + { |
| 188 | + y <- matrix(unlist(y), ncol = 2) |
| 189 | + if (length(x) != length(y)) stop("'x' and 'y' must have the same dimensions") |
| 190 | + xvar <- c(x[,1], y[,1]) |
| 191 | + yvar <- c(x[,2], y[,2]) |
| 192 | + nb <- dim(x)[1] |
| 193 | + zvar <- as.factor(c(rep("x", nb), rep("y", nb))) |
| 194 | + xy <- data.frame(xvar, yvar, zvar) |
| 195 | + } |
| 196 | + else |
| 197 | + { |
| 198 | + xvar <- x[,1] |
| 199 | + yvar <- x[,2] |
| 200 | + nb <- dim(x)[1] |
| 201 | + zvar <- as.factor(rep("x", nb)) |
| 202 | + xy <- data.frame(xvar, yvar, zvar) |
| 203 | + } |
| 204 | + |
| 205 | + #placeholder plot - prints nothing at all |
| 206 | + empty <- ggplot()+ggplot2::geom_point(ggplot2::aes(1,1), colour="white") + |
| 207 | + ggplot2::theme( |
| 208 | + plot.background = ggplot2::element_blank(), |
| 209 | + panel.grid.major = ggplot2::element_blank(), |
| 210 | + panel.grid.minor = ggplot2::element_blank(), |
| 211 | + panel.border = ggplot2::element_blank(), |
| 212 | + panel.background = ggplot2::element_blank(), |
| 213 | + axis.title.x = ggplot2::element_blank(), |
| 214 | + axis.title.y = ggplot2::element_blank(), |
| 215 | + axis.text.x = ggplot2::element_blank(), |
| 216 | + axis.text.y = ggplot2::element_blank(), |
| 217 | + axis.ticks = ggplot2::element_blank() |
| 218 | + ) |
| 219 | + |
| 220 | + # plot slow af |
| 221 | + |
| 222 | + #scatterplot of x and y variables |
| 223 | + scatter <- ggplot(xy, ggplot2::aes(xvar, yvar)) + |
| 224 | + ggplot2::geom_point(ggplot2::aes(color=zvar)) + |
| 225 | + ggplot2::scale_color_manual(values = c("blue", "red")) + |
| 226 | + ggplot2::theme(legend.position=c(1,1),legend.justification=c(1,1)) |
| 227 | + |
| 228 | + #marginal density of x - plot on top |
| 229 | + plot_top <- ggplot(xy, ggplot2::aes(xvar, fill=zvar)) + |
| 230 | + ggplot2::geom_density(alpha=.5) + |
| 231 | + ggplot2::scale_fill_manual(values = c("blue", "red")) + |
| 232 | + ggplot2::theme(legend.position = "none") |
| 233 | + |
| 234 | + #marginal density of y - plot on the right |
| 235 | + plot_right <- ggplot(xy, ggplot2::aes(yvar, fill=zvar)) + |
| 236 | + ggplot2::geom_density(alpha=.5) + |
| 237 | + ggplot2::coord_flip() + |
| 238 | + ggplot2::scale_fill_manual(values = c("blue", "red")) + |
| 239 | + ggplot2::theme(legend.position = "none") |
| 240 | + |
| 241 | + #arrange the plots together, with appropriate height and width for each row and column |
| 242 | + grid.arrange(plot_top, empty, scatter, plot_right, ncol=2, nrow=2, widths=c(4, 1), heights=c(1, 4)) |
| 243 | +} |
| 244 | + |
161 | 245 | # plot bands # work in progress #scratchinghead |
162 | 246 | bands_plot <- function(x, y_mean, ci_upper, ci_lower, col, y.goal = NULL, goal.col = "blue", ...) |
163 | 247 | { |
|
0 commit comments