Assignment 7: World GDP and Trade

Author

Blake Kraemer

#|label: setup
#|echo: FALSE
library(httr)
library(jsonlite)
library(dplyr)
library(readr)
library(ggplot2)

Research Question:

In 2024, how did merchandise trade as a share of GDP vary across different economies with different levels of GDP per capita?

Merchandise trade will be measured as (exports_usd_billions +imports_usd_billions) * 100 / gdp_usd_billions.

This analysis will give a clear comparison of GDP per capita which measures the economic output per person and the merchandise trade as a share of GDP which measures the trade scale relative to the economy.

Methods

This project will use the UNECE’s 2024 trade data because it has GDP, GDP per capita, imports, and exports for 208 economies around the world. I’ll first use an R script to scrape data from the HTML using a loop. From there, the variable of merchandise trade will be added to compare how much different economies trade relative to their size. Finally, scatterplots, boxplots, and tables will be shown to analyze how merchandise trade varies with GDP per capita.

 #Import Data

trade_df <- read_csv("https://myxavier-my.sharepoint.com/:x:/g/personal/kraemerb1_xavier_edu/IQCQc5VZgUAyTIcWKydTnv7sAUyzJPBUXwYIq5qTooDPPHU?e=blSETx?&download=1")

Data Wrangling

The data used in this project has been scraped and cleaned in an R script already. The 4 columns that required cleaning were “gdp_USD_billions”, “gdp_per_capita”, “avg_tariff_pct”, and “avg_clearance hours.”

The first two required to remove “N/A” as text and convert it to “NA” as a numeric. The second two required a removal of asterisks and then converting those numbers to numeric rather than characters. This allows the data to be used cleanly without dealing with errors due to data being stored as characters.

There are a few more edits needed for this project. First adding the merchandise trade column which will play a big part in determining the question. Second, removing the world row and any rows that dont have a listed GDP or gdp per capita, as both cateogires could skew data. Finally, creating a table that shows the top 10 countries in the merchandise trade

# Add "merchandise_trade" column

trade_df <- trade_df |> 
  mutate(merchandise_trade = (100* (exports_usd_billions + imports_usd_billions)/gdp_usd_billions))

# Adding quartiles for gdp per capita

trade_df<- trade_df |> 
  mutate(gdp_per_capita_group = ntile(gdp_per_capita,4),
         gdp_per_capita_group = factor(
           gdp_per_capita_group,
           levels = 1:4,
           labels = c("Lowest 25%", "Lower-Mid 25%", "Upper-Mid 25%", "Highest 25%")
         ))

#Adding a table that shows top 10 countries in merchandise trade percentage

top_trade <- trade_df |> 
  slice_max(merchandise_trade, n = 10)

#Removing World from the rows
trade_df<-trade_df |> 
  filter(country != "WORLD")

trade_df<-trade_df |> 
  filter(gdp_usd_billions>0,
         gdp_per_capita>0)

Data Analysis and Visualization

 # Plot 1: Merchandise Trade x GDP Per Capita
ggplot(trade_df , aes(x = gdp_per_capita, y = merchandise_trade))+
  geom_point(alpha = .6, color = "darkgrey")+
  scale_x_log10(labels = scales::label_dollar())+
  scale_y_continuous(labels = scales::label_number(suffix = "%"))+
  labs(
    title = "Merchandise trade and GDP per capita in Billions, 2024",
    x = "GDP per capita (USD, log scale)",
    y = "Merchandise trade as a percentage of GDP"
  )

This plot represents GDP per capita against merchandise trade as a percentage of GDP for each economy in 2024. Most economies had trade shares below 100% across a wide range of GDP per capita, and a few exceeded 200% at different per capita levels. There isn’t a clear pattern in this graph, suggesting that economies with higher GDP per capita didn’t trade more or less relative to the size of their economies.

# Plot 2: GDP Per Capita Groups x Merchandise Trade

ggplot(trade_df, aes(x = gdp_per_capita_group, y = merchandise_trade))+
  geom_boxplot(alpha = .6, fill = "darkgrey")+
  scale_y_continuous(labels = scales::label_number(suffix = "%"))+
  labs(
    title = "Merchandise Trade by GDP Per Capita Group, 2024",
    x = "GDP Per Capita Quartile",
    y = "Merchandise Trade as a percentage of GDP"
  )

This grouping is based on GDP per capita, and is compared to merchandise trade. There is a lot of overlap between the four box plots showing they are all relatively similar, however the highest GDP per capita group had the largest individual merchandise trade shares even though the median was below both mid groups. These findings also indicate that merchandise trade share didn’t consistently rise as GDP per capita increased.

# PLot 3: Ten highest Merchandise Trade Percentages

ggplot(top_trade, aes(x = merchandise_trade, 
                      y = reorder(country, merchandise_trade),
                      fill = gdp_per_capita_group))+
  geom_col()+
  scale_x_continuous(labels = scales::label_number(suffix = "%"))+
  labs(
    title = "Economies with the highest merchandise trade shares, 2024",
    x = "Merchandise trade as a percentage of GDP",
    y = NULL,
    fill = "GDP per capita group"
  )

This plot correlates with the previous box plots. Each one of the countries represents one of the outliers from the box plot. This graph shows that 5 of the 10 countries with the highest merchandise share are apart of the top 25% but each of the other baskets are represented. This graph shows that high trade shares occurred at a range of different GDP per capita levels.

# Correlation Test

cor.test(trade_df$gdp_per_capita,trade_df$merchandise_trade)

    Pearson's product-moment correlation

data:  trade_df$gdp_per_capita and trade_df$merchandise_trade
t = 0.054963, df = 186, p-value = 0.9562
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.1391605  0.1470555
sample estimates:
       cor 
0.00403003 

This correlation tests the relationship between GDP per capita and merchandise trade as share of GDP. The correlation was nearly zero (r = 0.004, p = 0.956), which matches the lack of a clear pattern in the scatter plot. This again shows that economies with higher GDP per capita didn’t consistently trade more or less relative to the size of the economy.

# Plot 4: Merchandise Trade Share Histogram

ggplot(trade_df, aes(x = merchandise_trade))+
  geom_histogram(binwidth = 25, fill = "darkgrey")+
  labs(
    title = "Distribution of merchandise trade shares, 2024",
    x = "Merchandise trade (% of GDP)",
    y = "Number of economies"
  )

This histogram shows how common the different levels of merchandise trade were in 2024. The tallest bar was around 50% with about 65 countries. There is a right skew to this distribution with most countries under 100% and a few above 200%. This shows how unusual it is for economies to have anything over a 200% merchandise trade share.

Conclusion

My original thoughts were that there would be a high correlation between the merchandise trade percentage and gdp per capita, but that was not the case. The plots and correlation used proved that point wrong. There was almost zero correlation between the two variables and nothing discovered had any chance of proving that there was a correlation