## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
set.seed(42)
library(clmplus)
library(ChainLadder)

## ----fits---------------------------------------------------------------------
data("AutoBI", package = "ChainLadder")
triangle <- ChainLadder::incr2cum(AutoBI$AutoBIPaid)
prepared <- AggregateDataPP(triangle)
model_ids <- c("a", "ac", "ap", "apc")
fits <- setNames(lapply(model_ids, function(id) {
  clmplus(prepared, hazard.model = id, verbose = FALSE)
}), model_ids)
predictions <- lapply(fits, predict)

## ----chain-ladder-comparison--------------------------------------------------
mack <- ChainLadder::MackChainLadder(triangle)
age_prediction <- predictions[["a"]]
stopifnot(isTRUE(all.equal(
  unname(age_prediction$full_triangle),
  unname(mack$FullTriangle),
  tolerance = 1e-10
)))
data.frame(
  accident_period = seq_along(age_prediction$reserve) - 1L,
  reserve = unname(age_prediction$reserve),
  ultimate = unname(age_prediction$ultimate_cost)
)
sum(age_prediction$reserve)

## ----partial-forecast---------------------------------------------------------
one_year <- predict(fits[["apc"]], forecasting_horizon = 1)
dim(one_year$full_triangle)
dim(one_year$apc_output$lower_triangle_apc)

## ----plots, fig.width=7, fig.height=4-----------------------------------------
plot(fits[["apc"]])
plot(predictions[["apc"]])

