+ - 0:00:00
Notes for current slide
Notes for next slide

Open Trade Statistics

Pachá
pachamaltese, pachamaltese

2019-10-19

1 / 24

Contents of the talk

  • Introduction

  • API

  • R Package

  • Dplyr + SQL

  • Creating your own API

  • Shiny apps

2 / 24

Where to reach me

Twitter and Github: pachamaltese

Email: pacha # dcc * uchile * cl

3 / 24

Introduction

  • Open Trade Statistics (OTS) was created with the intention to lower the barrier to working with international economic trade data.

  • It includes a public API, a dashboard, and an R package for data retrieval.

4 / 24

Introduction

5 / 24

Introduction

The project has a major reproducibility flaw.

cat

6 / 24

Introduction

Hardware and software stack

stack

7 / 24

Introduction

The next three slides are an oversimplification just to explain the work in wide terms.

8 / 24

Introduction

The raw data contains missing flows:

stack

9 / 24

Introduction

Possible solution (Anderson & van Wincoop, 2004 propose 8% rate):

stack

10 / 24

Introduction

Customs have changed their coding systems in order to reflect changes in exported products (i.e. in 1960 nobody exported laptops).

stack

11 / 24

Introduction

  • We have 2018 data, similar initiatives have datasets updated to 2016 or 2017.

  • But much more important than that, we converted all years to HS rev 2007 to allow time comparisons.

12 / 24

R package

# easy start
install.packages("tradestatistics")
13 / 24

R package

Fiji exports a lot of water. But how much of its exports to the US are actually water?

library(dplyr)
library(tradestatistics)
fji_usa <- ots_create_tidy_data(
years = 2017, reporters = "fji", partners = "usa",
include_shortnames = T
)
fji_usa_2 <- fji_usa %>%
select(product_shortname_english, export_value_usd) %>%
arrange(-export_value_usd) %>%
mutate(export_value_share = round(100 * export_value_usd /
sum(export_value_usd, na.rm = T), 2))
14 / 24

R package

fji_usa_2
## # A tibble: 736 x 3
## product_shortname_english export_value_usd export_value_share
## <chr> <int> <dbl>
## 1 Water 233431002 60
## 2 Processed Fish 61666883 15.8
## 3 Non-fillet Fresh Fish 18503975 4.76
## 4 Raw Sugar 12600118 3.24
## 5 Broadcasting Equipment 10967992 2.82
## 6 Perfume Plants 7273321 1.87
## 7 Fish Fillets 5540948 1.42
## 8 Unspecified 4246687 1.09
## 9 Non-fillet Frozen Fish 4033516 1.04
## 10 Molasses 3578212 0.92
## # … with 726 more rows
15 / 24

R package

Which country from America is the #1 partner with the European Union (EU-28)?

eu28 <- ots_countries %>%
filter(eu28_member == 1) %>%
select(country_iso)
ame_eu28 <- ots_create_tidy_data(
years = 2017, reporters = "c-am", partners = "all",
table = "yrp"
)
16 / 24
ame_eu28_2 <- ame_eu28 %>%
mutate(is_eu28 = ifelse(partner_iso %in% eu28$country_iso, 1, 0)) %>%
group_by(reporter_iso, is_eu28) %>%
summarise(export_value_usd = sum(export_value_usd, na.rm = T)) %>%
group_by(reporter_iso) %>%
mutate(pct_to_eu28 = export_value_usd / sum(export_value_usd, na.rm = T)) %>%
filter(is_eu28 == 1) %>%
select(reporter_iso, export_value_usd, pct_to_eu28) %>%
arrange(-export_value_usd)
17 / 24
ame_eu28_2
## # A tibble: 48 x 3
## # Groups: reporter_iso [48]
## reporter_iso export_value_usd pct_to_eu28
## <chr> <dbl> <dbl>
## 1 usa 406704170165 0.218
## 2 can 51259679792 0.109
## 3 bra 45042481104 0.174
## 4 mex 34405957170 0.0711
## 5 chl 10937519973 0.144
## 6 arg 10872575385 0.165
## 7 per 8375757038 0.170
## 8 col 8124020268 0.168
## 9 cri 3922634462 0.246
## 10 ecu 3917486407 0.169
## # … with 38 more rows
18 / 24

Code and documentation

github.com/tradestatistics

docs.ropensci.org/tradestatistics

tradestatistics.io

19 / 24

Dplyr + SQL

Your turn

20 / 24

Creating your own API

Your turn

21 / 24

Shiny apps

Your turn

22 / 24

Acknowledgements

  • rOpenSci <3: Amanda, Emily, Jorge, Maelle, Mark and Stefanie
  • DigitalOcean: Danny
  • Highcharter/Design: Joshua and Erasmo
23 / 24

This work is licensed as

Creative Commons Attribution-NonCommercial 4.0 International

To view a copy of this license visit https://creativecommons.org/licenses/by-nc/4.0/

24 / 24

Contents of the talk

  • Introduction

  • API

  • R Package

  • Dplyr + SQL

  • Creating your own API

  • Shiny apps

2 / 24
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow