Newer
Older
# This use case has been written to convert from NUTS 2003 to latest NUTS version (2016)
# in both directions, because FADN data is from 2004 to 2018.
#
requiredPackages = c('fadnUtils','data.table', 'devtools','jsonlite', 'ggplot2', 'tidyverse')
for(p in requiredPackages){
if(!require(p,character.only = TRUE)) install.packages(p)
library(p,character.only = TRUE)
}
CurrentProjectDirectory = "D:/data/fadn/lieferung_20210414/yang/fadn_work_space"
set.data.dir(CurrentProjectDirectory)
str.dir <- "str_dir"
str_data <- readRDS(paste0(get.data.dir(),"/rds/",str.dir,"/fadn.str.all.rds"))
# maps for time series of NUTS1, NUTS2, NUTS3 and FADN region ----
print("# maps for time series of NUTS1, NUTS2, NUTS3 and FADN region")
fadnUtils::nuts.heatmap.group(str_data$info, "NUTS1", countries = "DEU", onepage = FALSE)
fadnUtils::nuts.heatmap.group(str_data$info, "NUTS2", countries = "DEU", onepage = FALSE)
fadnUtils::nuts.heatmap.group(str_data$info, "NUTS3")
fadnUtils::nuts.heatmap.group(str_data$info, "REGION", onepage = TRUE)
cat("
#
# Conversion of fadn data to latest NUTS Version
# Depends on the MIND STEP project and provided data
# - 2004 (NED)-2007-2018
# - regional allocation does not necessarily match to official EU regulation
#
")
# test data ----
test_data <- str_data$info %>%
select(ID,WEIGHT,COUNTRY,REGION,YEAR,NUTS1,NUTS2,NUTS3,UAA,SIZEUR)
# FADN region (from 'fadnUtils' package) ----
final_fadn_region <- test_data %>% left_join(region.trans, by = c( "REGION", "COUNTRY")) %>%
mutate(REGION_final = if_else(is.na(REGION_new), REGION, REGION_new)) %>% select(-c(REGION_new, change_region))
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
cat("
# nuts2 ----
# nuts2.trans includes the complete list of changes at NUTS2 level from NUTS 2003 to latest version 2016
#
#1: Most of the countries need only be recoded. This means NUTS2 regions are merged. There are also
some boundary shifts, but these are not considered as we do not have the true location of the farms.
#2: Special rules for some countries: Some individual countries have their own rules. (split and boundary shift)
#3: Compare finally transformed and original data.
# Special rules are:
# 1: Conversion based on NUTS3 level rules. A visual inspection was made according to NUTS shapefiles.
# SVN: 2003-2006: NUTS3 <- NUTS2 <- NUTS1
# 2010-2013: shapefile check: SI01 = SI03, SI02=SI04
# HUN: recode HU10 to HU12 as we found FADN data only for 'HU12'.
# POL: PL91 and PL92: putting both together as PL9192
# SUO, LTU, IRE : NUTS3 <- NUTS2 <- NUTS1
# UKI: 2010-2013 shapefile for NUTS2: UKIx fadn no data
# 2006-2010 only code change
# 2013-2016: NUTS3 <- NUTS2 <-NUTS1
# UKN, UKN0, UKN1 -> recoded UKN0
# *ignore the number of regional surface except POL
")
NUTS2_convert <- function (test_data){
keep_columns <- c(colnames(test_data), "NUTS1_final", "NUTS2_final")
# print(keep_columns)
# 1: new NUTS2 will be added to fadn original data: only for recoded -----
special_countries <- c("SVN","HUN","POL","SUO","LTU","IRE","UKI")
cat("Sepcial COUNTRIES: ", special_countries,"\n")
cat("Now update NUTS2 (NUTS1) regulation for non-special countries.... \n")
## rules for only non-special countries
nuts2.tran.recoded <- nuts2.trans %>%
filter(!COUNTRY %in% special_countries)
## as we have at maximum two changes of NUTS regulation, we left join two times
test_data_nuts2 <- test_data %>% filter(!COUNTRY %in% special_countries) %>%
left_join(nuts2.tran.recoded, by = c( "NUTS2", "COUNTRY"))
test_data_nuts2 <- test_data_nuts2 %>%
left_join(nuts2.tran.recoded, by = c("NUTS2_new"="NUTS2", "COUNTRY"))
## Now we generate the final NUTS2 regulation based on the left joins
recoded_final <- test_data_nuts2 %>%
mutate(NUTS2_final=case_when(
is.na(NUTS2_new) & is.na(NUTS2_new.y) ~ NUTS2,
!is.na(NUTS2_new) & is.na(NUTS2_new.y) ~ NUTS2_new,
TRUE ~ NUTS2_new.y
)) %>% select(-contains(".x"),-contains(".y"), -NUTS2_new)
## Now we add new NUTS1 - NUTS1_final
recoded_final <- recoded_final %>%
mutate(NUTS1_final=str_sub(NUTS2_final,1,3)) %>% select(keep_columns)
cat("Non-special countries finished.\n")
cat("Special countrie.....\n")
# 2: special rules ----
# hun: fadn data only for HU12 ----
# rename
country_HUN <- test_data %>% filter(COUNTRY=="HUN") %>%
mutate( NUTS2_final = NUTS2,
NUTS2_final = if_else(NUTS2_final=="HU10", "HU12", NUTS2_final),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>% select(keep_columns)
# pol: calcalate boundary shift ----
pol_nuts2_trans <- nuts2.trans %>% filter(COUNTRY == "POL") %>%
filter(NUTS2!="PL12")
## POL code change for some NUTS2
country_POL <- test_data %>% filter(COUNTRY=="POL") %>%
left_join(pol_nuts2_trans, by = c( "NUTS2", "COUNTRY")) %>%
mutate(NUTS2_final=case_when(
is.na(NUTS2_new) ~ NUTS2,
TRUE ~ NUTS2_new
))
## Special case for PL12 in PL91/PL92
country_POL <- country_POL %>%
mutate(NUTS2_final=if_else(NUTS2_final %in% c("PL12","PL91","PL92"),"PL9192",NUTS2_final),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>% select(keep_columns)
## convert based on nuts3 ----
## SUO ----
country_SUO <- test_data %>% filter(COUNTRY=="SUO") %>%
left_join(nuts3.trans, by = c( "NUTS3", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3_new) ~ NUTS3,
TRUE ~ NUTS3_new
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns)
## LTU ----
country_LTU <- test_data %>% filter(COUNTRY=="LTU") %>%
left_join(nuts3.trans, by = c( "NUTS3", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3_new) ~ NUTS3,
TRUE ~ NUTS3_new
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns)
## IRE ----
## IE02 and IE06 must be an error in the FADN data - because it has only 4 digits (must have 5 because of NUTS3)
## adaption: as IE061/IE021 is Dublin and small and IE062/IE022 is missing, we make:
## - IE02 to IE022 and IE06 to IE062
country_IRE <- test_data %>% filter(COUNTRY=="IRE") %>%
mutate(NUTS3=if_else(NUTS3=="IE02","IE022",NUTS3)) %>%
left_join(nuts3.trans, by = c( "NUTS3", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3_new) ~ NUTS3,
TRUE ~ NUTS3_new
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns)
## SVN ----
country_SVN <- test_data %>% filter(COUNTRY=="SVN") %>%
left_join(nuts3.trans, by = c( "NUTS3", "COUNTRY")) %>%
left_join(nuts3.trans, by = c( "NUTS3_new"="NUTS3", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3_new) & is.na(NUTS3_new.y) ~ NUTS3,
!is.na(NUTS3_new) & is.na(NUTS3_new.y) ~ NUTS3_new,
TRUE ~ NUTS3_new.y
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns)
## UKI: ----
## UKI: 2010-2013 shapefile for NUTS2: UKIx fadn no data
## 2006-2010 only code change
## 2013-2016: NUTS3 <- NUTS2 <-NUTS1(UKN: NUTS2 has no change, NUTS3: UKM recoded)
## UKN, UKN0, UKN1 -> recoded UKN0
## United Kingdom has many inaccurate and misleading information for NUTS3
## - there are 4-digit codes as NUTS3 codes - but must be 5-digit
## - there are missing newer NUTS3 codes, but former NUTS3 codes are in the data
## - for instance:
## test_data %>% filter(grepl("UKM",NUTS1)) %>% group_by(YEAR,NUTS3) %>%
## summarise(n=sum(WEIGHT)) %>% pivot_wider(names_from = YEAR,values_from=n) %>%
## print(n=31) %>% view()
## --> there is no UKM21 and UKM22, but newer UKM71 and UKM72
## --> there is UKM35 and UKM36, but no newer UKM83 and UKM84
## solution: NUTS3 UKM2 -> UKM7 and UKM3 -> UKM9
country_UKI_UKM <- test_data %>% filter(grepl("UKM",NUTS2)) %>%
left_join(nuts3.trans, by = c("NUTS3", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3_new) ~ NUTS3,
TRUE ~ NUTS3_new
),
NUTS3_final=case_when(
NUTS3=="UKM2" ~ "UKM7",
NUTS3=="UKM3" ~ "UKM9",
TRUE ~ NUTS3_final
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns)
country_UKI <- test_data %>% filter(!grepl("UKM",NUTS2) & COUNTRY=="UKI") %>%
left_join(nuts2.trans %>% filter(grepl("UKN|UKD",NUTS2)), by = c( "NUTS2", "COUNTRY")) %>%
mutate(NUTS2_final=case_when(
is.na(NUTS2_new) ~ NUTS2,
TRUE ~ NUTS2_new
)) %>%
mutate( NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns)
## rbind UKI all together
country_UKI <- country_UKI %>% bind_rows(country_UKI_UKM)
cat("Special countries finished.\n")
# Combination all countries together ----
cat("Combination of special countries with non-special together....\n")
final_NUTS2 <- recoded_final %>%
bind_rows(list(country_HUN,
country_POL,
country_SUO,
country_LTU,
country_IRE,
country_SVN,
country_UKI))
return(final_NUTS2)
}
fadn.animal <- readRDS(file=paste0(get.data.dir(),"/rds/str_dir/fadn.str.animal.rds")) %>% as.data.table()
final_NUTS2_crops <- NUTS2_convert(test_data = str_data$crops)
final_NUTS2_info <- NUTS2_convert(test_data = str_data$info)
final_NUTS2_animals <- NUTS2_convert(test_data = fadn.animal)
# clear only unused data from global workspace
rm(list=setdiff(ls(), c("str_data",
"fadn.animal",
"final_NUTS2_info",
"final_NUTS2_crops",
"final_NUTS2_animals",
"NUTS2_convert",
"CurrentProjectDirectory")))
# Comparison of converted and original FADN data ----
#
# test_data_nuts2_old <- final_NUTS2 %>% group_by(NUTS2) %>%
# summarise(COUNTRY=unique(COUNTRY),
# UAA=sum(UAA*WEIGHT),
# SIZEUR=sum(SIZEUR*WEIGHT)) %>% group_by(COUNTRY) %>%
# mutate(UAA_NUTS0=sum(UAA),
# SIZEUR_NUTS0=sum(SIZEUR))
#
# test_data_nuts2_final <- final_NUTS2 %>% group_by(NUTS2_final) %>%
# summarise(COUNTRY=unique(COUNTRY),
# UAA=sum(UAA*WEIGHT),
# SIZEUR=sum(SIZEUR*WEIGHT)) %>% group_by(COUNTRY) %>%
# mutate(UAA_NUTS0=sum(UAA),
# SIZEUR_NUTS0=sum(SIZEUR))
#
# test_data_nuts2_old %>% select(COUNTRY,UAA_NUTS0,SIZEUR_NUTS0) %>% distinct() %>%
# left_join(test_data_nuts2_final %>% select(COUNTRY,UAA_NUTS0,SIZEUR_NUTS0) %>% distinct(),
# by=c("COUNTRY")) %>%
# mutate(UAA_NUTS0=UAA_NUTS0.x-UAA_NUTS0.y,
# SIZEUR_NUTS0=SIZEUR_NUTS0.x-SIZEUR_NUTS0.y) %>% print(n=28)
test_data_nuts2 <- final_NUTS2
# Group by NUTS2, NUTS1 and COUNTRY for original data ----
test_data_nuts2_old <- test_data_nuts2 %>% group_by(NUTS2,YEAR) %>%
summarise(COUNTRY=unique(COUNTRY),
NUTS1=unique(NUTS1),
UAA=sum(UAA*WEIGHT),
SIZEUR=sum(SIZEUR*WEIGHT)) %>% group_by(NUTS1,YEAR) %>%
mutate(COUNTRY=unique(COUNTRY),
UAA_NUTS1=sum(unique(UAA)),
SIZEUR_NUTS1=sum(unique(SIZEUR))) %>% group_by(COUNTRY,YEAR) %>%
mutate(UAA_NUTS0=sum(unique(UAA_NUTS1)),
SIZEUR_NUTS0=sum(unique(SIZEUR_NUTS1)))
# Group by NUTS2, NUTS1 and COUNTRY for final data ----
test_data_nuts2_final <- test_data_nuts2 %>% group_by(NUTS2_final,YEAR) %>%
summarise(COUNTRY=unique(COUNTRY),
NUTS1_final=unique(NUTS1_final),
UAA=sum(UAA*WEIGHT),
SIZEUR=sum(SIZEUR*WEIGHT)) %>% group_by(NUTS1_final,YEAR) %>%
mutate(COUNTRY=unique(COUNTRY),
UAA_NUTS1=sum(unique(UAA)),
SIZEUR_NUTS1=sum(unique(SIZEUR))) %>% group_by(COUNTRY,YEAR) %>%
mutate(UAA_NUTS0=sum(unique(UAA_NUTS1)),
SIZEUR_NUTS0=sum(unique(SIZEUR_NUTS1)))
test_data_nuts2_old %>% select(COUNTRY,UAA_NUTS0,SIZEUR_NUTS0) %>% distinct() %>%
left_join(test_data_nuts2_final %>% select(COUNTRY,UAA_NUTS0,SIZEUR_NUTS0) %>% distinct(),
by=c("COUNTRY")) %>%
mutate(UAA_NUTS0=UAA_NUTS0.x-UAA_NUTS0.y,
SIZEUR_NUTS0=SIZEUR_NUTS0.x-SIZEUR_NUTS0.y) %>% print(n=28)
test_data_nuts2_old %>% select(COUNTRY,UAA_NUTS0,SIZEUR_NUTS0) %>% distinct() %>%
left_join(test_data_nuts2_final %>% select(COUNTRY,UAA_NUTS0,SIZEUR_NUTS0) %>% distinct(),
by=c("COUNTRY")) %>%
mutate(UAA_NUTS0=UAA_NUTS0.x-UAA_NUTS0.y,
SIZEUR_NUTS0=SIZEUR_NUTS0.x-SIZEUR_NUTS0.y) %>% print(n=28)
# pivoting original data into a longer form.
test_data_nuts2_old <- test_data_nuts2 %>% select(WEIGHT,COUNTRY,YEAR,NUTS2,UAA,SIZEUR) %>%
pivot_longer(c(UAA:SIZEUR)) %>% mutate(reg=NUTS2,reg_agg="NUTS2") %>% select(-NUTS2) %>%
bind_rows(test_data_nuts2 %>% select(WEIGHT,COUNTRY,YEAR,NUTS1,UAA,SIZEUR) %>%
pivot_longer(c(UAA:SIZEUR)) %>% mutate(reg=NUTS1,reg_agg="NUTS1") %>% select(-NUTS1)) %>%
bind_rows(test_data_nuts2 %>% select(WEIGHT,COUNTRY,YEAR,UAA,SIZEUR) %>%
pivot_longer(c(UAA:SIZEUR)) %>% mutate(reg=COUNTRY,reg_agg="NUTS0"))
test_data_nuts2_old %>% group_by(COUNTRY,reg_agg,name) %>% summarise(value=sum(value*WEIGHT)) %>%
pivot_wider(names_from = name,values_from=value) %>% print(n=84)
# pivoting final data into a longer form.
test_data_nuts2_final <- test_data_nuts2 %>% select(WEIGHT,COUNTRY,YEAR,NUTS2_final,UAA,SIZEUR) %>%
pivot_longer(c(UAA:SIZEUR)) %>% mutate(reg=NUTS2_final,reg_agg="NUTS2") %>% select(-NUTS2_final) %>%
bind_rows(test_data_nuts2 %>% select(WEIGHT,COUNTRY,YEAR,NUTS1_final,UAA,SIZEUR) %>%
pivot_longer(c(UAA:SIZEUR)) %>% mutate(reg=NUTS1_final,reg_agg="NUTS1") %>% select(-NUTS1_final)) %>%
bind_rows(test_data_nuts2 %>% select(WEIGHT,COUNTRY,YEAR,UAA,SIZEUR) %>%
pivot_longer(c(UAA:SIZEUR)) %>% mutate(reg=COUNTRY,reg_agg="NUTS0"))
test_data_nuts2_final %>% group_by(COUNTRY,reg_agg,name) %>% summarise(value=sum(value*WEIGHT)) %>%
pivot_wider(names_from = name,values_from=value)%>% print(n=84)
# Calculating difference between original and final uaa and sizeur.
test_data_nuts2_old %>% group_by(COUNTRY,reg_agg,name) %>% summarise(value=sum(value*WEIGHT)) %>%
pivot_wider(names_from = name,values_from=value) %>%
left_join(test_data_nuts2_final %>% group_by(COUNTRY,reg_agg,name) %>% summarise(value=sum(value*WEIGHT)) %>%
pivot_wider(names_from = name,values_from=value),
by=c("COUNTRY","reg_agg")) %>%
mutate(UAA=UAA.x-UAA.y,
SIZEUR=SIZEUR.x-SIZEUR.y)%>% print(n=84)
# check coeherence ----
## check old NUTS1 -> NUTS0 for coeherence
test_data_nuts2_old %>% filter(reg_agg=="NUTS1") %>%
group_by(COUNTRY,reg,name,YEAR) %>% summarise(value=sum(value*WEIGHT)) %>%
group_by(COUNTRY,name,YEAR) %>% summarise(value=sum(value)) %>%
left_join(test_data_nuts2_old %>% filter(reg_agg=="NUTS0") %>%
group_by(COUNTRY,name,YEAR) %>% summarise(value=sum(value*WEIGHT)),by=c("COUNTRY","name","YEAR")) %>%
mutate(difference=(value.x-value.y),
difference_abs=abs(value.x-value.y)) %>% arrange(-difference_abs)
## check new NUTS1 -> NUTS0 for coeherence
test_data_nuts2_final %>% filter(reg_agg=="NUTS1") %>%
group_by(COUNTRY,reg,name,YEAR) %>% summarise(value=sum(value*WEIGHT)) %>%
group_by(COUNTRY,name,YEAR) %>% summarise(value=sum(value)) %>%
left_join(test_data_nuts2_final %>% filter(reg_agg=="NUTS0") %>%
group_by(COUNTRY,name,YEAR) %>% summarise(value=sum(value*WEIGHT)),by=c("COUNTRY","name","YEAR")) %>%
mutate(difference=(value.x-value.y),
difference_abs=abs(value.x-value.y)) %>% arrange(-difference_abs)
## check old NUTS2 -> NUTS0 for coeherence
test_data_nuts2_old %>% filter(reg_agg=="NUTS2") %>%
group_by(COUNTRY,reg,name,YEAR) %>% summarise(value=sum(value*WEIGHT)) %>%
group_by(COUNTRY,name,YEAR) %>% summarise(value=sum(value)) %>%
left_join(test_data_nuts2_old %>% filter(reg_agg=="NUTS0") %>%
group_by(COUNTRY,name,YEAR) %>% summarise(value=sum(value*WEIGHT)),by=c("COUNTRY","name","YEAR")) %>%
mutate(difference=(value.x-value.y),
difference_abs=abs(value.x-value.y)) %>% arrange(-difference_abs)
## check new NUTS2 -> NUTS0 for coeherence
test_data_nuts2_final %>% filter(reg_agg=="NUTS2") %>%
group_by(COUNTRY,reg,name,YEAR) %>% summarise(value=sum(value*WEIGHT)) %>%
group_by(COUNTRY,name,YEAR) %>% summarise(value=sum(value)) %>%
left_join(test_data_nuts2_final %>% filter(reg_agg=="NUTS0") %>%
group_by(COUNTRY,name,YEAR) %>% summarise(value=sum(value*WEIGHT)),by=c("COUNTRY","name","YEAR")) %>%
mutate(difference=(value.x-value.y),
difference_abs=abs(value.x-value.y)) %>% arrange(-difference_abs)
## check old vs new NUTS1 -> NUTS0 for coeherence
test_data_nuts2_old %>% filter(reg_agg=="NUTS1") %>%
group_by(COUNTRY,reg,name,YEAR) %>% summarise(value=sum(value*WEIGHT)) %>%
group_by(COUNTRY,name,YEAR) %>% summarise(value=sum(value)) %>%
left_join(test_data_nuts2_final %>% filter(reg_agg=="NUTS1") %>%
group_by(COUNTRY,reg,name,YEAR) %>% summarise(value=sum(value*WEIGHT)) %>%
group_by(COUNTRY,name,YEAR) %>% summarise(value=sum(value)),by=c("COUNTRY","name","YEAR")) %>%
mutate(difference=(value.x-value.y),
difference_abs=abs(value.x-value.y)) %>% arrange(-difference_abs)
## check old vs new NUTS2 -> NUTS0 for coeherence
test_data_nuts2_old %>% filter(reg_agg=="NUTS2") %>%
group_by(COUNTRY,name,YEAR) %>% summarise(value=sum(value*WEIGHT)) %>%
left_join(test_data_nuts2_final %>% filter(reg_agg=="NUTS2") %>%
group_by(COUNTRY,name,YEAR) %>% summarise(value=sum(value*WEIGHT)),by=c("COUNTRY","name","YEAR")) %>%
mutate(difference=(value.x-value.y),
difference_abs=abs(value.x-value.y)) %>% arrange(-difference_abs)
plot_data_use_old <- test_data_nuts2_old %>% filter(reg_agg==NUTS) %>%
group_by(COUNTRY,reg,name,YEAR) %>% summarise(value=sum(value*WEIGHT))
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
plot_data_use_final <- test_data_nuts2_final %>% filter(reg_agg==NUTS) %>%
group_by(COUNTRY,reg,name,YEAR) %>% summarise(value=sum(value*WEIGHT))
plot_data_use <- bind_rows("old" =plot_data_use_old , "final" =plot_data_use_final,.id = "dt" ) %>%
pivot_wider(names_from = name, values_from = value)
countries_all <- unique(plot_data_use_old$COUNTRY)
for (country in countries_all) {
cat(country, "\n")
if(length(unique((plot_data_use %>% filter(COUNTRY ==country))$reg))>20) {ncol = 4}
else{ncol = 3}
if (NUTS == "NUTS0") {
p_all <-
ggplot(data = plot_data_use %>% filter(COUNTRY == country &
dt == "old") ,
aes(x = YEAR, y = UAA)) +
geom_line(size = 1.1) +
geom_line(
data = plot_data_use %>% filter(COUNTRY == country & dt == "final"),
aes(x = YEAR, y = UAA),
colour = "red"
) +
facet_wrap(vars(reg),
# space="free",
ncol = ncol,
scale = "free_y") + ggtitle(paste0(country, " :original and converted data")) +
ylim(c(0, NA)) +
scale_x_continuous(breaks = unique(plot_data_use$YEAR))+
scale_y_continuous(labels = scales::comma)
ggsave(plot = p_all, filename = paste0(CurrentProjectDirectory,"/plots/check_nuts/", NUTS,"/" ,country, "_all.png"),width = 18, height = 8)
} else{
p_all <-
ggplot(data = plot_data_use %>% filter(COUNTRY == country &
dt == "old") ,
aes(x = YEAR, y = UAA)) +
geom_line(size = 1.1) +
geom_line(
data = plot_data_use %>% filter(COUNTRY == country & dt == "final"),
aes(x = YEAR, y = UAA),
colour = "red"
) +
facet_wrap(vars(reg),
# space="free",
ncol = ncol,
scale = "free_y") + ggtitle(paste0(country, " :original and converted data")) +
ylim(c(0, NA)) +
scale_x_continuous(breaks = unique(plot_data_use$YEAR))+
scale_y_continuous(labels = scales::comma)
p_final <- plot_data_use_final %>% filter(COUNTRY == country) %>%
pivot_wider(names_from = name, values_from = value) %>%
ggplot(aes(x = YEAR, y = UAA)) +
geom_line(alpha = 0.7) +
geom_point() +
facet_wrap(vars(reg),
# space="free",
ncol = ncol,
scale = "free_y") + ggtitle(paste0(country, ": final data")) +
ylim(c(0, NA)) +
scale_x_continuous(breaks = unique(plot_data_use_final$YEAR))+
scale_y_continuous(labels = scales::comma)
p_old <- plot_data_use_old %>% filter(COUNTRY == country) %>%
pivot_wider(names_from = name, values_from = value) %>%
ggplot(aes(x = YEAR, y = UAA), group = 1) +
geom_line(alpha = 0.7) +
geom_point() +
facet_wrap(vars(reg),
# space="free",
ncol = ncol,
scale = "free_y") +
ggtitle(paste0(country, ": old data")) +
ylim(c(0, NA)) +
scale_x_continuous(breaks = unique(plot_data_use_old$YEAR)) +
scale_y_continuous(labels = scales::comma)
ggsave(plot = p_all, filename = paste0(CurrentProjectDirectory,"/plots/check_nuts/", NUTS,"/" ,country, "_all.png"),width = 18, height = 8)
ggsave(plot = p_final, filename = paste0(CurrentProjectDirectory,"/plots/check_nuts/",NUTS,"/",country, "_final.png"),width = 18, height = 8)
ggsave(plot = p_old, filename = paste0(CurrentProjectDirectory,"/plots/check_nuts/",NUTS,"/",country, "_old.png"),width = 18, height = 8)
}
}
}
# line plots for NUTS2----
nuts_lineplots("NUTS2")
# line plot for NUTS1 ----
nuts_lineplots("NUTS1")
# line plot for NUTS0 ----
nuts_lineplots("NUTS0")
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
# Zwischen verschiedenen NUTS versionen -----
rm(list=setdiff(ls(), c("str_data","fadn.animal")))
test_data= str_data$info
all.2016 <- NUTS.convert.all(test_data, "all", 2016)
all.2003 %>% count(YEAR, NUTS2_final) %>%
pivot_wider(names_from =YEAR, values_from=n) %>% view()
final_ <- NUTS.convert.all(data = str_data$crops,
countries = "all",
NUTS.Year = 2016)
final_ %>% nrow()
ts <- NUTS.convert.all(data = str_data$crops,
countries = "HUN",
NUTS.Year = 2013)
ts %>% count(YEAR, NUTS2_final) %>% pivot_wider(names_from =YEAR, values_from=n)
ts %>% count(YEAR, NUTS2) %>% pivot_wider(names_from =YEAR, values_from=n)
str_data$crops %>% filter(COUNTRY == "UKI") %>% nrow()
NUTS.convert.all_ts(data = str_data$crops,
countries = "ELL",
NUTS.Year = 2010) %>%
count(YEAR, NUTS2_final) %>% pivot_wider(names_from =YEAR, values_from=n)
final_NUTS2_crops <- NUTS2_convert(test_data = str_data$crops)
final_NUTS2_crops %>% nrow()
all_equal(final_, final_NUTS2_crops) #TRUE
final_data %>% select(COUNTRY) %>% unique()