-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoingbusiness.Rmd
438 lines (355 loc) · 15.2 KB
/
doingbusiness.Rmd
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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
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
---
title: "'Ease of Doing Business' World Bank Ranking"
output:
html_document: default
---
```{r set_options, echo = FALSE, include = FALSE}
library(knitr)
opts_chunk$set(message = FALSE, warning = FALSE)
```
```{r, include=FALSE}
setwd("C:/Users/pc/Documents/slowdata/R/doing_business")
```
Reading about the internazionalization of italian SMEs (Small and Medium Enterprises, < 250 employees) I discovered an interesting indicator provided by the World Bank. It is called 'Ease of doing business' and it is used to rank economies worldwide on their business-friendliness regulations (1=most business-friendly regulations).
Rankings are determined by sorting the aggregate 'distance to frontier' scores on 10 topics, each consisting of several indicators, giving equal weigth to each topic. More information on the methodologies can be found here http://www.doingbusiness.org/methodology.
In R there is a package to access World Bank data so you can avoid the hassle of downloading, importing, reformatting missing values and so on...
## World Bank Data
### Access data
```{r}
# access data
library(WDI)
WDIsearch('doing business') # when you don't know the exact name of an indicator you can search it by keyword
dat <- WDI(country = "all",
indicator = "IC.BUS.EASE.XQ",
start = 2016,
end = 2016)
dat[150,'country'] <- 'North Korea' # fixing no-utf characters in north korea name
```
Fine, but I'd like to have as well all the 10 components behind the index.
```{r}
WDIsearch('Dealing with')
dim1 <- WDI(country = "all",
indicator = "IC.FRM.XQ",
start = 2016,
end = 2016)
```
Apparently these dimesions are not available from API...
...but you can download them manually from [here](http://www.doingbusiness.org/data) in excel format.
```{r}
library(readxl)
ease_dim <- read_excel(path = "./data/Reports.xlsx", sheet = 1, col_names = TRUE, na = "", skip = 0)
keep <- grep(pattern = "Rank", x = names(ease_dim)) # keep only ranking variables
ease_dim <- ease_dim[,c(1,2,keep)]
```
### Clean data
Unfortunately there are no iso2c codes in here
```{r}
length(dat$country)
length(ease_dim$Economy) # probably regions (Europe, Middle-East, etc.) do not have ranking by single dimension
sum(ease_dim$Economy %in% dat$country) # 176 country names out of 212 are matched
ease_dim[!ease_dim$Economy %in% dat$country,'Economy']
```
Some countries in the two datasets do not match. This is because in the data downloaded from website there is also the index decomposed at the level of cities for some countries. We are not interested to that now. But there are also mismatches due to different names for the same country and we need to fix this.
```{r}
which(!ease_dim$Economy %in% dat$country)
ease_dim[which(!ease_dim$Economy %in% dat$country)[c(7, 8,9,10,15,18,21,29,30,36)], 'Economy'] <- c('Congo, Dem. Rep.', 'Congo, Rep.', "Cote d'Ivoire", 'Egypt, Arab Rep.', 'Iran, Islamic Rep.', 'Korea, Rep.', 'Micronesia, Fed. Sts.', 'Sao Tome and Principe', 'St. Kitts and Nevis', 'Yemen, Rep.')
```
Convert ranking columns to numeric
```{r}
for(i in 3:ncol(ease_dim)) {
ease_dim[,i] <- as.numeric(unlist(ease_dim[,i]))
}
```
Now that names match we can merge
```{r}
dat2 <- merge(x = dat, y = ease_dim, by.x = 'country', by.y = 'Economy')
```
Are we getting the same data for the overall doingBusiness index??
```{r}
head(dat2[,c('IC.BUS.EASE.XQ', 'Rank')])
```
Well, actually not. Probably info from API are not updated, let's use what I found on the website (as of 29/03/2017).
## World admnistrative borders
Access borders from naturalearth website
```{r}
url <- "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip"
tmp <- tempdir()
file <- basename(url)
download.file(url, file)
unzip(file, exdir = tmp)
library(rgdal)
library(sp)
countries <- readOGR(dsn = tmp,
layer = "ne_50m_admin_0_countries",
encoding = "UTF-8",
verbose = FALSE)
```
## Preparing plotting data
### Cleaning before matching world bank and natural Earth datasets
```{r}
sum(!dat2$iso2c %in% countries$iso_a2)
# dat2[which(!dat2$iso2c %in% countries$iso_a2), c('iso2c', 'country')]
# countries@data[countries@data$name=='Kosovo','iso_a2']
countries@data$iso_new <- as.character(countries@data$iso_a2)
countries@data[countries@data$name=='Kosovo','iso_new'] <- 'XK' # Fixing Kosovo iso code...
# Fixing Somalia
# countries@data[countries@data$name=='Somaliland','iso_a2']
# dat2[which(dat2$country=='Somalia'),'iso2c']
countries@data[countries@data$name=='Somaliland','iso_new'] <- 'SO' # Fixing Somalia
countries@data$iso_new2 <- as.factor(countries@data$iso_new)
```
### Merging
```{r}
plot.data <- merge(countries,
dat2,
by.x = "iso_new2",
by.y = "iso2c",
sort = TRUE)
```
## Prepare layers
```{r}
library(leaflet)
library(RColorBrewer)
```
### Overall ranking
```{r}
q1 <- quantile(x = plot.data@data$Rank, probs = seq(0, 1, 0.1), na.rm = TRUE)
pal1 <- colorBin(rev(brewer.pal(n = 9, name = "Greens")), bins = q1)
popup1 <- paste0("<strong>Country: </strong>", plot.data$name, "<br><strong>", 'Ease of Doing Business Rank', ", ",
as.character(2016), ": </strong>", plot.data[['Rank']])
```
### Starting a Business ranking
```{r}
q2 <- quantile(x = plot.data@data[,'Starting a business-Rank'], probs = seq(0, 1, 0.1), na.rm = TRUE)
pal2 <- colorBin(rev(brewer.pal(n = 9, name = "Greens")), bins = q2)
popup2 <- paste0("<strong>Country: </strong>",
plot.data$name,
"<br><strong>",
'Starting a Business Rank',
", ",
as.character(2016),
": </strong>",
plot.data[['Starting a business-Rank']])
```
### Dealing with Construction Permits-Rank ranking
```{r}
q3 <- quantile(x = plot.data@data[,'Dealing with Construction Permits-Rank'], probs = seq(0, 1, 0.1), na.rm = TRUE)
pal3 <- colorBin(rev(brewer.pal(n = 9, name = "Greens")), bins = q3)
popup3 <- paste0("<strong>Country: </strong>",
plot.data$name,
"<br><strong>",
'Dealing with Construction Permits-Rank',
", ",
as.character(2016),
": </strong>",
plot.data[['Dealing with Construction Permits-Rank']])
```
### Dealing with Construction Permits-Rank
```{r}
q4 <- quantile(x = plot.data@data[,'Getting Electricity-Rank'], probs = seq(0, 1, 0.1), na.rm = TRUE)
pal4 <- colorBin(rev(brewer.pal(n = 9, name = "Greens")), bins = q4)
popup4 <- paste0("<strong>Country: </strong>",
plot.data$name,
"<br><strong>",
'Getting Electricity-Rank',
", ",
as.character(2016),
": </strong>",
plot.data[['Getting Electricity-Rank']])
```
### Registering Property-Rank
```{r}
q5 <- quantile(x = plot.data@data[,'Registering Property-Rank'], probs = seq(0, 1, 0.1), na.rm = TRUE)
pal5 <- colorBin(rev(brewer.pal(n = 9, name = "Greens")), bins = q5)
popup5 <- paste0("<strong>Country: </strong>",
plot.data$name,
"<br><strong>",
'Getting Electricity-Rank',
", ",
as.character(2016),
": </strong>",
plot.data[['Registering Property-Rank']])
```
### Getting Credit-Rank
```{r}
q6 <- quantile(x = plot.data@data[,'Getting Credit-Rank'], probs = seq(0, 1, 0.1), na.rm = TRUE)
pal6 <- colorBin(rev(brewer.pal(n = 9, name = "Greens")), bins = q6)
popup6 <- paste0("<strong>Country: </strong>",
plot.data$name,
"<br><strong>",
'Getting Credit-Rank',
", ",
as.character(2016),
": </strong>",
plot.data[['Getting Credit-Rank']])
```
### Protecting Minority Investors-Rank
```{r}
q7 <- quantile(x = plot.data@data[,'Protecting Minority Investors-Rank'], probs = seq(0, 1, 0.1), na.rm = TRUE)
pal7 <- colorBin(rev(brewer.pal(n = 9, name = "Greens")), bins = q7)
popup7 <- paste0("<strong>Country: </strong>",
plot.data$name,
"<br><strong>",
'Protecting Minority Investors-Rank',
", ",
as.character(2016),
": </strong>",
plot.data[['Protecting Minority Investors-Rank']])
```
### Paying Taxes-Rank
```{r}
q8 <- quantile(x = plot.data@data[,'Paying Taxes-Rank'], probs = seq(0, 1, 0.1), na.rm = TRUE)
pal8 <- colorBin(rev(brewer.pal(n = 9, name = "Greens")), bins = q8)
popup8 <- paste0("<strong>Country: </strong>",
plot.data$name,
"<br><strong>",
'Paying Taxes-Rank',
", ",
as.character(2016),
": </strong>",
plot.data[['Paying Taxes-Rank']])
```
### Trading Across Borders-Rank
```{r}
q9 <- quantile(x = plot.data@data[,'Trading Across Borders-Rank'], probs = seq(0, 1, 0.1), na.rm = TRUE)
pal9 <- colorBin(rev(brewer.pal(n = 9, name = "Greens")), bins = q9)
popup9 <- paste0("<strong>Country: </strong>",
plot.data$name,
"<br><strong>",
'Trading Across Borders-Rank',
", ",
as.character(2016),
": </strong>",
plot.data[['Trading Across Borders-Rank']])
```
### Enforcing Contracts-Rank
```{r}
q10 <- quantile(x = plot.data@data[,'Enforcing Contracts-Rank'], probs = seq(0, 1, 0.1), na.rm = TRUE)
pal10 <- colorBin(rev(brewer.pal(n = 9, name = "Greens")), bins = q10)
popup10 <- paste0("<strong>Country: </strong>",
plot.data$name,
"<br><strong>",
'Enforcing Contracts-Rank',
", ",
as.character(2016),
": </strong>",
plot.data[['Enforcing Contracts-Rank']])
```
### Resolving Insolvency-Rank
```{r}
q11 <- quantile(x = plot.data@data[,'Resolving Insolvency-Rank'], probs = seq(0, 1, 0.1), na.rm = TRUE)
q11 # breaks are not unique
manual_q11 <- c(1,21.2,40.2, 58.8, 77.4, 96.0, 115.6, 134.2, 152.8, 160, 169.0)
pal11 <- colorBin(rev(brewer.pal(n = 9, name = "Greens")), bins = manual_q11)
popup11 <- paste0("<strong>Country: </strong>",
plot.data$name,
"<br><strong>",
'Resolving Insolvency-Rank',
", ",
as.character(2016),
": </strong>",
plot.data[['Resolving Insolvency-Rank']])
```
### Prepare map background
```{r}
stamen_tiles <- "http://{s}.tile.stamen.com/toner-lite/{z}/{x}/{y}.png"
stamen_attribution <- 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Data by <a href="http://openstreetmap.org">OpenStreetMap</a>, under <a href="http://www.openstreetmap.org/copyright">ODbL</a>.'
```
## Plot
```{r}
DoingBusinessMap <-
leaflet(data = plot.data) %>%
addTiles(urlTemplate = stamen_tiles,
attribution = stamen_attribution) %>%
setView(0, 0, zoom = 3) %>%
addPolygons(fillColor = ~pal1(plot.data[['Rank']]),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = popup1,
group = "<span style='color: #7f0000; font-size: 11pt'><strong>Ease of Doing Business Rank</strong></span>") %>%
addPolygons(fillColor = ~pal2(plot.data[['Starting a business-Rank']]),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = popup2,
group = "Starting a Business-Rank") %>%
addPolygons(fillColor = ~pal3(plot.data[['Dealing with Construction Permits-Rank']]),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = popup3,
group = "Dealing with Construction Permits-Rank") %>%
addPolygons(fillColor = ~pal4(plot.data[['Getting Electricity-Rank']]),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = popup4,
group = "Getting Electricity-Rank") %>%
addPolygons(fillColor = ~pal5(plot.data[['Registering Property-Rank']]),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = popup5,
group = "Registering Property-Rank") %>%
addPolygons(fillColor = ~pal6(plot.data[['Getting Credit-Rank']]),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = popup6,
group = "Getting Credit-Rank") %>%
addPolygons(fillColor = ~pal7(plot.data[['Protecting Minority Investors-Rank']]),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = popup7,
group = "Protecting Minority Investors-Rank") %>%
addPolygons(fillColor = ~pal8(plot.data[['Paying Taxes-Rank']]),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = popup8,
group = "Paying Taxes-Rank") %>%
addPolygons(fillColor = ~pal9(plot.data[['Trading Across Borders-Rank']]),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = popup9,
group = "Trading Across Borders-Rank") %>%
addPolygons(fillColor = ~pal10(plot.data[['Enforcing Contracts-Rank']]),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = popup10,
group = "Enforcing Contracts-Rank") %>%
addPolygons(fillColor = ~pal11(plot.data[['Resolving Insolvency-Rank']]),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = popup11,
group = "Resolving Insolvency-Rank") %>%
addLayersControl(
baseGroups = c("<span style='color: #7f0000; font-size: 11pt'><strong>Ease of Doing Business Rank</strong></span>",
"Starting a Business-Rank",
"Dealing with Construction Permits-Rank",
"Getting Electricity-Rank",
"Registering Property-Rank",
"Getting Credit-Rank",
"Protecting Minority Investors-Rank",
"Paying Taxes-Rank",
"Trading Across Borders-Rank",
"Enforcing Contracts-Rank",
"Resolving Insolvency-Rank"
),
options = layersControlOptions(collapsed = FALSE)) %>%
addLegend(position = 'bottomleft',
colors = rev(brewer.pal(n = 9, name = 'Greens')),
labels = paste0(rep("<span style='font-size: 8pt'>", 9), seq(10,90,10), rep("<sup>th</sup></span>",9)),
opacity = 0.8, ##transparency again
title = "<span style='font-size: 9pt'> Top-Percentiles</span>")
```
## Saving plot
```{r}
library(htmlwidgets)
# saveWidget(DoingBusinessMap, "doingbiz.html", selfcontained = FALSE)
```