Skip to content

Commit 9927a06

Browse files
release v0.7.0 -- see NEWS.md
1 parent 4191a1f commit 9927a06

14 files changed

Lines changed: 863 additions & 135 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: ahead
22
Type: Package
33
Title: Time Series Forecasting
44
Version: 0.7.0
5-
Date: 2023-08-20
5+
Date: 2023-08-23
66
Author: T. Moudiki
77
Maintainer: T. Moudiki <thierry.moudiki@gmail.com>
88
Description: Univariate and multivariate time series forecasting.

NAMESPACE

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
useDynLib(ahead, .registration=TRUE)
22
S3method(plot, mtsforecast)
3+
S3method(plot, pairs)
34
S3method(predict, ridge)
45
importFrom(Rcpp, evalCpp)
56
importFrom(VineCopula, C2RVine)
@@ -16,8 +17,8 @@ importFrom(fGarch, garchFit)
1617
importFrom("grDevices", "colorRampPalette")
1718
importFrom("graphics", "abline", "par", "points", "polygon")
1819
importFrom("stats", "t.test")
19-
importFrom("stats", "as.ts", "complete.cases", "end", "frequency",
20-
"median", "predict", "predict.lm", "qnorm", "quantile",
20+
importFrom("stats", "as.ts", "complete.cases", "ecdf", "end", "frequency",
21+
"lag", "median", "predict", "predict.lm", "qnorm", "quantile",
2122
"residuals", "rnorm", "rt", "sd", "setNames", "start",
2223
"ts", "tsp", "tsp<-")
2324
importFrom("utils", "tail")
@@ -32,5 +33,5 @@ export("loessf")
3233
export("ridge2f")
3334
export("ridge")
3435
export("varf")
36+
export("getreturns")
3537
export("getsimulations")
36-
export("splitts")

NEWS.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# version 0.7.0
22

3-
- `getsimulations` extracts simulations from a given time series (from `ridge2f` and `basicf`)
4-
- moving block bootstrap in `ridge2f`, `basicf` and `loessf` (in addition to circular block bootstrap from 0.6.2)
5-
- adjust copulas on residuals for `ridge2f`
3+
- moving block bootstrap in `ridge2f`, `basicf` and `loessf`, in addition to circular block bootstrap from 0.6.2
4+
- adjust R-Vine copulas on residuals for `ridge2f` simulation
65
- new plots for simulations see (new) vignettes
7-
- split conformal prediction intervals (very experimental right now)
8-
- neutralization (TODO)(list with yM and mats, check with time(y) :o )
9-
- `Depends` vs `Imports` (beneficial to Python and rpy2 for installation time?)
6+
- split conformal prediction intervals (**very very experimental** and basic right now, too conservative)
7+
- `Depends` and selective `Imports` (beneficial to Python and rpy2 for installation time?)
8+
- `getsimulations` extracts simulations from a given time series (from `ridge2f` and `basicf`)
9+
- `getreturns` extracts returns/log-returns from multivariate time series
10+
- `splitts` splits time series using a proportion of data
1011

1112
# version 0.6.2
1213

R/basic.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ basicf <- function(y,
104104
fits <- ts(t(replicate(n = n_inputs, expr = point_forecast)),
105105
start = start_x, frequency = freq_x)
106106
resids <- y - fits
107+
colnames(resids) <- colnames(y)
107108

108109
# out-of-sample
109110
fcast <- ts(t(replicate(n = h, expr = point_forecast)),

R/getreturns.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#' Calculate returns or log-returns for multivariate time series
2+
#'
3+
#' @param x Multivariate time series
4+
#' @param type Type of return: basic return ("basic") or log-return ("log")
5+
#'
6+
#' @return The returns
7+
#' @export
8+
#'
9+
#' @examples
10+
#'
11+
#' returns <- getreturns(EuStockMarkets)
12+
#' log_returns <- getreturns(EuStockMarkets,
13+
#' type = "log")
14+
#'
15+
#' par(mfrow=c(1, 3))
16+
#' matplot(EuStockMarkets, type = 'l', main = "Closing Prices of \n European stocks (1991-1998)",
17+
#' xlab = "time")
18+
#' matplot(returns, type = 'l', main = "Returns", xlab = "time")
19+
#' matplot(log_returns, type = 'l', main = "Log-returns", xlab = "time")
20+
#'
21+
getreturns <- function(x, type = c("basic", "log"))
22+
{
23+
# ?rlang::abort #?
24+
stopifnot(is.ts(x))
25+
names_x <- colnames(x)
26+
type <- match.arg(type)
27+
28+
if (identical(type, "basic"))
29+
{
30+
res <- diff(x)/lag(x)
31+
colnames(res) <- names_x
32+
return (res)
33+
}
34+
res <- diff(log(x))
35+
colnames(res) <- names_x
36+
return (res)
37+
}

R/plot.R

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
#' Plot multivariate time series forecast
1+
#' Plot multivariate time series forecast or residuals
22
#'
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)
44
#' @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
67
#' @param level confidence levels for prediction intervals
78
#' @param ... additional parameters to be passed to \code{plot} or \code{matplot}
89
#'
@@ -41,9 +42,10 @@
4142
#'
4243
#'
4344
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, ...)
4749
{
4850
type_graph <- match.arg(type_graph)
4951

@@ -73,6 +75,8 @@ plot.mtsforecast <- function(x, selected_series,
7375
lines(y_upper, col="gray60")
7476
lines(y_lower, col="gray60")
7577
lines(y_mean)
78+
79+
return()
7680
}
7781

7882
if (type_graph == "dist")
@@ -107,6 +111,8 @@ plot.mtsforecast <- function(x, selected_series,
107111
bands_add(abs, y_mean, col = color[3], ci_upper = x_summary[2, ],
108112
ci_lower = x_summary[4, ])
109113

114+
return()
115+
110116
}
111117

112118
if (type_graph == "sims")
@@ -149,15 +155,93 @@ plot.mtsforecast <- function(x, selected_series,
149155
...
150156
)
151157
lines(x = time_inputs, y = input_series, lwd = 2)
152-
}
153-
154158

159+
return()
160+
}
155161
} else {
156-
stop("Method not implemented for this type of object")
162+
stop("Method not implemented for this type of object")
157163
}
158164
}
159165

160166

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+
161245
# plot bands # work in progress #scratchinghead
162246
bands_plot <- function(x, y_mean, ci_upper, ci_lower, col, y.goal = NULL, goal.col = "blue", ...)
163247
{

0 commit comments

Comments
 (0)