## ----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.asp=5.5/10-----------------------------------------------------------
nowcast_demo %>%
  plot_nc_input(
    option = "triangle",
    col_date_occurrence = date_occurrence,
    col_date_reporting = date_report,
    col_value = value,
    group_cols = "group"
  )


## -----------------------------------------------------------------------------
nowcast_demo %>%
  plot_nc_input(
    option = "millipede",
    col_date_occurrence = date_occurrence,
    col_date_reporting = date_report,
    col_value = value,
    group_cols = "group"
  )


## ----fig.asp=5.5/10-----------------------------------------------------------
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",
    do_model_fitting = TRUE
  )


## -----------------------------------------------------------------------------
S7::prop_names(nc_obj)


## ----slots--------------------------------------------------------------------
nc_obj@results # Final nowcasted values
nc_obj@delays # Delay distribution
nc_obj@completeness # Data with completeness estimates
str(nc_obj@params) # Parameters used


## ----plots--------------------------------------------------------------------
#| warning: false
plot(nc_obj, which = "delays") # Delay distribution
plot(nc_obj, which = "results") # Nowcast time series


## -----------------------------------------------------------------------------
#| eval: false
# explore_nowcast(nc_obj)


## ----grouped------------------------------------------------------------------
#| eval: false

# nowcast_cl(
#   # ...
#   group_cols = c("region", "disease")
# )


## ----calculate_retro_score----------------------------------------------------
# Calculate retro-scores (= number of actual value changes / max possible value changes)
nowcast_demo %>%
  calculate_retro_score(
    col_date_occurrence = date_occurrence,
    col_date_reporting = date_report,
    col_value = value,
    group_cols = c("group")
  )


## ----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 = c("group")
  )

