-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.R
1069 lines (817 loc) · 34 KB
/
main.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Settings ####
set.seed(2507)
debughsfclmap <- TRUE
# Parallel backend will be used only if required packages
# are installed
# It will be switched to FALSE if packages are not available
multicore <- TRUE
## If true, the reported values will be in $
## If false the reported values will be in k$
dollars <- FALSE
## If TRUE, use adjustments (AKA "conversion notes")
use_adjustments <- FALSE
# Libraries ####
suppressPackageStartupMessages(library(data.table))
library(stringr)
library(magrittr)
library(scales)
library(tidyr)
library(futile.logger)
suppressPackageStartupMessages(library(dplyr, warn.conflicts = FALSE))
library(faosws)
library(faoswsUtil)
library(faoswsTrade)
flog.threshold(TRACE)
# Development (not SWS-inside) mode addons ####
if(faosws::CheckDebug()){
localsettingspath <- "modules/complete_tf_cpc/sws.yml.example"
SETTINGS <- faoswsModules::ReadSettings(localsettingspath)
flog.debug("Local settings read from %s",
localsettingspath)
flog.debug("Local settings read:",
SETTINGS,
capture = TRUE)
## Define where your certificates are stored
faosws::SetClientFiles(SETTINGS[["certdir"]])
## Get session information from SWS. Token must be obtained from web interface
faosws::GetTestEnvironment(baseUrl = SETTINGS[["server"]],
token = SETTINGS[["token"]])
# Fall-back R_SWS_SHARE_PATH var
if(is.na(Sys.getenv("R_SWS_SHARE_PATH", unset = NA))) {
flog.debug("R_SWS_SHARE_PATH system variable not found.")
Sys.setenv("R_SWS_SHARE_PATH" = tempdir())
flog.debug("R_SWS_SHARE_PATH now points to R temp directory %s",
tempdir())
}
}
flog.debug("User's computation parameters:",
swsContext.computationParams, capture = TRUE)
# SWS user name ####
# Remove domain from username
SWS_USER <- regmatches(
swsContext.username,
regexpr("(?<=/).+$", swsContext.username, perl = TRUE))
stopifnot(!any(is.na(SWS_USER),
SWS_USER == ""))
# Reporting directory ####
reportdir <- file.path(
Sys.getenv("R_SWS_SHARE_PATH"),
SWS_USER,
paste0("complete_tf_cpc_",
format(Sys.time(), "%Y%m%d%H%M%S%Z")))
stopifnot(!file.exists(reportdir))
dir.create(reportdir, recursive = TRUE)
# Open report directory in system default file browser
if(interactive()) browseURL(reportdir)
flog.appender(appender.tee(file.path(reportdir,
"report.txt")))
flog.info("SWS-session is run by user %s", SWS_USER)
flog.debug("R session environment: ",
sessionInfo(), capture = TRUE)
if(!CheckDebug()){
options(error = function(){
dump.frames()
save(last.dump,
file = file.path(reportdir, "last.dump.RData"))
})
}
PID <- Sys.getpid()
# Check that all packages are up to date ####
local({
min_versions <- data.frame(package = c("faoswsUtil", "faoswsTrade",
"dplyr"),
version = c('0.2.11', '0.1.1', '0.5.0'),
stringsAsFactors = FALSE)
for (i in nrow(min_versions)){
# installed version
p <- packageVersion(min_versions[i,"package"])
# required version
v <- package_version(min_versions[i,"version"])
if(p < v){
stop(sprintf("%s >= %s required", min_versions[i,"package"], v))
}
}
})
# Register CPU cores ####
if(multicore) {
if(all(c("doParallel", "foreach") %in%
rownames(installed.packages()))) {
flog.debug("Multicore backend is available.")
cpucores <- parallel::detectCores(all.tests = TRUE)
flog.debug("CPU cores detected: %s.", cpucores)
doParallel::registerDoParallel(cores = cpucores)
} else {
flog.debug("Multicore backend is not available.")
multicore <- FALSE
}
}
##+ swsdebug
## ## local data
## install.packages("//hqfile4/ess/Team_working_folder/A/SWS/faosws_0.8.2.9901.tar.gz",
## repos = NULL,
## type = "source")
## ## SWS data
## install.packages("faosws",
## repos = "http://hqlprsws1.hq.un.fao.org/fao-sws-cran/")
stopifnot(
!is.null(swsContext.computationParams$year),
!is.null(swsContext.computationParams$out_coef))
##' # Parameters
##' - `year`: year for processing.
year <- as.integer(swsContext.computationParams$year)
flog.info("Working year: %s", year)
##' - `out_coef`: coefficient for outlier detection, i.e., the `k` parameter in
##' the *Outlier Detection and Imputation* section.
# See coef argument in ?boxplot.stats
out_coef <- as.numeric(swsContext.computationParams$out_coef)
flog.info("Coefficient for outlier detection: %s", out_coef)
##' - `hs_chapters`: specific HS chapters that are downloaded (this parameter
##' can not be set by the user as it is provided by Team B/C and harcoded).
##' The HS chapters are the following:
hs_chapters <- c(1:24, 33, 35, 38, 40:43, 50:53)
flog.info("HS chapters to be selected:", hs_chapters, capture = T)
startTime = Sys.time()
##' # Input Data
##'
##' ## Supplementary Datasets
##+ datasets
##' - `hsfclmap3`: Mapping between HS and FCL codes extracted from MDB files
##' used to archive information existing in the previous trade system
##' (Shark/Jellyfish). This mapping is provided by a separate package:
##' https://github.com/SWS-Methodology/hsfclmap
message(sprintf("[%s] Reading in hs-fcl mapping", PID))
flog.debug("Reading in hs-fcl mapping")
#data("hsfclmap3", package = "hsfclmap", envir = environment())
hsfclmap3 <- tbl_df(ReadDatatable("hsfclmap3"))
flog.info("HS->FCL mapping table preview:",
glimpse0(hsfclmap3), capture = TRUE)
hsfclmap <- hsfclmap3 %>%
filter_(~startyear <= year &
endyear >= year)
stopifnot(nrow(hsfclmap) > 0)
flog.info("Rows in mapping table after filtering by year: %s",
nrow(hsfclmap))
if(use_adjustments) {
##' - `adjustments`: Adjustment notes containing manually added conversion
##' factors to transform from non-standard units of measurement to standard
##' ones or to obtain quantities from traded values.
## Old precedure
#data("adjustments", package = "hsfclmap", envir = environment())
## New procedure
message(sprintf("[%s] Reading in adjustments", PID))
adjustments <- tbl_df(ReadDatatable("adjustments"))
colnames(adjustments) <- sapply(colnames(adjustments),
function(x) gsub("adj_","",x))
adj_cols_int <- c("year","flow","fcl","partner","reporter")
adj_cols_dbl <- c("hs")
adjustments <- adjustments %>%
mutate_each_(funs(as.integer),adj_cols_int) %>%
mutate_each_(funs(as.double),adj_cols_dbl)
}
##' - `unsdpartnersblocks`: UNSD Tariffline reporter and partner dimensions use
##' different list of geographic are codes. The partner dimesion is more
##' detailed than the reporter dimension. Since we can not split trade flows of
##' the reporter dimension, trade flows of the corresponding partner dimensions
##' have to be assigned the reporter dimension's geographic area code. For
##' example, the code 842 is used for the United States includes Virgin Islands
##' and Puerto Rico and thus the reported trade flows of those territories.
##' Analogous steps are taken for France, Italy, Norway, Switzerland and US
##' Minor Outlying Islands.
data("unsdpartnersblocks", package = "faoswsTrade", envir = environment())
#unsdpartnersblocks <- tbl_df(ReadDatatable("unsdpartnersblocks"))
##' - `fclunits`: For UNSD Tariffline units of measurement are converted to
##' meet FAO standards. According to FAO standard, all weights are reported in
##' tonnes, animals in heads or 1000 heads and for certain commodities,
##' only the value is provided.
data("fclunits", package = "faoswsTrade", envir = environment())
#fclunits <- tbl_df(ReadDatatable("fclunits"))
##' - `comtradeunits`: Translation of the `qunit` variable (supplementary
##' quantity units) in Tariffline data into intelligible unit of measurement,
##' which correspond to bthe standards of quantity recommended by the *World
##' Customs Organization* (WCO) (e.g., `qunit`=8 correspond to *kg*).
##' See: http://unstats.un.org/unsd/tradekb/Knowledgebase/UN-Comtrade-Reference-Tables
data("comtradeunits", package = "faoswsTrade", envir = environment())
#comtradeunits <- tbl_df(ReadDatatable("comtradeunits"))
##' - `EURconversionUSD`: Annual EUR/USD currency exchange rates table from SWS.
data("EURconversionUSD", package = "faoswsTrade", envir = environment())
#EURconversionUSD <- tbl_df(ReadDatatable("eur_conversion_usd"))
hs_chapters_str <-
formatC(hs_chapters, width = 2, format = "d", flag = "0") %>%
as.character %>%
shQuote(type = "sh") %>%
paste(collapse = ", ")
##' # Extract Eurostat Combined Nomenclature Data
##+ es-extract
#### Download ES data ####
##' 1. Download raw data from SWS, filtering by `hs_chapters`.
message(sprintf("[%s] Reading in Eurostat data", PID))
flog.info(toupper("##### Eurostat trade data #####"))
esdata <- ReadDatatable(paste0("ce_combinednomenclature_unlogged_",year),
columns = c("declarant", "partner",
"product_nc", "flow",
"period", "value_1k_euro",
"qty_ton", "sup_quantity",
"stat_regime"),
where = paste0("chapter IN (", hs_chapters_str, ")")
)
flog.info("Raw Eurostat data preview:",
glimpse0(esdata), capture = TRUE)
##' 1. Remove non-numeric codes for reporters/partners/commodities.
## Declarant and partner numeric
## This probably should be part of the faoswsEnsure
esdata <- esdata %>%
mutate_at(vars(declarant, partner),
funs(non_numeric = !grepl("^[[:digit:]]+$", .))) %T>%
{flog.info("Non-numeric area codes: ",
summarize_at(.,
.cols = vars(ends_with("non_numeric")),
.funs = funs(total = sum,
prop = percent(sum(.) / n()))),
capture = TRUE)} %>%
filter_(~!declarant_non_numeric & !partner_non_numeric) %>%
select(-ends_with("non_numeric"))
flog.info("Records after removing non-numeric area codes: %s", nrow(esdata))
## Removing TOTAL from product_nc column
esdata <- esdata[grepl("^[[:digit:]]+$",esdata$product_nc),]
flog.info("Records after removing non-numeric commodity codes: %s", nrow(esdata))
## Only regime 4 is relevant for Eurostat data
esdata <- esdata %>%
filter_(~stat_regime == "4") %>%
## Removing stat_regime as it is not needed anymore
select_(~-stat_regime)
flog.info("Records after filtering by 4th stat regime: %s", nrow(esdata))
# TODO: do we need this piece?
esdata <- tbl_df(esdata)
##' 1. Use standard (common) variable names (e.g., `declarant` becomes `reporter`).
esdata <- adaptTradeDataNames(tradedata = esdata, origin = "ES")
# Fiter out HS codes which don't participate in futher processing
# Such solution drops all HS codes shorter than 6 digits.
esdata <- filterHS6FAOinterest(esdata)
##' 1. Convert ES geonomenclature country/area codes to FAO codes.
##+ geonom2fao
# TODO now we turn esdata back from data.frame to data.table
# do we need it?
esdata <- data.table::as.data.table(esdata)
esdata[, `:=`(reporter = convertGeonom2FAO(reporter),
partner = convertGeonom2FAO(partner))]
esdata <- esdata[partner != 252, ]
flog.info("Records after removing partners' 252 code: %s", nrow(esdata))
esdata <- tbl_df(esdata)
##' 1. Remove reporters with area codes that are not included in MDB commodity
##' mapping area list.
##+ es-treat-unmapped
esdata_not_area_in_fcl_mapping <- esdata %>%
filter_(~!(reporter %in% unique(hsfclmap$area)))
write.csv(esdata_not_area_in_fcl_mapping,
file = file.path(reportdir,
"esdata_not_area_in_fcl_mapping.csv"))
esdata <- esdata %>%
filter_(~reporter %in% unique(hsfclmap$area))
flog.info("Records after removing areas absent in HS->FCL map: %s",
nrow(esdata))
## es_hs2fcl ####
message(sprintf("[%s] Convert Eurostat HS to FCL", PID))
##' 1. Map HS to FCL.
esdatalinks <- esdata %>% do(hsInRange(.$hs, .$reporter, .$flow,
hsfclmap,
parallel = multicore))
stopifnot(all(c("reporter", "flow", "hs") %in%
colnames(esdatalinks)))
stopifnot(nrow(esdatalinks) > 0)
esdata <- esdata %>%
left_join(esdatalinks, by = c("reporter", "flow", "hs"))
flog.info("Records after HS-FCL mapping: %s",
nrow(esdata))
##' 1. Remove unmapped FCL codes.
## es remove non mapped fcls
esdata_fcl_not_mapped <- esdata %>%
filter_(~is.na(fcl))
write.csv(esdata_fcl_not_mapped,
file = file.path(reportdir,
"esdata_fcl_not_mapped.csv"))
esdata <- esdata %>%
filter_(~!(is.na(fcl)))
flog.info("Records after removing non-mapped HS codes: %s",
nrow(esdata))
##' 1. Add FCL units.
## es join fclunits
esdata <- addFCLunits(tradedata = esdata, fclunits = fclunits)
##' 1. Specific ES conversions: some FCL codes are reported in Eurostat
##' with different supplementary units than those reported in FAOSTAT,
##' thus a conversion is done.
## specific supplementary unit conversion
es_spec_conv <- frame_data(
~fcl, ~conv,
1057L, 0.001,
1068L, 0.001,
1072L, 0.001,
1079L, 0.001,
1083L, 0.001,
1140L, 0.001,
1181L, 1000
)
esdata <- esdata %>%
left_join(es_spec_conv, by='fcl') %>%
mutate_(qty=~ifelse(is.na(conv), qty, qty*conv)) %>%
select_(~-conv)
##' # Extract UNSD Tariffline Data
##+ tradeload
##' 1. Download raw data from SWS, filtering by `hs_chapters`.
message(sprintf("[%s] Reading in Tariffline data", PID))
tldata <- ReadDatatable(paste0("ct_tariffline_unlogged_",year),
columns=c("rep", "tyear", "flow",
"comm", "prt", "weight",
"qty", "qunit", "tvalue",
"chapter"),
where = paste0("chapter IN (", hs_chapters_str, ")")
)
##+ tl_m49fao
## Based on Excel file from UNSD (unsdpartners..)
##' 1. Remove non-numeric commodity codes.
##+ tl-force-numeric-comm
# This probably should be part of the faoswsEnsure
tldata <- tldata[grepl("^[[:digit:]]+$",tldata$comm),]
tldata <- tbl_df(tldata)
##+ tl-aggregate-multiple-rows
##' 1. Identical combinations of reporter / partner / commodity / flow / year / qunit
##' are aggregated.
tldata <- preAggregateMultipleTLRows(tldata)
##' 1. Use standard (common) variable names (e.g., `rep` becomes `reporter`).
tldata <- adaptTradeDataNames(tradedata = tldata, origin = "TL")
tldata <- filterHS6FAOinterest(tldata)
##' 1. Tariffline M49 codes (which are different from official M49)
##' are converted in FAO country codes using a specific convertion
##' table provided by Team ENV.
message(sprintf("[%s] Converting from comtrade to FAO codes", PID))
tldata <- tldata %>%
left_join(unsdpartnersblocks %>%
select_(wholepartner = ~rtCode,
part = ~formula) %>%
# Exclude EU grouping and old countries
filter_(~wholepartner %in% c(251, 381, 579, 581, 711, 757, 842)),
by = c("partner" = "part")) %>%
mutate_(partner = ~ifelse(is.na(wholepartner), partner, wholepartner),
m49rep = ~reporter,
m49par = ~partner,
# Conversion from Comtrade M49 to FAO area list
reporter = ~as.integer(faoswsTrade::convertComtradeM49ToFAO(m49rep)),
partner = ~as.integer(faoswsTrade::convertComtradeM49ToFAO(m49par)))
##+ drop_es_from_tl
##' 1. European countries are removed (will be replaced by ES data).
# They will be replaced by ES data
tldata <- tldata %>%
anti_join(esdata %>%
select_(~reporter) %>%
distinct(),
by = "reporter")
##+ drop_reps_not_in_mdb
##' 1. Area codes not mapping to any FAO country code are removed.
# We drop reporters what are absent in MDB hsfcl map
# because in any case we can proceed their data
tldata_not_area_in_fcl_mapping <- tldata %>%
filter_(~!(reporter %in% unique(hsfclmap$area)))
tldata <- tldata %>%
filter_(~reporter %in% unique(hsfclmap$area))
##+ reexptoexp
##' 1. Re-imports become imports and re-exports become exports.
# { "id": "1", "text": "Import" },
# { "id": "2", "text": "Export" },
# { "id": "4", "text": "re-Import" },
# { "id": "3", "text": "re-Export" }
tldata <- tldata %>%
mutate_(flow = ~recode(flow, '4' = 1L, '3' = 2L))
# TF: Map HS to FCL ####
tldatalinks <- tldata %>%
do(hsInRange(.$hs, .$reporter, .$flow,
hsfclmap,
parallel = multicore))
# Join links with main dataset and simultaneously create and write statistics
# to report
# Probably it is better to split data hadling and report production
tldata <- tldata %>%
left_join(tldatalinks %>%
mutate_(nolink = ~is.na(fcl)) %>%
# prepare data for report
# it is unique links statistic
group_by_(~reporter) %>%
mutate_(uniq_nolink_count = ~sum(nolink),
uniq_nolink_prop = ~sum(nolink) / n()) %>%
ungroup,
by = c("reporter", "flow", "hs")) %T>% # <== magrittr's tee operator!
# We pass tldata with unique links statistics
# to next piece of code with tee operator.
# Tee op allows to evaluate the piece
# but further the previous chunk is passed
# So we use tee op for its side effect (of writing to report file)
{flog.info("Tariffline nonmapped links:",
# Non-mapped not unique links statistics
group_by_(.,
~reporter,
~uniq_nolink_count,
~uniq_nolink_prop) %>%
summarise_(nolink_count = ~sum(nolink),
nolink_prop = ~sum(nolink) / n()) %>%
ungroup %>%
filter_(~nolink_count > 0) %>%
arrange_(~desc(uniq_nolink_prop)) %>%
mutate_at(vars(ends_with("_prop")), percent) %>%
as.data.frame,
capture = TRUE)} %>%
# Everything between tee op and next pipe op is sent to the report
# and was forgotten
select_(~-starts_with("uniq_nolink_"))
##' 1. Remove unmapped FCL codes.
## Non mapped FCL
tldata_fcl_not_mapped <- tldata %>%
filter_(~nolink) %>%
select_(~-nolink)
tldata <- tldata %>%
filter_(~!nolink) %>%
select_(~-nolink)
write.csv(tldata_fcl_not_mapped,
file = file.path(reportdir,
"tldata_fcl_not_mapped.csv"))
#############Units of measurment in TL ####
##' 1. Add FCL units.
tldata <- addFCLunits(tradedata = tldata, fclunits = fclunits)
tldata <- tldata %>%
mutate_(qunit = ~as.integer(qunit)) %>%
left_join(comtradeunits %>%
select_(~qunit, ~wco),
by = "qunit")
## Dataset with all matches between Comtrade and FAO units
ctfclunitsconv <- tldata %>%
select_(~qunit, ~wco, ~fclunit) %>%
distinct() %>%
arrange_(~qunit)
################ Conv. factor (TL) ################
##### Table for conv. factor
##' 1. General TL conversions: some FCL codes are reported in Tariffline
##' with different units than those reported in FAOSTAT, thus a conversion
##' is done.
ctfclunitsconv$conv <- 0
ctfclunitsconv$conv[ctfclunitsconv$qunit == 1] <- NA # Missing quantity
ctfclunitsconv$conv[ctfclunitsconv$fclunit == "$ value only"] <- NA # Missing quantity
ctfclunitsconv$conv[ctfclunitsconv$fclunit == "mt" &
ctfclunitsconv$wco == "l"] <- .001
ctfclunitsconv$conv[ctfclunitsconv$fclunit == "heads" &
ctfclunitsconv$wco == "u"] <- 1
ctfclunitsconv$conv[ctfclunitsconv$fclunit == "1000 heads" &
ctfclunitsconv$wco == "u"] <- .001
ctfclunitsconv$conv[ctfclunitsconv$fclunit == "number" &
ctfclunitsconv$wco == "u"] <- 1
ctfclunitsconv$conv[ctfclunitsconv$fclunit == "mt" &
ctfclunitsconv$wco == "kg"] <- .001
ctfclunitsconv$conv[ctfclunitsconv$fclunit == "mt" &
ctfclunitsconv$wco == "m³"] <- 1
ctfclunitsconv$conv[ctfclunitsconv$fclunit == "mt" &
ctfclunitsconv$wco == "carat"] <- 5e-6
##### Add conv factor to the dataset
tldata <- tldata %>%
left_join(ctfclunitsconv,
by = c("qunit", "wco", "fclunit"))
##' 1. Specific TL conversions: some commodities need a specific conversion.
#### Commodity specific conversion
fcl_spec_mt_conv <- tldata %>%
filter_(~fclunit == "mt" & is.na(weight) & conv == 0) %>%
select_(~fcl, ~wco) %>%
distinct
if(NROW(fcl_spec_mt_conv) > 0){
conversion_factors_fcl <- tldata %>%
filter(!is.na(weight) & !is.na(qty)) %>%
mutate(qw=(weight/qty)/1000) %>%
group_by(fcl, wco) %>%
summarise(convspec=median(qw, na.rm=TRUE)) %>%
ungroup()
fcl_spec_mt_conv <- fcl_spec_mt_conv %>%
left_join(conversion_factors_fcl)
fcl_spec_mt_conv$convspec[is.na(fcl_spec_mt_conv$convspec)] <- 0
### Add commodity specific conv.factors to dataset
tldata <- tldata %>%
left_join(fcl_spec_mt_conv,
by = c("fcl", "wco"))
########## Conversion of units
#### FCL specific conv
tldata$qtyfcl <- tldata$qty * tldata$convspec
#### Common conv
# If no specific conv. factor, we apply general
tldata$qtyfcl <- ifelse(is.na(tldata$convspec),
tldata$qty * tldata$conv,
tldata$qtyfcl)
} else {
tldata$qtyfcl = NA
}
##' 1. If the `quantity` variable is not reported, but the `weight` variable is and
##' the final unit of measurement is tonnes the `weight` is used as `quantity`
tldata$qtyfcl <- ifelse((tldata$qty == 0 | is.na(tldata$qty)) &
tldata$fclunit == "mt" &
is.na(tldata$qtyfcl) &
tldata$weight > 0,
tldata$weight,
tldata$qtyfcl)
# Always use weight if available and fclunit is mt
tldata$qtyfcl <- ifelse(tldata$fclunit=='mt' & !is.na(tldata$weight) & tldata$weight>0,
tldata$weight*0.001,
tldata$qtyfcl)
######### Value from USD to thousands of USD
if (dollars){
esdata$value <- esdata$value * 1000
} else { ## This means it is in k$
tldata$value <- tldata$value / 1000
}
##' 1. Aggregate UNSD Tariffline Data to FCL.
##+ tl_aggregate
# Replace weight (first quantity column) by newly produced qtyfcl column
tldata <- tldata %>%
select_(~year,
~reporter,
~partner,
~flow,
~fcl,
~fclunit,
~hs,
weight = ~qtyfcl,
~qty,
~value)
tldata_mid = tldata
##' # Combine Trade Data Sources
if (use_adjustments) {
##' 1. Application of "adjustment notes" to both ES and TL data.
# TODO Check quantity/weight
# The notes should save the results in weight
esdata <- useAdjustments(tradedata = esdata, year = year, PID = PID,
adjustments = adjustments, parallel = multicore)
tldata <- useAdjustments(tradedata = tldata, year = year,
adjustments = adjustments, parallel = multicore)
}
##+ es_convcur
##' 1. Convert currency of monetary values from EUR to USD using the
##' `EURconversionUSD` table.
esdata$value <- esdata$value * as.numeric(EURconversionUSD %>%
filter(Year == year) %>%
select(ExchangeRate))
##' 1. Combine UNSD Tariffline and Eurostat Combined Nomenclature data sources
##' to single data set.
##' - TL: assign `weight` to `qty`
##' - ES: assign `weight` to `qty` if `fclunit` is equal to `mt`, else keep `qty`
##+ combine_es_tl
tradedata <- bind_rows(
tldata %>%
# Not using as.character() as it will retain scientific notation
mutate_(hs = ~format(hs, scientific = FALSE, trim = TRUE)) %>%
select_(~year, ~reporter, ~partner, ~flow,
~fcl, ~fclunit, ~hs,
qty = ~weight, ~value),
esdata %>%
mutate_(uniqqty = ~ifelse(fclunit == "mt", weight, qty)) %>%
select_(~year, ~reporter, ~partner, ~flow,
~fcl, ~fclunit,~hs,
qty = ~uniqqty, ~value)
)
##' # Outlier Detection and Imputation
##+ calculate_median_uv
tradedata <- tradedata %>%
mutate_(no_quant = ~near(qty, 0) | is.na(qty),
no_value = ~near(value, 0) | is.na(value))
##' 1. Unit values are calculated for each observation at the HS level as ratio
##' of monetary value over quantity `value / qty`.
tradedata <- mutate_(tradedata,
uv = ~ifelse(no_quant | no_value, NA, value / qty))
## Round UV in order to avoid floating point number problems (see issue #54)
tradedata$uv <- round(tradedata$uv, 10)
##+ boxplot_uv
##' 1. Outlier detection by using the logarithm of the unit value.
tradedata <- detectOutliers(tradedata = tradedata, method = "boxplot",
parameters = list(out_coef=out_coef))
##+ impute_qty_uv
##' 1. Imputation of missing quantities and quantities categorized as outliers by
##' applying the method presented in the *Outlier Detection and Imputation* section.
##' The `flagTrade` variable is given a value of 1 if an imputation was performed.
## These flags are also assigned to monetary values. This may need to be revised
## (monetary values are not supposed to be modified).
tradedata <- doImputation(tradedata = tradedata)
##' 1. Aggregate values and quantities by FCL codes.
# Aggregation by fcl
tradedata <- tradedata %>%
select_(~year,
~reporter,
~partner,
~flow,
~fcl,
~fclunit,
~qty,
~value,
~flagTrade) %>%
group_by_(~year, ~reporter, ~partner, ~flow, ~fcl, ~fclunit) %>%
summarise_each_(funs(sum(., na.rm = TRUE)),
vars = c("qty", "value","flagTrade")) %>%
ungroup()
##' 1. Map FCL codes to CPC.
# Adding CPC2 extended code
tradedata <- tradedata %>%
mutate_(cpc = ~fcl2cpc(sprintf("%04d", fcl), version = "2.1"))
# Not resolve mapping fcl2cpc
no_mapping_fcl2cpc = tradedata %>%
select_(~fcl, ~cpc) %>%
filter_(~is.na(cpc)) %>%
distinct_(~fcl) %>%
select_(~fcl) %>%
unlist()
##' 1. Map FAO area codes to M49.
# Converting back to M49 for the system
tradedata <- tradedata %>%
mutate_(reporterM49 = ~fs2m49(as.character(reporter)),
partnerM49 = ~fs2m49(as.character(partner)))
# Report of countries mapping to NA in M49
# 2011: fal 252: "Unspecified" in FAOSTAT area list
countries_not_mapping_M49 <- bind_rows(
tradedata %>% select_(fc = ~reporter, m49 = ~reporterM49),
tradedata %>% select_(fc = ~partner, m49 = ~partnerM49)) %>%
distinct_() %>%
filter_(~is.na(m49)) %>%
select_(~fc) %>%
unlist()
##+ mirror_estimation
##' # Mirror Trade Estimation
##' 1. Obtain list of non-reporting countries as difference between the list of
##' reporter countries and the list of partner countries.
nonreporting <- unique(tradedata$partner)[!is.element(unique(tradedata$partner),
unique(tradedata$reporter))]
##' 1. Swap the reporter and partner dimensions: the value previously appearing
##' as reporter country code becomes the partner country code (and vice versa).
##' 1. Invert the flow direction: an import becomes an export (and vice versa).
##' 1. Calculate monetary mirror value by adding (removing) a 12% mark-up on
##' imports (exports) to account for the difference between CIF and FOB prices.
## Mirroring for non reporting countries
tradedata <- mirrorNonReporters(tradedata = tradedata,
nonreporters = nonreporting)
##' ## Flag management
##' **Note**: work on this section is currently in progress.
##'
##' - observationStatus:
##' - Reporting countries:
##' - `X` if `flagTrade` is zero (i.e., no imputation) and FCL unit != "$ value only"
##' - `I` if `flagTrade` is non-zero (i.e., imputation) and FCL unit != "$ value only"
##' - Non-reporting countries: `E`
##'
##' - flagMethod:
##' - Reporting countries:
##' - `<BLANK>` if `flagTrade` is zero (i.e., no imputation) and FCL unit != "$ value only"
##' - `e` if `flagTrade` is non-zero (i.e., imputation) and FCL unit != "$ value only"
##' - Non-reporting countries: `e`
##+ sws_flag
## Flag from numeric to letters
## TO DO (Marco): need to discuss how to treat flags
## at the moment Status I and method e
## for both imputed and mirrored
## because applying 12% change in mirroring
addFlagsAfterMirror <- function(data=stop("'data' must be defined'"),
nonreporting=NULL) {
## data <- tradedata
copyData <- data
outData <-
copyData %>%
mutate_(
flagObservationStatus =
## ~ifelse((flagTrade > 0) & (fclunit != "$ value only"),
~ifelse(reporter %in% nonreporting,
"E",
ifelse((flagTrade > 0) & (fclunit != "$ value only"),
"I",
"X")
),
flagMethod =
~ifelse(reporter %in% nonreporting,
"e", # both measures in same row; need to overwrite with "c"
# flag for quantities when transforming to normalized
# format
ifelse((flagTrade > 0) & (fclunit != "$ value only"),
"e",
"")
)
)
return(outData)
}
## complete_trade <- tradedata %>%
## mutate_(
## flagObservationStatus = ~ifelse((flagTrade > 0) &
## (fclunit != "$ value only"),"I",""),
## flagMethod = ~ifelse((flagTrade > 0) &
## (fclunit != "$ value only"),"e",""))
complete_trade <-
tradedata %>% addFlagsAfterMirror(nonreporting = nonreporting)
##+ completed_trade_flow
##' # Output for SWS
##' 1. Filter observations with FCL code `1181` (bees).
##' 1. Filter observations with missing CPC codes.
##' 1. Rename dimensions to comply with SWS standard, e.g., `geographicAreaM49Reporter`
##' 1. Calculate unit value (US$ per quantity unit) at CPC level if the quantity is larger than zero
complete_trade_flow_cpc <- complete_trade %>%
filter_(~fcl != 1181) %>% ## Subsetting out bees
select_(~-fcl) %>%
filter_(~!(is.na(cpc))) %>%
transmute_(geographicAreaM49Reporter = ~reporterM49,
geographicAreaM49Partner = ~partnerM49,
flow = ~flow,
timePointYears = ~year,
flagObservationStatus = ~flagObservationStatus,
flagMethod = ~flagMethod,
measuredItemCPC = ~cpc,
qty = ~qty,
unit = ~fclunit,
value = ~value) %>%
## unit of monetary values is "1000 $"
mutate(uv = ifelse(qty > 0, value * 1000 / qty, NA))
##' 1. Transform dataset separating monetary values, quantities and unit values
##' in different rows.
##' 1. Convert monetary values, quantities and unit values to corresponding SWS
##' element codes. For example, a quantity import measured in metric tons is
##' assigned `5610`.
##+ convert_element
complete_trade_flow_cpc <- complete_trade_flow_cpc %>%
tidyr::gather(measuredElementTrade, Value, -geographicAreaM49Reporter,
-geographicAreaM49Partner, -measuredItemCPC,
-timePointYears, -flagObservationStatus,
-flagMethod, -unit, -flow) %>%
rowwise() %>%
mutate_(measuredElementTrade =
~convertMeasuredElementTrade(measuredElementTrade,
unit,
flow)) %>%
ungroup() %>%
filter_(~measuredElementTrade != "999") %>%
select_(~-flow,~-unit)
##' 1. Overwrite **flagMethod** for mirrored quantities: `e` becomes `c`
##+ overwrite_mirror_method_flag
overwriteFlagMethodMirrorQuantities <- function(data=stop("'data' cannot be empty"),
quantityElements=c("5608", "5609", "5610", "5908", "5909", "5910")) {
copyData <- data
outData <-
copyData %>%
mutate_(flagMethod =
~ifelse(flagObservationStatus == "E" & measuredElementTrade %in% quantityElements,
"c",
flagMethod)
)
return(outData)
}
complete_trade_flow_cpc <-
complete_trade_flow_cpc %>%
overwriteFlagMethodMirrorQuantities()