## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  message = FALSE,
  fig.width = 8,
  fig.height = 5
)

library(ggplot2)
library(dplyr)
library(knitr)
library(kableExtra)

pair_results  <- readRDS("data/pair_results.rds")
plot_df2       <- readRDS("data/plot_df2.rds")
table_diag    <- readRDS("data/table_diag.rds")
section_sizes <- readRDS("data/section_sizes.rds")
diag_table    <- readRDS("data/diag_table.rds")
plot_df_ess <- readRDS("data/plot_df_ess.rds")

## ----setup, include=FALSE-----------------------------------------------------
library(EcoEnsemble)

## ----eval=F-------------------------------------------------------------------
# #generate priors
# priors <- EnsemblePrior(4)
# 
# #run the model
# fit_sample <- fit_ensemble_model(observations = list(SSB_obs, Sigma_obs),
#                                  simulators = list(list(SSB_ewe, Sigma_ewe, "EwE"),
#                                                    list(SSB_lm,  Sigma_lm,  "LeMans"),
#                                                    list(SSB_miz, Sigma_miz, "mizer"),
#                                                    list(SSB_fs,  Sigma_fs,  "FishSUMS")),
#                                  priors = priors,
#                                  sampler = "kalman")

## ----echo=F, out.width = "75%",fig.align ='center'----------------------------

# ggplot2::ggplot(plot_df_ess %>%
#     dplyr::filter(setup == "Model + hierarchical") %>%
#     dplyr::mutate(sampler = dplyr::if_else(sampler == "E", "Explicit", sampler))%>%mutate(sampler = dplyr::if_else(sampler == "K", "Kalman", sampler))
#                 , ggplot2::aes(x = setup, y = value, fill = sampler)) +
#   ggplot2::geom_boxplot(
#     position = ggplot2::position_dodge(width = 0.72),
#     width = 0.58,
#     outlier.shape = NA
#   ) +
#   ggplot2::facet_wrap(metric ~ ., scales = "free_y", nrow=1) +
#   ggplot2::labs(
#     x = NULL,
#     y = NULL,
#     fill = "Sampler"
#   ) +
#   ggplot2::theme_bw(base_size = 11) +
#   ggplot2::theme(
#     legend.position = "top",
#     strip.placement = "outside",
#     strip.background = ggplot2::element_blank(),
#     panel.spacing.y = grid::unit(0.15, "lines"),
#     plot.margin = grid::unit(c(0.3, 0.4, 0.2, 0.2), "cm")
#   )+ ggplot2::theme(
#     axis.title.x = ggplot2::element_blank(),
#     axis.text.x  = ggplot2::element_blank(),
#     axis.ticks.x = ggplot2::element_blank()
#   )

knitr::include_graphics("data/explicit_plot1.png")


## ----echo=F, out.width = "85%",fig.align ='center'----------------------------
knitr::include_graphics("data/explicit_plot4.png")

## ----echo=F-------------------------------------------------------------------
table_diag %>%
    select(-section) %>%
    mutate(across(where(is.numeric), ~ formatC(., format = "fg", digits = 3))) %>%
    kbl(
        booktabs = TRUE,
        align = c("l", rep("r", 8)),
        col.names = c("Diagnostic", "E", "K", "E", "K", "E", "K", "E", "K")
    )  %>%
    add_header_above(c(
        " " = 1,
        "Drivers" = 2,
        "Model" = 2,
        "Model + hierarchical" = 2,
        "Drivers + hierarchical" = 2
    )) %>%
    kable_styling(
        full_width = FALSE,
        font_size = 10,
        latex_options = "hold_position"
    ) %>%
    row_spec(0, bold = TRUE) %>%
    pack_rows("Run time", 1, section_sizes["Run time"]) %>%
    pack_rows(
        "Convergence",
        section_sizes["Run time"] + 1,
        section_sizes["Run time"] + section_sizes["Convergence"]
    ) %>%
    pack_rows(
        "Bulk diagnostics",
        section_sizes["Run time"] + section_sizes["Convergence"] + 1,
        section_sizes["Run time"] + section_sizes["Convergence"] + section_sizes["Bulk diagnostics"]
    ) %>%
    pack_rows(
        "Tail diagnostics",
        section_sizes["Run time"] + section_sizes["Convergence"] + section_sizes["Bulk diagnostics"] + 1,
        sum(section_sizes)
    ) %>%
    column_spec(1, width = "4.2cm") %>%
    column_spec(2:9, width = "1.5cm")


## ----echo=F-------------------------------------------------------------------
diag_table %>%
    mutate(across(where(is.numeric), ~ formatC(., format = "fg", digits = 3))) %>%
    knitr::kable(
        booktabs = TRUE,
        align = c("l", rep("r", 8)),
        col.names = c("Diagnostic", "E", "K", "E", "K", "E", "K", "E", "K")
    ) %>%
  kableExtra::add_header_above(c(
    " " = 1,
    "Drivers" = 2,
    "Model" = 2,
    "Model + hierarchical" = 2,
    "Drivers + hierarchical" = 2
  )) %>%
  kableExtra::kable_styling(
    full_width = FALSE,
    font_size = 10,
    latex_options = "hold_position"
  ) %>%
  kableExtra::column_spec(1, width = "4.2cm") %>%
  kableExtra::column_spec(2:9, width = "1.5cm")

