---
title: "Germany 1990"
author: "Daniel Antal, based on the work of Jorg Beutel"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Germany 1990}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=TRUE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(iotables)
require(dplyr); require(tidyr)
```

## Germany sample files

The Germany example is used to test the code of the package. It follows the examples in [Eurostat Manual of Supply, Use and Input-Output Tables](http://ec.europa.eu/eurostat/documents/3859598/5902113/KS-RA-07-013-EN.PDF/b0b3d71e-3930-4442-94be-70b36cea9b39?version=1.0). by Jörg Beutel (Eurostat Manual).

The `germany_1990` dataset is a simplified 6x6 sized SIOT taken from the Eurostat Manual (page 481). It is brought to a long form similar to the Eurostat bulk files. The testthat infrastructure of the iotables package is checking data processing functions and analytical calculations against these published results.

The following data processing functions select and order the data from the Eurostat-type bulk file. Since the first version of this package, Eurostat moved all SIOT data products to the ESA2010 vocabulary, but the manual still follows the ESA95 vocabulary. The labels of the dataset were slightly changed to match the current metadata names. The changes are minor and self-evident, the comparison of the `germany_1990` dataset and the Manual should cause no misunderstandings.

```{r iotables}
data_table <- iotable_get( labelling = "iotables" )
input_flow <- input_flow_get ( 
                  data_table = data_table, 
                  households = FALSE)

de_output <- primary_input_get ( data_table, "output" )
print (de_output[c(1:4)])
```

This vignette uses `library(tidyverse)`, more particularly `dplyr` and `tidyr`, just like all analytical functions of `iotables`.  Even if you do not use `tidyverse`, this packages will be installed together with `iotables`. These functions are only used in the vignette to print the output, and they are not essential for the examples.

## Direct effects

The input coefficient matrix shows what happens in the whole domestic economy when an industry is facing additional demand, increases production. In the Germany example, all results are rounded to 4 digits for easier comparison with the Eurostat manual.

> The input coefficients for domestic intermediates are defined on page 486. You can check the following results against Table 15.8 of the Eurostat manual. (Only the top-right corner of the resulting input coefficient matrix is printed for readability.) 

$\sum_{i=1}^n X_i$

The `input_coefficient_matrix_create()` function relies on the following equation. The numbering of the equations is the numbering of the Eurostat Manual.

(9)  $a_{ij}$ = $X_{ij}$ / $x_j$  [recap: (43) is the same]


It checks the correct ordering of columns, and furthermore it fills up 0 values with 0.000001 to avoid division with zero. 


```{r inputcoeff, echo=FALSE}
de_input_coeff <- input_coefficient_matrix_create( 
     data_table = data_table, 
     digits = 4)

## which is equivalent to:

de_input_coeff <- coefficient_matrix_create( data_table, 
                             total = "output", 
                             return_part = "products", 
                             households = FALSE,
                             digits = 4)

print ( de_input_coeff[1:3, 1:3])
```

Similarly, the output coefficient matrix is the following:

```{r outputcoeff, echo=FALSE}
de_out <- output_coefficient_matrix_create ( 
                                    io_table = data_table, 
                                    total = 'tfu',
                                    digits = 4)

# Rounding is slightly different in the Eurostat manual:
print ( de_out[1:3, 1:3] )
```

These results can be checked on page 468 in the Manual.

The Leontieff matrix is derived from Leontieff equation system.  

(19) (I-A)x = y

The Leontieff matrix is defined as (I-A) and it is created with the `leontieff_matrix_create()` function.

The Leontieff inverse is (I-A)^-1^ and it is created with the `leontieff_inverse_create()` function from the Leontieff-matrix.

```{r leontieff}
L_de <- leontieff_matrix_create ( technology_coefficients_matrix =
                                 de_input_coeff )
I_de <- leontieff_inverse_create(de_input_coeff)
I_de_4 <- leontieff_inverse_create(de_input_coeff, digits = 4)
print (I_de_4[,1:3])
```

> You can check the Leontieff matrix against Table 15.9 on page 487 of the Euorstat manual, and the Leontieff inverse against Table 15.10 on page 488. The ordering of the industries is different in the manual.

## Creating indicators 

### Creating technical indicators

Technical indicators assume constant returns to scale and fixed relationship of all inputs to each industry. With these conditions the technical input coefficients show how much input products, labour or capital is required to produce a unit of industry output.

(60) $a_{ij}$ = $z_{ij}$ / $x_j$  [technical input coefficients]

The helper function `primary_input_get()` selects a row from the SIOT and brings it to a conforming form. The `input_indicator_create()` creates the vector of technical input coefficients.

```{r employment_indicator}
de_emp <- primary_input_get ( data_table,
                              primary_input = "employment_domestic_total" )

de_emp_indicator <- input_indicator_create ( 
    data_table  = data_table,
    input_vector = "employment_domestic_total")

print ( tidyr::gather( de_emp_indicator, indicators, values, !!2:ncol(de_emp_indicator))[,-1] )
```

Often we want to analyze the effect of growing domestic demand on some natural units, such as employment or $CO_2$ emissions. The only difficulty is that we need data that is aggregated / disaggregated precisely with the same industry breakup as our SIOT table.

European employment statistics have greater detail than our tables, so employment statistics must be aggregated to conform the 60 (61, 62) columns of the SIOT.  There is a difference in the columns based on how national statistics offices treat imputed real estate income and household production, and trade margins. Czech SIOTs are smaller than most SIOTs because they do not have these columns and rows.

In another vignette we will show examples on how to work with these real-life data. For the sake of following the calculations, we are continuing with the simplified 1990 German data. 

### Creating income indicators

The input coefficients for value added are created with `input_indicator_create()`. 

```{r gva_indicator}
de_gva <- primary_input_get ( data_table,
                              primary_input = "gva") 

de_gva_indicator  <- input_indicator_create( 
    data_table  = data_table,
    input_vector = "gva")

print( tidyr::gather(de_gva_indicator, indicators, values,!!2:ncol(de_gva_indicator))[,-1]  ) 
```


This is equal to the equation on page 495 of the Eurostat Manual. The results above can be checked on the bottom of page 498.

(44) $w_{ij}$ = $W_{j}$ / $x_j$  [input coefficients for value added]

You can create a matrix of input indicators, or direct effects on (final) demand with `direct_supply_effects_create()`. The function by default creates input requirements for final demand.  With the code below it re-creates the Table 15.14 of the Eurostat manual. 

```{r input_indicator}
direct_effects_de <- coefficient_matrix_create(
  data_table  = data_table, 
  total       = 'output', 
  return_part = 'primary_inputs')

knitr::kable ( direct_effects_de[1:6,1:4])
```

>The 'total' row above is labelled as Domestic goods and services in the Eurostat Manual. The table can be found on p498.

## Multipliers

### Income multipliers

The SIOTs contain (with various breakups) three types of income:

* Employee wages, which is usually a proxy for all household income.

* Gross operating surplus, which is a form of corporate sector income.

* Taxes that are the income of government.

These together make gross value added (GVA).  If you are working with SIOTs that use basic prices, then GVA = GDP at producers' prices, or basic prices.

The GVA multiplier shows the additional gross value created in the economy if demand for the industry products is growing with unity. The wage multiplier (not shown here) shows the increase in household income.

The following equation is used to work with different income effects and multipliers:

(63) Z = B(I-A)^-1^ 

B = vector of input coefficients for wages or GVA or taxes.

Z = direct and indirect requirements for wages (or other income)

The indicator shows that manufacturing has the lowest, and other services has the highest gross value added component. This is hardly surprising, because manufacturing needs a lot of materials and imported components. When the demand for manufacturing in the domestic economy is growing by 1 unit, the gross value added is `r as.numeric(de_gva_indicator[3])`.

> You can check these values against the Table 15.16 of the Eurostat Manual on page 501 (row 10).

You can recreate the whole matrix, when the data data permits, with `input_multipliers_create()` as shown here. Alternatively, you can create your own custom multipliers with `multiplier_create()` as shown in the following example. 

```{r inputmultipliers}
input_reqr <- coefficient_matrix_create(
    data_table  = iotable_get (), 
    total       = 'output', 
    return_part = 'primary_inputs') 

multipliers <- input_multipliers_create(
  input_requirements = input_reqr,
  inverse = I_de)

