Skip to content

Commit

Permalink
version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alexiosg authored and cran-robot committed Mar 10, 2024
0 parents commit fe2cd6e
Show file tree
Hide file tree
Showing 36 changed files with 1,735 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Changelog
@@ -0,0 +1,27 @@
# 2023-03-23 v0.3.2 [AG]
- Added additional methods of halflife, unconditional and pit for use with
tsgarch and tsarma.

# 2022-06-09 v0.3.1 [AG]
- Converted documentation to Roxygen and performed some clean up.

# 2022-04-15 v0.3.0 [AG]
- Added a tsmoments method
- Bumped the version to 0.3.0 to be consistent with major changes in other
packages with the same version.

# 2022-03-16 v0.1.8 [AG]
- Added a tscalibrate method

# 2021-05-20 v0.1.7 [AG]
- Added additional estimate_ad method for new plugin packages which use
automatic differentiation.
- Fix to tsgrowth documentation

# 2021-04-20 v0.1.6 [AG]
- Fix to prediction plots to allow missing values in observed series (ylim
fixed for na.rm)

# 2020-11-16 v0.1.5 [AG]
- Fix to tsreport method (previously dispatched to report rather than tsreport).
- Added Changelog to package
21 changes: 21 additions & 0 deletions DESCRIPTION
@@ -0,0 +1,21 @@
Package: tsmethods
Type: Package
Title: Time Series Methods
Version: 1.0.0
Date: 2024-03-03
Authors@R: c(person("Alexios", "Galanos", role = c("aut", "cre","cph"), email = "alexios@4dscape.com"))
Maintainer: Alexios Galanos <alexios@4dscape.com>
Description: Generic methods for use in a time series probabilistic framework, allowing for a common calling convention across packages. Additional methods for time series prediction ensembles and probabilistic plotting of predictions is included. A more detailed description is available at <https://www.nopredict.com/packages/tsmethods> which shows the currently implemented methods in the 'tsmodels' framework.
License: GPL-2
Imports: methods, zoo, xts
Encoding: UTF-8
URL: https://www.nopredict.com/packages/tsmethods,
https://github.com/tsmodels/tsmethods
RoxygenNote: 7.3.1
Suggests: testthat (>= 3.0.0)
Config/testthat/edition: 3
NeedsCompilation: no
Packaged: 2024-03-08 18:48:29 UTC; alexios
Author: Alexios Galanos [aut, cre, cph]
Repository: CRAN
Date/Publication: 2024-03-09 11:00:02 UTC
35 changes: 35 additions & 0 deletions MD5
@@ -0,0 +1,35 @@
00ec9a14ec1dafa6e88fac97384182c3 *Changelog
30e634f599938e04f6e9f00abae93b05 *DESCRIPTION
04e96b1340c8072787791b5d31a674b5 *NAMESPACE
e9b3821d37fd289fc1663ec88c84bd0f *R/distributions.R
f3956cf38c5f778e1b1fcdf9410adbbb *R/methods.R
244a7fa38f5dd4c272fe496d49f66baa *R/plots.R
1765c53b3d5321b919bd0d5ae4679ce9 *R/tsmethods-package.R
20f191df7cd9aa4293579c7278628b8f *man/ensemble_modelspec.Rd
aaadf33c852b427964f50c53df01826b *man/estimate.Rd
6deb816a4a119e64428ec9e8880d2003 *man/estimate_ad.Rd
af26c5981e7f6d40a8b628f1170a5e8c *man/halflife.Rd
65527ea0376d9bc83d7bd67a978c8f92 *man/pit.Rd
0277d9954f9cc77e0454569bcbe07b10 *man/plot.Rd
fe70876e6f9689ad736b725c372537a3 *man/tsaggregate.Rd
266de3fede27c18807cbec8452770aa6 *man/tsbacktest.Rd
0ed2d6bdd79a32369326e53c19e13f4b *man/tsbenchmark.Rd
af0567566067067cad4b93afcc319c9e *man/tscalibrate.Rd
05cc68b4b5e64a5193182ff508c221da *man/tsconvert.Rd
48fe9175d5ed620be700bcdbd7e46716 *man/tscor.Rd
08a3f90bc36c2246eb14efd24e55faef *man/tscov.Rd
618b4e2c730b7eec4fcfb6bcb842854a *man/tsdecompose.Rd
4df879080d6887229943888bbf631af3 *man/tsdiagnose.Rd
3516f9ab61b054cf18d824543fb50da7 *man/tsensemble.Rd
7bb176a909277b3dd1ee6a2760eb02c2 *man/tsequation.Rd
ddf6eced4b48e41f15668c5c1fbb4252 *man/tsfilter.Rd
a62beef78e3b6bddccfa013c9569ff14 *man/tsgrowth.Rd
695c59bdd220d730f6a4bcd65aba57f9 *man/tsmethods-package.Rd
80df777325c5017c2047451610ad6c59 *man/tsmetrics.Rd
9fc89b408cd79069404c98b80cbb5211 *man/tsmoments.Rd
e46dd55ad0503a3ec6e2d5b285cb98f5 *man/tsprofile.Rd
f39a2ce39b84c45dae3795211c1eac15 *man/tsreport.Rd
a76a3379713f56250eb7d479d049030c *man/tsspec.Rd
22d150757d09360f83fa2264b341aa8e *man/unconditional.Rd
f92b06bcdc0fd11b80ca1de9ac0a4825 *tests/testthat.R
31cab4aa8039503411190ac41835379f *tests/testthat/test-ensemble.R
63 changes: 63 additions & 0 deletions NAMESPACE
@@ -0,0 +1,63 @@
# Generated by roxygen2: do not edit by hand

