# Downloaded June, 2023.
# https://www.mohaddes.org/gvar
rm(list = ls())
library(dplyr)
library(lubridate)
library(readxl)
#library(seasonal)
library(tidyr)
library(zoo)
#### PPP Data ####
PPP <- as.data.frame(read_xls("data-raw/gvar2019/PPP-GDP WDI (1990-2018).xls", sheet = "WDI"))
PPP <- na.omit(PPP)[, -which(names(PPP) %in% c("Country Code", "Indicator Name", "Indicator Code"))]
nam <- PPP[, "Country Name"]
time <- names(PPP)[-1]
PPP <- data.frame(time, t(PPP[,-1]))
nam[which(nam == "Korea, Rep.")] <- "Korea"
nam[which(nam == "United Kingdom")] <- "UK"
nam[which(nam == "United States")] <- "USA"
names(PPP) <- c("Year", nam)
rownames(PPP) <- NULL
dates <- as.character(PPP[, "Year"])
PPP <- ts(PPP[, -1], start = 1990, frequency = 1)
dimnames(PPP)[[1]] <- dates
#### Country data ####
variables <- c("y", "Dp", "eq", "ep", "r", "lr")
countries <- as.data.frame(read_xls("data-raw/gvar2019/Country Codes.xls", col_types = "text"))
data <- c()
nam <- c()
for (i in countries[, "Country Name"]) {
temp <- as.data.frame(read_xls("data-raw/gvar2019/Country Data (1979Q2-2019Q4).xls", sheet = i))
dates <- as.character(zoo::as.yearqtr(temp[, "date"]))
temp <- ts(temp[, which(names(temp)%in%variables)], start = as.numeric(zoo::as.yearqtr(temp[, "date"])[1]), frequency = 4)
dimnames(temp)[[1]] <- dates
data <- c(data, list(temp))
nam <- c(nam, i)
rm(temp)
}
capcase <- function(x) { # from ?tolower
s <- strsplit(x, " ")[[1]]
paste(toupper(substring(s, 1, 1)), substring(s, 2),
sep = "", collapse = " ")
}
nam <- unlist(lapply(tolower(nam), capcase))
countries <- cbind(countries, "Name" = nam)
countries$Name <- as.character(countries$Name)
countries$Name[which(countries$Name == "United Kingdom")] <- "UK"
countries$Name[which(countries$Name == "Usa")] <- "USA"
names(data) <- countries$Name
country.data <- data
#### Global data ####
global.variables <- c("poil", "pmat", "pmetal")
global.data <- as.data.frame(read_xls("data-raw/gvar2019/Country Data (1979Q2-2019Q4).xls", sheet = 1))
dates <- as.character(zoo::as.yearqtr(global.data[, "date"]))
global.data <- ts(global.data[, which(names(global.data)%in%global.variables)], start = as.numeric(zoo::as.yearqtr(global.data[, "date"])[1]), frequency = 4)
dimnames(global.data)[[1]] <- dates
#### Weight matrix ####
#weights <- array(NA, dim = c(length(country.data), length(country.data), 37))
#dimnames(weights) <- list(countries[, "Country Code"], countries[, "Country Code"], as.character(1980:2016))
weights <- c()
w.names <- c()
for (i in countries[, "Country Code"]) {
temp <- as.data.frame(read_excel("data-raw/gvar2019/Trade Flows (1980-2016).xls",
sheet = i, na = "NaN"))
#dimnames(temp)[[1]] <- temp[, 1]
dates <- temp[, 1]
temp <- temp[, -(1:2)]
# Reorder columns
temp <- temp[ , countries[, "Country Code"]]
# Set home country to zero
temp[, i] <- 0
temp <- ts(temp, start = dates[1], frequency = 1)
dimnames(temp) <- list(as.character(dates), countries[, "Name"])
weights <- c(weights, list(temp))
w.names <- c(w.names, countries[which(countries["Country Code"] == i), "Name"])
#weights[i , ,] <- t(as.matrix(temp))
}
names(weights) <- w.names
weight.data <- weights
#### Rename countries ####
ctr <- c("AR", "AU", "AT", "BE", "BR", "CA", "CN", "CL", "FI", "FR", "DE",
"IN", "ID", "IT", "JP", "KR", "MY", "MX", "NL", "NO", "NZ", "PE",
"PH", "ZA", "SA", "SG", "ES", "SE", "CH", "TH", "TR", "GB", "US")
names(country.data) <- ctr
dimnames(PPP)[[2]] <- ctr
names(weight.data) <- ctr
for (i in ctr) {
dimnames(weight.data[[i]])[[2]] <- ctr
}
#### Save result ####
gvar2019 <- list("country_data" = country.data,
"global_data" = global.data,
"region_weights" = PPP,
"weight_data" = weight.data)
save(gvar2019, file = "data/gvar2019.rda", version = 2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.