## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## -----------------------------------------------------------------------------
library(tradeIndices)

## -----------------------------------------------------------------------------
macro <- data.frame(
  year = 2020:2024,
  exports = c(276, 302, 326, 351, 379),
  imports = c(365, 421, 457, 493, 528),
  gdp = c(2667, 3150, 3390, 3570, 3810)
)

macro$openness <- with(
  macro,
  trade_openness(exports, imports, gdp)
)
macro

## ----openness-plot, fig.width=6.5, fig.height=4-------------------------------
plot(
  macro$year, macro$openness,
  type = "o", pch = 16, lwd = 2, col = "#1B5E20",
  xlab = "Year", ylab = "Trade openness (% of GDP)",
  main = "Illustrative trade-openness series"
)
grid()

## -----------------------------------------------------------------------------
intensity <- data.frame(
  year = 2020:2024,
  bilateral_exports = c(14, 16, 19, 21, 25),
  reporter_exports = c(180, 195, 210, 225, 245),
  world_exports_to_partner = c(310, 335, 360, 390, 420),
  world_exports = c(4300, 4650, 4920, 5180, 5490)
)

intensity$tii <- with(
  intensity,
  trade_intensity(
    bilateral_exports,
    reporter_exports,
    world_exports_to_partner,
    world_exports
  )
)
intensity

## -----------------------------------------------------------------------------
products <- c(rice = 45, wheat = 25, tea = 20, spices = 10)

c(
  HHI = trade_concentration(products),
  HHI_10000 = trade_concentration(products, scale = 10000),
  One_minus_HHI = trade_diversification(products),
  Normalized_HHI_diversity = trade_diversification(
    products, method = "normalized_hhi"
  ),
  Shannon = trade_diversification(products, method = "shannon"),
  Effective_products = trade_diversification(
    products, method = "effective_number"
  )
)

## ----diversity-over-time, fig.width=6.5, fig.height=4-------------------------
example_file <- system.file(
  "extdata", "agricultural_trade_example.csv",
  package = "tradeIndices"
)
ag_trade <- read.csv(example_file)

diversity_by_year <- vapply(
  split(ag_trade$country_exports, ag_trade$year),
  trade_diversification,
  numeric(1),
  method = "normalized_hhi"
)

plot(
  as.integer(names(diversity_by_year)), diversity_by_year,
  type = "o", pch = 16, lwd = 2, col = "#8D6E63",
  xlab = "Year", ylab = "Normalized HHI diversification",
  main = "Illustrative agricultural export diversification"
)
grid()

## -----------------------------------------------------------------------------
country_exports <- c(rice = 45, wheat = 25, tea = 20, spices = 10)
world_exports <- c(spices = 30, tea = 15, wheat = 35, rice = 20)

c(
  Structural_distance = trade_diversification_index(
    country_exports, world_exports
  ),
  Export_similarity = export_similarity(
    country_exports, world_exports
  )
)

## -----------------------------------------------------------------------------
rca_balassa(
  country_product_exports = c(rice = 40, wheat = 20, tea = 10),
  country_total_exports = 100,
  world_product_exports = c(rice = 300, wheat = 500, tea = 200),
  world_total_exports = 5000
)

grubel_lloyd(
  exports = c(rice = 40, wheat = 25, tea = 30),
  imports = c(rice = 10, wheat = 20, tea = 28),
  aggregate = TRUE
)

