## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(elcf4R)

.elcf4r_available_data <- local({
  pkg_data <- utils::data(package = "elcf4R")
  if (is.null(pkg_data$results)) {
    character()
  } else {
    as.character(pkg_data$results[, "Item"])
  }
})

.elcf4r_load_data <- function(name) {
  if (!(name %in% .elcf4r_available_data)) {
    return(FALSE)
  }
  utils::data(list = name, package = "elcf4R", envir = environment())
  TRUE
}

.elcf4r_example_size_row <- function(object_name, dataset_label) {
  if (!exists(object_name, inherits = FALSE)) {
    return(NULL)
  }
  x <- get(object_name, inherits = FALSE)
  if (!is.data.frame(x)) {
    return(NULL)
  }
  data.frame(
    dataset = dataset_label,
    rows = nrow(x),
    ids = length(unique(x$entity_id)),
    stringsAsFactors = FALSE
  )
}

.elcf4r_benchmark_summary <- function(object_name, dataset_label) {
  if (!exists(object_name, inherits = FALSE)) {
    return(NULL)
  }
  x <- get(object_name, inherits = FALSE)
  if (!is.data.frame(x) || nrow(x) == 0L) {
    return(NULL)
  }
  transform(
    aggregate(
      cbind(nmae, nrmse, smape, mase) ~ method,
      data = x,
      FUN = function(v) round(mean(v, na.rm = TRUE), 4)
    ),
    dataset = dataset_label
  )
}

invisible(lapply(
  c(
    "elcf4r_iflex_example",
    "elcf4r_iflex_benchmark_results",
    "elcf4r_storenet_example",
    "elcf4r_storenet_benchmark_results",
    "elcf4r_sgsc_example",
    "elcf4r_lcl_example",
    "elcf4r_lcl_benchmark_results",
    "elcf4r_refit_example",
    "elcf4r_refit_benchmark_results"
  ),
  .elcf4r_load_data
))

## -----------------------------------------------------------------------------
example_sizes <- Filter(
  Negate(is.null),
  list(
    .elcf4r_example_size_row("elcf4r_iflex_example", "iflex"),
    .elcf4r_example_size_row("elcf4r_storenet_example", "storenet"),
    .elcf4r_example_size_row("elcf4r_sgsc_example", "sgsc"),
    .elcf4r_example_size_row("elcf4r_lcl_example", "lcl"),
    .elcf4r_example_size_row("elcf4r_refit_example", "refit")
  )
)

if (length(example_sizes) == 0L) {
  data.frame()
} else {
  do.call(rbind, example_sizes)
}

## -----------------------------------------------------------------------------
benchmark_summary <- Filter(
  Negate(is.null),
  list(
    .elcf4r_benchmark_summary("elcf4r_iflex_benchmark_results", "iflex"),
    .elcf4r_benchmark_summary("elcf4r_storenet_benchmark_results", "storenet"),
    .elcf4r_benchmark_summary("elcf4r_lcl_benchmark_results", "lcl"),
    .elcf4r_benchmark_summary("elcf4r_refit_benchmark_results", "refit")
  )
)

if (length(benchmark_summary) == 0L) {
  data.frame()
} else {
  benchmark_summary <- do.call(rbind, benchmark_summary)
  benchmark_summary[, c("dataset", "method", "nmae", "nrmse", "smape", "mase")]
}

## -----------------------------------------------------------------------------
if (exists("elcf4r_iflex_example", inherits = FALSE)) {
  iflex_segments <- elcf4r_build_daily_segments(
    elcf4r_iflex_example,
    carry_cols = c("dataset", "participation_phase", "price_signal")
  )

  dim(iflex_segments$segments)
  head(iflex_segments$covariates[, c("entity_id", "date", "temp_mean", "price_signal")])
} else {
  data.frame()
}

## -----------------------------------------------------------------------------
if (exists("elcf4r_sgsc_example", inherits = FALSE)) {
  sgsc_segments <- elcf4r_build_daily_segments(
    elcf4r_sgsc_example,
    carry_cols = "dataset"
  )

  dim(sgsc_segments$segments)
  head(sgsc_segments$covariates[, c("entity_id", "date", "temp_mean")])
} else {
  data.frame()
}

## -----------------------------------------------------------------------------
if (exists("elcf4r_lcl_example", inherits = FALSE)) {
  tiny_index <- elcf4r_build_benchmark_index(
    elcf4r_lcl_example,
    carry_cols = "dataset"
  )

  tiny_benchmark <- elcf4r_benchmark(
    panel = elcf4r_lcl_example,
    benchmark_index = tiny_index,
    methods = c("gam", "kwf"),
    cohort_size = 1,
    train_days = 10,
    test_days = 2,
    include_predictions = FALSE
  )

  tiny_benchmark$results[, c("entity_id", "method", "test_date", "nmae", "mase")]
} else {
  data.frame()
}

