# Analyse Correspondence Table

## Overview

This vignette demonstrates how to extract key diagnostic information from correspondence tables using the `analyseCorrespondenceTable()` function, including:

- the inventory of codes involved on both sides of the correspondence;
- the presence of one‑to‑one, one‑to‑many, or many‑to‑many relationships;
- potential structural issues that may affect downstream use (aggregation, updates, or quality checks).

    library(correspondenceTables)

## Application of `analyseCorrespondenceTable`

### Example 1: Basic analysis of a correspondence table

In this first example, only a correspondence table is provided. No explicit source or target classification tables are supplied.

    # Load example correspondence table
    AB <- read.csv(
      system.file("extdata/test", "ExempleAnnexe.csv", package = "correspondenceTables"),
      stringsAsFactors = FALSE
    )

The analysis is then performed using default settings:

    # Perform analysis
    result <- analyseCorrespondenceTable(
      AB = AB,
      A  = NULL,
      B  = NULL,
      longestAcodeOnly = FALSE,
      longestBcodeOnly = FALSE
    )

The function returns a list containing multiple outputs. Two key components are displayed below.

The tables below illustrate the first rows of the outputs returned by the function:

    # Display results

    knitr::kable(
      head(result$Inventory),
      caption = "Inventory results: Basic analysis of a correspondence table",
      align = "c"
    )

Inventory results: Basic analysis of a correspondence table
Component | CorrespondenceType | SourcePositions | TargetPositions | nSourcePositions | nTargetPositions
--- | --- | --- | --- | --- | ---
Component 1 | M:M | C01, C06, C12, C21, C24 | D04, D21, D13, D20, D11 | 5 | 5
Component 2 | M:M | C02, C09, C11, C17, C18, C22, C25 | D22, D03, D12 | 7 | 3
Component 3 | M:M | C03, C05, C08, C15 | D07, D08, D05, D02, D17 | 4 | 5
Component 4 | M:M | C04, C16, C20 | D24, D19, D10 | 3 | 3
Component 5 | M:M | C07, C14, C23 | D16, D06 | 3 | 2
Component 6 | 1:M | C10 | D01, D18, D09 | 1 | 3

    knitr::kable(
      head(result$Analysis),
      caption = "Analysis results: Basic analysis of a correspondence table",
      align = "c"
    )

Analysis results: Basic analysis of a correspondence table
ClassA | ClassB | nTargetClasses | SourceToTargetMapping | nSourceClasses | TargetToSourceMapping
--- | --- | --- | --- | --- | ---
C01 | D04 | 5 | D04, D21, D13, D20, D11 | 1 | C01
C01 | D11 | 5 | D04, D21, D13, D20, D11 | 1 | C01
C01 | D13 | 5 | D04, D21, D13, D20, D11 | 1 | C01
C01 | D20 | 5 | D04, D21, D13, D20, D11 | 1 | C01
C01 | D21 | 5 | D04, D21, D13, D20, D11 | 5 | C01, C06, C12, C21, C24
C02 | D03 | 2 | D22, D03 | 3 | C02, C11, C18

Interpretation of the results

The `analyseCorrespondenceTable()` function returns a list of outputs that summarise the structure of the correspondence table from complementary perspectives. In this example, two components are displayed: Inventory and Analysis.

Inventory table

The `Inventory` table provides descriptive information about the codes appearing on each side of the correspondence. Typical columns include:

- the identifiers of codes on side A and side B;
- the number of times each code appears in the correspondence;
- summary indicators describing how codes are connected.

This table is useful for identifying which codes are involved in the correspondence and for detecting duplicates or unexpected patterns.

Analysis table

The `Analysis` table summarises the relationship structure between the two classifications. In particular, it classifies the relationships between codes as:

- 1:1 (one‑to‑one): each code on side A corresponds to exactly one code on side B;
- 1:M (one‑to‑many): one code on side A corresponds to multiple codes on side B;
- M:1 (many‑to‑one): multiple codes on side A correspond to one code on side B;
- M:M (many‑to‑many): multiple codes on side A correspond to multiple codes on side B.

Many‑to‑many (M:M) relationships are typically the most complex and may require special attention, as they can introduce ambiguity in aggregation, conversion, or validation processes.

Together, the inventory and analysis outputs provide an overview of both the content and the structural properties of the correspondence table.

### Example 2: Full analysis with source and target classifications

In this second example, all main parameters of the function are used.

- `AB`: correspondence table between NACE Rev.2 and NACE Rev.2.1
- `A`: source classification (NACE Rev.2)
- `B`: target classification (NACE Rev.2.1)

The analysis is restricted to the lowest‑level codes on both sides of the correspondence.

    # Load correspondence table (NACE Rev.2 -> NACE Rev.2.1)
    AB <- read.csv(
      system.file("extdata/test", "ab_data.csv", package = "correspondenceTables"),
      stringsAsFactors = FALSE
    )

    # Load source classification (NACE Rev.2)
    A <- read.csv(
      system.file("extdata/test", "a_data.csv", package = "correspondenceTables"),
      stringsAsFactors = FALSE
    )

    # Load target classification (NACE Rev.2.1)
    B <- read.csv(
      system.file("extdata/test", "b_data.csv", package = "correspondenceTables"),
      stringsAsFactors = FALSE
    )

    # Perform analysis using all function parameters
    result2 <- analyseCorrespondenceTable(
      AB = AB,
      A  = A,
      B  = B,
      longestAcodeOnly = TRUE,
      longestBcodeOnly = TRUE
    )

The tables illustrate the first rows of the outputs returned by the function: The resulting inventory and analysis summary are shown below:

    # Display results

    knitr::kable(
      head(result2$Inventory),
      caption = "Inventory results: Full analysis with source and target classifications",
      align = "c"
    )

Inventory results: Full analysis with source and target classifications
Component | CorrespondenceType | SourcePositions | TargetPositions | nSourcePositions | nTargetPositions
--- | --- | --- | --- | --- | ---
Component 1 | 1:1 | 10.11 | 10.11 | 1 | 1
Component 2 | 1:1 | 10.12 | 10.12 | 1 | 1
Component 3 | 1:1 | 10.13 | 10.13 | 1 | 1
Component 4 | 1:1 | 10.31 | 10.31 | 1 | 1
Component 5 | 1:1 | 10.32 | 10.32 | 1 | 1
Component 6 | 1:1 | 10.39 | 10.39 | 1 | 1

    knitr::kable(
      head(result2$Analysis[, 1:5]),
      caption = "Analysis results: Full analysis with source and target classifications",
      align = "c"
    )

Analysis results: Full analysis with source and target classifications
NACE.Rev..2.Code | NACE.Rev..2.1.Code | nTargetClasses | SourceToTargetMapping | nSourceClasses
--- | --- | --- | --- | ---
10.11 | 10.11 | 1 | 10.11 | 1
10.12 | 10.12 | 1 | 10.12 | 1
10.13 | 10.13 | 1 | 10.13 | 1
10.31 | 10.31 | 1 | 10.31 | 1
10.32 | 10.32 | 1 | 10.32 | 1
10.39 | 10.39 | 1 | 10.39 | 1

Restricting the analysis to the lowest‑level codes can be useful when the correspondence table is intended for aggregation or conversion at the most granular classification level.

## Notes

If `A` and `B` are provided, the function performs additional consistency checks against the source and target classification domains.

The `longestAcodeOnly` and `longestBcodeOnly` arguments allow users to restrict the analysis to the most detailed codes, which is often desirable in practical statistical workflows.

This functionality supports the validation and interpretation of correspondence tables, particularly when preparing data for aggregation, transformation, or integration tasks.