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

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

url <- "https://sdmx.oecd.org/public/rest/data/OECD.SDD.NAD,DSD_NAMAIN1@DF_QNA_EXPENDITURE_NATIO_CURR,1.1/Q.Y.........L.."
qna_dat <- read_sdmx(path = url)

qna_dat <- qna_dat %>%
  filter(REF_AREA %in% c("DEU", "FRA"),
         TRANSACTION %in% c("B1GQ", "P3", "P51G", "P6", "P7"),
         PRICE_BASE %in% c("L"))

# check
head(qna_dat)
tail(qna_dat)

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

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

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

# check
head(qna_dat)
tail(qna_dat)

# transform data ----
qna_dat <- qna_dat %>%
  left_join(
    qna_dat %>%
      filter(TRANSACTION == "B1GQ") %>%
      select(REF_AREA, ObsDimension, ObsValue) %>%
      rename(B1GQ_lev = ObsValue),
    by = c("REF_AREA", "ObsDimension")
  ) %>%
  group_by(REF_AREA, TRANSACTION) %>%
  arrange(ObsDimension) %>% 
  mutate(
    lev = ObsValue,
    gro = (lev - lag(lev))/lag(lev)*100,
    sha = (lev)/B1GQ_lev*100,
    gro_con = gro*lag(lev)/lag(B1GQ_lev)
  )

# check 
head(qna_dat)
tail(qna_dat)

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