## create R program in working directory
## knitr::purl('sandbox/amis/descriptionAmis.Rmd')
## Load required functions
library(data.table)
library(ggplot2)
library(scales)
library(knitr)
library(devtools)
library("yarrr")
library(RColorBrewer)
library(faoswsUtil)
knitr::opts_chunk$set(echo = TRUE)
dlpath <- file.path("C:", "Users", "caetano", "Documents", "Github", 
                    "faoswsStock", "sandbox", "validation", "2016-08-27-stocks_data.csv")

dataValidation <- fread(dlpath)

Abstract

This vignette provides a detailed analysis of the stock module. In addition to analysing the delta stocks, this document has analysis about the opening stocks and % of stocks available.
The group of commodities studied are cereals, pulses and refined sugar.

1. Descriptive Statistics

The methodology used to imputate the delta stock data was based on previous analyses done with AMIS Data and F.O Lichts Data. Find below the basic description of the data.

basic_info <- dataValidation[, list(countries = length(unique(geographicAreaM49)),
commodities = length(unique(measuredItemCPC)),
min_year = min(timePointYears),
max_year = max(timePointYears))]

kable(basic_info, format = "markdown", padding = 0, 
col.names=c("Country", "Commodity", "Min Year", "Max Year"))

2. Data Visualization

There is no production for refined sugar in 2014.

2.1 World

options(warn=-1)

dataValidation[totalSupply == 0, closingStocksEstimated := 0]
dataValidation[totalSupply == 0, deltaStocksEstimated := 0]
dataValidation[totalSupply == 0, openingStocksEstimated := 0]
dataValidation[totalSupply == 0, percentOpen := 0]

dataValidation[, timePointYears := as.numeric(timePointYears)]

tabPercent <- dataValidation[, list(
          percent = sum(openingStocksEstimated, na.rm=T)/sum(totalSupply, na.rm = T)
), by = list(timePointYears, type)
]

plotcolors <- colorRampPalette(c("black", "blue", "green"))

ggplot(data=tabPercent,
      aes(x=timePointYears, y=percent, colour=type)) +
 geom_line(aes(), stat = "identity", position=position_dodge(), size=1) + 
scale_y_continuous(labels = scales::percent) +
  scale_color_manual( values = plotcolors(length(unique(tabPercent$type))) ) +
  theme(legend.title=element_blank(), strip.text = element_text(size=7)) +
  ylab('% Opening Stocks') + xlab('') +
  ggtitle("World: % Opening Stocks by Year") +
  scale_x_continuous(lim=c(1999, 2014), breaks=seq(1999, 2014, 3)) +
  theme_bw() +
  theme(
    axis.text.x = element_text(size = 8, face = "bold", angle = 45, vjust = .5),
    legend.position = "top",
    legend.title=element_blank()
)
options(warn=-1)

tabSupply <- dataValidation[, list(
totalSupply = sum(totalSupply, na.rm = T)
), by = list(timePointYears, type)
]

plotcolors <- colorRampPalette(c("black", "blue", "green"))

ggplot(data=tabSupply,
      aes(x=timePointYears, y=totalSupply, colour=type)) +
 geom_line(aes(), stat = "identity", position=position_dodge(), size=1) + 
 facet_grid(
    type ~ .
      , scales = "free"
    ) +
# scale_y_continuous(labels = scales::percent) +
  scale_color_manual( values = plotcolors(length(unique(tabSupply$type))) ) +
  theme(legend.title=element_blank(), strip.text = element_text(size=7)) +
  ylab('Total Supply (tonnes)') + xlab('') +
  ggtitle("World: Total Supply by Year") +
  scale_x_continuous(lim=c(1999, 2014), breaks=seq(1999, 2014, 3)) +
  theme_bw() +
  theme(
    axis.text.x = element_text(size = 8, face = "bold", angle = 45, vjust = .5),
    legend.position = "top",
    legend.title=element_blank()
)
options(warn=-1)

