-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassumptions.R
112 lines (66 loc) · 2.64 KB
/
assumptions.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
#
# Assumptions and parameters used for constructing the TCI index
#
# minimum number of indicators a country must have to be included
min.num.inds <- 6
# chosen indicators
indicators <- list(
# Transboundary Water
"Transboundary_Water" = list( fname = "transboundary_water.csv"),
# Bilateral climate weighted FDI
"FDI" = list (fname = "fdi.csv"),
# Remittances
"Remittances" = list (fname = "remittances.csv"),
# Openness to asylum
"Asylum" = list (fname = "refugees.csv"),
# Migration from climate vulnerable countries
"Migration" = list (fname = "migration.csv"),
# Trade openness
"Trade_Openness" = list (fname = "trade_openness.csv"),
# Cereal import dependency
"Cereal_Imports" = list (fname = "cereal.csv", title = "Cereal Import Dependency"),
# Embedded water risk
"Embedded_Water" = list (fname = "embedded_water.csv"),
# KOF Globalization Index
"Globalization" = list (fname = "kof_globalization.csv")
)
# number of quantile 'bins' to use for generating indicator scores
num.quantiles <- 10
# Ranking function to calculate indicator scores
score <- function( values , bins ){
# apply a raw ranking of values
rnk <- rank(values , ties = 'min', na.last = 'keep')
# note: The ties = "min" parameter ensures that countries with the same
# value receive the same rank.
# The na.last='keep' maintains missing values in their proper place.
# bin boundaries
bin.bounds = quantile(unique(rnk), probs = 0:bins/bins, na.rm= TRUE)
#return a vector of scores
cut( rnk, bin.bounds , include.lowest =TRUE, labels = FALSE)
}
#####
# Sensitivity analysis
# possible noise to add, as a percentage of range
percent.error = c(15)
###########################
# Alternative scoring function
score2 <- function(values, bins){
set.seed(1989)
# apply a raw ranking of values
rnk <- rank(values , ties = 'min', na.last = 'keep')
# omit (and record position of) NA values
rnk <- na.omit(rnk)
# note: The ties = "min" parameter ensures that countries with the same
# value receive the same rank.
# The na.last='keep' maintains missing values in their proper place.
# apply kmeans classification to rank levels to deal with duplicate values
# K-means will find groupings which minimize
clu <- kmeans(rnk, bins)$cluster
#reassign cluster ids starting with the smallest group
a <- aggregate(rnk, by = list(clu), FUN=min, na.rm=TRUE)
out <- rank(a$x)[clu]
for (i in attr(rnk, 'na.action')){
out <- append(out, NA, after = i-1 )
}
out
}