trade_data {qPRAentry}R 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:

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)
  

[Package qPRAentry version 0.1.0 Index]