Newer
Older
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
#' Get detailed balance tables (demand broken down to its comoponents)
#'
#' @param region_list List of regions (CAPRI code) for which the market balances should be derived
#' @param product_list List of commodities (CAPRI code) for which the market balances should be derived
#' @param scenario_list List of CAPRI scenarios for which the market balances should be derived
#'
convert_balance_detailed <- function(region_list, product_list, scenario_list, folder = "mydata"){
# load all scenario results into memory
for(i in 1:length(scenario_list)) {load(file=paste(folder, "/", scenario_list[i], ".RData", sep=""))}
# get market balances for the first scenario
balance <- get_market_balance(get(scenario_list[1]), region_list, product_list)
# continue with the other scenarios
if (length(scenario_list) > 1) {
for(i in 2:length(scenario_list)){
balance <- rbind(balance, get_market_balance(get(scenario_list[i]), region_list, product_list))
}
}
balance <- spread(balance, i3, value)
# replace NAs with zeros
balance[is.na(balance)] <- 0
# add columns if missing (missing data)
if(!("PROD" %in% colnames(balance))) { balance <- mutate(balance, PROD = 0)}
if(!("HCON" %in% colnames(balance))) { balance <- mutate(balance, HCON = 0)}
if(!("PROC" %in% colnames(balance))) { balance <- mutate(balance, PROC = 0)}
if(!("BIOF" %in% colnames(balance))) { balance <- mutate(balance, BIOF = 0)}
if(!("FEED" %in% colnames(balance))) { balance <- mutate(balance, FEED = 0)}
if(!("ISCH" %in% colnames(balance))) { balance <- mutate(balance, ISCH = 0)}
if(!("Imports_noIntra" %in% colnames(balance))) { balance <- mutate(balance, Imports_noIntra = 0)}
if(!("Exports_noIntra" %in% colnames(balance))) { balance <- mutate(balance, Exports_noIntra = 0)}
# calculate net trade
balance <- balance %>% mutate(nettrade = Exports_noIntra - Imports_noIntra)
# TODO: change the structure to a uniform 'balance', i.e. no distinction by product groups
balance <- balance %>%
mutate(supply = PROD) %>%
mutate(human_cons = HCON) %>%
mutate(processing = PROC) %>%
mutate(biofuels = BIOF) %>%
mutate(feed = FEED) %>%
mutate(interv_ch = ISCH) %>%
mutate(imports = Imports_noIntra) %>%
mutate(exports = Exports_noIntra)
balance <- balance %>%
select(i1, i4, i5, supply, human_cons, processing, biofuels, feed, interv_ch, imports, exports, nettrade)
balance <- balance %>%
arrange(i4, i1, i5) %>%
select(i4, i1, i5, supply, human_cons, processing, biofuels, feed, interv_ch, imports, exports, nettrade)
return(balance)
}
#' Get the product balances for all baseline years and all products
#'
#'
convert_product_balance <- function(region_list, product_list, scenario_list, folder = "mydata"){
for(i in 1:length(scenario_list)) {load(file=paste(folder, "/", scenario_list[i], ".RData", sep=""))}
balance <- get_product_balance(get(scenario_list[1]), region_list, product_list)
if (length(scenario_list) > 1) {
for(i in 2:length(scenario_list)){
balance <- rbind(balance, get_product_balance(get(scenario_list[i]), region_list, product_list))
}
}
balance <- spread(balance, i3, value)
# replace NAs with zeros
balance[is.na(balance)] <- 0
# add potentially missing columns
if(!("LOSF" %in% colnames(balance))) { balance <- mutate(balance, LOSF = 0)}
if(!("MAPR" %in% colnames(balance))) { balance <- mutate(balance, MAPR = 0)}
if(!("GROF" %in% colnames(balance))) { balance <- mutate(balance, GROF = 0)}
if(!("SEDF" %in% colnames(balance))) { balance <- mutate(balance, SEDF = 0)}
if(!("INTF" %in% colnames(balance))) { balance <- mutate(balance, INTF = 0)}
if(!("IMPT" %in% colnames(balance))) { balance <- mutate(balance, IMPT = 0)}
if(!("INTP" %in% colnames(balance))) { balance <- mutate(balance, INTP = 0)}
if(!("EXPT" %in% colnames(balance))) { balance <- mutate(balance, EXPT = 0)}
if(!("NTRD" %in% colnames(balance))) { balance <- mutate(balance, NTRD = 0)}
if(!("HCOM" %in% colnames(balance))) { balance <- mutate(balance, HCOM = 0)}
if(!("DOMM" %in% colnames(balance))) { balance <- mutate(balance, DOMM = 0)}
if(!("FEDM" %in% colnames(balance))) { balance <- mutate(balance, FEDM = 0)}
if(!("SEDM" %in% colnames(balance))) { balance <- mutate(balance, SEDM = 0)}
if(!("PRCM" %in% colnames(balance))) { balance <- mutate(balance, PRCM = 0)}
if(!("BIOF" %in% colnames(balance))) { balance <- mutate(balance, BIOF = 0)}
if(!("INDM" %in% colnames(balance))) { balance <- mutate(balance, INDM = 0)}
if(!("LOSM" %in% colnames(balance))) { balance <- mutate(balance, LOSM = 0)}
if(!("STCM" %in% colnames(balance))) { balance <- mutate(balance, STCM = 0)}
# load("data/product_list.RData")
all_product <- product_list$code
my_all <- balance %>% filter(i4 %in% all_product)
my_all <- my_all %>%
mutate(supply = GROF) %>%
mutate(mark_prod = MAPR) %>%
mutate(losses_farm = LOSF + INTF) %>%
mutate(seed_farm = SEDF) %>%
mutate(imports = IMPT) %>%
mutate(exports = EXPT) %>%
mutate(nettrade = NTRD) %>%
mutate(intervention = INTP) %>%
mutate(dom_use = DOMM) %>%
mutate(human_cons = HCOM) %>%
mutate(feed = FEDM) %>%
mutate(seed_market = SEDM) %>%
mutate(processing_sec = PRCM) %>%
mutate(biofuels = BIOF) %>%
mutate(other_ind = INDM) %>%
mutate(losses_market = LOSM) %>%
mutate(stockch_market = STCM)
#select columns
my_all <- my_all %>%
select(i1, i4, i5, supply, mark_prod, losses_farm, seed_farm, imports, exports
, nettrade, intervention, dom_use, human_cons, feed, seed_market, processing_sec
, biofuels, other_ind, losses_market, stockch_market)
#re-arrange ordering
my_all <- my_all %>%
arrange(i4, i1, i5)
# meaningful column names
colnames(my_all)[1] <- "region"
colnames(my_all)[2] <- "product"
colnames(my_all)[3] <- "year"
return(my_all)
}
#' Get the Farm|Supply details tables for all regions/activities/year
#'
#'
#'
convert_supply_details <- function(region_list, product_list, scenario_list, folder = "mydata"){
for(i in 1:length(scenario_list)) {load(file=paste(folder, "/", scenario_list[i], ".RData", sep=""))}
balance <- get_supply_detail(get(scenario_list[1]), region_list, product_list)
if (length(scenario_list) > 1) {
for(i in 2:length(scenario_list)){
balance <- rbind(balance, get_supply_detail(get(scenario_list[i]), region_list, product_list))
}
}
balance <- spread(balance, i4, value)
# replace NAs with zeros
balance[is.na(balance)] <- 0
# add potentially missing columns
if(!("MGVA" %in% colnames(balance))) { balance <- mutate(balance, MGVA = 0)}
if(!("YILD" %in% colnames(balance))) { balance <- mutate(balance, YILD = 0)}
if(!("LEVL" %in% colnames(balance))) { balance <- mutate(balance, LEVL = 0)}
# load("data/activity_list.RData")
all_activity <- activity_list$code
my_all <- balance %>% filter(i3 %in% all_activity)
my_all <- my_all %>%
mutate(gross_value_added = MGVA) %>%
mutate(yield = YILD) %>%
mutate(level = LEVL)
my_all <- my_all %>%
mutate(supply = yield * level)
#select columns
my_all <- my_all %>%
select(i1, i3, i5, supply, yield, level, gross_value_added)
#re-arrange ordering
my_all <- my_all %>%
arrange(i3, i1, i5)
# meaningful column names
colnames(my_all)[1] <- "region"
colnames(my_all)[2] <- "product"
colnames(my_all)[3] <- "year"
return(my_all)
}