-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFAOAggregates.R
298 lines (278 loc) · 13.7 KB
/
FAOAggregates.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
###########################################################################
## Title: This script compute the FAO regional offices aggregates
## Created: 01-09-2013
## Modified: 22-12-2013
###########################################################################
## NOTE (FILIPPO): For the same geographical region we can have several
## aggregates that are not comparable for several reasons.
## Let's take for example Africa. The regional office for Africa does not
## include North Africa while M49 Africa does. Even then, M49 Africa includes
## territories like Reunion that are not in the Regional office for Africa
## (shold we compute a different country level aggregation for FAO regional
## offices? is Runion just discarded? big issue...). Moreover, M49 Africa
## aggregate is computed following the 65% aggregation rule at the world level,
## while the Regional Office for Africa is aggregated following the rule of
## 65% at African level.
# Africa ------------------------------------------------------------------
## Relation data frame
RAFsubReg = FAOcountryProfile[, c("FAOST_CODE", "FAO_RAF_SUB_REG")]
## Aggregation
RAFsubReg.df =
Aggregation(data = country.df,
relationDF = RAFsubReg,
aggVar = con.df[, "STS_ID"],
aggMethod = con.df[, "AGGREGATION"],
weightVar = con.df[, "STS_ID_WEIGHT"],
thresholdProp = con.df[, "THRESHOLD_PROP"],
# thresholdCountry = con.df[, "THRESHOLD_COUNTRIES"],
# applyRules = TRUE,
keepUnspecified = FALSE)
colnames(RAFsubReg.df)[which(colnames(RAFsubReg.df) == "FAO_RAF_SUB_REG")] = "FAOST_CODE"
## Specify the area
RAFsubReg.df$Area = "RAFsubReg"
## Add country names
RAFsubRegName = data.frame(FAOST_CODE = c("RAFCentralAfrica", "RAFEastAfrica",
"RAFNorthAfrica", "RAFSouthernAfrica",
"RAFWestAfrica"),
FAO_TABLE_NAME = c("Central Africa",
"Eastern Africa", "North Africa",
"Southern Africa", "Western Africa"),
stringsAsFactors = FALSE)
RAFsubReg.df = merge(RAFsubReg.df, RAFsubRegName, all.x = TRUE, by = "FAOST_CODE")
## Relation data frame
RAFregion = FAOcountryProfile[, c("FAOST_CODE", "FAO_RAF_REG")]
## Aggregation
RAFregion.df =
Aggregation(data = country.df,
relationDF = RAFregion,
aggVar = con.df[, "STS_ID"],
aggMethod = con.df[, "AGGREGATION"],
weightVar = con.df[, "STS_ID_WEIGHT"],
thresholdProp = con.df[, "THRESHOLD_PROP"],
# thresholdCountry = con.df[, "THRESHOLD_COUNTRIES"],
# applyRules = TRUE,
keepUnspecified = FALSE)
colnames(RAFregion.df)[which(colnames(RAFregion.df) == "FAO_RAF_REG")] = "FAOST_CODE"
## Specify the area
RAFregion.df$Area = "RAFregion"
## Add country names
RAFregion.df[, "FAO_TABLE_NAME"] = "Regional Office for Africa"
# Asia and Pacific --------------------------------------------------------
## Relation data frame
RAPdev = FAOcountryProfile[, c("FAOST_CODE", "FAO_RAP_DVDDVG_REG")]
## Aggregation
RAPdev.df =
Aggregation(data = country.df,
relationDF = RAPdev,
aggVar = con.df[, "STS_ID"],
aggMethod = con.df[, "AGGREGATION"],
weightVar = con.df[, "STS_ID_WEIGHT"],
thresholdProp = con.df[, "THRESHOLD_PROP"],
# thresholdCountry = con.df[, "THRESHOLD_COUNTRIES"],
# applyRules = TRUE,
keepUnspecified = FALSE)
colnames(RAPdev.df)[which(colnames(RAPdev.df) == "FAO_RAP_DVDDVG_REG")] = "FAOST_CODE"
## Specify the area
RAPdev.df$Area = "RAPdev"
## Add country names
RAPdevName = data.frame(FAOST_CODE = c("RAPDeveloped", "RAPDeveloping"),
FAO_TABLE_NAME = c("Developed countries",
"Developing countries"),
stringsAsFactors = FALSE)
RAPdev.df = merge(RAPdev.df, RAPdevName, all.x = TRUE, by = "FAOST_CODE")
## Relation data frame
RAPsubReg = FAOcountryProfile[, c("FAOST_CODE", "FAO_RAP_SUB_REG")]
## Aggregation
RAPsubReg.df =
Aggregation(data = country.df,
relationDF = RAPsubReg,
aggVar = con.df[, "STS_ID"],
aggMethod = con.df[, "AGGREGATION"],
weightVar = con.df[, "STS_ID_WEIGHT"],
thresholdProp = con.df[, "THRESHOLD_PROP"],
# thresholdCountry = con.df[, "THRESHOLD_COUNTRIES"],
# applyRules = TRUE,
keepUnspecified = FALSE)
colnames(RAPsubReg.df)[which(colnames(RAPsubReg.df) == "FAO_RAP_SUB_REG")] = "FAOST_CODE"
## Specify the area
RAPsubReg.df$Area = "RAPsubReg"
## Add country names
RAPsubRegName = data.frame(FAOST_CODE = c("RAPCentralAsia", "RAPDevelopedCountries",
"RAPEastAsia", "RAPPacificIslands",
"RAPSoutheastAsia",
"RAPSouthSouthwestAsia"),
FAO_TABLE_NAME = c("Central Asia",
"Developed countries",
"East Asia",
"Pacific Islands",
"Southeast Asia",
"South and Southwest Asia"),
stringsAsFactors = FALSE)
RAPsubReg.df = merge(RAPsubReg.df, RAPsubRegName, all.x = TRUE, by = "FAOST_CODE")
## Relation data frame
RAPregion = FAOcountryProfile[, c("FAOST_CODE", "FAO_RAP_REG")]
## Aggregation
RAPregion.df =
Aggregation(data = country.df,
relationDF = RAPregion,
aggVar = con.df[, "STS_ID"],
aggMethod = con.df[, "AGGREGATION"],
weightVar = con.df[, "STS_ID_WEIGHT"],
thresholdProp = con.df[, "THRESHOLD_PROP"],
# thresholdCountry = con.df[, "THRESHOLD_COUNTRIES"],
# applyRules = TRUE,
keepUnspecified = FALSE)
colnames(RAPregion.df)[which(colnames(RAPregion.df) == "FAO_RAP_REG")] = "FAOST_CODE"
## Specify the area
RAPregion.df$Area = "RAPregion"
## Add country names
RAPregion.df[, "FAO_TABLE_NAME"] = "Regional Office for Asia and the Pacific"
# Europe and Central Asia -------------------------------------------------
## Relation data frame
REUsubReg = FAOcountryProfile[, c("FAOST_CODE", "FAO_REU_SUB_REG")]
## Aggregation
REUsubReg.df =
Aggregation(data = country.df,
relationDF = REUsubReg,
aggVar = con.df[, "STS_ID"],
aggMethod = con.df[, "AGGREGATION"],
weightVar = con.df[, "STS_ID_WEIGHT"],
thresholdProp = con.df[, "THRESHOLD_PROP"],
# thresholdCountry = con.df[, "THRESHOLD_COUNTRIES"],
# applyRules = TRUE,
keepUnspecified = FALSE)
colnames(REUsubReg.df)[which(colnames(REUsubReg.df) == "FAO_REU_SUB_REG")] = "FAOST_CODE"
## Specify the area
REUsubReg.df$Area = "REUsubReg"
## Add country names
REUsubRegName = data.frame(FAOST_CODE = c("REUCISeurope", "REUCaucAndTurkey",
"REUCentralAsia", "REUCentralEasternEurope",
"REUIsrael", "REUOtherAndEFTA",
"REUSouthEasternEurope"),
FAO_TABLE_NAME = c("REU CIS Europe", "REU Caucasus and Turkey",
"REU Central Asia", "REU Central Eastern Europe",
"Israel", "REU Other and EFTA",
"REU South Eastern Europe"),
stringsAsFactors = FALSE)
REUsubReg.df = merge(REUsubReg.df, REUsubRegName, all.x = TRUE, by = "FAOST_CODE")
## Relation data frame
REUregion = FAOcountryProfile[, c("FAOST_CODE", "FAO_REU_REG")]
## Aggregation
REUregion.df =
Aggregation(data = country.df,
relationDF = REUregion,
aggVar = con.df[, "STS_ID"],
aggMethod = con.df[, "AGGREGATION"],
weightVar = con.df[, "STS_ID_WEIGHT"],
thresholdProp = con.df[, "THRESHOLD_PROP"],
# thresholdCountry = con.df[, "THRESHOLD_COUNTRIES"],
# applyRules = TRUE,
keepUnspecified = FALSE)
colnames(REUregion.df)[which(colnames(REUregion.df) == "FAO_REU_REG")] = "FAOST_CODE"
## Specify the area
REUregion.df$Area = "REUregion"
## Add country names
REUregion.df[, "FAO_TABLE_NAME"] = "Regional Office for Europe and Central Asia"
# Latin America and the Caribbean -----------------------------------------
## Relation data frame
LACsubReg = FAOcountryProfile[, c("FAOST_CODE", "FAO_LAC_SUB_REG")]
## Aggregation
LACsubReg.df =
Aggregation(data = country.df,
relationDF = LACsubReg,
aggVar = con.df[, "STS_ID"],
aggMethod = con.df[, "AGGREGATION"],
weightVar = con.df[, "STS_ID_WEIGHT"],
thresholdProp = con.df[, "THRESHOLD_PROP"],
# thresholdCountry = con.df[, "THRESHOLD_COUNTRIES"],
# applyRules = TRUE,
keepUnspecified = FALSE)
colnames(LACsubReg.df)[which(colnames(LACsubReg.df) == "FAO_LAC_SUB_REG")] = "FAOST_CODE"
## Specify the area
LACsubReg.df$Area = "LACsubReg"
## Add country names
LACsubRegName = data.frame(FAOST_CODE = c("LACCaribbean", "LACCentralAmerica",
"LACNorthAmerica", "LACSouthAmerica"),
FAO_TABLE_NAME = c("Caribbean", "Central America",
"North America", "South America"),
stringsAsFactors = FALSE)
LACsubReg.df = merge(LACsubReg.df, LACsubRegName, all.x = TRUE, by = "FAOST_CODE")
## Relation data frame
LACregion = FAOcountryProfile[, c("FAOST_CODE", "FAO_LAC_REG")]
## NOTE (FILIPPO): I'm removing LACNorthAmerica because it is not part of the
## Regional Office for Latin America and the Caribbean and (although they
## want to show it) I want to keep the two aggregates separate.
NAcountries =
na.omit(FAOcountryProfile[FAOcountryProfile[, "FAO_LAC_SUB_REG"] == "LACNorthAmerica", "FAOST_CODE"])
LACregion[LACregion[, "FAOST_CODE"] %in% NAcountries, "FAO_LAC_REG"] = NA
## Aggregation
LACregion.df =
Aggregation(data = country.df,
relationDF = LACregion,
aggVar = con.df[, "STS_ID"],
aggMethod = con.df[, "AGGREGATION"],
weightVar = con.df[, "STS_ID_WEIGHT"],
thresholdProp = con.df[, "THRESHOLD_PROP"],
# thresholdCountry = con.df[, "THRESHOLD_COUNTRIES"],
# applyRules = TRUE,
keepUnspecified = FALSE)
colnames(LACregion.df)[which(colnames(LACregion.df) == "FAO_LAC_REG")] = "FAOST_CODE"
## Specify the area
LACregion.df$Area = "LACregion"
## Add country names
LACregion.df[, "FAO_TABLE_NAME"] = "Regional Office for Latin America and the Caribbean"
# Near East ---------------------------------------------------------------
## Relation data frame
RNEsubReg = FAOcountryProfile[, c("FAOST_CODE", "FAO_RNE_SUB_REG")]
## Aggregation
RNEsubReg.df =
Aggregation(data = country.df,
relationDF = RNEsubReg,
aggVar = con.df[, "STS_ID"],
aggMethod = con.df[, "AGGREGATION"],
weightVar = con.df[, "STS_ID_WEIGHT"],
thresholdProp = con.df[, "THRESHOLD_PROP"],
# thresholdCountry = con.df[, "THRESHOLD_COUNTRIES"],
# applyRules = TRUE,
keepUnspecified = FALSE)
colnames(RNEsubReg.df)[which(colnames(RNEsubReg.df) == "FAO_RNE_SUB_REG")] = "FAOST_CODE"
## Specify the area
RNEsubReg.df$Area = "RNEsubReg"
## Add country names
RNEsubRegName = data.frame(FAOST_CODE = c("RNEgccsy", "RNEna",
"RNEome"),
FAO_TABLE_NAME = c("Gulf Cooperation Council States and Yemen",
"North Africa",
"Other Near East countries"),
stringsAsFactors = FALSE)
RNEsubReg.df = merge(RNEsubReg.df, RNEsubRegName, all.x = TRUE, by = "FAOST_CODE")
## Relation data frame
RNEregion = FAOcountryProfile[, c("FAOST_CODE", "FAO_RNE_REG")]
## Aggregation
RNEregion.df =
Aggregation(data = country.df,
relationDF = RNEregion,
aggVar = con.df[, "STS_ID"],
aggMethod = con.df[, "AGGREGATION"],
weightVar = con.df[, "STS_ID_WEIGHT"],
thresholdProp = con.df[, "THRESHOLD_PROP"],
# thresholdCountry = con.df[, "THRESHOLD_COUNTRIES"],
# applyRules = TRUE,
keepUnspecified = FALSE)
colnames(RNEregion.df)[which(colnames(RNEregion.df) == "FAO_RNE_REG")] = "FAOST_CODE"
## Specify the area
RNEregion.df$Area = "RNEregion"
## Add country names
RNEregion.df[, "FAO_TABLE_NAME"] = "Regional Office for the Near East"
# FAOregions --------------------------------------------------------------
FAOregions.df = rbind(RAFsubReg.df, RAFregion.df,
RAPsubReg.df, RAPregion.df, RAPdev.df,
REUsubReg.df, REUregion.df,
LACsubReg.df, LACregion.df,
RNEsubReg.df, RNEregion.df)
rm(list = c("RAFsubReg", "RAFsubReg.df", "RAFsubRegName", "RAFregion", "RAFregion.df",
"RAPsubReg", "RAPsubReg.df", "RAPsubRegName", "RAPregion", "RAPregion.df",
"RAPdev", "RAPdev.df", "RAPdevName",
"REUsubReg", "REUsubReg.df", "REUsubRegName", "REUregion", "REUregion.df",
"LACsubReg", "LACsubReg.df", "LACsubRegName", "LACregion", "LACregion.df",
"RNEsubReg", "RNEsubReg.df", "RNEsubRegName", "RNEregion", "RNEregion.df"))