Introduction
The US Census compiles data for exports, imports, and balances of goods for countries and groups of countries globally. This dataset has data back to 2009. Select years and countries are chosen for analysis below.
Load packages.
## Warning: package 'knitr' was built under R version 3.2.4
## Warning: package 'stringr' was built under R version 3.2.4
## Warning: package 'tidyr' was built under R version 3.2.4
## Warning: package 'dplyr' was built under R version 3.2.4
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## Warning: package 'ggplot2' was built under R version 3.2.4
## Loading required package: bitops
##
## Attaching package: 'RCurl'
## The following object is masked from 'package:tidyr':
##
## complete
Import CSV, column header = True.
rawgoods<-data.frame(read.csv(file="ctyseasonal.csv", header=T, stringsAsFactors = FALSE))
Take subset of countries and years.
goods<-subset(rawgoods, CTYNAME== "China" | CTYNAME=="Germany" | CTYNAME=="Canada" | CTYNAME=="Mexico" | CTYNAME == "Japan")
goods<-subset(goods, year==2014 | year==2015)
goods<-subset(goods[,c(1:3,20:31,36:47)] )
kable(head(goods,10))
| 92 |
2014 |
1220 |
Canada |
27,401 |
27,957 |
28,639 |
28,184 |
29,388 |
29,451 |
30,394 |
29,542 |
29,952 |
29,297 |
28,433 |
29,160 |
24,386 |
25,310 |
25,455 |
25,777 |
26,142 |
26,239 |
27,168 |
26,598 |
26,342 |
26,501 |
26,399 |
26,103 |
| 93 |
2014 |
5700 |
China |
37,564 |
37,969 |
37,515 |
38,060 |
38,220 |
38,932 |
37,919 |
39,000 |
40,223 |
40,306 |
40,388 |
40,659 |
10,850 |
10,132 |
10,277 |
9,922 |
10,074 |
10,171 |
10,180 |
10,352 |
10,118 |
10,690 |
10,429 |
10,482 |
| 95 |
2014 |
4280 |
Germany |
10,275 |
9,966 |
10,022 |
10,595 |
10,745 |
10,196 |
10,120 |
10,960 |
10,244 |
10,080 |
10,211 |
9,846 |
4,115 |
4,421 |
4,234 |
4,078 |
3,956 |
4,367 |
4,245 |
4,059 |
4,153 |
3,852 |
3,931 |
3,952 |
| 98 |
2014 |
5880 |
Japan |
11,334 |
11,157 |
11,397 |
11,055 |
11,349 |
11,345 |
11,192 |
10,971 |
11,163 |
11,133 |
10,869 |
11,039 |
5,673 |
5,652 |
5,694 |
5,573 |
5,245 |
5,584 |
5,631 |
6,059 |
5,468 |
5,460 |
5,026 |
5,764 |
| 100 |
2014 |
2010 |
Mexico |
23,414 |
24,035 |
23,890 |
24,192 |
24,313 |
24,766 |
24,911 |
24,839 |
24,513 |
25,504 |
24,593 |
25,104 |
19,443 |
19,890 |
19,589 |
19,788 |
20,709 |
20,257 |
20,452 |
20,295 |
19,957 |
20,061 |
20,123 |
19,687 |
| 110 |
2015 |
1220 |
Canada |
25,729 |
25,017 |
24,229 |
23,798 |
23,990 |
26,161 |
25,709 |
25,356 |
24,506 |
23,175 |
23,538 |
23,982 |
24,870 |
23,596 |
23,421 |
23,684 |
24,151 |
23,062 |
23,733 |
23,191 |
22,661 |
22,788 |
22,561 |
22,609 |
| 111 |
2015 |
5700 |
China |
40,225 |
36,119 |
48,222 |
37,694 |
40,142 |
39,410 |
39,092 |
42,696 |
40,932 |
39,750 |
39,298 |
38,301 |
10,013 |
8,996 |
9,297 |
10,217 |
9,530 |
10,475 |
10,356 |
9,791 |
10,208 |
9,551 |
9,136 |
8,618 |
| 113 |
2015 |
4280 |
Germany |
10,423 |
10,541 |
10,007 |
10,287 |
10,221 |
10,862 |
10,298 |
10,652 |
9,897 |
10,748 |
9,711 |
10,493 |
3,979 |
4,151 |
4,286 |
4,354 |
3,861 |
4,035 |
4,316 |
3,866 |
4,195 |
4,580 |
4,183 |
4,140 |
| 116 |
2015 |
5880 |
Japan |
11,762 |
9,746 |
11,815 |
12,518 |
11,621 |
10,349 |
10,696 |
10,471 |
10,134 |
10,337 |
10,647 |
11,023 |
5,250 |
5,353 |
5,542 |
5,836 |
5,262 |
5,172 |
5,314 |
5,275 |
4,635 |
5,063 |
5,011 |
4,759 |
| 118 |
2015 |
2010 |
Mexico |
23,883 |
24,152 |
23,983 |
24,215 |
24,043 |
25,487 |
24,553 |
24,881 |
25,024 |
26,001 |
24,221 |
24,298 |
20,050 |
19,556 |
18,992 |
20,012 |
19,930 |
20,045 |
20,716 |
19,555 |
19,554 |
19,668 |
18,811 |
19,488 |
Reshape data, separate column, convert month abbreviations to numbers.
goods<- gather(goods,"Month","Volume", 4:27)
str(goods) #check structure
## 'data.frame': 240 obs. of 5 variables:
## $ year : int 2014 2014 2014 2014 2014 2015 2015 2015 2015 2015 ...
## $ CTY_CODE: int 1220 5700 4280 5880 2010 1220 5700 4280 5880 2010 ...
## $ CTYNAME : chr "Canada" "China" "Germany" "Japan" ...
## $ Month : chr "IJAN" "IJAN" "IJAN" "IJAN" ...
## $ Volume : chr "27,401" "37,564" "10,275" "11,334" ...
goods<-separate(goods,Month, into = c("Type", "Month"), sep = 1)
goods$Volume<-as.numeric(gsub(",", "",goods$Volume))
goods$Month<-gsub("JAN","01",goods$Month)
goods$Month<-gsub("FEB","02",goods$Month)
goods$Month<-gsub("MAR","03",goods$Month)
goods$Month<-gsub("APR","04",goods$Month)
goods$Month<-gsub("MAY","05",goods$Month)
goods$Month<-gsub("JUN","06",goods$Month)
goods$Month<-gsub("JUL","07",goods$Month)
goods$Month<-gsub("AUG","08",goods$Month)
goods$Month<-gsub("SEP","09",goods$Month)
goods$Month<-gsub("OCT","10",goods$Month)
goods$Month<-gsub("NOV","11",goods$Month)
goods$Month<-gsub("DEC","12",goods$Month)
str(goods) #check structur
## 'data.frame': 240 obs. of 6 variables:
## $ year : int 2014 2014 2014 2014 2014 2015 2015 2015 2015 2015 ...
## $ CTY_CODE: int 1220 5700 4280 5880 2010 1220 5700 4280 5880 2010 ...
## $ CTYNAME : chr "Canada" "China" "Germany" "Japan" ...
## $ Type : chr "I" "I" "I" "I" ...
## $ Month : chr "01" "01" "01" "01" ...
## $ Volume : num 27401 37564 10275 11334 23414 ...
| 2014 |
1220 |
Canada |
I |
01 |
27401 |
| 2014 |
5700 |
China |
I |
01 |
37564 |
| 2014 |
4280 |
Germany |
I |
01 |
10275 |
| 2014 |
5880 |
Japan |
I |
01 |
11334 |
| 2014 |
2010 |
Mexico |
I |
01 |
23414 |
| 2015 |
1220 |
Canada |
I |
01 |
25729 |
Reshape data to create columns for Exports/Imports.
goods<-spread(goods,Type,Volume)
kable(head(goods))
| 2014 |
1220 |
Canada |
01 |
24386 |
27401 |
| 2014 |
1220 |
Canada |
02 |
25310 |
27957 |
| 2014 |
1220 |
Canada |
03 |
25455 |
28639 |
| 2014 |
1220 |
Canada |
04 |
25777 |
28184 |
| 2014 |
1220 |
Canada |
05 |
26142 |
29388 |
| 2014 |
1220 |
Canada |
06 |
26239 |
29451 |
Add column for YYYY-MM
goods$ymdate<-paste(goods$year, goods$Month, sep="-")
goods<-mutate(goods,ymdate)
kable(head(goods))
| 2014 |
1220 |
Canada |
01 |
24386 |
27401 |
2014-01 |
| 2014 |
1220 |
Canada |
02 |
25310 |
27957 |
2014-02 |
| 2014 |
1220 |
Canada |
03 |
25455 |
28639 |
2014-03 |
| 2014 |
1220 |
Canada |
04 |
25777 |
28184 |
2014-04 |
| 2014 |
1220 |
Canada |
05 |
26142 |
29388 |
2014-05 |
| 2014 |
1220 |
Canada |
06 |
26239 |
29451 |
2014-06 |
Show plots.
ggplot(
goods, aes(x = ymdate, y = E)) +
geom_bar(stat = "identity",fill="#00ffbf",color="black", width = 0.8) +
ggtitle("Exports by Month in 2014 and 2015")+
facet_wrap(~ CTYNAME) +
theme(panel.grid.minor.x=element_blank(), panel.grid.major.x=element_blank(),axis.text=element_text(angle=90)) +
scale_y_continuous(breaks=seq(0,40000,20000))+
labs(x="Year and Month",y="Millions of Dollars")

ggplot(
goods, aes(x = ymdate, y = I)) +
geom_bar(stat = "identity",fill="blue",color="black", width = 0.8) +
ggtitle("Imports by Month in 2014 and 2015") +
facet_wrap(~ CTYNAME) +
theme(panel.grid.minor.x=element_blank(), panel.grid.major.x=element_blank(),axis.text=element_text(angle=90)) +
scale_y_continuous(breaks=seq(0,40000,20000))+
labs(x="Year and Month",y="Millions of Dollars")

Conclusion
China had the most imports between January 2014 and December 2015. Canada had the most exports for this time period, more than twice China’s exports. Canada and China showed a similar dip in exports in early 2015. However, China had an outlier month for imports in March 2015 which was not mirrored by the other countries chosen. Othewrise, exports and imports for any chosen country showed parallel trends.