---
title: "BM 2025 Output-Based GVC Decomposition"
author: "gvcAnalyzer Package"
date: "`r Sys.Date()`"
output:
  rmarkdown::html_vignette:
    toc: true
    toc_depth: 3
    number_sections: true
vignette: >
  %\VignetteIndexEntry{BM 2025 Output-Based GVC Decomposition}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5
)

library(gvcAnalyzer)
```

# Introduction

The BM 2025 framework extends GVC measurement beyond trade flows to the **production side**, decomposing gross output into GVC-related and non-GVC components. This vignette demonstrates the output-based decomposition methodology.

## References

Borin, A., Mancini, M., & Taglioni, D. (2025). Economic consequences of trade and global value chain integration: A measurement perspective. *The World Bank Economic Review*.

# Conceptual Framework

## Output-Based Decomposition

The BM 2025 output decomposition classifies gross output of country *s* into:

1. **DomX**: Purely domestic production (value added never crosses borders)

2. **TradX**: Traditional one-crossing trade

3. **GVC\_PF\_X**: Pure-forward GVC output (intermediates for foreign production)

4. **GVC\_PB\_X**: Pure-backward GVC output (using foreign intermediates)

5. **GVC\_TSImp**: Two-sided GVC output via imported intermediates

6. **GVC\_TSDom**: Two-sided GVC output via domestic intermediates

7. **GVC\_TS\_X**: Total two-sided GVC output (TSImp + TSDom)

8. **GVC\_X**: Total GVC-related output (PF + PB + TS)

The fundamental identity: **X\_total = DomX + TradX + GVC\_X**.

## Key Advantages

The output-based approach:

- Captures GVC involvement regardless of export destination

- Provides production-side perspective complementary to trade flows

- Enables sector-level analysis of GVC integration

- Supports industrial policy analysis

# Data and Setup

```{r}
io <- bm_build_io(
  Z = bm_toy_Z,
  Y = bm_toy_Y,
  VA = bm_toy_VA,
  X = bm_toy_X,
  countries = bm_toy_countries,
  sectors = bm_toy_sectors
)
```

# Country-Level Output Decomposition

## Computing Output Components

```{r}
out_comp <- bm_2025_output_components(io)
out_comp
```

## Interpreting Results

Each row represents a country's gross output decomposition:

- **X\_total**: Total gross output

- **DomX**: Production for domestic final use only

- **TradX**: Production for traditional one-crossing trade

- **GVC\_PF\_X**: Production for foreign intermediate use (upstream)

- **GVC\_PB\_X**: Production using foreign intermediates (downstream)

- **GVC\_TS\_X**: Production with both foreign inputs and re-export linkages

## Output-Based Participation Measures

From the components, we compute participation indicators:

```{r}
out_meas <- bm_2025_output_measures(io)
out_meas
```

Key indicators:

- **share\_GVC\_output**: GVC-related output as a share of total output

- **share\_PF\_output**, **share\_TS\_output**, **share\_PB\_output**: Composition of GVC output

- **forward\_output**: Output-based forward orientation index

# Sector-Level Output Decomposition

The BM 2025 framework extends to country–sector pairs:

```{r}
# Compute sectoral components and measures
out_comp_sec <- bm_2025_output_components_sector(io)
out_meas_sec <- bm_2025_output_measures_sector(io)
head(out_meas_sec, 12)
```

## Sectoral Analysis

```{r}
# Example: Compare manufacturing sectors across countries
# Note: Using column names specific to the sectoral function (X_i, share_GVC_output_i, etc.)
manufacturing <- out_meas_sec[out_meas_sec$sector == "Manufacturing", ]

# Select key columns for display
cols_to_show <- c("country", "sector", "X_i", "share_GVC_output_i", "forward_output_i")
manufacturing[, cols_to_show]
```

# Visualization

## GVC Participation by Country

```{r}
oldpar <- par(mar = c(5, 5, 3, 2))

barplot(
  out_meas$share_GVC_output,
  names.arg = out_meas$country,
  col = "steelblue",
  ylab = "GVC Share of Output",
  main = "Output-Based GVC Participation",
  ylim = c(0, max(out_meas$share_GVC_output, na.rm = TRUE) * 1.2)
)

grid()

par(oldpar)
```

## GVC Composition

```{r}
oldpar <- par(mar = c(5, 5, 3, 2))

composition <- as.matrix(out_meas[, c("share_PF_output", "share_TS_output", "share_PB_output")])
rownames(composition) <- out_meas$country

barplot(
  t(composition),
  beside = FALSE,
  col = c("darkgreen", "orange", "darkred"),
  ylab = "Share of GVC Output",
  main = "GVC Output Composition",
  legend.text = c("Pure-Forward", "Two-Sided", "Pure-Backward"),
  args.legend = list(x = "topright", bty = "n")
)

grid()

par(oldpar)
```

## Forward Orientation

```{r}
oldpar <- par(mar = c(5, 5, 3, 2))

barplot(
  out_meas$forward_output,
  names.arg = out_meas$country,
  col = ifelse(out_meas$forward_output > 0, "darkgreen", "darkred"),
  ylab = "Forward Orientation Index",
  main = "Output-Based Forward Orientation",
  ylim = c(-1, 1)
)

abline(h = 0, lty = 2, col = "gray", lwd = 2)

grid()

par(oldpar)
```

# Comparison: Country vs Sector Level

```{r}
# Aggregate sector-level results to country level
# Note: Using X_i and GVC_Xi for sector-level columns
sec_agg <- aggregate(
  cbind(X_i, GVC_Xi) ~ country,
  data = out_comp_sec,
  FUN = sum
)

# Calculate implied country share from sector sums
sec_agg$share_GVC_output <- sec_agg$GVC_Xi / sec_agg$X_i

# Compare with direct country-level calculation
# CORRECTED: Using out_meas (which has the shares) instead of out_comp
comparison <- merge(
  out_meas[, c("country", "share_GVC_output")],
  sec_agg[, c("country", "share_GVC_output")],
  by = "country",
  suffixes = c("_direct", "_sectoral")
)

comparison
```

The sector-level aggregation matches the direct country-level calculation, confirming consistency.

# Summary

This vignette demonstrated the BM 2025 output-based GVC decomposition:

1. Country-level output components and participation measures

2. Sector-level decomposition for detailed industrial analysis

3. Visualization of GVC participation and orientation

4. Consistency between country and sector aggregation

The output-based approach provides a comprehensive view of GVC integration from the production side, complementing trade-based measures for robust GVC analysis.

# Session Information

```{r}
sessionInfo()
```
