-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSofiAggregates.R
138 lines (126 loc) · 6.11 KB
/
SofiAggregates.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
###########################################################################
## Title: This script compute the SOFI aggregates
## Created: 01-09-2013
## Modified: 22-12-2013
###########################################################################
# Sub-region -------------------------------------------------------------
## Relation data frame
SOFIsubReg = FAOcountryProfile[, c("FAOST_CODE", "SOFI_SUB_REG_CODE")]
## Aggregation
SOFIsubReg.df =
Aggregation(data = country.df,
relationDF = SOFIsubReg,
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(SOFIsubReg.df)[grep("SOFI_SUB_REG_CODE", colnames(SOFIsubReg.df))] = "FAOST_CODE"
## Specify the area
SOFIsubReg.df$Area = "SOFISubRegion"
## Add country names
SOFIsubReg.df =
merge(SOFIsubReg.df,
na.omit(unique(FAOcountryProfile[, c("SOFI_SUB_REG_CODE", "SOFI_SUB_REG")])),
by.x = "FAOST_CODE", by.y = "SOFI_SUB_REG_CODE", all.x = TRUE)
colnames(SOFIsubReg.df)[grep("SOFI_SUB_REG", colnames(SOFIsubReg.df))] = "FAO_TABLE_NAME"
# Other sub-regions -------------------------------------------------------
## Relation data frame
SOFIotherReg = FAOcountryProfile[, c("FAOST_CODE", "SOFI_OTHER_REG_CODE")]
## Aggregation
SOFIotherReg.df =
Aggregation(data = country.df,
relationDF = SOFIotherReg,
aggVar = con.df[!is.na(con.df["AGGREGATION"]), "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(SOFIotherReg.df)[grep("SOFI_OTHER_REG_CODE", colnames(SOFIotherReg.df))] = "FAOST_CODE"
## Specify the area
SOFIotherReg.df$Area = "SOFIOtherRegion"
## Add country names
SOFIotherReg.df =
merge(SOFIotherReg.df,
na.omit(unique(FAOcountryProfile[, c("SOFI_OTHER_REG_CODE", "SOFI_OTHER_REG")])),
by.x = "FAOST_CODE", by.y = "SOFI_OTHER_REG_CODE", all.x = TRUE)
colnames(SOFIotherReg.df)[grep("SOFI_OTHER_REG", colnames(SOFIotherReg.df))] = "FAO_TABLE_NAME"
# Macro regions -----------------------------------------------------------
## Relation data frame
SOFImacroReg = FAOcountryProfile[, c("FAOST_CODE", "SOFI_MACRO_REG_CODE")]
## Aggregation
SOFImacroReg.df =
Aggregation(data = country.df,
relationDF = SOFImacroReg,
aggVar = con.df[!is.na(con.df["AGGREGATION"]), "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(SOFImacroReg.df)[grep("SOFI_MACRO_REG_CODE", colnames(SOFImacroReg.df))] = "FAOST_CODE"
## Specify the area
SOFImacroReg.df$Area = "SOFIMacroRegion"
## Add country names
SOFImacroReg.df =
merge(SOFImacroReg.df,
na.omit(unique(FAOcountryProfile[, c("SOFI_MACRO_REG_CODE", "SOFI_MACRO_REG")])),
by.x = "FAOST_CODE", by.y = "SOFI_MACRO_REG_CODE", all.x = TRUE)
colnames(SOFImacroReg.df)[grep("SOFI_MACRO_REG", colnames(SOFImacroReg.df))] = "FAO_TABLE_NAME"
# Developed / developing --------------------------------------------------
## Relation data frame
SOFIdevReg = FAOcountryProfile[, c("FAOST_CODE", "SOFI_DVDDVG_REG_CODE")]
## Aggregation
SOFIdevReg.df =
Aggregation(data = country.df,
relationDF = SOFIdevReg,
aggVar = con.df[!is.na(con.df["AGGREGATION"]), "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(SOFIdevReg.df)[grep("SOFI_DVDDVG_REG_CODE", colnames(SOFIdevReg.df))] = "FAOST_CODE"
## Specify the area
SOFIdevReg.df$Area = "SOFIDevelopmentRegion"
## Add country names
SOFIdevReg.df =
merge(SOFIdevReg.df,
na.omit(unique(FAOcountryProfile[, c("SOFI_DVDDVG_REG_CODE", "SOFI_DVDDVG_REG")])),
by.x = "FAOST_CODE", by.y = "SOFI_DVDDVG_REG_CODE", all.x = TRUE)
colnames(SOFIdevReg.df)[grep("SOFI_DVDDVG_REG", colnames(SOFIdevReg.df))] = "FAO_TABLE_NAME"
# World -------------------------------------------------------------------
## Relation data frame
SOFIworld = FAOcountryProfile[, c("FAOST_CODE", "SOFI_WORLD_REG_CODE")]
## Aggregation
SOFIworld.df =
Aggregation(data = country.df,
relationDF = SOFIworld,
aggVar = con.df[!is.na(con.df["AGGREGATION"]), "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(SOFIworld.df)[grep("SOFI_WORLD_REG_CODE", colnames(SOFIworld.df))] = "FAOST_CODE"
## Specify the area
SOFIworld.df$Area = "SOFIWorld"
## Add country names
SOFIworld.df =
merge(SOFIworld.df,
na.omit(unique(FAOcountryProfile[, c("SOFI_WORLD_REG_CODE", "SOFI_WORLD_REG")])),
by.x = "FAOST_CODE", by.y = "SOFI_WORLD_REG_CODE", all.x = TRUE)
colnames(SOFIworld.df)[grep("SOFI_WORLD_REG", colnames(SOFIworld.df))] = "FAO_TABLE_NAME"
# R bind ------------------------------------------------------------------
sofiAggs.df = rbind(SOFIsubReg.df, SOFIotherReg.df,
SOFImacroReg.df, SOFIdevReg.df, SOFIworld.df)
rm(list = c("SOFIdevReg", "SOFIdevReg.df", "SOFImacroReg", "SOFImacroReg.df",
"SOFIotherReg", "SOFIotherReg.df", "SOFIsubReg", "SOFIsubReg.df",
"SOFIworld", "SOFIworld.df"))