|
# 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 |