The most business-friendly countries in 2016

March 30, 2017
By

[This article was first published on R – SLOW DATA, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


infographic with world bank index ease of doing business

Which is the most business-friendly country, according to World Bank?

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

# 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.

WDIsearch('Dealing with')

dim1 <- WDI(country = "all", 
           indicator = "IC.FRM.XQ", 
           start = 2016, 
           end = 2016)

As of March 2017 these dimesions are not available from API…

…so I downloaded them manually from here in excel format.

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

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.

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

for(i in 3:ncol(ease_dim)) {
  ease_dim[,i] <- as.numeric(unlist(ease_dim[,i]))
}

Now that names match we can merge

dat2 <- merge(x = dat, y = ease_dim, by.x = 'country', by.y = 'Economy')

Are we getting the same data for the overall doingBusiness index??

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 natural earth website

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

sum(!dat2$iso2c %in% countries$iso_a2)
# dat2[which(!dat2$iso2c %in% countries$iso_a2), c('iso2c', 'country')] 
# [email protected][[email protected]$name=='Kosovo','iso_a2']
[email protected]$iso_new <- as.character([email protected]$iso_a2)
[email protected][[email protected]$name=='Kosovo','iso_new'] <- 'XK' # Fixing Kosovo iso code...

# Fixing Somalia       
# [email protected][[email protected]$name=='Somaliland','iso_a2']
# dat2[which(dat2$country=='Somalia'),'iso2c']
[email protected][[email protected]$name=='Somaliland','iso_new'] <- 'SO' # Fixing Somalia
[email protected]$iso_new2 <- as.factor([email protected]$iso_new)

Merging

plot.data <- merge(countries, 
                    dat2, 
                    by.x = "iso_new2", 
                    by.y = "iso2c",
                    sort = TRUE)

Prepare libraries for plotting

library(leaflet)
library(RColorBrewer)

Overall ranking

q1 <- quantile(x = [email protected]$Rank, probs = seq(0, 1, 0.1), na.rm = TRUE)
pal1 <- colorBin(rev(brewer.pal(n = 9, name = "Greens")), bins = q1)
popup1 <- paste0("Country: ", plot.data$name, "
", 'Ease of Doing Business Rank', ", ", as.character(2016), ": ", plot.data[['Rank']])

Starting a Business ranking

q2 <- quantile(x = [email protected][,'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("Country: ", 
                 plot.data$name, 
                 "
", 'Starting a Business Rank', ", ", as.character(2016), ": ", plot.data[['Starting a business-Rank']])

Dealing with Construction Permits-Rank ranking

q3 <- quantile(x = [email protected][,'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("Country: ", 
                 plot.data$name, 
                 "
", 'Dealing with Construction Permits-Rank', ", ", as.character(2016), ": ", plot.data[['Dealing with Construction Permits-Rank']])

Dealing with Construction Permits-Rank

q4 <- quantile(x = [email protected][,'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("Country: ", 
                 plot.data$name, 
                 "
", 'Getting Electricity-Rank', ", ", as.character(2016), ": ", plot.data[['Getting Electricity-Rank']])

Registering Property-Rank

q5 <- quantile(x = [email protected][,'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("Country: ", 
                 plot.data$name, 
                 "
", 'Getting Electricity-Rank', ", ", as.character(2016), ": ", plot.data[['Registering Property-Rank']])

Getting Credit-Rank

q6 <- quantile(x = [email protected][,'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("Country: ", 
                 plot.data$name, 
                 "
", 'Getting Credit-Rank', ", ", as.character(2016), ": ", plot.data[['Getting Credit-Rank']])

Protecting Minority Investors-Rank

q7 <- quantile(x = [email protected][,'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("Country: ", 
                 plot.data$name, 
                 "
", 'Protecting Minority Investors-Rank', ", ", as.character(2016), ": ", plot.data[['Protecting Minority Investors-Rank']])

Paying Taxes-Rank

q8 <- quantile(x = [email protected][,'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("Country: ", 
                 plot.data$name, 
                 "
", 'Paying Taxes-Rank', ", ", as.character(2016), ": ", plot.data[['Paying Taxes-Rank']])

Trading Across Borders-Rank

q9 <- quantile(x = [email protected][,'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("Country: ", 
                 plot.data$name, 
                 "
", 'Trading Across Borders-Rank', ", ", as.character(2016), ": ", plot.data[['Trading Across Borders-Rank']])

Enforcing Contracts-Rank

q10 <- quantile(x = [email protected][,'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("Country: ", 
                 plot.data$name, 
                 "
", 'Enforcing Contracts-Rank', ", ", as.character(2016), ": ", plot.data[['Enforcing Contracts-Rank']])

Resolving Insolvency-Rank

q11 <- quantile(x = [email protected][,'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("Country: ", 
                  plot.data$name, 
                  "
", 'Resolving Insolvency-Rank', ", ", as.character(2016), ": ", plot.data[['Resolving Insolvency-Rank']])

Prepare map background

stamen_tiles <- "http://{s}.tile.stamen.com/toner-lite/{z}/{x}/{y}.png"
stamen_attribution <- 'Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.'

Plot

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 = "Ease of Doing Business Rank") %>%
  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("Ease of Doing Business Rank", 
                   "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("", 9), seq(10,90,10), rep("th",9)),  
            opacity = 0.8,      ##transparency again
            title = " Top-Percentiles")

This map can be heavy to load, but it allows to explore all the dimensions of the index at once by changing layer of visualization.

Saving plot

library(htmlwidgets)
# saveWidget(DoingBusinessMap, "doingbiz.html", selfcontained = FALSE)

For all code visit this repo on GitHub.

The post The most business-friendly countries in 2016 appeared first on SLOW DATA.

To leave a comment for the author, please follow the link and comment on their blog: R – SLOW DATA.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)