Revision | ead5c3b4e42a711babe2a69ef5f59a641abf6e4a (tree) |
---|---|
Time | 2021-01-17 07:07:42 |
Author | Lorenzo Isella <lorenzo.isella@gmai...> |
Commiter | Lorenzo Isella |
I completed the code with all the ranking stuff.
@@ -1,7 +1,7 @@ | ||
1 | 1 | rm(list=ls()) |
2 | 2 | |
3 | 3 | |
4 | -## last saved on Time-stamp: "2021-01-15 16:22:40 lorenzo" | |
4 | +## last saved on Time-stamp: "2021-01-16 23:06:49 lorenzo" | |
5 | 5 | |
6 | 6 | |
7 | 7 |
@@ -15,6 +15,9 @@ | ||
15 | 15 | |
16 | 16 | |
17 | 17 | |
18 | + | |
19 | +n_rank <- 20 ### number of values for the discretized ranking | |
20 | + | |
18 | 21 | eu_list <- c("BE", "BG","CZ", "DK", "DE", "EE", "IE", "GR", |
19 | 22 | "ES", "FR", "IT", "CY", "LV", "LT", "LU", "HU", "MT", |
20 | 23 | "NL", "AT","PL", "PT", "RO", "SI", "SK", "FI","SE", "HR" ) %>% |
@@ -339,12 +342,42 @@ | ||
339 | 342 | full_join(y=a6) %>% |
340 | 343 | full_join(y=a7) %>% |
341 | 344 | full_join(y=a8) %>% |
342 | - round_all(3) %>% | |
345 | + ## round_all(3) %>% | |
343 | 346 | left_join(y=prod_codes, by=c("k"="code")) |
344 | 347 | |
345 | 348 | |
346 | 349 | |
347 | 350 | |
351 | +## test_a2 <- a2 %>% | |
352 | +## mutate(N_rank=dense_rank(N_eu_imports)) %>% | |
353 | +## arrange(N_rank) | |
354 | + | |
355 | + | |
356 | +## test_a2_round <- a2 %>% | |
357 | +## round_all(3) %>% | |
358 | +## mutate(N_rank=dense_rank(N_eu_imports)) %>% | |
359 | +## arrange(N_rank) | |
360 | + | |
361 | + | |
362 | + | |
363 | + | |
364 | +## test_a4 <- a4 %>% | |
365 | +## mutate(HHI_rank=dense_rank(HHI_EU_imports)) %>% | |
366 | +## arrange(HHI_rank) | |
367 | + | |
368 | + | |
369 | +## test_a4_round <- a4 %>% | |
370 | +## round_all(3) %>% | |
371 | +## mutate(HHI_rank=dense_rank(HHI_EU_imports)) %>% | |
372 | +## arrange(HHI_rank) | |
373 | + | |
374 | + | |
375 | + | |
376 | + | |
377 | + | |
378 | + | |
379 | + | |
380 | + | |
348 | 381 | ## df_tot_round <- a1 %>% |
349 | 382 | ## full_join(y=a2) %>% |
350 | 383 | ## full_join(y=a3) %>% |
@@ -378,7 +411,8 @@ | ||
378 | 411 | |
379 | 412 | extra_eu_imports <- df_imports_raw %>% |
380 | 413 | select(k, i_value) %>% |
381 | - group_by(k) %>% | |
414 | + group_by(k) %>% ## I just consider the value of the imports from | |
415 | + ### extra EU for every product line k | |
382 | 416 | summarise(extra_EU_import_value=sum(i_value, na.rm=T)) |
383 | 417 | |
384 | 418 |
@@ -394,7 +428,7 @@ | ||
394 | 428 | mutate(rank_N_eu=dense_rank(N_eu_imports), |
395 | 429 | rank_HHI_EU=dense_rank(desc(HHI_EU_imports)), |
396 | 430 | rank_balance=dense_rank(balance_over_imports), |
397 | - rank_price_diff=dense_rank(desc(import_price_difference)), | |
431 | + rank_price_diff=dense_rank(desc(import_price_difference)) | |
398 | 432 | ) %>% |
399 | 433 | rowwise %>% |
400 | 434 | mutate(mean_ranking=mean(c_across(rank_N_eu:rank_price_diff),na.rm=T)) %>% |
@@ -440,11 +474,15 @@ | ||
440 | 474 | select(-q) %>% |
441 | 475 | filter(i %in% codes_eu$country_code, j %in% codes_eu$country_code, |
442 | 476 | t==max(t)) %>% |
443 | - filter(k %in% unique(df_rank$k)) %>% | |
477 | + filter(k %in% df_rank$k) %>% | |
444 | 478 | group_by(k) %>% |
445 | 479 | summarise(number_EU_providers=length(unique(i)), |
446 | 480 | value_intra_EU_exports=sum(v, na.rm=T)) %>% |
447 | - ungroup | |
481 | + ungroup ### it is very important that I use the unique number of EU | |
482 | +### exporters (unique(i)): in general they are repeated because, for a given | |
483 | + # product k, one exporter can export to different EU countries. | |
484 | +###Instead, I can just sum the value of the exports because I do not need | |
485 | +### to consider whether the exporter exports to one or more EU countrues | |
448 | 486 | |
449 | 487 | |
450 | 488 |
@@ -667,6 +705,21 @@ | ||
667 | 705 | save_excel(df_hs2, "share_hs2.xlsx") |
668 | 706 | |
669 | 707 | |
708 | + | |
709 | +df_rank_final <- df_rank %>% | |
710 | + mutate(rank_N_eu_discrete=ntile(N_eu_imports, n_rank), | |
711 | + rank_HHI_EU_discrete=ntile(desc(HHI_EU_imports), n_rank), | |
712 | + rank_balance_discrete=ntile(balance_over_imports,n_rank), | |
713 | + rank_price_diff_discrete=ntile(desc(import_price_difference),n_rank) | |
714 | + ) %>% | |
715 | + rowwise %>% | |
716 | + mutate(mean_ranking_discrete=mean(c_across(rank_N_eu_discrete:rank_price_diff_discrete),na.rm=T)) %>% | |
717 | + ungroup | |
718 | + | |
719 | + | |
720 | +save_excel(df_rank_final, "final_ranking_complete.xlsx") | |
721 | + | |
722 | + | |
670 | 723 | print("So far so good") |
671 | 724 | |
672 | 725 |