This program does some basic analysis and graphical visualizations of the Heritage Foundation’s Economic Freedom Index (EFI) using ggplot and Plotly functions.

The EFI is calculated by measuring a number of metrics like fiscal health, regulations, government integrity, etc. Some economists like Jeffrey Sachs have criticized the index stating that a high Freedom Index does not necessarily correlate with higher economic growth per capita. So we will begin by analyzing that claim.

library(tidyverse)
library(plotly)
library(scales)

#Data Source
#https://www.heritage.org/index/explore

url <- "https://raw.github.com/jrpineda/Developing-Data-Products/master/economic_freedom_index2019_data.csv"
efi <- read_csv(url)

efi2 <- efi %>% 
        select('2019 Score', 'GDP Growth Rate (%)', 'GDP per Capita (PPP)', 'WEBNAME', 'Region', 'Population (Millions)') %>% 
        rename(Score = '2019 Score', GDP_growth='GDP Growth Rate (%)',
               Country='WEBNAME', Region='Region', GDP_pc='GDP per Capita (PPP)', pop ='Population (Millions)') %>% 
        filter(Score!="N/A")

#We do a bit of cleaning with the variables we will use

efi2$Score <- as.numeric(efi2$Score)
efi2$GDP_growth <- as.numeric(efi2$GDP_growth)
efi2$GDP_pc <- as.numeric(gsub("[\\$,]","",efi2$GDP_pc))
efi2$pop <- as.numeric(efi2$pop)


#Removing outliers (Venezuela, Cuba, North Korea) and small islands
#with high gdp per capita growth

efi2 <- efi2 %>% 
        filter(Score>35) %>% 
        mutate(GDP_growth_percap = round(GDP_growth/pop, digits=2)) %>% 
        filter(GDP_growth_percap < 10, GDP_growth_percap>-10)

#Create formats for later

f <- list(
        family = "Cambria, monospace",
        size = 18,
        color = "#7f7f7f"
)
x <- list(
        title = "Economic Freedom Index",
        titlefont = f
)
y <- list(
        title = "GDP growth per capita(%)",
        titlefont = f
)
z <- list(
        title = "GDP per capita (USD)",
        titlefont = f
)

#First plot - Showing GDP per capita and Score

fig <- efi2 %>% 
        plot_ly(
                type = 'scatter',
                mode = 'markers',
                x = ~Score, 
                y = ~GDP_growth_percap, 
                color = ~Region,
                text = ~paste("GDP growth per capita:", GDP_growth_percap, '%<br>Country:', Country, '<br>Score:', Score)) 

fig %>% layout(title = "Countries of the World", xaxis = x, yaxis = y)

As we can see in this first graph, there does not seem to be a clear trend between EFI and GDP growth per capita, with the countries with higher growth located somewhere in the middle of the index. This confirms what Jeffrey Sachs argues.

However, this could be evidence that countries with high economic growth are likely developing nations that still have not created strong institutions and freedoms. We could expect countries that are developed to have high EFI and low GDP growth. Still, from the graph it does not seem like a high EFI is a predictor of high growth.

summary(fit <- lm(Score ~ log(GDP_pc), data = efi2))
## 
## Call:
## lm(formula = Score ~ log(GDP_pc), data = efi2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -27.004  -3.869   1.074   4.487  19.358 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   8.1717     4.4695   1.828   0.0694 .  
## log(GDP_pc)   5.7028     0.4743  12.024   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.317 on 161 degrees of freedom
## Multiple R-squared:  0.4731, Adjusted R-squared:  0.4699 
## F-statistic: 144.6 on 1 and 161 DF,  p-value: < 2.2e-16

Next, here we can see that there is a trend between a country’s GDP per capita and the EFI. Unsurprisingly, richer countries have a higher rate of freedom. This remains true when broken down by region.The trend is not linear so I transformed GDP per capita by taking its log, which gave a better fit than a linear regression.

Lastly, I have created an interactive chart of the countries of the world using plotly. This gives us an additional tool to visualize the interaction between EFI and income per capita.

ggplot(efi2, aes(x = Score, y = GDP_pc))+
        geom_point(aes(col = Region))+
        geom_smooth(method="loess", formula = y ~ x, span=3)+
        scale_y_continuous(label = dollar, limits = c(0,130000))+
        labs(title = "Countries of the World", y = "GDP per capita (USD)", x = "Economic Freedom Index")

ggplot(efi2, aes(x = Score, y = GDP_pc))+
        geom_point(aes(col = Region))+geom_smooth(method="loess", formula = y ~ x, span=3, se=FALSE)+
        facet_wrap(~Region)+
        scale_y_continuous(label = dollar)+
        labs(title = "Regions of the World", y = "GDP per capita (USD)", x = "Economic Freedom Index")

fig <- efi2 %>% 
        plot_ly(
                type = 'scatter',
                mode = 'markers',
                x = ~Score, 
                y = ~GDP_pc, 
                color = ~Region,
                text = ~paste("GDP per capita: $", GDP_pc, '<br>Country:', Country, '<br>Score:', Score)) 

fig %>% layout(title = "Countries of the World", xaxis = x, yaxis = z)
#Finally, we show each region separately to help us see individual cases

p <- efi2 %>%
        plot_ly(
                type = 'scatter', 
                x = ~Score, 
                y = ~GDP_pc,
                text = ~paste("GDP per capita: $", GDP_pc, '<br>Country:', Country, '<br>Score:', Score),
                hoverinfo = 'text',
                mode = 'markers', 
                transforms = list(
                        list(
                                type = 'filter',
                                target = ~Region,
                                operation = '=',
                                value = unique(efi2$Region)[1]
                        )
                )) %>% layout(
                        updatemenus = list(
                                list(
                                        type = 'dropdown',
                                        active = 0,
                                        buttons = list(
                                                list(method = "restyle",
                                                     args = list("transforms[0].value", unique(efi2$Region)[1]),
                                                     label = unique(efi2$Region)[1]),
                                                list(method = "restyle",
                                                     args = list("transforms[0].value", unique(efi2$Region)[2]),
                                                     label = unique(efi2$Region)[2]),
                                                list(method = "restyle",
                                                     args = list("transforms[0].value", unique(efi2$Region)[3]),
                                                     label = unique(efi2$Region)[3]),
                                                list(method = "restyle",
                                                     args = list("transforms[0].value", unique(efi2$Region)[4]),
                                                     label = unique(efi2$Region)[4]),
                                                list(method = "restyle",
                                                     args = list("transforms[0].value", unique(efi2$Region)[5]),
                                                     label = unique(efi2$Region)[5])
                                        )
                                )
                        )
                )
p %>% layout(title = "Regions of the World", xaxis = c(x,list(range = c(0,100))), 
             yaxis = c(z,list(range = c(0, 130000))))

The dataset contains a number of other variables available at: https://www.heritage.org/index/explore