Title: | Quantitative Pest Risk Assessment at the Entry Step |
---|---|
Description: | Supports risk assessors in performing the entry step of the quantitative Pest Risk Assessment. It allows the estimation of the amount of a plant pest entering a risk assessment area (in terms of founder populations) through the calculation of the imported commodities that could be potential pathways of pest entry, and the development of a pathway model. Two 'Shiny' apps based on the functionalities of the package are included, that simplify the process of assessing the risk of entry of plant pests. The approach is based on the work of the European Food Safety Authority (EFSA PLH Panel et al., 2018) <doi:10.2903/j.efsa.2018.5350>. |
Authors: | Martina Cendoya [aut, cre] , Maria Chiara Rosace [aut] |
Maintainer: | Martina Cendoya <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.1.0 |
Built: | 2025-01-16 06:57:14 UTC |
Source: | CRAN |
Simulated trade data for a commodity within the European Union (EU). This dataset illustrates the trade and production flow of a commodity that could serve as a potential pathway for the entry of a pest into EU countries, in yearly time periods. It also includes simulated consumption data for the commodity at NUTS1 level. This dataset serves as a reference for how the data should be structured and as a basis for the examples included in the qPRAentry package to deal with the NUTS code system (Nomenclature of territorial units for statistics).
datatrade_EU
datatrade_EU
A list of four data frames:
extra_import
Data on imports from countries outside of the EU.
A data frame with 216 rows and 4 columns:
reporter |
|
NUTS0 code of the importing country. |
partner |
|
ID of the exporting countries, coded as "Extra_Total" for all external countries, and "CNTR_1", "CNTR_2", "CNTR_3" to indicate individual non-EU countries where the pest is present. |
time_period |
|
Time period of trade, with values 2020 and 2021. |
value |
|
Quantity of the commodity imported. |
intra_trade
Data on internal trade within EU member countries.
A data frame with 1404 rows and 4 columns:
reporter |
|
NUTS0 code of the importing country. |
partner |
|
NUTS0 code of the exporting country. |
time_period |
|
Time period of trade, with values 2020 and 2021. |
value |
|
Quantity of the commodity imported. |
internal_production
Data on the internal production of
the commodity within the EU. A data frame with 54 rows and 3 columns:
reporter |
|
NUTS0 code of the producing country. |
time_period |
|
Time period of trade, in years (2020 and 2021). |
value |
|
Quantity of the commodity produced. |
consumption_nuts1
Data on the consumption of the commodity
in the EU at NUTS1 level (administrative divisions). A data frame with 92 rows
and 2 columns:
NUTS_ID |
|
NUTS1 code of the administrative division. |
value |
|
Quantity of the commodity consumed. |
Simulated trade data for a commodity within Northern America. This dataset illustrates the trade and production flow of a commodity that could serve as a potential pathway for the entry of a pest into Northern American countries. The data is organised in three-month periods within a year. It also includes simulated consumption data of the commodity at at the level of principal sub-national divisions (ISO 3166-2 codes). This dataset serves as a reference on how the data should be structured and provides the basis for the examples included in the qPRAentry package to deal with the ISO 3166 code system (ISO 3166 Maintenance Agency).
datatrade_NorthAm
datatrade_NorthAm
A list of four data frames:
extra_import
Data on imports from countries outside of
Northern America. A data frame with 100 rows and 4 columns:
reporter |
|
ISO 3166-1 alpha-2 code of the importing country. |
partner |
|
ID of the exporting country, coded as "CNTR_1" to "CNTR_5" to represent specific non-Northern American countries. |
time_period |
|
Time period of trade, in three-month periods ("January-March", "April-June", "July-September", "October-December"). |
value |
|
Quantity of the commodity imported. |
intra_trade
Data on internal trade within Northern American countries.
A data frame with 80 rows and 4 columns:
reporter |
|
ISO 3166-1 alpha-2 code of the importing country. |
partner |
|
ISO 3166-1 alpha-2 code of the exporting country. |
time_period |
|
Time period of trade, in three-month periods ("January-March", "April-June", "July-September", "October-December"). |
value |
|
Quantity of the commodity imported. |
internal_production
Data on the internal production of the commodity
within Northern America. A data frame with 20 rows and 3 columns:
reporter |
|
ISO 3166-1 alpha-2 code of the producing country. |
time_period |
|
Time period of trade, in three-month periods ("January-March", "April-June", "July-September", "October-December"). |
value |
|
Quantity of the commodity produced. |
consumption_iso2
Data on the consumption of the commodity in
Northern America at ISO 3166-2 level (principal subdivisions of countries).
A data frame with 83 rows and 2 columns:
iso_3166_2 |
|
ISO 3166-2 code of the subdivision. |
value |
|
Quantity of the commodity consumed. |
Reads CSV files containing more than one column of data. It automatically detects common separators (i.e., comma, semicolon, tab), while allowing users to specify encoding and decimal separators as required.
load_csv(filepath, dec = ".", encoding = getOption("encoding"))
load_csv(filepath, dec = ".", encoding = getOption("encoding"))
filepath |
A string specifying the path to the CSV file (.csv extension). |
dec |
A character specifying the decimal separator to use. Default is "." (period). |
encoding |
A string specifying the encoding of the file. Default is the native
system encoding set by |
A data frame containing the data from the CSV file.
# file path fpath <- system.file("extdata", "data_ex.csv", package="qPRAentry") # Load a CSV file df <- load_csv(fpath) head(df) # value as character # Load a CSV file with comma separated decimals df <- load_csv(fpath, dec = ",") # value as numeric
# file path fpath <- system.file("extdata", "data_ex.csv", package="qPRAentry") # Load a CSV file df <- load_csv(fpath) head(df) # value as character # Load a CSV file with comma separated decimals df <- load_csv(fpath, dec = ",") # value as numeric
Calculates the quantity of potentially infested imported commodity
() from third countries where the pest is present, based on the
provided trade data (
TradeData
object output of the trade_data()
function).
ntrade( trade_data, filter_IDs = NULL, filter_period = NULL, summarise_result = NULL )
ntrade( trade_data, filter_IDs = NULL, filter_period = NULL, summarise_result = NULL )
trade_data |
An object of class |
filter_IDs |
A vector containing the country IDs to filter (identification codes
of the countries of interest). By default, it is set to |
filter_period |
A vector specifying the time periods to filter, based on
the |
summarise_result |
A character vector specifying functions to summarise the
|
The calculation of for each country of interest
is based on the equation:
where:
: quantity of commodity from third countries remaining in
country
, taking into account the direct importation from third countries
where the pest is present, the re-exportation to other countries of interest,
and the indirect importation of the commodity from other countries of interest.
and
: quantity of commodity imported by
country
and country
from third countries where the pest is present
(direct import), during the period of time considered.
and
: proportion of intra-regional trade relative
to the total available quantity in the exporting country defined as:
Specifically, indicates the proportion of the commodity that is exported
from country
to country
(
), while
indicates the proportion exported from country
to country
(
),
in both cases out of the total available commodity in the exporter country. The total
available quantity is considered as tha sum of the internal production of the country
(
) and the total quantity imported from third countries (
).
Thus, the quantity of
re-exported from country
to all countries
is approximated by
, and the quantity
of
re-exported from all countries
to country
as
.
A data frame with the quantity of commodity imported by each country of interest
(country_IDs
) from countries or regions where the pest is present. The result
is returned for each time period if summarise_result
is not specified
(default is NULL
). If a summary function is specified, the result will be
summarised accordingly.
## Example with simulated trade data for Northern America library(dplyr) 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) # Calculation of the Ntrade for each time period ntrade_NorthAm <- ntrade(trade_data = trade_NorthAm) head(ntrade_NorthAm) # Ntrade summary for the time periods ntrade_NorthAm_summary <- ntrade(trade_data = trade_NorthAm, summarise_result = c("mean", "sd", "quantile(0.025)", "median", "quantile(0.975)")) head(ntrade_NorthAm_summary) # Plot the median of Ntrade library(ggplot2) plot_countries(data = ntrade_NorthAm_summary, iso_col = "country_IDs", values_col = "median") + xlim(-180,-20) + ylim(0,90) ## Example with simulated trade data for Europe # 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 # 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) # Ntrade mean and sd for the time periods ntrade_EU <- ntrade(trade_data = trade_EU, summarise_result = c("mean", "sd")) # Plot Ntrade mean plot_countries(data = ntrade_EU, iso_col="country_IDs", values_col="mean") + xlim(-40,50) + ylim(25,70) # Ntrade for selected countries and a specific time period # Sample 5 countries from trade data country_IDs <- sample(unique(trade_EU$total_trade$country_IDs), 5) ntrade_EU_s <- ntrade(trade_data = trade_EU, filter_IDs = country_IDs, filter_period = 2020) head(ntrade_EU_s) # Plot Ntrade result plot_countries(data = ntrade_EU_s, iso_col="country_IDs", values_col="Ntrade_2020") + xlim(-40,50) + ylim(25,70)
## Example with simulated trade data for Northern America library(dplyr) 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) # Calculation of the Ntrade for each time period ntrade_NorthAm <- ntrade(trade_data = trade_NorthAm) head(ntrade_NorthAm) # Ntrade summary for the time periods ntrade_NorthAm_summary <- ntrade(trade_data = trade_NorthAm, summarise_result = c("mean", "sd", "quantile(0.025)", "median", "quantile(0.975)")) head(ntrade_NorthAm_summary) # Plot the median of Ntrade library(ggplot2) plot_countries(data = ntrade_NorthAm_summary, iso_col = "country_IDs", values_col = "median") + xlim(-180,-20) + ylim(0,90) ## Example with simulated trade data for Europe # 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 # 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) # Ntrade mean and sd for the time periods ntrade_EU <- ntrade(trade_data = trade_EU, summarise_result = c("mean", "sd")) # Plot Ntrade mean plot_countries(data = ntrade_EU, iso_col="country_IDs", values_col="mean") + xlim(-40,50) + ylim(25,70) # Ntrade for selected countries and a specific time period # Sample 5 countries from trade data country_IDs <- sample(unique(trade_EU$total_trade$country_IDs), 5) ntrade_EU_s <- ntrade(trade_data = trade_EU, filter_IDs = country_IDs, filter_period = 2020) head(ntrade_EU_s) # Plot Ntrade result plot_countries(data = ntrade_EU_s, iso_col="country_IDs", values_col="Ntrade_2020") + xlim(-40,50) + ylim(25,70)
Interactive application for the calculation and redistribution of the potentially
infected/infested quantity of commodities imported by a country from third countries
where the pest is present () using the NUTS coding system. See
Nomenclature of territorial units for statistics.
ntrade_app()
ntrade_app()
No return value, called for side effects
ntrade()
, redist_nuts()
, redist_iso()
if (interactive()) { ntrade_app() }
if (interactive()) { ntrade_app() }
Interactive application to estimate the number of potential founder populations ()
of a pest in different regions or countries using the NUTS
coding system. See
Nomenclature of territorial units for statistics.
pathway_app()
pathway_app()
No return value, called for side effects
if (interactive()) { pathway_app() }
if (interactive()) { pathway_app() }
Estimates the number of potential founder populations () of a pest
in different regions, using
data combined with additional user-defined
parameters.
pathway_model( ntrade_data, IDs_col, values_col, expression, parameters, niter = 100 )
pathway_model( ntrade_data, IDs_col, values_col, expression, parameters, niter = 100 )
ntrade_data |
A data frame with the quantity of potentially infested commodities
imported from third countries where the pest is present ( |
IDs_col |
A string specifying the column name in |
values_col |
A string specifying the column name in |
expression |
A string of characters representing the equation for the pathway model.
This expression must not include
|
parameters |
A named list specifying the distributions for each parameter
used in
See details on Parameter distributions for a list of available distributions and examples on how to specify them. |
niter |
The number of iterations to generate random samples from the distributions. The default is 100 iterations. |
The use of ISO 3166 (alpha-2) codes
(ISO 3166 Maintenance Agency),
or NUTS codes in the case of European countries
Nomenclature of territorial units for statistics,
as country or region identifiers (IDs_col
) is recommended
for subsequent compatibility with other functions of the qPRAentry package.
The following distributions are supported. For details on their parameters, refer to the linked R documentation:
Distribution | |
Documentation |
"beta" | |
rbeta() (Beta distribution) |
"binom" | |
rbinom() (Binomial distribution) |
"cauchy" | |
rcauchy() (Cauchy distribution) |
"chisq" | |
rchisq() (Chi-squared distribution) |
"exp" | |
rexp() (Exponential distribution) |
"f" | |
rf() (F distribution) |
"gamma" | |
rgamma() (Gamma distribution) |
"geom" | |
rgeom() (Geometric distribution) |
"lnorm" | |
rlnorm() (Log-normal distribution) |
"nbinom" | |
rnbinom() (Negative Binomial distribution) |
"norm" | |
rnorm() (Normal distribution) |
"pois" | |
rpois() (Poisson distribution) |
"t" | |
rt() (Student's t distribution) |
"unif" | |
runif() (Uniform distribution) |
"weibull" | |
rweibull() (Weibull distribution) |
For example, to specify a normal distribution with mean 0 and standard deviation 1:list(dist = "norm", mean = 0, sd = 1)
Ensure that all parameters required by the chosen distribution are included.
A data frame with the statistics (mean, SD, minimum, first quartile,
median, third quartile, and maximum) resulting from the iterations of the
for each country/region and for the total (i.e., the results for the set of all
countries/regions).
## Example using Northern American countries and ntrade simulated data data("datatrade_NorthAm") # Extract country IDs and simulate ntrade data IDs <- datatrade_NorthAm$internal_production$reporter df <- data.frame(IDs = IDs, ntrade_values = abs(rnorm(length(IDs), 10000, 2000))) # Expression for the pathway model using 3 parameters eq <- "(1/P1)*P2*P3" # Distribution for each parameter parameters <- list( P1 = list(dist = "beta", shape1 = 0.5, shape2 = 1), P2 = list(dist = "gamma", shape = 1.5, scale = 100), P3 = list(dist = "lnorm", mean = 5, sd = 2) ) # Run pathway_model() res_pathway <- pathway_model(ntrade_data = df, IDs_col = "IDs", values_col = "ntrade_values", expression = eq, parameters = parameters, niter = 100) head(res_pathway) # summary of the total for all countries res_pathway[res_pathway$IDs == "Total",] # plot plot_countries(res_pathway, "IDs", "Median")
## Example using Northern American countries and ntrade simulated data data("datatrade_NorthAm") # Extract country IDs and simulate ntrade data IDs <- datatrade_NorthAm$internal_production$reporter df <- data.frame(IDs = IDs, ntrade_values = abs(rnorm(length(IDs), 10000, 2000))) # Expression for the pathway model using 3 parameters eq <- "(1/P1)*P2*P3" # Distribution for each parameter parameters <- list( P1 = list(dist = "beta", shape1 = 0.5, shape2 = 1), P2 = list(dist = "gamma", shape = 1.5, scale = 100), P3 = list(dist = "lnorm", mean = 5, sd = 2) ) # Run pathway_model() res_pathway <- pathway_model(ntrade_data = df, IDs_col = "IDs", values_col = "ntrade_values", expression = eq, parameters = parameters, niter = 100) head(res_pathway) # summary of the total for all countries res_pathway[res_pathway$IDs == "Total",] # plot plot_countries(res_pathway, "IDs", "Median")
Plots country values on a map using data provided and allows customisation of various aesthetics, such as colors, legend title, and title.
plot_countries( data, iso_col, values_col, colors = NULL, na_value = "grey", title = NULL, legend_title = NULL )
plot_countries( data, iso_col, values_col, colors = NULL, na_value = "grey", title = NULL, legend_title = NULL )
data |
A data frame containing the values to be plotted on the map. |
iso_col |
A string specifying the column name in |
values_col |
A string specifying the column name in |
colors |
Optional vector of colors used in the gradient scale. |
na_value |
Color for missing values (default is "grey"). |
title |
A title for the plot (default is |
legend_title |
A title for the legend. Default |
Extracts an sf
object from the giscoR
package.
It uses the ggplot2
package for the representation. Also, it supports the addition
of other ggplot2
options (see examples).
A ggplot
object with the plotted countries.
# Simulated data trade in Northern America data("datatrade_NorthAm") # Mean of internal production for each country library(dplyr) data_plot <- datatrade_NorthAm$internal_production %>% group_by(reporter) %>% summarise(mean_value = mean(value)) head(data_plot) #Plot pl <- plot_countries(data = data_plot, iso_col = "reporter", values_col = "mean_value") pl # Changing colors and adding other ggplot2 options library(ggplot2) pl <- plot_countries(data = data_plot, iso_col = "reporter", values_col = "mean_value", colors = c("white", "lightblue", "darkblue"), title = "Plot internal production", legend_title = "units") pl + xlim(-170, -20) + ylim(10, 90) + theme_bw()
# Simulated data trade in Northern America data("datatrade_NorthAm") # Mean of internal production for each country library(dplyr) data_plot <- datatrade_NorthAm$internal_production %>% group_by(reporter) %>% summarise(mean_value = mean(value)) head(data_plot) #Plot pl <- plot_countries(data = data_plot, iso_col = "reporter", values_col = "mean_value") pl # Changing colors and adding other ggplot2 options library(ggplot2) pl <- plot_countries(data = data_plot, iso_col = "reporter", values_col = "mean_value", colors = c("white", "lightblue", "darkblue"), title = "Plot internal production", legend_title = "units") pl + xlim(-170, -20) + ylim(10, 90) + theme_bw()
Plots NUTS region values on a map using the provided data and allows customisation of various aesthetics, such as colors, legend title, and title.
plot_nuts( data, nuts_col, values_col, nuts_level = 2, nuts_year = "2016", colors = NULL, na_value = "grey", title = NULL, legend_title = NULL )
plot_nuts( data, nuts_col, values_col, nuts_level = 2, nuts_year = "2016", colors = NULL, na_value = "grey", title = NULL, legend_title = NULL )
data |
A data frame containing the values to be plotted on the map. |
nuts_col |
A string specifying the column name in |
values_col |
A string specifying the column name in |
nuts_level |
A numeric value (0, 1, 2, or 3) specifying the NUTS level to plot. Default is 2 indicating NUTS2. See Nomenclature of territorial units for statistics. |
nuts_year |
Year of NUTS classification. Accepted values are '2003','2006', '2010','2013','2016' (default),'2021', or '2024'. See NUTS - History. |
colors |
Optional vector of colors used in the gradient scale. |
na_value |
Color for missing values (default is "grey"). |
title |
A title for the plot (default is |
legend_title |
A title for the legend. Default |
Extracts an sf
object from the giscoR
package.
It uses the ggplot2
package for the representation. Also, it supports the addition
of other ggplot2
options (see examples).
A ggplot object with the plotted NUTS regions.
## Example plot at NUTS0 level (country level) # Simulated data trade in European countries data("datatrade_EU") # Mean of internal production for each country library(dplyr) data_plot <- datatrade_EU$internal_production %>% group_by(reporter) %>% summarise(mean_value = mean(value)) head(data_plot) #Plot pl <- plot_nuts(data = data_plot, nuts_col = "reporter", values_col = "mean_value", nuts_level = 0) pl ## Example plot at NUTS1 level (codes extracted from 'giscoR' package) library(dplyr) library(giscoR) data_plot <- gisco_get_nuts(nuts_level=1) %>% select(NUTS_ID) %>% # simulate values for each NUTS1 mutate(values = abs(rnorm(nrow(.), 0, 1000))) #Plot pl <- plot_nuts(data = data_plot, nuts_col = "NUTS_ID", values_col = "values", nuts_level = 1, colors = c("white", "lightblue", "darkblue"), title = "NUTS1", legend_title = "units") # Changing colors and adding other ggplot2 options library(ggplot2) pl + xlim(-40, 50) + ylim(20, 70) + theme_bw()
## Example plot at NUTS0 level (country level) # Simulated data trade in European countries data("datatrade_EU") # Mean of internal production for each country library(dplyr) data_plot <- datatrade_EU$internal_production %>% group_by(reporter) %>% summarise(mean_value = mean(value)) head(data_plot) #Plot pl <- plot_nuts(data = data_plot, nuts_col = "reporter", values_col = "mean_value", nuts_level = 0) pl ## Example plot at NUTS1 level (codes extracted from 'giscoR' package) library(dplyr) library(giscoR) data_plot <- gisco_get_nuts(nuts_level=1) %>% select(NUTS_ID) %>% # simulate values for each NUTS1 mutate(values = abs(rnorm(nrow(.), 0, 1000))) #Plot pl <- plot_nuts(data = data_plot, nuts_col = "NUTS_ID", values_col = "values", nuts_level = 1, colors = c("white", "lightblue", "darkblue"), title = "NUTS1", legend_title = "units") # Changing colors and adding other ggplot2 options library(ggplot2) pl + xlim(-40, 50) + ylim(20, 70) + theme_bw()
Value redistribution from country-level (i.e., ISO 3166-1 alpha-2 codes) to principal subdivisions (i.e., ISO 3166-2 codes). See ISO 3166 Maintenance Agency.
redist_iso( data, iso_col, values_col, redist_data, redist_iso_col, redist_values_col )
redist_iso( data, iso_col, values_col, redist_data, redist_iso_col, redist_values_col )
data |
A data frame containing the data at the country-level to redistribute. |
iso_col |
A string specifying the column name in |
values_col |
A string or vector specifying the column name(s) in
|
redist_data |
A data frame with values for each subdivision that will be used as the basis for redistribution. |
redist_iso_col |
A string specifying the column name in |
redist_values_col |
A string specifying the column name in |
This function enables redistribution of values from country-level to principal subdivisions (e.g., provinces or states), proportionally to user-supplied redistribution proportions.
Note that more than one column of values provided in the data frame data can be
redistributed at the same time. The values in columns values_col
and
redist_values_col
must be numeric and positive.
In the context of quantitative pest risk assessment (qPRA) at the entry step,
this function can be applied to redistribute the quantity of potentially infested
commodities (, see
ntrade()
) or the number of potential
founder populations (, see
pathway_model()
). For this purpose,
human population or consumption data from subdivisions are often used for redistribution.
A data frame with the redistributed values across the specified subnational
level. The data frame contains the columns ISO_1
with the codes at country
level, ISO_2
with the codes at subdivision level, proportion
with the
proportion according to which the values have been redistributed, and the columns
corresponding to the redistributed values with the same name specified in values_col
.
## Example of data redistribution in Northern American countries data(datatrade_NorthAm) # Selection of internal production data from January to March to be proportionally # redistributed based on sub-national consumption data data_ip <- datatrade_NorthAm$internal_production data_ip <- data_ip[data_ip$time_period=="January-March",] # consumption data at sub-national level data_sub <- datatrade_NorthAm$consumption_iso2 # Redistribution data_redist <- redist_iso(data = data_ip, iso_col = "reporter", values_col = "value", redist_data = data_sub, redist_iso_col = "iso_3166_2", redist_values_col = "value") head(data_redist)
## Example of data redistribution in Northern American countries data(datatrade_NorthAm) # Selection of internal production data from January to March to be proportionally # redistributed based on sub-national consumption data data_ip <- datatrade_NorthAm$internal_production data_ip <- data_ip[data_ip$time_period=="January-March",] # consumption data at sub-national level data_sub <- datatrade_NorthAm$consumption_iso2 # Redistribution data_redist <- redist_iso(data = data_ip, iso_col = "reporter", values_col = "value", redist_data = data_sub, redist_iso_col = "iso_3166_2", redist_values_col = "value") head(data_redist)
Value redistribution from country-level (NUTS0) to smaller territories (i.e., NUTS1, NUTS2 or NUTS3). See Nomenclature of territorial units for statistics.
redist_nuts( data, nuts_col, values_col, to_nuts = 2, redist_data = "population", redist_nuts_col = NULL, redist_values_col = NULL, population_year = 2023, nuts_year = 2016 )
redist_nuts( data, nuts_col, values_col, to_nuts = 2, redist_data = "population", redist_nuts_col = NULL, redist_values_col = NULL, population_year = 2023, nuts_year = 2016 )
data |
A data frame containing the data at the country-level to redistribute. |
nuts_col |
A string specifying the column name in |
values_col |
A string or vector specifying the column name(s) in |
to_nuts |
A numeric value (1, 2, or 3) specifying the NUTS level for redistribution. Default 2, indicating redistribution to NUTS2. |
redist_data |
A data frame containing values for each subdivision that will be
used as the basis for proportional redistribution. By default, this is set to
|
redist_nuts_col |
A string specifying the column name in |
redist_values_col |
A string specifying the column name in |
population_year |
A numeric value specifying the year(s) of the human population
data to be used for redistribution. Only necessary if |
nuts_year |
Year of NUTS classification. Accepted values are '2003','2006','2010','2013', '2016' (default),'2021', or '2024'. See NUTS - History. |
This function enables redistribution of values from national-level NUTS0 to smaller territorial units (i.e., NUTS1, NUTS2, or NUTS3), either proportionally based on human population data from Eurostat or using user-supplied redistribution proportions. Human population data for redistribution is automatically fetched for the specified time period from the Eurostat database.
Note that more than one column of values provided in the data frame data
can be redistributed at the same time. The values in columns values_col
and redist_values_col
must be numeric and positive.
In the context of quantitative pest risk assessment (qPRA) at the entry step,
this function can be applied to redistribute the quantity of potentially infested
commodities (, see
ntrade()
) or the number of potential
founder populations (, see
pathway_model()
). For this purpose,
human population or consumption data from subdivisions are often used for redistribution.
A data frame with the redistributed values across the specified NUTS
level. The data frame contains the columns NUTSX
with the codes at the
selected NUTS level, NUTS0
with the codes at country level, proportion
with the
proportion according to which the values have been redistributed, and the columns
corresponding to the redistributed values with the same name specified in values_col
.
## Example of data redistribution to NUTS2 using human population data data("datatrade_EU") # extract NUTS0 codes (country level) nuts0 <- unique(datatrade_EU$internal_production$reporter) # simulate values for each country nuts0_data <- data.frame(nuts0 = nuts0, value = abs(rnorm(length(nuts0), 30000, 10000))) # Redistribution data_redist <- redist_nuts(data = nuts0_data, nuts_col = "nuts0", values_col = "value", to_nuts = 2, redist_data = "population", population_year = c(2017, 2018, 2019)) head(data_redist) # Plot plot_nuts(data = data_redist, nuts_level = 2, nuts_col = "NUTS2", values_col = "value") ## Example of data redistribution to NUTS1 using custom data # consumption data at NUTS1 level nuts1_data <- datatrade_EU$consumption_nuts1 # Redistribution data_redist <- redist_nuts(data = nuts0_data, nuts_col = "nuts0", values_col = "value", to_nuts = 1, redist_data = nuts1_data, redist_nuts_col = "NUTS_ID", redist_values_col = "value") head(data_redist) # Plot plot_nuts(data = data_redist, nuts_level = 1, nuts_col = "NUTS1", values_col = "value")
## Example of data redistribution to NUTS2 using human population data data("datatrade_EU") # extract NUTS0 codes (country level) nuts0 <- unique(datatrade_EU$internal_production$reporter) # simulate values for each country nuts0_data <- data.frame(nuts0 = nuts0, value = abs(rnorm(length(nuts0), 30000, 10000))) # Redistribution data_redist <- redist_nuts(data = nuts0_data, nuts_col = "nuts0", values_col = "value", to_nuts = 2, redist_data = "population", population_year = c(2017, 2018, 2019)) head(data_redist) # Plot plot_nuts(data = data_redist, nuts_level = 2, nuts_col = "NUTS2", values_col = "value") ## Example of data redistribution to NUTS1 using custom data # consumption data at NUTS1 level nuts1_data <- datatrade_EU$consumption_nuts1 # Redistribution data_redist <- redist_nuts(data = nuts0_data, nuts_col = "nuts0", values_col = "value", to_nuts = 1, redist_data = nuts1_data, redist_nuts_col = "NUTS_ID", redist_values_col = "value") head(data_redist) # Plot plot_nuts(data = data_redist, nuts_level = 1, nuts_col = "NUTS1", values_col = "value")
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.
trade_data( extra_total, extra_pest, intra_trade, internal_production, filter_IDs = NULL, filter_period = NULL )
trade_data( extra_total, extra_pest, intra_trade, internal_production, filter_IDs = NULL, filter_period = NULL )
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: |
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:
|
intra_trade |
A data frame containing the quantity of commodity traded between
countries of interest. It must contain the following columns:
|
internal_production |
A data frame containing the quantity of commodity
produced internally within each country of interest. It must contain the following columns:
|
filter_IDs |
A vector containing the country IDs to filter (identification codes
of the countries of interest). By default, it is set to |
filter_period |
A vector specifying the time periods to filter, based on
the |
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.
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 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 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.
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 ). |
## 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)
## 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)