## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4)
library(tsintermittent)

## ----data---------------------------------------------------------------------
plot(ts.data1, ylab = "Demand")

## ----crost--------------------------------------------------------------------
fit <- crost(ts.data1, type = "sba", outplot = TRUE)
fit$weights   # Smoothing parameters for demand and intervals
fit$initial   # Initial values for demand and intervals

## ----tsb----------------------------------------------------------------------
fit.tsb <- tsb(ts.data1, outplot = TRUE)
fit.tsb$weights

## ----others-------------------------------------------------------------------
sexsm(ts.data1)$alpha
crost.ma(ts.data1)$order

## ----optimisation-options-----------------------------------------------------
# Optimise everything (default)
crost(ts.data1, type = "sba")$frc.out[1]

# Single parameter for demand and intervals
crost(ts.data1, type = "sba", nop = 1)$weights

# Fixed parameters, optimised initial values
crost(ts.data1, type = "sba", w = c(0.1, 0.05))$initial

# Everything fixed
crost(ts.data1, type = "sba", w = c(0.1, 0.05), init = c(5, 3), init.opt = FALSE)$frc.out[1]

## ----costs--------------------------------------------------------------------
sapply(c("mar", "msr", "mse", "mae"),
       function(x){crost(ts.data1, type = "sba", cost = x)$weights})

## ----costs-cumulative---------------------------------------------------------
crost(ts.data1, h = 3, type = "sba", cost = "msce")$weights
crost(ts.data1, h = 3, type = "sba", cost = "mace")$weights

## ----lt-----------------------------------------------------------------------
fit <- crost(ts.data1, h = 3, type = "sba", probs = c(0.9, 0.95))
fit$lt$frc        # Cumulative forecast over the lead time
fit$lt$quantile   # Quantiles of the cumulative demand over the lead time
fit$lt$ss         # Safety stock for each probability
summary(fit)

## ----lt-plot------------------------------------------------------------------
fit <- crost(ts.data1, h = 3, type = "sba", probs = c(0.9, 0.95), outplot = "leadtime")
plot(fit)                     # Per-period view
plot(fit, view = "leadtime")  # Lead-time view
plot(fit, view = "leadtime", main = "Demand over a 3-month lead time")

## ----reuse--------------------------------------------------------------------
fit <- crost(ts.data1, type = "sba")
forecast(fit, h = 6)
lt.dist(fit, h = 3, probs = c(0.9, 0.95))$quantile

## ----reuse-new----------------------------------------------------------------
fit <- crost(window(ts.data1, end = c(1985, 12)), type = "sba")
crost(ts.data1, model = fit)$frc.out[1]
forecast(fit, h = 3, newdata = ts.data1)
crost(ts.data2, model = fit, init.opt = TRUE)$initial

## ----idclass------------------------------------------------------------------
set.seed(1)
dataset <- cbind(simID(20, 60, idi = 1.05, cv2 = 0.1),
                 simID(20, 60, idi = 1.20, cv2 = 0.1),
                 simID(20, 60, idi = 2.00, cv2 = 0.8))
colnames(dataset) <- paste0("ts", 1:60)
cls <- idclass(dataset, type = "PKa", outplot = "summary")
cls$summary
cls.sbc <- idclass(dataset, type = "SBC", outplot = "detail")

## ----imapa--------------------------------------------------------------------
fit <- imapa(ts.data1, outplot = 1)   # Series and combined forecast
plot(fit, view = "levels")            # Forecasts from each aggregation level
plot(fit, view = "selection")         # Summary of the methods selected
plot(fit, view = "selection.detail")  # Classification per level
summary(fit)                          # Characteristics and method per level

## ----imapa-refit--------------------------------------------------------------
imapa(ts.data1, model.fit = fit$model.fit)$frc.out

## ----imapa-lt-----------------------------------------------------------------
fit <- imapa(ts.data1, h = 3, maximumAL = 12, probs = c(0.9, 0.95))
fit$lt$quantile
plot(fit, view = "leadtime")

## ----datafrc------------------------------------------------------------------
out <- data.frc(dataset, method = "crost", type = "sba", h = 6)
out$frc.out[, 1:3]

## ----datafrc-lt---------------------------------------------------------------
out <- data.frc(dataset, method = "auto", h = 3, probs = c(0.9, 0.95))
out$lt.out[, 1:3]

