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

## ----setup--------------------------------------------------------------------
library(data4health)

## ----inspect_data-------------------------------------------------------------
head(example_dataset)

## ----see_example_files--------------------------------------------------------
d4h_example()

## ----load_single--------------------------------------------------------------
csv_path <- d4h_example("example_dataset.csv")
head(d4h_load(csv_path))

## ----load_multiple------------------------------------------------------------
rds2023_path <- d4h_example("example_dataset_2023.rds")
rds2024_path <- d4h_example("example_dataset_2024.rds")

# to check that both files were loaded correctly,
# we will check the number of rows of each file
nrow(d4h_load(rds2023_path))
nrow(d4h_load(rds2024_path))

# the number of rows corresponds to the sum of the rows in both files:
nrow(d4h_load(c(rds2023_path, rds2024_path)))

## ----load_sheet---------------------------------------------------------------
xlsx_path <- d4h_example("example_dataset.xlsx")
d4h_load(xlsx_path)

# as you can see all dates in DATE_INI, are from 2023

## ----load_specific_sheet------------------------------------------------------
d4h_load(xlsx_path, sheet = "year2024")

# now, all dates in DATE_INI are from 2024

## ----header-------------------------------------------------------------------
head(d4h_load(rds2023_path)) # the function automatically detects the header

# forcing the first row as header
head(d4h_load(rds2023_path, header = TRUE)) 

# forcing the first row to be included in the data
head(d4h_load(rds2023_path, header = FALSE))

## ----colnames_1---------------------------------------------------------------
colnames(example_dataset)

## ----cols_to_remove-----------------------------------------------------------
dengue_clean <- d4h_clean(data = example_dataset,
                          cols_to_remove = c("CODE_GEO", "LOCALCOD", "NOTES"))

## ----colnames_2---------------------------------------------------------------
colnames(dengue_clean)

## ----rename_columns_1---------------------------------------------------------
dengue_clean <- d4h_clean(data = dengue_clean,
                          rename_columns = "lower")

## ----colnames_3---------------------------------------------------------------
colnames(dengue_clean)

## ----rename_columns_2---------------------------------------------------------
dengue_clean <- d4h_clean(data = dengue_clean,
                          rename_columns = c(name_geo   = "region",
                                            date_brthd = "birthday_date",
                                            date_ini = "onset_date"))

## ----colnames_4---------------------------------------------------------------
colnames(dengue_clean)

## -----------------------------------------------------------------------------
colSums(is.na(dengue_clean))

## ----remove_rows_missing------------------------------------------------------
dengue_clean <- d4h_clean(data = dengue_clean,
                          remove_rows_missing = c("onset_date", "region"))

## -----------------------------------------------------------------------------
nrow(dengue_clean)

## ----NA_percentage------------------------------------------------------------
colMeans(is.na(dengue_clean)) * 100

## ----threshold_remove---------------------------------------------------------
dengue_clean <- d4h_clean(data = dengue_clean, threshold_remove = 50)

## ----rename_categories--------------------------------------------------------
dengue_clean <- d4h_clean(data = dengue_clean,
                          rename_categories = list(
                            sexo           = c("M" = "Male",   "F" = "Female"),
                            dengue_type   = c("1" = "Dengue without warning signs",
                                              "2" = "Dengue with warning signs",
                                              "3" = "Severe dengue"),
                            hospitalised  = c("S" = "Yes",    "N" = "No")
                          ))

## ----date_to_week-------------------------------------------------------------
dengue_clean <- d4h_clean(data = dengue_clean,
    date_to_weekdate = "onset_date",
    date_to_weeknumber = "onset_date",
    date_to_monthdate = "onset_date",
    date_to_monthnumber = "onset_date",
    date_to_yearnumber = "onset_date"
)

## -----------------------------------------------------------------------------
dengue_clean[1, ]

## ----clean_all----------------------------------------------------------------
dengue_clean <- d4h_clean(
  data = example_dataset,
  cols_to_remove     = c("CODE_GEO", "LOCALCOD", "NOTES"),
  rename_columns = c(
    DATE_BRTHD         = "birthdate",
    DATE_INI         = "onset_date",
    NAME_GEO            = "region",
    SEXO              = "sex",
    DENGUE_TYPE       = "dengue_type",
    HOSPITALISED     = "hospitalised",
    RESULT_LABORATORY     = "lab_result",
    SEROTYPE      = "serotype"
  ),
  remove_rows_missing = c("onset_date", "region"),
  threshold_remove    = 50,
  rename_categories   = list(
    sex               = c(M = "Male",   F = "Female"),
    dengue_type       = c("1" = "Dengue without warning signs",
                          "2" = "Dengue with warning signs",
                          "3" = "Severe dengue"),
    hospitalised      = c(S = "Yes",    N = "No")),
  date_to_weekdate    = "onset_date",
  date_to_weeknumber  = "onset_date",
  date_to_monthdate   = "onset_date",
  date_to_monthnumber = "onset_date",
  date_to_yearnumber  = "onset_date"
)