S3method(plot,tsmodel.distribution)
S3method(plot,tsmodel.predict)
S3method(tsensemble,ensemble.spec)
S3method(tsgrowth,tsmodel.predict)
export(ensemble_modelspec)
export(estimate)
export(estimate_ad)
export(halflife)
export(pit)
export(tsaggregate)
export(tsbacktest)
export(tsbenchmark)
export(tscalibrate)
export(tsconvert)
export(tscor)
export(tscov)
export(tsdecompose)
export(tsdiagnose)
export(tsensemble)
export(tsequation)
export(tsfilter)
export(tsgrowth)
export(tsmetrics)
export(tsmoments)
export(tsprofile)
export(tsreport)
export(tsspec)
export(unconditional)
import(methods)
importFrom(grDevices,col2rgb)
importFrom(grDevices,colorRampPalette)
importFrom(grDevices,gray)
importFrom(grDevices,n2mfrow)
importFrom(grDevices,rgb)
importFrom(graphics,abline)
importFrom(graphics,axis)
importFrom(graphics,axis.Date)
importFrom(graphics,axis.POSIXct)
importFrom(graphics,box)
importFrom(graphics,grid)
importFrom(graphics,hist)
importFrom(graphics,layout)
importFrom(graphics,legend)
importFrom(graphics,lines)
importFrom(graphics,par)
importFrom(graphics,plot)
importFrom(graphics,points)
importFrom(graphics,polygon)
importFrom(stats,fitted)
importFrom(stats,median)
importFrom(stats,na.omit)
importFrom(stats,quantile)
importFrom(stats,ts)
importFrom(utils,data)
importFrom(utils,head)
importFrom(utils,tail)
importFrom(xts,endpoints)
importFrom(xts,xts)
importFrom(zoo,coredata)
importFrom(zoo,index)
importFrom(zoo,zoo)
136 changes: 136 additions & 0 deletions R/distributions.R
@@ -0,0 +1,136 @@
#' Ensemble specification
#'
#' @description Creates and validates an ensemble specification.
#' @param ... objects of either all of class \dQuote{tsmodel.predict} or
#' \dQuote{tsmodel.distribution} representing the probabilistic forecasts for
#' the same horizon of optionally different models on the same series and with
#' same number of draws. It is expected that the predictive distributions are
#' based on joint simulated draws passed to the \code{innov} argument in
#' the \code{predict} function of the supporting models. Instead of \ldots
#' it is also possible to pass a list of the objects.
#' @returns An object of class \dQuote{ensemble.spec}.
#' @aliases ensemble_modelspec
#' @rdname ensemble_modelspec
#' @export
#'
#'
ensemble_modelspec <- function(...)
{
m <- list(...)
# if its already a list
if (length(m) == 1) {
if (is.list(m[[1]])) {
m <- m[[1]]
}
}
object_class <- sapply(1:length(m), function(i) tail(class(m[[i]]),1))
if (length(unique(object_class)) != 1) stop("\nThe object must have the same class (tsmodel.predict or tsmodel.distribution)")
if (!object_class[1] %in% c("tsmodel.predict","tsmodel.distribution")) stop("\nThe object must be of class tsmodel.predict or tsmodel.distribution")
object_class <- object_class[1]
if (object_class == "tsmodel.predict") {
original_series <- do.call(cbind, lapply(1:length(m), function(i) m[[i]]$original_series))
} else {
original_series <- NULL
}
n <- length(m)
if (object_class == "tsmodel.predict") {
horizon <- NCOL(m[[1]]$distribution)
forc_dates <- colnames(m[[1]]$distribution)
sims <- sapply(1:length(m), function(i) NROW(m[[i]]$distribution))
if (length(unique(sims)) != 1) stop("\nThe simulated distributions must have the same number of draws (rows)")
# check horizon match
check <- sapply(2:length(m), function(i){
all.equal(forc_dates, colnames(m[[i]]$distribution))
})
if (!all(check)) stop("\nhorizon and forecast dates of each distribution must match exactly.")
M <- array(0, dim = c(dim(m[[1]]$distribution), length(m)))
for (i in 1:length(m)) M[,,i] <- as.matrix(m[[i]]$distribution)
} else {
horizon <- NCOL(m[[1]])
forc_dates <- colnames(m[[1]])
sims <- sapply(1:length(m), function(i) NROW(m[[i]]))
if (length(unique(sims)) != 1) stop("\nThe simulated distributions must have the same number of draws (rows)")
# check horizon match
check <- sapply(2:length(m), function(i){
all.equal(forc_dates, colnames(m[[i]]))
})
if (!all(check)) stop("\nhorizon and forecast dates of each distribution must match exactly.")
M <- array(0, dim = c(dim(m[[1]]), length(m)))
for (i in 1:length(m)) M[,,i] <- as.matrix(m[[i]])
}
out <- list(distribution = M, original_series = original_series, object_class = object_class, forc_dates = forc_dates)
class(out) <- "ensemble.spec"
return(out)
}

