Synopsis

After hearing Donald Trump repeat ad nauseam that “We [the U.S.] are losing in trade to China, losing to Mexico…”, I decide to check for myself !

library(dplyr)
library(magrittr)
library(stringr)
library(ggplot2)
library(RColorBrewer)
library(ggrepel)


setwd("C:\\Users\\karim\\Documents\\R\\usa_trade")

Read CPI data

CPI <- read.table("cu.data.0.Current.txt", sep='\t', header=TRUE)

Summarise to get yearly CPI.

CPI_yearly <- CPI %>%
              group_by(year) %>%
              summarise(value=mean(value))

Save 2015 CPI value as reference.

CPI_2015 <- CPI_yearly %>% filter(year==2015)

Recalculate yearly CPI values using 2015 as reference.

CPI_yearly$CPI_factor <- CPI_2015$value / CPI_yearly$value 

Read US trade data.

trade_df <- read.csv("country.csv")

Filter for years >= 1997 and countries only. Keep year import/export columns only (discard monthly data).

trade_yr_df <- trade_df %>% 
                select(year, country=CTYNAME, import=IYR, export=EYR) %>%
                filter(year >= 1997 &
                         year < 2016 &
                         !str_detect(tolower(country), "opec") &
                         !str_detect(tolower(country), "world") &
                         !str_detect(tolower(country), "europe") &
                         !str_detect(tolower(country), "nafta") &  
                         !str_detect(tolower(country), "advance") &  
                         !str_detect(tolower(country), "nics") &  
                         !str_detect(tolower(country), "america") &  
                         !str_detect(tolower(country), "africa") &  
                         !str_detect(tolower(country), "pacific") &  
                         !str_detect(tolower(country), "asia") &  
                         !str_detect(tolower(country), "cafta") &  
                         !str_detect(tolower(country), "oceania")
                )

Merge CPI data and recalculate (constant 2015 $) import & export.

trade_yr_df %<>% left_join(CPI_yearly[,c("year", "CPI_factor")], by="year") %>%
                  mutate(import=import*CPI_factor, export=export*CPI_factor) %>%
                  mutate(balance=export-import, total=export+import)

Filter for year 2015 to get top 8 trading partners later.

trade_2015_df <- trade_yr_df %>% filter(year==2015)

Rank countries according to total trade (imports + exports) with US in 2015.

trade_2015_df %<>% mutate(rank_total=rank(desc(total), ties.method="first"))

Merge the rank on trade data.

trade_yr_df %<>% left_join(trade_2015_df[,c("country", "rank_total")], by=c("country"))

Filter to keeptop 8 only.

trade_top8total <- trade_yr_df %>% filter(rank_total <= 8)

Save 2015 trade data for top 8, to be used for the text (country names) in the graph.

trade_top8total_2015 <- trade_top8total %>% filter(year==2015)

Some charts now:

Trade Balance

ggplot(data=trade_top8total, aes(x=year, y=balance)) + geom_line(aes(col=country), size=1.25) +
  scale_color_manual(values=brewer.pal(8,"Dark2")) +
  geom_text_repel(data=trade_top8total_2015, aes(label=country, col=country), nudge_x=1) +
  scale_x_continuous(limits=c(NA, 2017)) + 
  labs(title="US Trade Balance in Goods - Top 8 partner countries", 
       x="Year", y="Balance, in millions of constant (2015) U.S. dollars") 

As you can see, the trade balance with Mexico has actually been relatively constant in the last 5 years. However, China has ben crushing the U.S. and it doesn’t look like it’s about to stop ! Let’s now check the detail (exports and imports).

Exports

ggplot(data=trade_top8total, aes(x=year, y=export)) + geom_line(aes(col=country), size=1.25) +
  scale_color_manual(values=brewer.pal(8,"Dark2")) +
  geom_text_repel(data=trade_top8total_2015, aes(label=country, col=country), nudge_x=1) +
  scale_x_continuous(limits=c(NA, 2017)) + 
  labs(title="US Exports in Goods - Top 8 partner countries", 
       x="Year", y="Exports, in millions of constant (2015) U.S. dollars") 

Imports

ggplot(data=trade_top8total, aes(x=year, y=import)) + geom_line(aes(col=country), size=1.25) +
  scale_color_manual(values=brewer.pal(8,"Dark2")) +
  geom_text_repel(data=trade_top8total_2015, aes(label=country, col=country), nudge_x=1) +
  scale_x_continuous(limits=c(NA, 2017)) + 
  labs(title="US Imports in Goods - Top 8 partner countries", 
       x="Year", y="Imports, in millions of constant (2015) U.S. dollars")

Observations: