---
title: Understanding the data
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Understanding the data}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup, echo = FALSE}
library(pacta.multi.loanbook)

plot_table <- function(table) {
  table_plot <- gt::gt(dplyr::select(table, -"dataset"))
  
  table_plot <- 
    gt::cols_width(
      .data = table_plot,
      column ~ gt::px(150),
      typeof ~ gt::px(90)
    )
  
  table_plot <-
    gt::tab_style(
      data = table_plot,
      style = gt::cell_text(size = "smaller"),
      locations = gt::cells_body(columns = 1:2)
    )
  
  table_plot <-
    gt::tab_options(
      data = table_plot,
      ihtml.active = TRUE,
      ihtml.use_pagination = FALSE,
      ihtml.use_sorting = TRUE,
      ihtml.use_highlight = TRUE
    )
  
  gt::fmt_passthrough(table_plot)
}
```
# Intro

In many cases, users of this package will want to use the outputs of the analyses for further processing, such as additional analyses or making visualizations based on the design guide of their own organisation. To facilitate such additional use cases, but also simplify interpretation of the outputs generated with this package, this data dictionary documents each type of output table in detail, focusing on data types and definitions.

This article is structured based on the output tables generated by `pacta.multi.loanbook` and follows the standard flow of the user experience as much as possible, so it can be read in the same sequence as the analysis is run.

# Tables

The main steps that generate output tables are:

* Diagnostics and coverage
* Standard PACTA analysis
* Aggregated PACTA metrics

## Diagnostics

The diagnostics section is split into determining the match success rate of the loan books analysed and inspecting the real economy activity related to the financing made by the banks through the matched loan books. The former is influenced by the quality of the input loan book data and the completeness of the reference production data against which the loan books are matched. The latter, while it depends on a solid match success rate, is mainly driven by the financing decisions and the portfolio allocation made by the banks. If a sector split is applied to the loan book, any companies that are lost in the process are documented for every loan book.

### Match success rate

```{r dd_lbk_match_success_rate, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "lbk_match_success_rate")
```

```{r dd_lbk_match_success_rate_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "lbk_match_success_rate")
plot_table(table)
```

### Loan book coverage

```{r dd_summary_statistics_loanbook_coverage, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "summary_statistics_loanbook_coverage")
```

```{r dd_summary_statistics_loanbook_coverage_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "summary_statistics_loanbook_coverage")
plot_table(table)
```

### Companies lost in sector split

```{r dd_lost_companies_sector_split, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "lost_companies_sector_split")
```

```{r dd_lost_companies_sector_split_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "lost_companies_sector_split")
plot_table(table)
```


## Standard PACTA analysis

The standard PACTA analysis is run across all input banking books, but produces the same output metrics as known from the `r2dii.*` packages. Results are given at portfolio level grouped by banking book. Beyond the standard output format, tables are provided that can be used as input for visualizations, for each of the standard sectors and technologies.

### Target Market Share results (all groups)

Target market share results at the portfolio level for each included banking book

```{r dd_tms_results, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "tms_results")
```

```{r dd_tms_results_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "tms_results")
plot_table(table)
```


### Sectoral Decarbonization Approach results (all groups)

SDA results at the portfolio level for each included banking book

```{r dd_sda_results, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "sda_results")
```

```{r dd_sda_results_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "sda_results")
plot_table(table)
```


### Data tech mix

Results for a given portfolio and sector, tailored to be used in the tech mix chart

```{r dd_data_tech_mix, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "data_tech_mix")
```

```{r dd_data_tech_mix_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "data_tech_mix")
plot_table(table)
```


### Data trajectory

Results for a given portfolio, sector and technology, tailored to be used in the volume trajectory chart

```{r dd_data_trajectory, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "data_trajectory")
```

```{r dd_data_trajectory_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "data_trajectory")
plot_table(table)
```


### Data emission intensity

Results for a given portfolio and sector, tailored to be used in the emission intensity chart

```{r dd_data_emission_intensity, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "data_emission_intensity")
```

```{r dd_data_emission_intensity_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "data_emission_intensity")
plot_table(table)
```


### Companies included

Lists all companies including exposures, that were analysed for the given loan book and that are therefore included in the data to be visualized.

```{r dd_companies_included, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "companies_included")
```

```{r dd_companies_included_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "companies_included")
plot_table(table)
```


## Aggregated PACTA metrics

The aggregated PACTA metrics are also run across all input banking books. The calculations produce the net aggregate alignment metric, which is defined in the vignettes ["Calculation of a company alignment metric"](company_alignment_metric.html) and ["Calculation of exposure-weighted aggregated alignment metric"](loanbook_aggregated_alignment_metric.html) and allows producing the corresponding plots. Results are grouped at the level defined by the `by_group` parameter.

### Company technology deviation

For each company in the analyzed banking books, shows the deviation of the technology build-out in the final year of the analysis from the corresponding allocated scenario value. This is an intermediate result that is further processed in the calculation of the net aggregate alignment metric. Only available for sectors, which have technology level calculations using the target market share, namely `automotive, coal, oil and gas, power`.

```{r dd_company_technology_deviation_tms, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "company_technology_deviation_tms")
```

```{r dd_company_technology_deviation_tms_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "company_technology_deviation_tms")
plot_table(table)
```

### Company net alignment metric for TMS sectors

For each company in the analyzed banking books, shows the net aggregate alignment metric for sectors, which have technology level calculations using the target market share, namely `automotive, coal, oil and gas, power`. See `vignette("company_alignment_metric")` for methodological documentation.

```{r dd_company_alignment_net_tms, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "company_alignment_net_tms")
```

```{r dd_company_alignment_net_tms_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "company_alignment_net_tms")
plot_table(table)
```

### Disaggregated company buildout/phaseout alignment metric for TMS sectors

For each company in the analyzed banking books, shows the aggregate alignment metric - disaggregated into its buildout and phaseout components - for sectors, which have technology level calculations using the target market share, namely `automotive, coal, oil and gas, power`. See `vignette("company_alignment_metric")` for methodological documentation.

```{r dd_company_alignment_bo_po_tms, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "company_alignment_bo_po_tms")
```

```{r dd_company_alignment_bo_po_tms_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "company_alignment_bo_po_tms")
plot_table(table)
```

### Company net alignment metric for SDA sectors

For each company in the analyzed banking books, shows the net aggregate alignment metric for sectors, which have sector level calculations using the sectoral decarbonization approach (SDA), namely `aviation, cement, steel`. See `vignette("company_alignment_metric")` for methodological documentation.

```{r dd_company_alignment_net_sda, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "company_alignment_net_sda")
```

```{r dd_company_alignment_net_sda_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "company_alignment_net_sda")
plot_table(table)
```

### Company net aggregate alignment metric with financial exposures

For each company in the analyzed banking books, shows the net aggregate alignment metric for all available sectors. This table includes the financial exposure to each of the analyzed parts of the banking books, split as defined in `by_group`.

```{r dd_company_exposure_net_aggregate_alignment, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "company_exposure_net_aggregate_alignment")
```

```{r dd_company_exposure_net_aggregate_alignment_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "company_exposure_net_aggregate_alignment")
plot_table(table)
```

### Disaggregated company buildout/phaseout alignment metric with financial exposures

For each company in the analyzed banking books, shows the net aggregate alignment metric - disaggregated by its buildout and phaseout components - for all sectors that use technology level TMS calculations, namely `automotive, coal, oil and gas, power`. This table includes the financial exposure to each of the analyzed parts of the banking books, split as defined in `by_group`. Note that the financial exposure is not disaggregated, the alignment metric is.

```{r dd_company_exposure_bo_po_aggregate_alignment, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "company_exposure_bo_po_aggregate_alignment")
```

```{r dd_company_exposure_bo_po_aggregate_alignment_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "company_exposure_bo_po_aggregate_alignment")
plot_table(table)
```

### Loan book net aggregate alignment metric with financial exposures

For each loan book level group (split as defined in `by_group`), shows the net aggregate alignment metric for all available sectors. This table includes the financial exposure to each of the analyzed parts of the banking books. Company level results are aggregated to the loan book level, using their relative financial exposure as weights.

```{r dd_loanbook_exposure_net_aggregate_alignment, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "loanbook_exposure_net_aggregate_alignment")
```

```{r dd_loanbook_exposure_net_aggregate_alignment_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "loanbook_exposure_net_aggregate_alignment")
plot_table(table)
```

### Disaggregated loan book buildout/phaseout alignment metric with financial exposures

For each loan book level group (split as defined in `by_group`), shows the net aggregate alignment metric - disaggregated by its buildout and phaseout components - for all sectors using technology level TMS calculations, namely `automotive, coal, oil and gas, power`. Company level results are aggregated to the loan book level, using their relative financial exposure as weights.

```{r dd_loanbook_exposure_bo_po_aggregate_alignment, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "loanbook_exposure_bo_po_aggregate_alignment")
```

```{r dd_loanbook_exposure_bo_po_aggregate_alignment_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "loanbook_exposure_bo_po_aggregate_alignment")
plot_table(table)
```

### Input data for Sankey plot

Data set meant to be used as input into `plot_sankey()`.

```{r dd_data_sankey, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "data_sankey")
```

```{r dd_data_sankey_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "data_sankey")
plot_table(table)
```
  
### Input data for alignment-by-exposure scatter plot

Data set meant to be used as input into `plot_scatter_alignment_exposure()`.

```{r dd_data_scatter_alignment_exposure, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "data_scatter_alignment_exposure")
```

```{r dd_data_scatter_alignment_exposure_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "data_scatter_alignment_exposure")
plot_table(table)
```
  
### Input data for buildout/phaseout scatter plot

Data set meant to be used as input into `plot_scatter()`.

```{r dd_data_scatter_sector, results = FALSE}
dplyr::filter(data_dictionary, .data[["dataset"]] == "data_scatter_sector")
```

```{r dd_data_scatter_sector_table, echo = FALSE}
table <- dplyr::filter(data_dictionary, .data[["dataset"]] == "data_scatter_sector")
plot_table(table)
```