tabDelta <- dataValidation[, list(
totalDeltaStocks = sum(deltaStocksEstimated, na.rm = T),
totalSupply = sum(totalSupply, na.rm = T)
), by = list(timePointYears, type)
]

setkey(tabDelta, type, timePointYears)
tabDelta[, deltaTotalSupply := c(0, diff(totalSupply)),
         by = list(type)]


ggplot(data = tabDelta[timePointYears > 1999 & timePointYears < 2014],
       aes(x=deltaTotalSupply, y=totalDeltaStocks, group=1)) +
  geom_point(aes(), stat = "identity", position=position_dodge(), size=1.5) +
  facet_wrap(~ type, scales = "free") +
  theme_bw() +
  theme(legend.title=element_blank(), strip.text = element_text(size=7)) +
  ggtitle("World \n Delta Total Supply vs. Delta Stocks") +
  ylab('Delta Stocks (Million tonnes)') + xlab('Delta Total Supply (Million tonnes)') +
  geom_smooth(method=lm)

2.2 Income Group

options(warn=-1)

tabPercent <- dataValidation[, list(
          percent = sum(openingStocksEstimated, na.rm=T)/sum(totalSupply, na.rm = T)
# ,
# totalSupply = sum(totalSupply, na.rm = T)
), by = list(timePointYears, type, incomeGroup)
]

tabPercent$incomeGroup <- factor(tabPercent$incomeGroup, 
levels = c("High income", "Upper middle income", "Lower middle income", "Low income"))

plotcolors <- colorRampPalette(c("gray70", "blue", "green", "black"))
tabPercent <- tabPercent[!is.na(incomeGroup)]

ggplot(data=tabPercent,
      # aes(x=timePointYears, y=percent, colour=type)) +
      aes(x=timePointYears, y=percent, group=incomeGroup, colour = incomeGroup)) +
 geom_line(aes(), stat = "identity", position=position_dodge(), size=1) + 
 facet_grid(
    type ~ .
      , scales = "free"
    ) +
scale_y_continuous(labels = scales::percent) +
   scale_color_manual( values = plotcolors(length(unique(tabPercent$incomeGroup)))) +
   theme(legend.title=element_blank(), strip.text = element_text(size=5)) +
  ylab('% Opening Stocks') + xlab('') +
  ggtitle("Income Group: % Opening Stocks by Year") +
  scale_x_continuous(lim=c(1999, 2014), breaks=seq(1999, 2014, 3)) +
  theme_bw() +
  theme(
    axis.text.x = element_text(size = 8, face = "bold", angle = 45, vjust = .5),
    legend.position = "top",
    legend.title=element_blank()
)

````

```r
options(warn=-1)

tabSupplyIncome <- dataValidation[, list(
totalSupply = sum(totalSupply, na.rm = T)
), by = list(timePointYears, type, incomeGroup)
]

tabSupplyIncome$incomeGroup <- factor(tabSupplyIncome$incomeGroup, 
levels = c("High income", "Upper middle income", "Lower middle income", "Low income"))

plotcolors <- colorRampPalette(c("gray70", "blue", "green", "black"))
tabSupplyIncome <- tabSupplyIncome[!is.na(incomeGroup)]

ggplot(data=tabSupplyIncome,
      # aes(x=timePointYears, y=percent, colour=type)) +
      aes(x=timePointYears, y=totalSupply, group=incomeGroup, colour = incomeGroup)) +
 geom_line(aes(), stat = "identity", position=position_dodge(), size=1) + 
 facet_grid(
    type ~ .
      , scales = "free"
    ) +
# scale_y_continuous(labels = scales::percent) +
   scale_color_manual( values = plotcolors(length(unique(tabSupplyIncome$incomeGroup)))) +
   theme(legend.title=element_blank(), strip.text = element_text(size=5)) +
  ylab('Total Supply (tonnes)') + xlab('') +
  ggtitle("Income Group: Total Supply by Year") +
  scale_x_continuous(lim=c(1999, 2014), breaks=seq(1999, 2014, 3)) +
  theme_bw() +
  theme(
    axis.text.x = element_text(size = 8, face = "bold", angle = 45, vjust = .5),
    legend.position = "top",
    legend.title=element_blank()
)

````

```r
options(warn=-1)

