## ----setup, include=FALSE-----------------------------------------------------
library(dplyr) # Ensure pipe operator is available


## -----------------------------------------------------------------------------
#| eval: false
# pak::pak("whocov/nowcastr")

## -----------------------------------------------------------------------------
library(nowcastr)


## -----------------------------------------------------------------------------
print(nowcast_demo)


## -----------------------------------------------------------------------------
#| echo: false
#| eval: false
# # generate_test_data(
# #   n_reportdates = 5,
# #   n_delays = 5
# # )


## ----fig.width=9, fig.asp=5.5/10, out.width="100%"----------------------------
nowcast_demo %>%
  plot_nc_input(
    option = "triangle",
    col_date_occurrence = date_occurrence,
    col_date_reporting = date_report,
    col_value = value,
    group_cols = "group" # use a vector for multiple columns: c("column1", "column2"... )
  )


## ----fig.width=9, fig.asp=5.5/10, out.width="100%"----------------------------
nowcast_demo %>%
  plot_nc_input(
    option = "millipede",
    col_date_occurrence = date_occurrence,
    col_date_reporting = date_report,
    col_value = value,
    group_cols = "group"
  )


## ----fig.width=9, fig.asp=5.5/10, out.width="100%"----------------------------
data_filled <- nowcast_demo %>%
  fill_future_reported_values(
    col_date_occurrence = date_occurrence,
    col_date_reporting = date_report,
    col_value = value,
    group_cols = "group",
    max_delay = "auto"
  )
data_filled %>%
  plot_nc_input(
    option = "triangle",
    col_date_occurrence = date_occurrence,
    col_date_reporting = date_report,
    col_value = value,
    group_cols = "group"
  )


## -----------------------------------------------------------------------------
nc_obj <-
  data_filled %>%
  nowcast_cl(
    col_date_occurrence = date_occurrence,
    col_date_reporting = date_report,
    col_value = value,
    group_cols = "group",
    time_units = "weeks",
    # max_delay = 5,
    # max_reportunits = 8,
    do_model_fitting = TRUE
  )


## -----------------------------------------------------------------------------
S7::prop_names(nc_obj)


## -----------------------------------------------------------------------------
nc_obj@results # Final nowcasted values
nc_obj@delays # Summarised completeness values by delay
nc_obj@completeness # Detailed completeness estimates


## ----fig.width=9, fig.asp=5.5/10, out.width="100%"----------------------------
#| warning: false
# Delay distribution
plot(nc_obj, which = "delays") +
  ggplot2::labs(
    caption = NULL,
    subtitle = paste0("From data reported on: ", max(data_filled$date_report))
  )

# Nowcast time series
plot(nc_obj, which = "results") +
  ggplot2::labs(
    caption = NULL,
    subtitle = paste0("From data reported on: ", max(data_filled$date_report))
  )


## -----------------------------------------------------------------------------
#| eval: false
# nowcast_explore(nc_obj)


## ----calculate_retro_score----------------------------------------------------
# Calculate retro-scores
retroscores <- nowcast_demo %>%
  calculate_retro_score(
    col_date_occurrence = date_occurrence,
    col_date_reporting = date_report,
    col_value = value,
    group_cols = "group"
  )
print(retroscores)


## ----fig.width=9, fig.asp=5.5/10, out.width="100%"----------------------------
retroscores %>%
  ggplot2::ggplot(ggplot2::aes(y = stats::reorder(group, retro_score), x = retro_score)) +
  ggplot2::geom_bar(stat = "identity", fill = "dodgerblue1") +
  theme_nowcastr() +
  ggplot2::scale_x_continuous(
    limits = c(0, 1),
    # labels = scales::label_percent()
  ) +
  ggplot2::labs(
    y = "Group",
    x = "Retro-Score"
  )


## ----rm_repeated_values-------------------------------------------------------
# Remove duplicate reported values (same value and higher reporting date)
nowcast_demo %>%
  rm_repeated_values(
    col_date_occurrence = date_occurrence,
    col_date_reporting = date_report,
    col_value = value,
    group_cols = "group"
  )

