imapa: iMAPA: MAPA for intermittent demand

View source: R/imapa.R

imapaR Documentation

iMAPA: MAPA for intermittent demand

Description

Forecast intermittent demand time series by modelling them at multiple temporal aggregation levels and combining the forecasts, selecting between Croston's method, SBA, and SES at each level.

Usage

imapa(
  data,
  h = 10,
  w = NULL,
  minimumAL = 1,
  maximumAL = NULL,
  comb = c("mean", "median"),
  init.opt = c(TRUE, FALSE),
  paral = c(0, 1, 2),
  outplot = c(0, 1, 2, 3, 4),
  model.fit = NULL,
  na.rm = c(FALSE, TRUE),
  probs = NULL,
  qtype = 7,
  cl = NULL
)

Arguments

data

Intermittent demand time series. This can be a vector, a ts object, or a data frame, in which case only the first column is used.

h

Forecast horizon. When probs is provided, h is understood as the lead time. See the section "Lead time quantiles".

w

Smoothing parameters for Croston's method and SBA. If w == NULL then the parameters are optimised. If w is a single parameter, then the same is used for smoothing both the demand and the intervals. If two parameters are provided, then the second is used to smooth the intervals. SES is always optimised.

minimumAL

Lowest aggregation level to use.

maximumAL

Highest aggregation level to use. If NULL, the larger of the maximum inter-demand interval of the series and its sampling frequency is used, e.g., at least 12 for monthly data. The frequency is taken from ts objects only, so for other inputs the maximum interval is used. The periods before the first demand count as an interval, as in crost.decomp. If the result is lower than minimumAL, then minimumAL is used. Aggregation levels with fewer than four aggregated observations are not used, whatever the value of maximumAL.

comb

Combination operator, "mean" or "median".

init.opt

If TRUE, the initial values of Croston's method and SBA are optimised. This does not affect SES.

paral

Use parallel processing: 0 for no, 1 for yes using an initialised cluster provided in cl, and 2 for yes, initialising a cluster with all available cores. The workers load the installed package, so it must be installed at the same version as the one in use; this is checked, and an error is given otherwise. In particular, parallel processing does not work when the package is loaded with devtools::load_all().

outplot

Plot of the forecast:

  • 0 or FALSE: no plot;

  • 1, TRUE, or "period": time series and combined forecast;

  • 2 or "levels": time series and the forecasts of all aggregation levels;

  • 3 or "selection": summary of the models selected;

  • 4 or "selection.detail": detailed plot of the models selected, with the classification of the series at each aggregation level;

  • "leadtime": the lead time view, see plot.tsint.

All plots are also available afterwards with plot.imapa.

model.fit

Optional input with model types and parameters. This can be the model.fit output of this function, or a model previously fitted with this function, in which case its combination operator is also used, unless comb is given. If used, it overrides other model settings, and no classification or optimisation is performed. This can be used to apply a fitted model to new data, for example when new observations become available. The same is available with forecast.tsint.

na.rm

If TRUE, NA values are removed before modelling.

probs

Probabilities of the quantiles of the demand over the lead time h, for example c(0.9,0.95). If NULL, no quantiles are calculated. See the section "Lead time quantiles".

qtype

Type of empirical quantile calculation, passed to quantile.

cl

Cluster to use when paral=1, created with parallel::makeCluster. If NULL, a cluster named cl in the global environment is used, for compatibility with earlier versions.

Details

Temporal aggregation sums the series into non-overlapping buckets of several periods. At higher aggregation levels there are fewer zeros, so the series becomes easier to forecast, but the best level differs between series and is not known in advance. iMAPA avoids this choice by using all levels from minimumAL to maximumAL (Petropoulos and Kourentzes, 2015). At each level, the first observations are dropped where needed, so that all buckets are complete; the aggregated series is classified with the PKa scheme of idclass; and Croston's method, SBA, or SES is fitted accordingly, with parameters optimised using MAR. The forecast of each level is divided equally over the periods of a bucket, and the forecasts of all levels are combined. Levels with fewer than four aggregated observations, or fewer than two non-zero values, are not used.

The forecast is a demand rate, constant over the horizon. The in-sample forecast frc.in of each level is produced at the aggregated frequency, so within each bucket it is the forecast made at the start of the bucket.

Value

An object of class imapa, which is a list containing:

frc.in

In-sample demand rate.

frc.out

Out-of-sample demand rate.

summary

An array containing information for each aggregation level: AL, the aggregation level; n, the number of observations of the aggregated series; p, the average inter-demand interval; cv2, the squared coefficient of variation of the non-zero demand; model, the selected model, where 1 is Croston, 2 is SBA, and 3 is SES; and use, which is 0 if the aggregation level is not used.

model.fit

Parameters and initialisation values of the fitted model at each aggregation level, with aggregation level and model information.

lt

Only when probs is provided. See crost.

fit.info

Information used by the methods of the object, such as the series, the forecast from the end of the sample, and the cost function. Not intended for direct use.

The object can be used with print, summary, plot, coef, fitted, and forecast.

Lead time quantiles

The quantiles are calculated as described in crost, from the cumulative errors over h periods. Because the in-sample forecasts of the aggregation levels are made at the start of each bucket, rather than at every period, the cumulative errors are calculated from forecasts made at every in-sample origin, using the fitted models of each level with fixed parameters. Levels that do not yet have enough observations at an origin are left out of the combination at that origin. This requires forecasts from every origin and every level, so it is slower than the other methods; paral can be used to speed it up. Unlike the other methods, the cumulative cost functions MSCE and MACE are not offered, as temporal aggregation already targets the demand over multiple periods.

Author(s)

Nikolaos Kourentzes, nikolaos@kourentzes.com

References

Petropoulos, F., and Kourentzes, N. (2015). Forecast combinations for intermittent demand. Journal of the Operational Research Society, 66(6), 914–924. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1057/jors.2014.62")}

See Also

plot.imapa, idclass, crost.

Examples

# iMAPA with a plot of the combined forecast
fit <- imapa(ts.data1, outplot=1)
summary(fit)

# Forecasts of each aggregation level and the models selected
plot(fit, view="levels")
plot(fit, view="selection.detail")

# Fewer aggregation levels, combined with the median
imapa(ts.data1, maximumAL=12, comb="median")$frc.out

# Reuse the fitted models, for example on an updated series
imapa(ts.data1, model.fit=fit)$frc.out

# Quantiles of the demand over a lead time of 3 periods
fit <- imapa(ts.data1, h=3, maximumAL=12, probs=c(0.9,0.95), outplot="leadtime")
fit$lt$quantile


# Parallel processing with a cluster
cl <- parallel::makeCluster(2)
imapa(ts.data1, paral=1, cl=cl)$frc.out
parallel::stopCluster(cl)



tsintermittent documentation built on Nov. 14, 2026, 5:06 p.m.