# the resulting dataframe
head(dengue_clean)

## ----filter-------------------------------------------------------------------
dengue_filtered <- d4h_filter(
  data = dengue_clean,
  lab_result = list(include = "Positive"),
  onset_date = list(during = as.Date(c("2023-01-01", "2023-12-31"))),
  region     = list(exclude = "Arendelle")
)

nrow(dengue_filtered)

## ----agg-weekly---------------------------------------------------------------
weekly_by_region <- d4h_aggregate(
  data      = dengue_filtered,
  time_col  = "onset_date_weekdate",
  space_col = "region"
)

head(weekly_by_region)

## ----agg-monthly--------------------------------------------------------------
monthly_by_type <- d4h_aggregate(
  data      = dengue_filtered,
  time_col  = "onset_date_monthdate",
  space_col = "region",
  add_col   = "dengue_type"
)

head(monthly_by_type, 12)

## ----agg-complete-------------------------------------------------------------
all_weeks <- seq(
  from = as.Date("2023-01-01"),
  to   = as.Date("2023-12-31"),
  by   = "week"
)

weekly_complete <- d4h_aggregate(
  data       = dengue_filtered,
  time_col   = "onset_date_weekdate",
  space_col  = "region",
  all_times  = all_weeks,
  all_spaces = c("Lima", "Cusco", "Loreto")
)

# confirm zeros are present
sum(weekly_complete$cases == 0)

## ----save, eval = FALSE-------------------------------------------------------
# # Save the weekly aggregation as a CSV for sharing
# d4h_save(
#   data   = weekly_complete,
#   name   = "dengue_weekly_2023"
# )
# 
# # Save the cleaned individual-level data as RDS for fast reloading in R
# d4h_save(
#   data   = dengue_filtered,
#   name   = "dengue_analysis_clean",
#   format = "rds"
# )
# 
# # Save for teams that use Excel
# d4h_save(
#   data   = monthly_by_type,
#   name   = "dengue_monthly_by_type",
#   format = "xls"
# )

## ----pipeline, eval = FALSE---------------------------------------------------
# d4h_load(csv_path)                |>
#   d4h_clean(
#     cols_to_remove     = c("CODE_GEO", "LOCALCOD", "NOTES"),
#     rename_columns     = c(
#       DATE_BRTHD        = "birthdate",
#       DATE_INI          = "onset_date",
#       NAME_GEO          = "region",
#       SEXO              = "sex",
#       DENGUE_TYPE       = "dengue_type",
#       HOSPITALISED      = "hospitalised",
#       RESULT_LABORATORY = "lab_result",
#       SEROTYPE          = "serotype"
#     ),
#     remove_rows_missing = c("onset_date", "region"),
#     threshold_remove    = 50,
#     rename_categories   = list(
#       sex               = c(M = "Male",   F = "Female"),
#       dengue_type       = c("1" = "Dengue without warning signs",
#                             "2" = "Dengue with warning signs",
#                             "3" = "Severe dengue"),
#       hospitalised      = c(S = "Yes",    N = "No")),
#     date_to_weekdate    = "onset_date",
#     date_to_weeknumber  = "onset_date",
#     date_to_monthdate   = "onset_date",
#     date_to_monthnumber = "onset_date",
#     date_to_yearnumber  = "onset_date"
#   )                                              |>
#   d4h_filter(
#     lab_result = list(include = "Positive"),
#     onset_date = list(during = as.Date(c("2023-01-01", "2023-12-31"))),
#     region     = list(exclude = "Arendelle")
#   )                                              |>
#   d4h_aggregate(
#     time_col   = "onset_date_weekdate",
#     space_col  = "region",
#     all_times  = all_weeks,
#     all_spaces = c("Lima", "Cusco", "Loreto")
#   )                                              |>
#   d4h_save(name = "dengue_weekly_confirmed", format = "csv")

