## ---------------------------
##
## Script name: 00_explore.R
##
## Purpose of script: Clean and explore Laos Dengue data provided by DHIS2
## Author: Daniela Lührsen & Giovenale Moirano
## Date Created: 2024-07-17
## Email: daniela.luhrsen@bsc.es
##
## ---------------------------

## load up the packages
packages <- c("ggplot2", "dplyr", "tidyr", "lubridate", "sf","scales")
lapply(packages, require, character.only = TRUE)

## ---------------------------

##############################################################
##### CLEANING ----------------------------------------------
##############################################################
# load data
data <- read.csv("data/laos/dengue.csv")

# fix dates
data$year <- substr(data$periodid, 1, 4)
data$month <- substr(data$periodid, 5, 6)
data$date <- as.Date(paste0(data$year, data$month, "01"), format = "%Y%m%d")



# fix orgunit
colnames(data) <- gsub("^X\\d+\\.", "", colnames(data))
colnames(data)[colnames(data) == "Vientiane.Capital"] <- "Vientiane Capital"

# select only necessary columns
data <- data[, c("year", "month", "date", "Xaisomboun", "Attapu",
                 "Champasak", "Xekong", "Salavan", "Savannakhet", "Khammouan",
                 "Bolikhamxai", "Vientiane", "Xiangkhouang", "Xainyabouli",
                 "Houaphan", "Louangphabang", "Bokeo", "Oudomxai",
                 "Louangnamtha", "Phongsali", "Vientiane Capital")]

data <- data %>%
  pivot_longer(cols = c("Xaisomboun", "Attapu", "Champasak", "Xekong",
                        "Salavan", "Savannakhet", "Khammouan", "Bolikhamxai",
                        "Vientiane", "Xiangkhouang", "Xainyabouli", "Houaphan",
                        "Louangphabang", "Bokeo", "Oudomxai", "Louangnamtha", 
                        "Phongsali", "Vientiane Capital"),
               names_to = "province",
               values_to = "dengue_cases")

data$dengue_cases[is.na(data$dengue_cases)] <- 0

# save clean data
write.csv(data, "data/laos/laos_dengue_2015-2023.csv", row.names = FALSE)



##############################################################
##### Compute Average Yearly Incidence -----------------------
##############################################################

data <- read.csv("data/laos/laos_dengue_2015-2023.csv")
pop <- read.csv("data/laos/laos_population_2022.csv")
data <- merge(data, pop, by = "province")
data$date <- as.Date(data$date)

data$incidence <- data$dengue_cases / data$population *100000

# total cases
total_cases <- aggregate(data$dengue_cases,
                         by = list(orgunit = data$province),
                         FUN = sum, na.rm = TRUE)
# Add pop to total cases

total_cases <- merge(total_cases, pop, by.x = "orgunit", by.y ="province")

# Compute avg yearly incidence

total_cases$avg_y_inc <- total_cases$x/(total_cases$population*12) * 100000



##############################################################
##### Visualise Time Series-------------------------------------
##############################################################

# plot a timeseries
ggplot(data, aes(x = date, y = incidence, group=1)) +
  geom_line() +
  facet_wrap(~ province, ncol = 3) +
  labs(title = "",
       x = "Date",
       y = "Incidence (per 100,000 inhabitants)") +
  theme_minimal() +
  theme(legend.position = "bottom",
        axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_y_continuous(limits = range(data$incidence, na.rm = TRUE)) +  # Ensure the same y-axis range for all plots
  scale_x_date(date_labels = "%Y", date_breaks = "1 year")   # Ensure the same y-axis range for all plots


##############################################################
##### Visualise Seasonality-------------------------------------
##############################################################

data$month <- format(data$date, "%m")
data$month <- factor(data$month, levels = sprintf("%02d", 1:12))

# Create the plot
ggplot(data, aes(x = month, y = incidence, color = as.factor(year), group = year)) +
  geom_line() +
  facet_wrap(~ province, ncol = 3) +
  labs(title = "",
       x = "Month",
       y = "Incidence (per 100,000 inhabitants)",
       color = "Year") +
  theme_minimal() +
  theme(legend.position = "right",
        axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_y_continuous(limits = range(data$incidence, na.rm = TRUE)) +  # Ensure the same y-axis range for all plots
  scale_x_discrete(labels = month.abb)  # Use month abbreviations for x-axis labels



##############################################################
##### Visualise Maps-------------------------------------
##############################################################


orgunits_shapefile <- sf::st_read("data/laos/shapefile/gadm41_LAO1_cleaned.shp")
plot(orgunits_shapefile)


data_year <- data %>% 
  group_by(year, province) %>%
  summarise(population = mean(population),
            dengue_cases  = sum(dengue_cases )) %>%
  mutate(inc = dengue_cases/population * 100000)

orgunits_shapefile<- orgunits_shapefile %>% 
  left_join(data_year, by = c("NAME_1" = "province"))


orgunits_shapefile <- orgunits_shapefile %>%
  mutate(inc_decile = cut(inc, breaks = quantile(inc, probs = seq(0, 1, by = 0.1),
                                                 na.rm = TRUE), include.lowest = TRUE))

inc_decile_labels <- quantile(orgunits_shapefile$inc, probs = seq(0, 1, by = 0.1),
                              na.rm = TRUE)

inc_decile_labels <- paste0(round(head(inc_decile_labels, -1), 2), " - ",
                            round(tail(inc_decile_labels, -1), 2))

# Apply the labels to the decile breaks
orgunits_shapefile <- orgunits_shapefile %>%
  mutate(inc_decile = cut(inc, 
                          breaks = quantile(inc, probs = seq(0, 1, by = 0.1), na.rm = TRUE), 
                          include.lowest = TRUE,
                          labels = inc_decile_labels))

# Create the map
win.graph()
orgunits_shapefile %>%
  ggplot(aes(fill = inc_decile)) +
  geom_sf() +
  coord_sf(expand = F) +
  theme_minimal() +
  facet_wrap(~ year, nrow=2) +
  scale_fill_viridis_d(name = "IR (per 100,000)") +
  labs(title = "Dengue Incidence Rate by Province and Year",
       fill = "Incidence Rate (per 100,000)") +
  theme(axis.text.x = element_text(hjust = 1, vjust = 1,size = 7))+
  scale_x_continuous( breaks=c(101,103,105,107))+
  theme(axis.text.y = element_text(hjust = 1, vjust = 1,size = 7))
