Skip to contents

Compute the Herfindahl-Hirschman Index measuring the concentration of a country's trade across partners or products.

Usage

ct_hhi(
  reporter,
  flow = "X",
  year = NULL,
  by = "partner",
  level = 2L,
  cache = TRUE
)

Arguments

reporter

Character. Reporter country ISO3 code.

flow

Character. "X" or "M". Default "X".

year

Integer. Year to query.

by

Character. Concentrate by "partner" or "product". Default "partner".

level

Integer. HS digit level (only used when by = "product"). Default 2.

cache

Logical. Default TRUE.

Value

A data.frame with columns: year, hhi, concentration, n_items, top_item, top_share_pct.

Details

HHI ranges from 0 (perfectly diversified) to 10,000 (single partner/product). Interpretation: < 1,500 = low concentration, 1,500-2,500 = moderate, > 2,500 = high.

Examples

# \donttest{
op <- options(comtrade.cache_dir = tempdir())

# Export partner concentration
tryCatch(ct_hhi("AUS", flow = "X", year = 2023, by = "partner"),
         error = function(e) NULL)
#> Warning: Country code "ALL" not found in reference table. Passing to API as-is.
#>  No API key set. Using preview endpoint (500 records max, no descriptions).
#>  For full access (100k records, descriptions), get a free key at
#>   <https://comtradedeveloper.un.org/>
#>  Then run: `ct_set_key("your-key")`
#> NULL

# Export product concentration
tryCatch(ct_hhi("AUS", flow = "X", year = 2023, by = "product"),
         error = function(e) NULL)
#>  No API key set. Using preview endpoint (500 records max, no descriptions).
#>  For full access (100k records, descriptions), get a free key at
#>   <https://comtradedeveloper.un.org/>
#>  Then run: `ct_set_key("your-key")`
#> Waiting 2s for retry backoff ■■■■■■■■■■■■■■■                 
#> Waiting 2s for retry backoff ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  
#> NULL

options(op)
# }