修订版 | 98937fb8782470cfb04572a13e5d87f7d56c12a7 (tree) |
---|---|
时间 | 2022-03-19 01:03:31 |
作者 | Lorenzo Isella <lorenzo.isella@gmai...> |
Commiter | Lorenzo Isella |
Script to process the scoreboard with the 2020 data.
@@ -0,0 +1,273 @@ | ||
1 | +rm(list=ls()) | |
2 | +library(tidyverse) | |
3 | +library(janitor) | |
4 | +library(openxlsx) | |
5 | +library(RJSDMX) | |
6 | +library(stringi) | |
7 | +library(stringr) | |
8 | +library(ggsci) | |
9 | +library(Cairo) | |
10 | +library(scales) | |
11 | + | |
12 | +source("/home/lorenzo/myprojects-hg/R-codes/stat_lib.R") | |
13 | + | |
14 | +###################################################################### | |
15 | + | |
16 | +golden_ratio <- golden() | |
17 | + | |
18 | + | |
19 | +## year_expenditure <- 2020 | |
20 | + | |
21 | +year_cut <- 2017 | |
22 | + | |
23 | +years_recent <- 2017:2019 | |
24 | + | |
25 | +df_ini <- read_excel("scb_new.xlsx") | |
26 | + | |
27 | +df <- df_ini %>% | |
28 | + clean_data() %>% | |
29 | + mutate(across(contains("duration"), ~excel_numeric_to_date(.x))) | |
30 | + | |
31 | +year_expenditure <- df %>% | |
32 | + pull(expenditure_year) %>% | |
33 | + max | |
34 | + | |
35 | +df_single_year <- df %>% | |
36 | + filter(expenditure_year==year_expenditure) | |
37 | + | |
38 | + | |
39 | +expenditure_ms <- df_single_year %>% | |
40 | + group_by(member_state) %>% | |
41 | + summarise(aid_elem_mio_eur=sum(aid_element_eur, na.rm=T)) %>% | |
42 | + ungroup %>% | |
43 | + arrange(desc(aid_elem_mio_eur)) | |
44 | + | |
45 | + | |
46 | +ms_list <- expenditure_ms %>% | |
47 | + select(member_state) | |
48 | + | |
49 | + | |
50 | +query1 <- "tec00001/A.B1GQ.CP_MEUR." | |
51 | + | |
52 | + | |
53 | + | |
54 | + | |
55 | + | |
56 | +gdp <- estat_retrieval(query1) %>% | |
57 | + clean_data() | |
58 | + | |
59 | + | |
60 | + | |
61 | +gdp_sel <- gdp %>% | |
62 | + mutate(time_period=as.numeric(time_period)) %>% | |
63 | + filter(time_period==year_expenditure) %>% | |
64 | + select(obs_value, geo) %>% | |
65 | + left_join(y=iso_map_eu28, by=c("geo"="iso2")) %>% | |
66 | + filter(iso3 %in% ms_list$member_state) %>% | |
67 | + select(-geo) | |
68 | + | |
69 | + | |
70 | + | |
71 | + | |
72 | +exp_year_ms <- df %>% | |
73 | + group_by(member_state, expenditure_year) %>% | |
74 | + summarise(exp_mio_eur=sum(aid_element_eur, na.rm=T)) %>% | |
75 | + ungroup | |
76 | + | |
77 | + | |
78 | + | |
79 | +exp_year_ms_wide <- exp_year_ms %>% | |
80 | + make_wide_with_total("expenditure_year", "exp_mio_eur") | |
81 | + | |
82 | + | |
83 | +save_excel(exp_year_ms_wide, "aggregated_exp_year.xlsx") | |
84 | + | |
85 | + | |
86 | +exp_ms_comp <- df %>% | |
87 | + filter(expenditure_year>=year_cut) %>% | |
88 | + mutate(period=if_else(expenditure_year %in% years_recent, | |
89 | + "recent", "last")) %>% | |
90 | + group_by(member_state, expenditure_year, period) %>% | |
91 | + summarise(exp_mio_eur=sum(aid_element_eur, na.rm=T)) %>% | |
92 | + ungroup %>% | |
93 | + group_by(member_state, period) %>% | |
94 | + summarise(aver_exp=mean(exp_mio_eur)) %>% | |
95 | + ungroup %>% | |
96 | + arrange(desc(aver_exp)) %>% | |
97 | + mutate(member_state=fct_inorder(member_state)) %>% | |
98 | + mutate(member_state=fct_rev(member_state)) | |
99 | + | |
100 | + | |
101 | +save_excel(exp_ms_comp, "data_aggreation_last_recent.xlsx") | |
102 | + | |
103 | + | |
104 | +gpl <- ggplot(exp_ms_comp, aes(y = member_state, x=aver_exp, fill=period)) + | |
105 | + geom_bar( stat="identity", alpha=1, position="dodge")+ | |
106 | + my_ggplot_theme2("top") + | |
107 | + scale_fill_npg( labels=c("2020", "Average 2017-2019")) + | |
108 | + | |
109 | + theme(legend.title = element_blank())+ | |
110 | + | |
111 | +coord_cartesian(xlim=c(0,12e4))+ | |
112 | + | |
113 | + | |
114 | +## labs(title="Outward FATS - Persons employed in foreign affiliates\nof EU28 enterprises by partner country (2017)")+ | |
115 | +## ## coord_cartesian(ylim=c(0,8000))+ | |
116 | + | |
117 | +scale_x_continuous(breaks=pretty_breaks(n=5) )+ | |
118 | +## scale_y_discrete(labels = function(x) str_wrap(x, width = 20))+ | |
119 | + | |
120 | +xlab("Aid Expenditure (MIO €)") + | |
121 | +ylab(NULL) | |
122 | +## ylab(NULL) | |
123 | + | |
124 | +ggsave("expenditure.pdf", gpl, width=7*golden_ratio, | |
125 | + height=9, device = cairo_pdf) | |
126 | + | |
127 | + | |
128 | + | |
129 | + | |
130 | + | |
131 | + | |
132 | +#################################################################à | |
133 | +### Covid Expenditure and GDP | |
134 | + | |
135 | + | |
136 | +expenditure_gdp <- expenditure_ms %>% | |
137 | + left_join(y=gdp_sel, by=c("member_state"="iso3")) %>% | |
138 | + mutate(percentage_gdp=aid_elem_mio_eur/obs_value) %>% | |
139 | + arrange(desc(percentage_gdp)) | |
140 | + | |
141 | + | |
142 | +expenditure_covid <- df_single_year %>% | |
143 | + filter(str_detect(all_intq, "(?i)covid")) %>% | |
144 | + filter(!str_detect(all_intq, "Temporary Framework 2009")) %>% | |
145 | + mutate(qualifier_extended=str_split(all_intq, ";")) %>% | |
146 | + mutate(count_qualifiers=map_dbl(qualifier_extended, length)) | |
147 | + | |
148 | + | |
149 | + | |
150 | +expenditure_covid_ms <- expenditure_covid %>% | |
151 | + group_by(member_state) %>% | |
152 | + summarise(aid_elem_mio_eur_covid=sum(aid_element_eur, na.rm=T)) %>% | |
153 | + ungroup %>% | |
154 | + ## arrange(desc(aid_elem_mio_eur_covid)) %>% | |
155 | + left_join(expenditure_ms, by="member_state") %>% | |
156 | + mutate(covid_share=aid_elem_mio_eur_covid/aid_elem_mio_eur) %>% | |
157 | + arrange(desc(covid_share)) %>% | |
158 | + mutate(member_state=fct_inorder(member_state)) %>% | |
159 | + mutate(member_state=fct_rev(member_state)) | |
160 | + | |
161 | + | |
162 | +save_excel(expenditure_covid_ms, "covid_expenditure.xlsx") | |
163 | + | |
164 | +gpl <- ggplot(expenditure_covid_ms, aes(y = member_state, x=covid_share)) + | |
165 | + geom_bar( stat="identity", alpha=1, position="dodge")+ | |
166 | + my_ggplot_theme2("top") + | |
167 | + geom_vline(xintercept=0.5, col="blue")+ | |
168 | + coord_cartesian(xlim=c(0,1))+ | |
169 | + | |
170 | + | |
171 | +## labs(title="Outward FATS - Persons employed in foreign affiliates\nof EU28 enterprises by partner country (2017)")+ | |
172 | +## ## coord_cartesian(ylim=c(0,8000))+ | |
173 | + | |
174 | +scale_x_continuous(breaks=pretty_breaks(n=5) , labels = label_percent())+ | |
175 | +## scale_y_discrete(labels = function(x) str_wrap(x, width = 20))+ | |
176 | + | |
177 | +xlab("Share of Covid Aid") + | |
178 | +ylab(NULL) | |
179 | +## ylab(NULL) | |
180 | + | |
181 | +ggsave("covid_expenditure.pdf", gpl, width=7*golden_ratio, | |
182 | + height=9, device = cairo_pdf) | |
183 | + | |
184 | + | |
185 | + | |
186 | +########################################################################### | |
187 | +########################################################################### | |
188 | +########################################################################### | |
189 | +########################################################################### | |
190 | + | |
191 | +gdp_aver <- gdp %>% | |
192 | + mutate(time_period=as.numeric(time_period)) %>% | |
193 | + filter(time_period >= year_cut ) %>% | |
194 | + mutate(period=if_else(time_period %in% years_recent, | |
195 | + "recent", "last")) %>% | |
196 | + group_by(geo, period) %>% | |
197 | + summarise(aver_gdp=mean(obs_value, na.rm=T)) %>% | |
198 | + ungroup %>% | |
199 | + left_join(y=iso_map_eu28, by=c("geo"="iso2")) | |
200 | + | |
201 | + | |
202 | +expenditure_and_gdp <- exp_ms_comp %>% | |
203 | + left_join(y=gdp_aver, by=c("member_state"="iso3", "period" )) %>% | |
204 | + filter(geo!="UK") %>% | |
205 | + mutate(aid_on_gdp=aver_exp/aver_gdp) %>% | |
206 | + arrange(desc(aid_on_gdp)) %>% | |
207 | + mutate(member_state=fct_inorder(member_state)) %>% | |
208 | + mutate(member_state=fct_rev(member_state)) | |
209 | + | |
210 | + | |
211 | +save_excel(expenditure_and_gdp, "expenditure_vs_gdp.xlsx") | |
212 | + | |
213 | + | |
214 | +gpl <- ggplot(expenditure_and_gdp, aes(y = member_state, x=aid_on_gdp, fill=period)) + | |
215 | + geom_bar( stat="identity", alpha=1, position="dodge")+ | |
216 | + my_ggplot_theme2("top") + | |
217 | + scale_fill_npg( labels=c("2020", "Average 2017-2019")) + | |
218 | + | |
219 | + theme(legend.title = element_blank())+ | |
220 | + | |
221 | +coord_cartesian(xlim=c(0,5e-2))+ | |
222 | + | |
223 | + | |
224 | +## labs(title="Outward FATS - Persons employed in foreign affiliates\nof EU28 enterprises by partner country (2017)")+ | |
225 | +## ## coord_cartesian(ylim=c(0,8000))+ | |
226 | + | |
227 | + scale_x_continuous(breaks=pretty_breaks(n=5), | |
228 | + labels = label_percent(accuracy=1) )+ | |
229 | +## scale_y_discrete(labels = function(x) str_wrap(x, width = 20))+ | |
230 | + | |
231 | +xlab("Aid over GDP") + | |
232 | +ylab(NULL) | |
233 | +## ylab(NULL) | |
234 | + | |
235 | +ggsave("expenditure_vs_gdp.pdf", gpl, width=7*golden_ratio, | |
236 | + height=9, device = cairo_pdf) | |
237 | + | |
238 | + | |
239 | + | |
240 | + | |
241 | + | |
242 | + | |
243 | +####################################################################### | |
244 | + | |
245 | + | |
246 | + | |
247 | + | |
248 | + | |
249 | +## A test for Antoine | |
250 | + | |
251 | +## df_tot_by_year <- df %>% | |
252 | +## filter(expenditure_year>=year_cut)%>% | |
253 | +## group_by(expenditure_year) %>% | |
254 | +## summarise(expenditure=sum(aid_element_eur, na.rm=T)) %>% | |
255 | +## ungroup %>% | |
256 | +## arrange(expenditure_year) | |
257 | + | |
258 | + | |
259 | +## df_env <- df %>% | |
260 | +## filter(scoreboard_objective=="Environmental protection including energy savings", expenditure_year>=year_cut ) %>% | |
261 | +## group_by(expenditure_year, scoreboard_objective) %>% | |
262 | +## summarise(objective_exp=sum(aid_element_eur, na.rm=T)) %>% | |
263 | +## ungroup %>% | |
264 | +## arrange(expenditure_year) %>% | |
265 | +## mutate(percentage=objective_exp/df_tot_by_year$expenditure) | |
266 | + | |
267 | + | |
268 | + | |
269 | + | |
270 | + | |
271 | + | |
272 | + | |
273 | +print("So far so good") |