knitr::kable(multipliers, digits= 4)
```

> You can check these results against the Table 15.16 on p501 of the Eurostat manual. the label 'total' refers to domestic intermediaries. The ordering of the rows is different from the Manual.

These multipliers are Type-I multipliers. The type-I GVA multiplier shows the total effect in the domestic economy. The initial extra demand creates new orders in the backward linking industries, offers new product to build on in the forward-linking industry and creates new corporate and employee income that can be spent. Type-II multipliers will be introduced in a forthcoming vignette [not yet available.]

### Employment multipliers

The E matrix contains the input coefficients for labor (created by `input_indicator_create()`). The following matrix equation defines the employment multipliers.

(64) Z = E(I-A)^-1^ 

The `multiplier_create()` function performs the matrix multiplication, after handling many exceptions and problems with real-life data, such as different missing columns and rows in the national variations of the standard European table. 

Please send a bug report on [Github](https://github.com/rOpenGov/iotables/issues) if you run into further real-life problems. 

```{r employment_multiplier}
de_emp_indicator <- input_indicator_create (
  data_table = data_table, 
  input = 'employment_domestic_total') 

employment_multipliers <- multiplier_create ( 
  input_vector    = de_emp_indicator,
  Im              = I_de,
  multiplier_name = "employment_multiplier", 
  digits = 4 )

print (tidyr::gather(employment_multipliers,
              multipliers, values, 
              !!2:ncol(employment_multipliers))[-1])
```

You can check against page 501 that these values are correct and on page 501 that the highest employment multiplier is indeed $z_i$ = `r max ( as.numeric ( employment_multipliers), na.rm=TRUE)`, the employment multiplier of agriculture.

For working with real-life, current employment data, there is a helper function to retrieve and process Eurostat employment statistics to a SIOT-conforming vector `employment_get()`. This function will be explained in a separate vignette. 

### Output multipliers

`Output multipliers` and `forward linkages` are calculated with the help of output coefficients for product as defined on p486 and p495 of the the Eurostat Manual. The Eurostat Manual uses the definition of _output at basic prices_ to define output coefficients which is no longer part of SNA as of SNA2010.

(5) $b_{ij}$ = $X_{ij}$ / $x_i$  [also (45) output coefficients for products / intermediates].

$x_i$: output of sector i

```{r outputmult}
de_input_coeff <- input_coefficient_matrix_create( 
                         data_table = iotable_get(), 
                         digits = 4)
                           
output_multipliers <- output_multiplier_create ( 
                        input_coefficient_matrix = de_input_coeff )

knitr::kable (
  tidyr::gather(output_multipliers, multipliers, values)[-1,]
  )
```

These multipliers can be checked against the Table 15.15 on p500 of the Eurostat Manual.

## Interindustrial linkage analysis

The `backward linkages`, i.e. demand side linkages, show how much incremental demand is created via suppliers when an industry is facing increased demand and produces more. 

`Forward linkages` on the other hand show the effect of increased production, which gives either more or cheaper supplies for other industries that rely on the output of the given industry.

For example, when a new concert is put on stage, orders are filled for real estate, security services, catering, etc, which show in the backward linkages. The concert attracts visitors that creates new opportunities for the hotel industry in forward linkages. 

### Backward linkages 

```{r backward}
de_coeff <- input_coefficient_matrix_create( iotable_get(), digits = 4)
I_de <- leontieff_inverse_create ( de_coeff )

de_bw <- backward_linkages(I_de)
print (tidyr::gather(de_bw, backward_linkages, values)[-1,])
```

> You can check the results against Table 15.19 on page 506 of the Eurostat Manual.

Manufacturing has the highest backward linkages, and other services the least. An increased demand for manufacturing usually effects supplier industries. Service industry usually have a high labor input, and their main effect is increased spending of the wages earned in the services.

### Forward linkages 

Forward linkages show the strength of the new business opportunities when industry i starts to increase its production. Whereas backward linkages show the increased demand of the suppliers in industry i, forward linkages show the increased availability of inputs for other industries that rely on industry i as a supplier.

```{r forward}
de_out <- output_coefficient_matrix_create ( 
  data_table, "final_demand", digits = 4
  )
                                    
forward_linkages ( output_coefficient_matrix = de_out )
```

> You can check forward linkages against the Table 15.20 of Eurostat Manual on page 507.