This repository has been archived by the owner on Apr 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
00-create-historic-series.R
executable file
·308 lines (267 loc) · 11.4 KB
/
00-create-historic-series.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
source("99-pkgs-funs-dirs.R")
# conversion codes ----
load("../comtrade-codes/02-2-tidy-product-data/product-codes.RData")
load("../comtrade-codes/02-2-tidy-product-data/product-correlation.RData")
hs02_to_hs12 <- product_correlation %>%
select(hs02, hs12) %>%
arrange(hs12) %>%
distinct(hs02, .keep_all = T)
# country codes ----
# d_hs <- open_dataset("hs12-historic",
# partitioning = c("trade_flow", "year", "reporter_iso"))
#
# codes <- map_df(
# 2002:2020,
# function(y) {
# message(y)
#
# y2 <- paste0("year=", y)
#
# d1 <- d_hs02 %>%
# filter(year == y2, aggregate_level == "aggregate_level=0") %>%
# select(year, reporter_iso, reporter_code, reporter) %>%
# collect() %>%
# mutate(
# year = remove_hive(year),
# reporter_iso = remove_hive(reporter_iso)
# ) %>%
# rename(country_iso = reporter_iso,
# country_code = reporter_code,
# country = reporter) %>%
# distinct()
#
# d2 <- d_hs02 %>%
# filter(year == y2, aggregate_level == "aggregate_level=0") %>%
# select(year, partner_iso, partner_code, partner) %>%
# collect() %>%
# mutate(year = remove_hive(year)) %>%
# rename(country_iso = partner_iso,
# country_code = partner_code,
# country = partner) %>%
# distinct()
#
# d <- d1 %>% bind_rows(d2) %>% distinct()
#
# return(d)
# }
# )
# hs02 ----
d_hs02 <- open_dataset("../uncomtrade-datasets-arrow/hs-rev2002/parquet/",
partitioning = c("aggregate_level", "trade_flow", "year", "reporter_iso"))
map(
2002:2014,
function(y) {
message(y)
y2 <- paste0("year=", y)
if (dir.exists(paste0("hs12-historic/trade_flow=export/", y2))) {
return(TRUE)
}
d6 <- d_hs02 %>%
filter(
year == y2,
aggregate_level == "aggregate_level=6"
) %>%
select(trade_flow, year,
reporter_iso, partner_iso,
reporter_code, partner_code,
commodity_code, trade_value_usd) %>%
collect() %>%
filter(!partner_iso %in% c("wld","all")) %>%
mutate(
trade_flow = remove_hive(trade_flow),
year = remove_hive(year),
reporter_iso = remove_hive(reporter_iso)
) %>%
left_join(hs02_to_hs12, by = c("commodity_code" = "hs02"))
d6 <- d6 %>%
mutate(
reporter_iso = case_when(
reporter_code == 80 ~ "e-80", # br. antarctic terr.
reporter_code == 129 ~ "e-129", # Caribbean, not elsewhere specified
reporter_code == 221 ~ "e-221", # Eastern Europe, not elsewhere specified
reporter_code == 290 ~ "e-290", # Northern Africa, not elsewhere specified
reporter_code == 471 ~ "e-471", # Central American Common Market, not elsewhere specified
reporter_code == 472 ~ "e-472", # Africa CAMEU region, not elsewhere specified
reporter_code == 473 ~ "e-473", # laia, nes
reporter_code == 490 ~ "e-490", # other asia, nes
reporter_code == 492 ~ "e-492", # Europe EU, not elsewhere specified
reporter_code == 527 ~ "e-527", # oceania, nes
reporter_code == 536 ~ "e-536", # neutral zone
reporter_code == 568 ~ "e-568", # other europe, nes
reporter_code == 577 ~ "e-577", # other africa, nes
reporter_code == 636 ~ "e-636", # Rest of America, not elsewhere specified
reporter_code == 637 ~ "e-637", # north america and central america, nes
reporter_code == 697 ~ "e-697", # Europe EFTA, not elsewhere specified
reporter_code == 837 ~ "e-837", # bunkers
reporter_code == 838 ~ "e-838", # free zones
reporter_code == 839 ~ "e-839", # special categories
reporter_code == 849 ~ "e-849", # us misc. pacific isds
reporter_code == 879 ~ "e-879", # Western Asia, not elsewhere specified
reporter_code == 899 ~ "e-899", # areas, nes
TRUE ~ reporter_iso
),
partner_iso = case_when(
partner_code == 80 ~ "e-80", # br. antarctic terr.
partner_code == 129 ~ "e-129", # Caribbean, not elsewhere specified
partner_code == 221 ~ "e-221", # Eastern Europe, not elsewhere specified
partner_code == 290 ~ "e-290", # Northern Africa, not elsewhere specified
partner_code == 471 ~ "e-471", # Central American Common Market, not elsewhere specified
partner_code == 472 ~ "e-472", # Africa CAMEU region, not elsewhere specified
partner_code == 473 ~ "e-473", # laia, nes
partner_code == 490 ~ "e-490", # other asia, nes
partner_code == 492 ~ "e-492", # Europe EU, not elsewhere specified
partner_code == 527 ~ "e-527", # oceania, nes
partner_code == 536 ~ "e-536", # neutral zone
partner_code == 568 ~ "e-568", # other europe, nes
partner_code == 577 ~ "e-577", # other africa, nes
partner_code == 636 ~ "e-636", # Rest of America, not elsewhere specified
partner_code == 637 ~ "e-637", # north america and central america, nes
partner_code == 697 ~ "e-697", # Europe EFTA, not elsewhere specified
partner_code == 837 ~ "e-837", # bunkers
partner_code == 838 ~ "e-838", # free zones
partner_code == 839 ~ "e-839", # special categories
partner_code == 849 ~ "e-849", # us misc. pacific isds
partner_code == 879 ~ "e-879", # Western Asia, not elsewhere specified
partner_code == 899 ~ "e-899", # areas, nes
TRUE ~ partner_iso
)
)
d6_check1 <- d6 %>%
ungroup() %>%
select(country_iso = reporter_iso) %>%
distinct()
d6_check2 <- d6 %>%
ungroup() %>%
select(country_iso = partner_iso) %>%
distinct()
d6_check <- d6_check1 %>% bind_rows(d6_check2) %>% distinct()
d6_check <- d6_check %>%
filter(nchar(country_iso) > 5) %>% distinct()
print(d6_check)
stopifnot(nrow(d6_check) == 0)
d6 <- d6 %>%
select(trade_flow, year, reporter_iso, partner_iso, commodity_code = hs12, trade_value_usd) %>%
mutate(
commodity_code = case_when(
is.na(commodity_code) ~ "999999",
TRUE ~ commodity_code
)
) %>%
group_by(trade_flow, year, reporter_iso, partner_iso, commodity_code) %>%
summarise(trade_value_usd = sum(trade_value_usd, na.rm = T))
gc()
missing_codes_count <- d6 %>%
ungroup() %>%
filter(is.na(commodity_code)) %>%
count() %>%
pull()
stopifnot(missing_codes_count == 0)
d6 %>%
group_by(trade_flow, year, reporter_iso) %>%
write_dataset(out_dir, hive_style = T)
rm(d6); gc()
}
)
# hs12 ----
d_hs12 <- open_dataset("../uncomtrade-datasets-arrow/hs-rev2012/parquet/",
partitioning = c("aggregate_level", "trade_flow", "year", "reporter_iso"))
map(
2015:2020,
function(y) {
message(y)
y2 <- paste0("year=", y)
if (dir.exists(paste0("hs12-historic/trade_flow=export/", y2))) {
return(TRUE)
}
d6 <- d_hs12 %>%
filter(
year == y2,
aggregate_level == "aggregate_level=6",
) %>%
select(trade_flow, year,
reporter_iso, partner_iso,
reporter_code, partner_code,
commodity_code, trade_value_usd) %>%
collect() %>%
filter(!partner_iso %in% c("wld","all")) %>%
mutate(
trade_flow = remove_hive(trade_flow),
year = remove_hive(year),
reporter_iso = remove_hive(reporter_iso)
)
d6 <- d6 %>%
mutate(
reporter_iso = case_when(
reporter_code == 80 ~ "e-80", # br. antarctic terr.
reporter_code == 129 ~ "e-129", # Caribbean, not elsewhere specified
reporter_code == 221 ~ "e-221", # Eastern Europe, not elsewhere specified
reporter_code == 290 ~ "e-290", # Northern Africa, not elsewhere specified
reporter_code == 471 ~ "e-471", # Central American Common Market, not elsewhere specified
reporter_code == 472 ~ "e-472", # Africa CAMEU region, not elsewhere specified
reporter_code == 473 ~ "e-473", # laia, nes
reporter_code == 490 ~ "e-490", # other asia, nes
reporter_code == 492 ~ "e-492", # Europe EU, not elsewhere specified
reporter_code == 527 ~ "e-527", # oceania, nes
reporter_code == 536 ~ "e-536", # neutral zone
reporter_code == 568 ~ "e-568", # other europe, nes
reporter_code == 577 ~ "e-577", # other africa, nes
reporter_code == 636 ~ "e-636", # Rest of America, not elsewhere specified
reporter_code == 637 ~ "e-637", # north america and central america, nes
reporter_code == 697 ~ "e-697", # Europe EFTA, not elsewhere specified
reporter_code == 837 ~ "e-837", # bunkers
reporter_code == 838 ~ "e-838", # free zones
reporter_code == 839 ~ "e-839", # special categories
reporter_code == 849 ~ "e-849", # us misc. pacific isds
reporter_code == 879 ~ "e-879", # Western Asia, not elsewhere specified
reporter_code == 899 ~ "e-899", # areas, nes
TRUE ~ reporter_iso
),
partner_iso = case_when(
partner_code == 80 ~ "e-80", # br. antarctic terr.
partner_code == 129 ~ "e-129", # Caribbean, not elsewhere specified
partner_code == 221 ~ "e-221", # Eastern Europe, not elsewhere specified
partner_code == 290 ~ "e-290", # Northern Africa, not elsewhere specified
partner_code == 471 ~ "e-471", # Central American Common Market, not elsewhere specified
partner_code == 472 ~ "e-472", # Africa CAMEU region, not elsewhere specified
partner_code == 473 ~ "e-473", # laia, nes
partner_code == 490 ~ "e-490", # other asia, nes
partner_code == 492 ~ "e-492", # Europe EU, not elsewhere specified
partner_code == 527 ~ "e-527", # oceania, nes
partner_code == 536 ~ "e-536", # neutral zone
partner_code == 568 ~ "e-568", # other europe, nes
partner_code == 577 ~ "e-577", # other africa, nes
partner_code == 636 ~ "e-636", # Rest of America, not elsewhere specified
partner_code == 637 ~ "e-637", # north america and central america, nes
partner_code == 697 ~ "e-697", # Europe EFTA, not elsewhere specified
partner_code == 837 ~ "e-837", # bunkers
partner_code == 838 ~ "e-838", # free zones
partner_code == 839 ~ "e-839", # special categories
partner_code == 849 ~ "e-849", # us misc. pacific isds
partner_code == 879 ~ "e-879", # Western Asia, not elsewhere specified
partner_code == 899 ~ "e-899", # areas, nes
TRUE ~ partner_iso
)
)
d6_check1 <- d6 %>%
ungroup() %>%
select(country_iso = reporter_iso) %>%
distinct()
d6_check2 <- d6 %>%
ungroup() %>%
select(country_iso = partner_iso) %>%
distinct()
d6_check <- d6_check1 %>% bind_rows(d6_check2) %>% distinct()
d6_check <- d6_check %>%
filter(nchar(country_iso) > 5) %>% distinct()
print(d6_check)
stopifnot(nrow(d6_check) == 0)
d6 <- d6 %>%
group_by(trade_flow, year, reporter_iso, partner_iso, commodity_code) %>%
summarise(trade_value_usd = sum(trade_value_usd, na.rm = T))
gc()
d6 %>%
group_by(trade_flow, year, reporter_iso) %>%
write_dataset(out_dir, hive_style = T)
rm(d6); gc()
}
)