---
title: "Leontief's Input-Output Model in R"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Leontief's Input-Output Model in R}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(knitr)
options(scipen = 1, digits = 3)
```

## Common variables

Let `X` be the input-output matrix, `w` the wage vector, `c` the household
consumption vector, `d` the total final demand vector, and `e` the employment coefficient.

```{r}
library(leontief)

X <- transaction_matrix
w <- wage_demand_matrix[, "wage"]
c <- wage_demand_matrix[, "household_consumption"]
d <- wage_demand_matrix[, "final_total_demand"]
e <- employment_matrix[, "employees"]
```

## Input requirement matrix

Let `A` be the direct coefficients matrix.

```{r}
A <- input_requirement(X, d)
A_aug <- augmented_input_requirement(X, w, c, d)
rownames(A_aug) <- c(rownames(X), "wage_over_demand")
colnames(A_aug) <- c(rownames(X), "consumption_over_demand")
kable(A_aug)
```

## Output allocation matrix

Let `B` be the output allocation matrix.

```{r}
B <- output_allocation(X, d)
rownames(B) <- rownames(X)
colnames(B) <- rownames(X)
kable(B)
```

## Leontief inverse matrix

Let `I` be the identity matrix. Leontief inverse is the same as solving `I - A`.

```{r}
L <- leontief_inverse(A)
rownames(L) <- rownames(X)
colnames(L) <- rownames(X)
kable(L)
```

## Equilibrium output

The required output is given by `L * d`.
```{r}
eq <- equilibrium_output(L, d)
rownames(eq) <- rownames(X)
colnames(eq) <- "output"
kable(eq)
```

## Multipliers

### Output multiplier

The output multiplier is the column sum of `L`.

```{r}
out <- output_multiplier(L)
```

### Income multiplier

Let `W` be a matrix where each column is `w` with the same dimension as `L`. The income multiplier is the column sum of the element-wise multiplication of `L` and `W` element-wise divided by `w`.

```{r}
inc <- income_multiplier(L, w / d)
```

### Employment multiplier

Let `E` be a matrix where each column is `e` with the same dimension as `L`. The employment multiplier is the column sum of the element-wise multiplication of `L` and `E` element-wise divided by `e`.

```{r}
emp <- employment_multiplier(L, e / d)
```

### Summary of multipliers

```{r}
sm <- round(cbind(out, inc, emp), 4)
rownames(sm) <- rownames(X)
colnames(sm) <- c("output_multiplier", "income_multiplier", "employment_multiplier")
kable(sm)
```

## Linkages

### Backward and forward linkage

```{r}
bl <- backward_linkage(A)
fl <- forward_linkage(A)
bfl <- cbind(bl, fl)
rownames(bfl) <- rownames(X)
colnames(bfl) <- c("backward_linkage", "forward_linkage")
kable(bfl)
```

### Power of dispersion

```{r}
bl <- power_dispersion(L)
bl_cv <- power_dispersion_cv(L)
bl_t <- cbind(bl, bl_cv)
rownames(bl_t) <- rownames(X)
colnames(bl_t) <- c("power_dispersion", "power_dispersion_cv")
kable(bl_t)
```

### Sensitivity of dispersion

```{r}
sl <- sensitivity_dispersion(L)
sl_cv <- sensitivity_dispersion_cv(L)
sl_t <- cbind(sl, sl_cv)
rownames(sl_t) <- rownames(X)
colnames(sl_t) <- c("power_dispersion", "power_dispersion_cv")
kable(sl_t)
```

### Multiplier product matrix

```{r}
mp <- multiplier_product_matrix(L)
rownames(mp) <- rownames(X)
colnames(mp) <- rownames(X)
kable(mp)
```

### Induced effects (labor/consumption)

```{r}
bli <- backward_linkage(A_aug)
fli <- forward_linkage(A_aug)
bfli <- cbind(bli, fli)
rownames(bfli) <- c(rownames(X), "wage")
# wie = with induced effect
colnames(bfli) <- c("backward_linkage_wie", "forward_linkage_wie")
kable(bfli)
```

## References

Schuschny, Andres Ricardo. *Topicos sobre el modelo de insumo-producto: teoria y aplicaciones*. Cepal, 2005.

Pino Arriagada, Andres y Fuentes Navarro, Silvia. *Derivacion y analisis de los multiplicadores de empleo para la economia nacional*. Universidad del Bio-Bio, 2018.
