data-raw/wiot.R

################################################################################
#                                                                              #
# This script downloads input output tables from the World Input Output        #
# Database and adapts them for use in our simulations. See the vignette        #
# "preparing_wiod_data" for details.                                           #
#                                                                              #
################################################################################

# sections marked by four "-" are reused in the vignette

# Libraries ====================================================================

## ---- setup

library(dplyr)
library(tidyr)
# if not yet done install the iotr package
# library(devtools)
# install_github(repo = "okrebs/iotr")
library(iotr)

## ----

# Download the WIOD ============================================================

## ---- get_wiot

wiot <- get_wiot(years = c(2000, 2014)) %>%
  wiot2long() %>%
  filter(Country != "TOT") %>%
  select(origin = Country, sector = RNr, destination, use, flow,
         year = Year)

## ----

# Apply Modifications to WIOD ==================================================

## ---- adapt

wiot <- wiot %>%
  # apply the respective function to the IO table from each year separately
  nest_by(year, .key = "iot") %>%
  mutate(iot = list(rm_negative_vad(iot, category_to_scale = 57) %>%
                      rm_dynamics(dynamic_categories = c(60, 61),
                                  category_to_scale = 57)))

# after adapting dynamic use categories combine all final use types into one
wiot <- wiot %>%
  unnest(cols = c(iot)) %>%
  mutate(use = ifelse(use > 57, 57L, as.integer(use))) %>%
  group_by(year, origin, sector, destination, use) %>%
  summarise(flow = sum(flow), .groups = "drop") %>%
  nest(iot = c(origin, sector, destination, use, flow))

wiot <- wiot %>%
  rowwise() %>%
  # wiot is in millions so 1e-6 is $1
  mutate(iot_nozero = list(gen_own_trade(iot, max_replace = 1e-6))) %>%
  pivot_longer(c(iot, iot_nozero),
               names_to = "includes_zeros",
               names_transform = list("includes_zeros" = ~.x == "iot"),
               values_to = "iot")

# we only use the WIOD from 2000 as a robustness check and so do not need the
# version that includes zeros
wiot <- wiot %>% filter(year != 2000 | includes_zeros == FALSE)

## ----

# Save Data ====================================================================

usethis::use_data(wiot, overwrite = TRUE, version = 3)
okrebs/DeGVC documentation built on March 15, 2021, 1:17 a.m.