tabDeltaIncome <- dataValidation[, list(
totalDeltaStocks = sum(deltaStocksEstimated, na.rm = T),
totalSupply = sum(totalSupply, na.rm = T)
), by = list(timePointYears, type, incomeGroup)
]


tabDeltaIncome$incomeGroup <- factor(tabDeltaIncome$incomeGroup, 
levels = c("High income", "Upper middle income", "Lower middle income", "Low income"))

plotcolors <- colorRampPalette(c("gray70", "blue", "green", "black"))
tabDeltaIncome <- tabDeltaIncome[!is.na(incomeGroup)]

setkey(tabDeltaIncome, type, incomeGroup, timePointYears)
tabDeltaIncome[, deltaTotalSupply := c(0, diff(totalSupply)),
         by = list(type, incomeGroup)]


ggplot(data = tabDeltaIncome[timePointYears > 1999 & timePointYears < 2014 & type == "cereals"],
       aes(x=deltaTotalSupply, y=totalDeltaStocks, group=incomeGroup, colour=incomeGroup)) +
  geom_point(aes(), stat = "identity", position=position_dodge(), size=1.5) +
  facet_grid(type ~ incomeGroup, scales="free_y") +
  # facet_grid(incomeGroup ~ type, scales="free_y") +
  scale_color_manual( values = plotcolors(length(unique(tabDeltaIncome$incomeGroup)))) +
  theme_bw() +
  theme(legend.title=element_blank(), strip.text = element_text(size=7)) +
  ggtitle("Income Group & Cereals \n Delta Total Supply vs. Delta Stocks") +
  ylab('Delta Stocks (Million tonnes)') + xlab('Delta Total Supply (Million tonnes)') #+
  #geom_smooth(method=lm)
options(warn=-1)

ggplot(data = tabDeltaIncome[timePointYears > 1999 & timePointYears < 2014 & type == "pulses"],
       aes(x=deltaTotalSupply, y=totalDeltaStocks, group=incomeGroup, colour=incomeGroup)) +
  geom_point(aes(), stat = "identity", position=position_dodge(), size=1.5) +
  facet_grid(type ~ incomeGroup, scales="free_y") +
  # facet_grid(incomeGroup ~ type, scales="free_y") +
  scale_color_manual( values = plotcolors(length(unique(tabDeltaIncome$incomeGroup)))) +
  theme_bw() +
  theme(legend.title=element_blank(), strip.text = element_text(size=7)) +
  ggtitle("Income Group & Pulses \n Delta Total Supply vs. Delta Stocks") +
  ylab('Delta Stocks (Million tonnes)') + xlab('Delta Total Supply (Million tonnes)') #+
  #geom_smooth(method=lm)

````

```r
options(warn=-1)

ggplot(data = tabDeltaIncome[timePointYears > 1999 & timePointYears < 2014 & type == "refined_sugar"],
       aes(x=deltaTotalSupply, y=totalDeltaStocks, group=incomeGroup, colour=incomeGroup)) +
  geom_point(aes(), stat = "identity", position=position_dodge(), size=1.5) +
  facet_grid(type ~ incomeGroup, scales="free_y") +
  # facet_grid(incomeGroup ~ type, scales="free_y") +
  scale_color_manual( values = plotcolors(length(unique(tabDeltaIncome$incomeGroup)))) +
  theme_bw() +
  theme(legend.title=element_blank(), strip.text = element_text(size=7)) +
  ggtitle("Income Group & Refined Sugar \n Delta Total Supply vs. Delta Stocks") +
  ylab('Delta Stocks (Million tonnes)') + xlab('Delta Total Supply (Million tonnes)') #+
  #geom_smooth(method=lm)

````


## 2.3 Country

There is no production for **China** (**1248**) from 2012 to 2014.

```r
options(warn=-1)

