# = SECO, machine-readable data ================================================ library(tidyverse) library(jsonlite) library(tsbox) # - Examples: how to use machine-readable data from SECO ----------------------- # -- read most recent data from the SECO-website ---- # read time series data url_csv <- "https://www.seco.admin.ch/dam/seco/de/dokumente/Wirtschaft/Wirtschaftslage/BIP_Daten/ch_seco_gdp_csv.csv.download.csv/ch_seco_gdp.csv" data <- read_csv(url_csv) # read time series meta data url_json <- "https://www.seco.admin.ch/dam/seco/de/dokumente/Wirtschaft/Wirtschaftslage/BIP_Daten/ch_seco_gdp_json.txt.download.txt/ch_seco_gdp_json.txt" meta <- read_json(url_json) # check publication date meta$updated_utc # -- compile information table ---- # chose your preferred language: "en", "de", "fr", "it" mylang <- "en" translate_tbl <- function(x) { enframe( sapply(meta$labels[[x]], function(e) e[[mylang]]), value = paste0(x, "_label") ) } # information table info <- data %>% select(structure, type, seas_adj) %>% unique() %>% left_join(translate_tbl("structure"), by = c("structure" = "name")) %>% left_join(translate_tbl("type"), by = c("type" = "name")) %>% left_join(translate_tbl("seas_adj"), by = c("seas_adj" = "name")) # show all variables (time series) published unique(info$structure) # time series names unique(info$structure_label) # time series descriptions # show information on the variable of interest info %>% filter(structure == "gdp") # -- plot variables of interest ---- # plot real gdp, original data and all available variants of adjusted data data %>% filter(structure == "gdp", type == "real") %>% ts_plot() # plot real gdp; seasonally, calendar and sports event adjusted; level data %>% filter(structure == "gdp", type == "real", seas_adj == "cssa") %>% ts_plot() # plot real gdp; seasonally, calendar and sports event adjusted; q-o-q growth rates data %>% filter(structure == "gdp", type == "real", seas_adj == "cssa") %>% ts_pc() %>% ts_plot() # -- analyze gdp growth, expenditure approach ---- # expenditure approach components to gdp growth v_comp <- c("cons", "inv", "exp", "imp") # latest year containing data y_act <- max(data$date) %>% substr(., 1, 4) %>% as.numeric() data_comp <- data %>% filter(seas_adj == "csa", type == "gc_q", structure %in% v_comp) %>% ts_span(start = y_act - 10) data_gdp <- data %>% filter(seas_adj == "csa", type == "real", structure == "gdp") %>% ts_pc() %>% ts_span(start = y_act - 10) ggplot(data_comp, aes(x = date, y = value)) + geom_col(aes(fill = structure), position = "stack") + geom_line(data = data_gdp)