Skip to content

Commit 7eb926b

Browse files
committed
Standardised parameters for adam() and ces().
1 parent 1e557cf commit 7eb926b

File tree

6 files changed

+92
-128
lines changed

6 files changed

+92
-128
lines changed

R/adam-ces.R

+28-67
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ utils::globalVariables(c("xregData","xregModel","xregNumber","initialXregEstimat
2323
#' @template ssAuthor
2424
#' @template ssKeywords
2525
#'
26+
#' @template ADAMDataFormulaRegLossSilentHHoldout
27+
#'
2628
#' @template smoothRef
2729
#' @template ssADAMRef
2830
#' @template ssGeneralRef
@@ -48,12 +50,6 @@ utils::globalVariables(c("xregData","xregModel","xregNumber","initialXregEstimat
4850
#' @param formula Formula to use in case of explanatory variables. If \code{NULL},
4951
#' then all the variables are used as is. Can also include \code{trend}, which would add
5052
#' the global trend. Only needed if \code{data} is a matrix or if \code{trend} is provided.
51-
#' @param regressors The variable defines what to do with the provided explanatory
52-
#' variables:
53-
#' \code{"use"} means that all of the data should be used, while
54-
#' \code{"select"} means that a selection using \code{ic} should be done,
55-
#' \code{"adapt"} will trigger the mechanism of time varying parameters for the
56-
#' explanatory variables.
5753
#' @param initial Should be a character, which can be \code{"optimal"},
5854
#' meaning that all initial states are optimised, or \code{"backcasting"},
5955
#' meaning that the initials of dynamic part of the model are produced using
@@ -69,46 +65,11 @@ utils::globalVariables(c("xregData","xregModel","xregNumber","initialXregEstimat
6965
#' @param b Second complex smoothing parameter. Can be real if
7066
#' \code{seasonality="partial"}. In case of \code{seasonality="full"} must be
7167
#' complex number.
72-
#' @param ic The information criterion to use in the model selection.
73-
#' @param loss The type of Loss Function used in optimization. \code{loss} can
74-
#' be:
75-
#' \itemize{
76-
#' \item \code{likelihood} - the model is estimated via the maximisation of the
77-
#' likelihood of the function specified in \code{distribution};
78-
#' \item \code{MSE} (Mean Squared Error),
79-
#' \item \code{MAE} (Mean Absolute Error),
80-
#' \item \code{HAM} (Half Absolute Moment),
81-
#' \item \code{LASSO} - use LASSO to shrink the parameters of the model;
82-
#' \item \code{RIDGE} - use RIDGE to shrink the parameters of the model;
83-
#' \item \code{TMSE} - Trace Mean Squared Error,
84-
#' \item \code{GTMSE} - Geometric Trace Mean Squared Error,
85-
#' \item \code{MSEh} - optimisation using only h-steps ahead error,
86-
#' \item \code{MSCE} - Mean Squared Cumulative Error.
87-
#' }
88-
#'
89-
#' Note that model selection and combination works properly only for the default
90-
#' \code{loss="likelihood"}.
91-
#'
92-
#' Furthermore, just for fun the absolute and half analogues of multistep estimators
93-
#' are available: \code{MAEh}, \code{TMAE}, \code{GTMAE}, \code{MACE},
94-
#' \code{HAMh}, \code{THAM}, \code{GTHAM}, \code{CHAM}.
95-
#'
96-
#' Last but not least, user can provide their own function here as well, making sure
97-
#' that it accepts parameters \code{actual}, \code{fitted} and \code{B}. Here is an
98-
#' example:
99-
#'
100-
#' \code{lossFunction <- function(actual, fitted, B) return(mean(abs(actual-fitted)))}
101-
#'
102-
#' \code{loss=lossFunction}
103-
#' @param h The forecast horizon. Mainly needed for the multistep loss functions.
104-
#' @param holdout Logical. If \code{TRUE}, then the holdout of the size \code{h}
105-
#' is taken from the data (can be used for the model testing purposes).
10668
#' @param bounds The type of bounds for the persistence to use in the model
10769
#' estimation. Can be either \code{admissible} - guaranteeing the stability of the
10870
#' model, or \code{none} - no restrictions (potentially dangerous).
109-
#' @param silent Specifies, whether to provide the progress of the function or not.
110-
#' If \code{TRUE}, then the function will print what it does and how much it has
111-
#' already done.
71+
#' @param model A previously estimated GUM model, if provided, the function
72+
#' will not estimate anything and will use all its parameters.
11273
#' @param ... Other non-documented parameters. See \link[smooth]{adam} for
11374
#' details
11475
#'
@@ -119,7 +80,6 @@ utils::globalVariables(c("xregData","xregModel","xregNumber","initialXregEstimat
11980
#'
12081
#' @examples
12182
#' y <- rnorm(100,10,3)
122-
#' ces(y, h=20, holdout=TRUE)
12383
#' ces(y, h=20, holdout=FALSE)
12484
#'
12585
#' y <- 500 - c(1:100)*0.5 + rnorm(100,10,3)
@@ -136,11 +96,10 @@ utils::globalVariables(c("xregData","xregModel","xregNumber","initialXregEstimat
13696
ces <- function(data, seasonality=c("none","simple","partial","full"), lags=c(frequency(data)),
13797
formula=NULL, regressors=c("use","select","adapt"),
13898
initial=c("optimal","backcasting","complete"), a=NULL, b=NULL,
139-
ic=c("AICc","AIC","BIC","BICc"),
99+
# ic=c("AICc","AIC","BIC","BICc"),
140100
loss=c("likelihood","MSE","MAE","HAM","MSEh","TMSE","GTMSE","MSCE"),
141-
h=0, holdout=FALSE,
142-
bounds=c("admissible","none"),
143-
silent=TRUE, ...){
101+
h=0, holdout=FALSE, bounds=c("admissible","none"), silent=TRUE,
102+
model=NULL, ...){
144103
# Function estimates CES in state space form with sigma = error
145104
# and returns complex smoothing parameter value, fitted values,
146105
# residuals, point and interval forecasts, matrix of CES components and values of
@@ -166,31 +125,31 @@ ces <- function(data, seasonality=c("none","simple","partial","full"), lags=c(fr
166125
profilesRecentTable <- NULL;
167126

168127
# If a previous model provided as a model, write down the variables
169-
if(!is.null(ellipsis$model)){
170-
if(is.null(ellipsis$model$model)){
128+
if(!is.null(model)){
129+
if(is.null(model$model)){
171130
stop("The provided model is not CES.",call.=FALSE);
172131
}
173-
else if(smoothType(ellipsis$model)!="CES"){
132+
else if(smoothType(model)!="CES"){
174133
stop("The provided model is not CES.",call.=FALSE);
175134
}
176135
# This needs to be fixed to align properly in case of various seasonals
177-
profilesRecentInitial <- profilesRecentTable <- ellipsis$model$profileInitial;
136+
profilesRecentInitial <- profilesRecentTable <- model$profileInitial;
178137
profilesRecentProvided[] <- TRUE;
179138
# This is needed to save initials and to avoid the standard checks
180-
initialValueProvided <- ellipsis$model$initial;
181-
initialOriginal <- initial <- ellipsis$model$initialType;
182-
a <- ellipsis$model$parameters$a;
183-
b <- ellipsis$model$parameters$b;
184-
seasonality <- ellipsis$model$seasonality;
185-
matVt <- t(ellipsis$model$states);
186-
matWt <- ellipsis$model$measurement;
187-
matF <- ellipsis$model$transition;
188-
vecG <- ellipsis$model$persistence;
189-
ellipsis$B <- coef(ellipsis$model);
190-
lags <- lags(ellipsis$model);
191-
192-
model <- ellipsis$model$model;
193-
ellipsis$model <- NULL;
139+
initialValueProvided <- model$initial;
140+
initialOriginal <- initial <- model$initialType;
141+
a <- model$parameters$a;
142+
b <- model$parameters$b;
143+
seasonality <- model$seasonality;
144+
matVt <- t(model$states);
145+
matWt <- model$measurement;
146+
matF <- model$transition;
147+
vecG <- model$persistence;
148+
ellipsis$B <- coef(model);
149+
lags <- lags(model);
150+
151+
model <- model$model;
152+
model <- NULL;
194153
modelDo <- modelDoOriginal <- "use";
195154
}
196155
else{
@@ -237,7 +196,9 @@ ces <- function(data, seasonality=c("none","simple","partial","full"), lags=c(fr
237196
constant=FALSE, arma=NULL,
238197
outliers="ignore", level=0.99,
239198
persistence=NULL, phi=NULL, initial,
240-
distribution="dnorm", loss, h, holdout, occurrence="none", ic, bounds=bounds[1],
199+
distribution="dnorm", loss, h, holdout, occurrence="none",
200+
# This is not needed by the function
201+
ic="AICc", bounds=bounds[1],
241202
regressors=regressors, yName=yName,
242203
silent, modelDo, ParentEnvironment=environment(), ellipsis, fast=FALSE);
243204

R/adam.R

+2-52
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,15 @@ utils::globalVariables(c("adamFitted","algorithm","arEstimate","arOrders","arReq
7878
#' @template ssAuthor
7979
#' @template ssKeywords
8080
#'
81+
#' @template ADAMDataFormulaRegLossSilentHHoldout
82+
#'
8183
#' @template smoothRef
8284
#' @template ssADAMRef
8385
#' @template ssGeneralRef
8486
#' @template ssIntermittentRef
8587
#' @template ssETSRef
8688
#' @template ssIntervalsRef
8789
#'
88-
#' @param data Vector, containing data needed to be forecasted. If a matrix (or
89-
#' data.frame / data.table) is provided, then the first column is used as a
90-
#' response variable, while the rest of the matrix is used as a set of explanatory
91-
#' variables. \code{formula} can be used in the latter case in order to define what
92-
#' relation to have.
9390
#' @param model The type of ETS model. The first letter stands for the type of
9491
#' the error term ("A" or "M"), the second (and sometimes the third as well) is for
9592
#' the trend ("N", "A", "Ad", "M" or "Md"), and the last one is for the type of
@@ -153,9 +150,6 @@ utils::globalVariables(c("adamFitted","algorithm","arEstimate","arOrders","arReq
153150
#' mechanism similar to \code{auto.msarima()}, but implemented in \code{auto.adam()}.
154151
#' The values \code{list(ar=...,i=...,ma=...)} specify the maximum orders to check in
155152
#' this case.
156-
#' @param formula Formula to use in case of explanatory variables. If \code{NULL},
157-
#' then all the variables are used as is. Can also include \code{trend}, which would add
158-
#' the global trend. Only needed if \code{data} is a matrix or if \code{trend} is provided.
159153
#' @param constant Logical, determining, whether the constant is needed in the model or not.
160154
#' This is mainly needed for ARIMA part of the model, but can be used for ETS as well. In
161155
#' case of pure regression, this is completely ignored (use \code{formula} instead).
@@ -164,41 +158,6 @@ utils::globalVariables(c("adamFitted","algorithm","arEstimate","arOrders","arReq
164158
#' "density". The names align with the names of distribution functions in R.
165159
#' For example, see \link[stats]{dnorm}. For detailed explanation of available
166160
#' distributions, see vignette in greybox package: \code{vignette("greybox","alm")}.
167-
#' @param loss The type of Loss Function used in optimization. \code{loss} can
168-
#' be:
169-
#' \itemize{
170-
#' \item \code{likelihood} - the model is estimated via the maximisation of the
171-
#' likelihood of the function specified in \code{distribution};
172-
#' \item \code{MSE} (Mean Squared Error),
173-
#' \item \code{MAE} (Mean Absolute Error),
174-
#' \item \code{HAM} (Half Absolute Moment),
175-
#' \item \code{LASSO} - use LASSO to shrink the parameters of the model;
176-
#' \item \code{RIDGE} - use RIDGE to shrink the parameters of the model;
177-
#' \item \code{TMSE} - Trace Mean Squared Error,
178-
#' \item \code{GTMSE} - Geometric Trace Mean Squared Error,
179-
#' \item \code{MSEh} - optimisation using only h-steps ahead error,
180-
#' \item \code{MSCE} - Mean Squared Cumulative Error.
181-
#' }
182-
#' In case of LASSO / RIDGE, the variables are not normalised prior to the estimation,
183-
#' but the parameters are divided by the mean values of explanatory variables.
184-
#'
185-
#' Note that model selection and combination works properly only for the default
186-
#' \code{loss="likelihood"}.
187-
#'
188-
#' Furthermore, just for fun the absolute and half analogues of multistep estimators
189-
#' are available: \code{MAEh}, \code{TMAE}, \code{GTMAE}, \code{MACE},
190-
#' \code{HAMh}, \code{THAM}, \code{GTHAM}, \code{CHAM}.
191-
#'
192-
#' Last but not least, user can provide their own function here as well, making sure
193-
#' that it accepts parameters \code{actual}, \code{fitted} and \code{B}. Here is an
194-
#' example:
195-
#'
196-
#' \code{lossFunction <- function(actual, fitted, B) return(mean(abs(actual-fitted)))}
197-
#'
198-
#' \code{loss=lossFunction}
199-
#' @param h The forecast horizon. Mainly needed for the multistep loss functions.
200-
#' @param holdout Logical. If \code{TRUE}, then the holdout of the size \code{h}
201-
#' is taken from the data (can be used for the model testing purposes).
202161
#' @param persistence Persistence vector \eqn{g}, containing smoothing
203162
#' parameters. If \code{NULL}, then estimated. Can be also passed as a names list of
204163
#' the type: \code{persistence=list(level=0.1, trend=0.05, seasonal=c(0.1,0.2),
@@ -248,15 +207,6 @@ utils::globalVariables(c("adamFitted","algorithm","arEstimate","arOrders","arReq
248207
#' estimation. Can be either \code{admissible} - guaranteeing the stability of the
249208
#' model, \code{usual} - restricting the values with (0, 1) or \code{none} - no
250209
#' restrictions (potentially dangerous).
251-
#' @param regressors The variable defines what to do with the provided explanatory
252-
#' variables:
253-
#' \code{"use"} means that all of the data should be used, while
254-
#' \code{"select"} means that a selection using \code{ic} should be done,
255-
#' \code{"adapt"} will trigger the mechanism of time varying parameters for the
256-
#' explanatory variables.
257-
#' @param silent Specifies, whether to provide the progress of the function or not.
258-
#' If \code{TRUE}, then the function will print what it does and how much it has
259-
#' already done.
260210
#' @param ... Other non-documented parameters. For example, \code{FI=TRUE} will
261211
#' make the function also produce Fisher Information matrix, which then can be
262212
#' used to calculated variances of smoothing parameters and initial states of

R/autoces.R

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#' @param ic The information criterion to use in the model selection.
12
#' @examples
23
#'
34
#' y <- ts(rnorm(100,10,3),frequency=12)

R/ces.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ ces_old <- function(data, seasonality=c("none","simple","partial","full"),
2323
startTime <- Sys.time();
2424

2525
### Depricate the old parameters
26-
ellipsis <- list(...)
26+
ellipsis <- list(...);
2727

2828
y <- data;
29-
cumulative <- FALSE
29+
cumulative <- FALSE;
3030
interval <- ifelse(!is.null(ellipsis$interval),ellipsis$interval,"none");
3131
level <- ifelse(!is.null(ellipsis$level),ellipsis$level,0.95);
3232
xreg <- ellipsis$xreg;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#' @param data Vector, containing data needed to be forecasted. If a matrix (or
2+
#' data.frame / data.table) is provided, then the first column is used as a
3+
#' response variable, while the rest of the matrix is used as a set of explanatory
4+
#' variables. \code{formula} can be used in the latter case in order to define what
5+
#' relation to have.
6+
#' @param formula Formula to use in case of explanatory variables. If \code{NULL},
7+
#' then all the variables are used as is. Can also include \code{trend}, which would add
8+
#' the global trend. Only needed if \code{data} is a matrix or if \code{trend} is provided.
9+
#' @param regressors The variable defines what to do with the provided explanatory
10+
#' variables:
11+
#' \code{"use"} means that all of the data should be used, while
12+
#' \code{"select"} means that a selection using \code{ic} should be done,
13+
#' \code{"adapt"} will trigger the mechanism of time varying parameters for the
14+
#' explanatory variables.
15+
#' @param loss The type of Loss Function used in optimization. \code{loss} can
16+
#' be:
17+
#' \itemize{
18+
#' \item \code{likelihood} - the model is estimated via the maximisation of the
19+
#' likelihood of the function specified in \code{distribution};
20+
#' \item \code{MSE} (Mean Squared Error),
21+
#' \item \code{MAE} (Mean Absolute Error),
22+
#' \item \code{HAM} (Half Absolute Moment),
23+
#' \item \code{LASSO} - use LASSO to shrink the parameters of the model;
24+
#' \item \code{RIDGE} - use RIDGE to shrink the parameters of the model;
25+
#' \item \code{TMSE} - Trace Mean Squared Error,
26+
#' \item \code{GTMSE} - Geometric Trace Mean Squared Error,
27+
#' \item \code{MSEh} - optimisation using only h-steps ahead error,
28+
#' \item \code{MSCE} - Mean Squared Cumulative Error.
29+
#' }
30+
#' In case of LASSO / RIDGE, the variables are not normalised prior to the estimation,
31+
#' but the parameters are divided by the mean values of explanatory variables.
32+
#'
33+
#' Note that model selection and combination works properly only for the default
34+
#' \code{loss="likelihood"}.
35+
#'
36+
#' Furthermore, just for fun the absolute and half analogues of multistep estimators
37+
#' are available: \code{MAEh}, \code{TMAE}, \code{GTMAE}, \code{MACE},
38+
#' \code{HAMh}, \code{THAM}, \code{GTHAM}, \code{CHAM}.
39+
#'
40+
#' Last but not least, user can provide their own function here as well, making sure
41+
#' that it accepts parameters \code{actual}, \code{fitted} and \code{B}. Here is an
42+
#' example:
43+
#'
44+
#' \code{lossFunction <- function(actual, fitted, B) return(mean(abs(actual-fitted)))}
45+
#'
46+
#' \code{loss=lossFunction}
47+
#' @param silent Specifies, whether to provide the progress of the function or not.
48+
#' If \code{TRUE}, then the function will print what it does and how much it has
49+
#' already done.
50+
#' @param h The forecast horizon. Mainly needed for the multistep loss functions.
51+
#' @param holdout Logical. If \code{TRUE}, then the holdout of the size \code{h}
52+
#' is taken from the data (can be used for the model testing purposes).

man/ces.Rd

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)