tabPercentCountry <- dataValidation[, list(
          percent = sum(openingStocksEstimated, na.rm=T)/sum(totalSupply, na.rm = T)
# ,
# totalSupply = sum(totalSupply, na.rm = T)
), by = list(timePointYears, type, geographicAreaM49_description)
]

tabPercentCountry <- tabPercentCountry[!is.na(percent)]

plotcolors <- colorRampPalette(c("gray70", "blue", "green", "black"))
# plotcolors <- colorRampPalette(c("gray70", "blue", "black"))

ggplot(data=tabPercentCountry[geographicAreaM49_description %in% c("Brazil", "India", "Germany", "United States of America", "China")],
      # aes(x=timePointYears, y=percent, colour=type)) +
      aes(x=timePointYears, y=percent, group=geographicAreaM49_description, colour = geographicAreaM49_description)) +
 geom_line(aes(), stat = "identity", position=position_dodge(), size=1) + 
 facet_grid(
    type ~ .
      , scales = "free"
    ) +
scale_y_continuous(labels = scales::percent) +
    scale_color_manual( values = plotcolors(length(unique(tabPercentCountry[geographicAreaM49_description %in% c("Brazil", "India", "Germany", "United States of America", "China")]$geographicAreaM49_description)))) +
   theme(legend.title=element_blank(), strip.text = element_text(size=5)) +
  ylab('% Opening Stocks') + xlab('') +
  ggtitle("Country: % Opening Stocks by Year") +
  scale_x_continuous(lim=c(1999, 2014), breaks=seq(1999, 2014, 3)) +
  theme_bw() +
  theme(
    axis.text.x = element_text(size = 8, face = "bold", angle = 45, vjust = .5),
    legend.position = "top",
    legend.title=element_blank()
)
````

```r
options(warn=-1)

tabSupplyCountry <- dataValidation[, list(
totalSupply = sum(totalSupply, na.rm = T)
), by = list(timePointYears, type, geographicAreaM49_description)
]

tabSupplyCountry <- tabSupplyCountry[!is.na(totalSupply)]

plotcolors <- colorRampPalette(c("gray70", "blue", "green", "black"))
# plotcolors <- colorRampPalette(c("gray70", "blue", "black"))

ggplot(data=tabSupplyCountry[geographicAreaM49_description %in% c("Brazil", "India", "Germany", "United States of America", "China")],
      # aes(x=timePointYears, y=percent, colour=type)) +
      aes(x=timePointYears, y=totalSupply, group=geographicAreaM49_description, colour = geographicAreaM49_description)) +
 geom_line(aes(), stat = "identity", position=position_dodge(), size=1) + 
 facet_grid(
    type ~ .
      , scales = "free"
    ) +
# scale_y_continuous(labels = scales::percent) +
    scale_color_manual( values = plotcolors(length(unique(tabSupplyCountry[geographicAreaM49_description %in% c("Brazil", "India", "Germany", "United States of America", "China")]$geographicAreaM49_description)))) +
   theme(legend.title=element_blank(), strip.text = element_text(size=5)) +
  ylab('Total Supply (tonnes)') + xlab('') +
  ggtitle("Country: Total Supply by Year") +
  scale_x_continuous(lim=c(1999, 2014), breaks=seq(1999, 2014, 3)) +
  theme_bw() +
  theme(
    axis.text.x = element_text(size = 8, face = "bold", angle = 45, vjust = .5),
    legend.position = "top",
    legend.title=element_blank()
)
````


```r
options(warn=-1)

tabDeltaCountry <- dataValidation[, list(
totalDeltaStocks = sum(deltaStocksEstimated, na.rm = T),
totalSupply = sum(totalSupply, na.rm = T)
), by = list(timePointYears, type, geographicAreaM49_description)
]

tabDeltaCountry <- tabDeltaCountry[!is.na(totalSupply)]