#' @method tsensemble ensemble.spec
#' @details Returns the weighted distribution, under the assumption that the
#' predictions were generated using a joint error distribution whose values were
#' passed to the \code{innov} argument of the predict function used for each model.
#' @returns An object of class \dQuote{tsmodel.predict} or \dQuote{tsmodel.distribution}
#' depending on the input class in \code{\link{ensemble_modelspec}}.
#' @rdname tsensemble
#' @export
#'
#
tsensemble.ensemble.spec <- function(object, weights = NULL, ...)
{
n <- dim(object$distribution)[3]
if (is.null(weights)) {
weights <- matrix(rep(1/n, n), ncol = 1)
warning("\nweights are NULL. Setting to equal weight ensemble")
} else {
if (length(as.numeric(weights)) != n) stop("\nlength of weights must be equal to number of distributions passed.")
weights <- matrix(weights, ncol = 1)
}
d <- t(apply(object$distribution, 1, function(x) x %*% weights))
colnames(d) <- object$forc_dates
class(d) <- "tsmodel.distribution"
# ensemble the original series if present
if (object$object_class == "tsmodel.predict") {
ind1 <- index(object$original_series)
original_series <- xts(rowSums(coredata(object$original_series) * matrix(weights, ncol = n, nrow = nrow(object$original_series), byrow = TRUE), na.rm = TRUE), ind1)
L <- list(distribution = d, original_series = original_series, mean = zoo(colMeans(d), as.Date(object$forc_dates)))
class(L) <- "tsmodel.predict"
} else {
L <- d
class(L) <- "tsmodel.distribution"
}
return(L)
}


#' @param object an object.
#' @param d the period back to look at for growth calculations.
#' @param type the type of growth calculation. \dQuote{diff} is simply the
#' difference in values over n periods, \dQuote{simple} if the
#' rate of change and \dQuote{log} the difference in logs.
#' @param ... additional parameters passed to the method.
#' @returns an object of class \dQuote{tsmodel.predict} transformed to a growth distribution.
#' @method tsgrowth tsmodel.predict
#' @rdname tsgrowth
#' @export
#'
#
tsgrowth.tsmodel.predict <- function(object, d = 1, type = c("diff","simple","log"), ...)
{
Z <- matrix(coredata(object$original_series), ncol = length(object$original_series), nrow = nrow(object$distribution), byrow = TRUE)
colnames(Z) <- as.character(index(object$original_series))
G <- cbind(Z, as.matrix(object$distribution))
if (type == "diff") {
P <- G[,(d + 1):ncol(G)] - G[,1:(ncol(G) - d)]
} else if (type == "simple") {
P <- G[,(d + 1):ncol(G)]/G[,1:(ncol(G) - d)] - 1
} else if (type == "log") {
P <- log(G)
P <- P[,(d + 1):ncol(P)] - P[,1:(ncol(P) - d)]
}
N <- NCOL(object$distribution)
PN <- NCOL(P)
D <- P[,tail(1:PN,N)]
class(D) <- "tsmodel.distribution"
original_series <- zoo(P[1,1:(PN - N)], index(object$original_series)[-(1:d)])
L <- list(original_series = original_series, distribution = D)
class(L) <- "tsmodel.predict"
return(L)
}

0 comments on commit fe2cd6e

Please sign in to comment.