trade_data: Prepare Trade Data

View source: R/trade_data.R

trade_dataR Documentation

Prepare Trade Data

Description

Prepares trade data for each country of interest based on the provided data. Generates objects of class TradeData required to be used in the ntrade() function of the qPRAentry package.

Usage

trade_data(
  extra_total,
  extra_pest,
  intra_trade,
  internal_production,
  filter_IDs = NULL,
  filter_period = NULL
)

Arguments

extra_total

A data frame containing the total quantity of commodity imported from third countries (pest-free and pest-present countries). It must contain the following columns: reporter (importing country), partner (exporting country), value (quantity of commodity) and time_period (time period of the trade activity).

extra_pest

A data frame containing the quantity of commodity imported from third countries where the pest is present. It must contain the following columns: reporter (importing country), partner (exporting country), value (quantity of commodity) and time_period (time period of the trade activity). The quantity of imported commodity detailed in this data frame must also be included in the extra_total data frame.

intra_trade

A data frame containing the quantity of commodity traded between countries of interest. It must contain the following columns: reporter (importing country), partner (exporting country), value (quantity of commodity) and time_period (time period of the trade activity).

internal_production

A data frame containing the quantity of commodity produced internally within each country of interest. It must contain the following columns: reporter (producing country), value (quantity of commodity) and time_period (time period of the production).

filter_IDs

A vector containing the country IDs to filter (identification codes of the countries of interest). By default, it is set to NULL, meaning all reporter countries in the data frames will be considered.

filter_period

A vector specifying the time periods to filter, based on the time_period column. By default, it is set to NULL, meaning all time periods in the data frames will be considered.

Details

The function combines external imports from third countries, internal trade between the countries of interest and internal production data. It calculates the total amount of product available per country in each time period as the sum of external imports (from pest-free and pest-present countries) and internal production.

IDs - country identification codes:

For the IDs of the countries of interest (i.e., in the the columns reporter of the four trade data frames and in the column partner of intra_trade) it is recommended to use the the ISO 3166-1 (alpha-2) codes (ISO 3166 Maintenance Agency) or NUTS0 codes in case of European countries (Nomenclature of territorial units for statistics) for subsequent compatibility with other functions of the qPRAentry package.

Time periods:

Time periods can be specified in any way, both numeric and character formatting is supported. For example, it can be expressed as years, months, specific periods, seasons, etc.

Trade adjustments:

Trade imbalances are adjusted, so that in case the internal export for a given country exceeds the total quantity available in that country, the internal export is recalculated proportionally based on the total available. Missing values are treated as zeros.

Value

An object of class TradeData is returned containing the following list of data frames:

  • total_trade ⁠ ⁠ A data frame with one row for each ID and each time period with 9 variables:

    country_IDs ⁠ ⁠ IDs of the countries of interest.
    time_period ⁠ ⁠ Time period.
    extra_total ⁠ ⁠ Total imports from third countries.
    extra_pest ⁠ ⁠ Imports from third countries where the pest of interest is present.
    intra_import ⁠ ⁠ Internal import from the countries of interest.
    intra_export ⁠ ⁠ Internal export to the countries of interest.
    internal_production ⁠ ⁠ Internal production in the countries of interest.
    total_available ⁠ ⁠ Total available quantity in the countries of interest.
    export_prop ⁠ ⁠ Proportion of internal export to the total available commodity. A value of 1 indicates that internal export is less than or equal to the total available commodity; a value less than 1 [0, 1) indicates that internal export exceeds the total available.
  • intra_trade ⁠ ⁠ A data frame with values of trade commodity between countries of interest:

    reporter ⁠ ⁠ Importing country ID.
    partner ⁠ ⁠ Exporting country ID.
    time_period ⁠ ⁠ Time period.
    value ⁠ ⁠ Quantity of the commodity traded.
    export_prop ⁠ ⁠ Proportion of internal export to the total available commodity for each trading partner according to the proportion for each partner (export_prop in total_trade).

See Also

load_csv(), ntrade()

Examples

## Example with simulated trade data for Northern America
library(dplyr)
# Load data
data("datatrade_NorthAm")
# Total extra-import data: data contains imports from 5 third countries (column partner). 
extra_total <- datatrade_NorthAm$extra_import
# Extra-import data from countries where the pest is present (e.g., CNTR_1 and CNTR_2)
CNTR_pest <- c("CNTR_1", "CNTR_2")
extra_pest <- datatrade_NorthAm$extra_import %>% filter(partner%in%CNTR_pest)
# Intra-trade data
intra_trade  <- datatrade_NorthAm$intra_trade
# Internal production data
internal_production  <- datatrade_NorthAm$internal_production
# Generate trade data (TradeData object)
trade_NorthAm <- trade_data(extra_total = extra_total,
                            extra_pest = extra_pest,
                            intra_trade = intra_trade,
                            internal_production = internal_production)
head(trade_NorthAm$total_trade)
head(trade_NorthAm$intra_trade)
# Plot the total available quantity of commodity available in each country
library(ggplot2)
plot_countries(data = trade_NorthAm$total_trade,
               iso_col = "country_IDs", 
               values_col = "total_available") +
  xlim(-180,-20) + ylim(0,90)

## Example with simulated trade data for Europe 
# with selected countries and a specific time period
# Load data
data("datatrade_EU")
# Total extra-import data: the total import is identified as partner "Extra_Total"
extra_total <- datatrade_EU$extra_import %>% filter(partner=="Extra_Total")
# Extra-import data from countries where the pest is present
extra_pest <- datatrade_EU$extra_import %>% filter(partner!="Extra_Total")
# Intra-trade data
intra_trade  <- datatrade_EU$intra_trade
# Internal production data
internal_production  <- datatrade_EU$internal_production
# Sample 5 countries from data
filter_IDs <- sample(unique(extra_total$reporter), 5)
# Generate trade data (TradeData object)
trade_EU <- trade_data(extra_total = extra_total,
                       extra_pest = extra_pest,
                       intra_trade = intra_trade,
                       internal_production = internal_production,
                       filter_IDs = filter_IDs,
                       filter_period = 2020)
# Plot the total available quantity of commodity available in each country
plot_countries(data = trade_EU$total_trade, 
               iso_col = "country_IDs", 
               values_col = "total_available") +
  xlim(-30,50) + ylim(25,70)
  

qPRAentry documentation built on April 12, 2025, 1:12 a.m.