plotcolors <- colorRampPalette(c("gray70", "blue", "green", "black"))

setkey(tabDeltaCountry, type, geographicAreaM49_description, timePointYears)
tabDeltaCountry[, deltaTotalSupply := c(0, diff(totalSupply)),
         by = list(type, geographicAreaM49_description)]


ggplot(data = tabDeltaCountry[timePointYears > 1999 & timePointYears < 2014 &
geographicAreaM49_description %in% c("Brazil", "India", "Germany", "United States of America", "China") & type == "cereals"],
       aes(x=deltaTotalSupply, y=totalDeltaStocks, group=geographicAreaM49_description, colour=geographicAreaM49_description)) +
  geom_point(aes(), stat = "identity", position=position_dodge(), size=1.5) +
  facet_grid(type ~ geographicAreaM49_description, scales="free_y") +
  # facet_grid(incomeGroup ~ type, scales="free_y") +
  scale_color_manual( values = plotcolors(length(unique(tabSupplyCountry[geographicAreaM49_description %in% c("Brazil", "India", "Germany", "United States of America", "China")]$geographicAreaM49_description)))) +
  theme_bw() +
  theme(legend.title=element_blank(), strip.text = element_text(size=7)) +
  ggtitle("Country & Cereals \n Delta Total Supply vs. Delta Stocks") +
  ylab('Delta Stocks (Million tonnes)') + xlab('Delta Total Supply (Million tonnes)') #+
  #geom_smooth(method=lm)

```r options(warn=-1)

ggplot(data = tabDeltaCountry[timePointYears > 1999 & timePointYears < 2014 & geographicAreaM49_description %in% c("Brazil", "India", "Germany", "United States of America", "China") & type == "pulses"], aes(x=deltaTotalSupply, y=totalDeltaStocks, group=geographicAreaM49_description, colour=geographicAreaM49_description)) + geom_point(aes(), stat = "identity", position=position_dodge(), size=1.5) + facet_grid(type ~ geographicAreaM49_description, scales="free_y") + # facet_grid(incomeGroup ~ type, scales="free_y") + scale_color_manual( values = plotcolors(length(unique(tabSupplyCountry[geographicAreaM49_description %in% c("Brazil", "India", "Germany", "United States of America", "China")]$geographicAreaM49_description)))) + theme_bw() + theme(legend.title=element_blank(), strip.text = element_text(size=7)) + ggtitle("Country & Pulses \n Delta Total Supply vs. Delta Stocks") + ylab('Delta Stocks (Million tonnes)') + xlab('Delta Total Supply (Million tonnes)') #+ #geom_smooth(method=lm)

```r
options(warn=-1)

ggplot(data = tabDeltaCountry[timePointYears > 1999 & timePointYears < 2014 &
geographicAreaM49_description %in% c("Brazil", "India", "Germany", "United States of America", "China") & type == "refined_sugar"],
       aes(x=deltaTotalSupply, y=totalDeltaStocks, group=geographicAreaM49_description, colour=geographicAreaM49_description)) +
  geom_point(aes(), stat = "identity", position=position_dodge(), size=1.5) +
  facet_grid(type ~ geographicAreaM49_description, scales="free_y") +
  # facet_grid(incomeGroup ~ type, scales="free_y") +
  scale_color_manual( values = plotcolors(length(unique(tabSupplyCountry[geographicAreaM49_description %in% c("Brazil", "India", "Germany", "United States of America", "China")]$geographicAreaM49_description)))) +
  theme_bw() +
  theme(legend.title=element_blank(), strip.text = element_text(size=7)) +
  ggtitle("Country & Refined Sugar \n Delta Total Supply vs. Delta Stocks") +
  ylab('Delta Stocks (Million tonnes)') + xlab('Delta Total Supply (Million tonnes)') #+
  #geom_smooth(method=lm)


SWS-Methodology/faoswsStock documentation built on May 23, 2021, 7:35 a.m.