Skip to content

Instantly share code, notes, and snippets.

@jasonrwang
Last active March 9, 2024 16:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jasonrwang/fed65b0766b442ae4dee8ed3efddf6a9 to your computer and use it in GitHub Desktop.
Save jasonrwang/fed65b0766b442ae4dee8ed3efddf6a9 to your computer and use it in GitHub Desktop.
Analyze World Input-Output Tables (inter-regional and inter-industry economic activity)

Analyze World Input-Output Tables

Quite simply, this R script can be used to sort through the world input-output tables provided by Timmer et al. at the World Input-Output Database. All the column numbers I've used here should match for the provided 2016 RData file.

This work was completed for an assignment in the EPA1223 Macro-economics for Policy-Analysis course. It's now been validated against (and corrected for) the answers provided by Dr. Enno Schröder (who taught us the input-output model).

I'm putting this file online in the hope that it might save someone some time in dealing with the WIOD dataset and for research. None of the analysis should be used by anyone to complete their own assignments.

# Load nice packages
library(tidyr)
library(dplyr)
# Load in data
# Data source: Timmer, M. P., Dietzenbacher, E., Los, B., Stehrer, R. and de Vries, G. J. (2015),
# "An Illustrated User Guide to the World Input–Output Database: the Case of Global Automotive Production",
# Review of International Economics., 23: 575–605)
load("WIOT2014_October16_ROW.RData") # Loads as var "wiot"
wiot = data.frame(wiot) # Change to tidyr data frame type
# Re-index the row names in the same way as columns
wiot <- wiot %>% mutate(Code = paste(Country, RNr, sep=""))
# Column 1-5 are row descriptions, the countries' data end at col 2469, and 2690 is the total output
# Row 2470 is value added
# Therefore, let's subset the data into our matrices.
unwanted_cols <- c("IndustryCode", "IndustryDescription", "Country", "RNr", "Year", "TOT")
# Inter-industry matrix
Z <- wiot %>% slice(1:2464) %>% select(Code, 6:2469)
# Total Output
x <- wiot %>% select(c(Code,TOT)) %>% slice(1:2464) # x behaves differently than v, so we need to use tidyr here
# Value-add
v <- wiot %>% slice(2470) %>% select(Code, 6:2469)
# And let's calculate a few other useful variables
i = 0
x_hat <- as.matrix(diag(as.vector(x$TOT)))
x_hat_inv <- x_hat^-1 # We have to create this so we can clean out the infinite values
is.na(x_hat_inv) <- sapply(x_hat_inv, is.infinite)
x_hat_inv[is.na(x_hat_inv)] <- 0
Z_mat <- as.matrix(Z %>% select(-Code))
A <- Z_mat %*% x_hat_inv
is.na(A) <- sapply(A, is.infinite)
A[is.na(A)] <- 0
I <- diag(dim(A)[1])
L <- solve(I - A) # Must inverse, not just ^-1
is.na(L) <- sapply(L, is.infinite)
L[is.na(L)] <- 0
# Now let's answer the questions.
country <- "NLD" # Change this
ind <- "C21" # Change this
# Don't change below
ind <- wiot %>% filter(Country == country, IndustryCode == ind) %>% select(RNr)
code = paste(country, ind, sep="")
## Calculations
# 1. My industry's value added
v %>% select(code) # same as v[code], but using pipes for clarity
# 2. World total final demand for industry's output
f <- wiot %>% select(-unwanted_cols)
f %>% filter(Code == code) %>%
select(matches(".5[7-9]|6[0-1]")) %>% # Final demand is in columns c59-c61
sum()
# 3. Final domestic demand for my industry
# This domestic demand is coded as 57 to 61
f <- wiot %>% select(c(Code, num_range(country, 57:61)))
f %>% filter(Code == code) %>% select(-Code) %>% sum()
# 4. Total $ sold by industry in intermediate goods
Z %>% filter(Code == code) %>% select(-Code) %>% sum()
# 5. Total $ of domestic intermediate goods purchased by industry
Z %>% filter(grepl(country, Code)) %>% select(code) %>% sum()
# 6. Total $ of foreign intermediate goods purchased by industry
Z %>% filter(!grepl(country, Code)) %>% select(code) %>% sum()
# Prepare the Leontiff for the next part
L <- data.frame(L)
colnames(L) <- colnames(Z %>% select(-Code))
codes = data.frame(Z$Code)
colnames(codes) <- "Code"
L <- L %>% bind_cols(codes) # Add the codes back in
# 7. Industry's simple world output multipler
L %>% select(code) %>% sum()
# 8. Industry's intra-regional output multiplier
L %>% filter(grepl(country, Code)) %>% select(code) %>% sum()
# 9. Industry's simple world value added multiplier (he meant effect)
a <- v %>% select(-Code) / t(x$TOT) # All world coefficients (which are not multipliers)
a[is.na(a)] <- 0
a <- diag(as.vector(a))
va <- a %*% L # Value added multiplier matrix
colnames(va) <- colnames(Z %>% select(-Code))
va <- data.frame(va) %>% bind_cols(codes)
va %>% select(code) %>% sum()
# 10. Industry's simple inter-regional value added multipler
va %>% filter(!grepl(country, Code)) %>% select(code) %>% sum()
# 11. World total final demand for my industry increases by 1%, what is change on my country's total value added (within country)
# World total final demand
a <- f %>% filter(Code == code) %>%
select(matches(".5[7-9]|6[0-1]")) %>% # Final demand is in columns c59-c61
sum()
# Industry's va within country multiplier
b <- va %>% filter(grepl(country, Code)) %>% select(code) %>% sum()
# Country's total va
c <- v %>% select(starts_with(country)) %>% sum()
# Change in value added in country if world total final demand increases by 1%
d <- (a*0.01 * b)
d
# d in percent of country's total va
d / c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment