
# -> 0) set up
rm(list=ls())

library(OECD)
library(dplyr)
library(zoo)
library(ggplot2)
library(plotly)

# -> 0) load data structure for qna
dataset <- "QNA"
url <- paste0("https://stats.oecd.org/restsdmx/sdmx.ashx/GetDataStructure/", dataset)
data_structure <- readsdmx::read_sdmx(url)
data_structure %>%
  filter(id == c("CL_QNA_SUBJECT"),
         value %in% c("B1G", "B1GVA", "B1GVB_E", "B1GVF", "B1GVG_I", "B1GVJ", "B1GVK", "B1GVL", "B1GVM_N", "B1GVO_Q", "B1GVR_U")) %>%
  select(value, en_description) %>%
  kable(col.names = c("Code", "Description"))

# -> 1) load data

dataset <- "QNA"
start_time <- as.Date("2010-01-01")
end_time <- as.Date("2023-10-01")
# Note: Q1: 2020-01-01; Q2: 2020-04-01; Q3: 2020-07-01; Q4: 2020-10-01
filter = list(LOCATION = c("DEU", "FRA"),
              SUBJECT = c("B1G", "B1GVA", "B1GVB_E", "B1GVF", "B1GVG_I", "B1GVJ", "B1GVK", "B1GVL", "B1GVM_N", "B1GVO_Q", "B1GVR_U"),
              MEASURE = c("LNBQRSA"),
              FREQUENCY = c("Q"))

qna_dat <- get_dataset(dataset, filter, start_time, end_time)

# check
head(qna_dat)
tail(qna_dat)

# save original data
save(qna_dat, file = "./01_data/qna_dat_00.RData")

# handle values
class(qna_dat$ObsValue)
qna_dat$ObsValue <- as.numeric(qna_dat$ObsValue)

# handle dates
class(qna_dat$Time)
qna_dat$TIME <- as.yearqtr(qna_dat$Time, "%Y-Q%q")

# check
head(qna_dat)
tail(qna_dat)

# transform data ----
qna_dat <- qna_dat %>%
  left_join(
    qna_dat %>%
      filter(SUBJECT == "B1G") %>%
      select(LOCATION, Time, ObsValue) %>%
      rename(B1G_lev = ObsValue),
    by = c("LOCATION", "Time")
  ) %>%
  group_by(LOCATION, SUBJECT) %>%
  mutate(
    lev = ObsValue,
    gro = (lev - lag(lev))/lag(lev)*100,
    sha = (lev)/B1G_lev*100,
    gro_con = gro*lag(lev)/lag(B1G_lev)
  )

# check 
head(qna_dat)
tail(qna_dat)

# save transformed data ----
save(qna_dat, file = "./01_data/qna_dat_01.RData")
