## Load required functions
library(data.table)
library(ggplot2)
library(scales)
library(knitr)
library(devtools)
#install_github("ndphillips/yarrr")
library("yarrr")
library(RColorBrewer)
knitr::opts_chunk$set(echo = TRUE)
## This code was used to create the table tabGroup

# library(faosws)
# library(data.table)
# library(ggplot2)
# library(faoswsUtil)
# 
# ## set up for the test environment and parameters
# # initialYear = 1961 # Change documentation if this changes!
# 
# R_SWS_SHARE_PATH <- Sys.getenv("R_SWS_SHARE_PATH")
# 
# # This return FALSE if on the Statistical Working System
# if(CheckDebug()){
#   
#   message("Not on server, so setting up environment...")
#   
#   library(faoswsModules)
#   SETTINGS <- ReadSettings("modules/impute_stocks/sws.yml")
#   
#   # If you're not on the system, your settings will overwrite any others
#   R_SWS_SHARE_PATH <- SETTINGS[["share"]]
#   
#   # Define where your certificates are stored
#   SetClientFiles(SETTINGS[["certdir"]])
#   
#   # Get session information from SWS. Token must be obtained from web interface
#   GetTestEnvironment(baseUrl = SETTINGS[["server"]],
#                      token = SETTINGS[["token"]])
#   
# }
# 
# elements = c("51", "61", "71", "91")
# 
# # 51 production (Mt)
# # 61 imports (Mt)
# # 71 Variation Intial Exstenc (Mt) 
# # 91 exports (Mt)  
# # 101 feed (Mt)
# # 131 processed (Mt)
# # 141 food (Mt)
# 
# # Data
# key = DatasetKey(domain = "faostat_one", dataset = "FS1_SUA_UPD", dimensions = list(
#   Dimension(name = "geographicAreaFS", keys = GetCodeList("faostat_one", "FS1_SUA_UPD", "geographicAreaFS")[, code]),
#   Dimension(name = "measuredElementFS", keys = elements),
#   Dimension(name = "timePointYears", keys = as.character(2005:2012)),
#   Dimension(name = "measuredItemFS", keys = GetCodeList("faostat_one", "FS1_SUA_UPD", "measuredItemFS")[, code]))
# )
# data = GetData(key)
# data[, geographicAreaM49 := fs2m49(as.character(geographicAreaFS))]
# data[, measuredItemCPC := fcl2cpc(as.character(formatC(as.numeric(measuredItemFS), width = 4,
#                                                        flag = "0")))]
# # data = data[!is.na(geographicAreaM49) & 
# #               !(flagFaostat %in% c("M", "T"))]
# 
# data[, c("geographicAreaFS", "measuredItemFS", "flagFaostat") := NULL]
# setcolorder(data, c("geographicAreaM49", "measuredItemCPC", "measuredElementFS", 
#                     "timePointYears", "Value"))
# 
# # data[grepl("Hen", measuredItemCPC_description)]
# 
# data = data[, list(Value = sum(Value)), 
#             by = list(geographicAreaM49, measuredItemCPC, 
#                       measuredElementFS, timePointYears)]
# 
# item <- ReadDatatable("fbs_stocks_comm_codes")
# item <- item[fbs_key == "measuredItemCPC", fbs_code]
# 
# # Select just cereals, pulses and refined sugar
# 
# data <- data[measuredItemCPC %in% item]
# data[substr(measuredItemCPC, 1, 3) == "011", group := "cereals"]
# data[substr(measuredItemCPC, 1, 3) == "017", group := "pulses"]
# data[substr(measuredItemCPC, 1, 3) == "235", group := "refined_sugar"]
# data
# 
# ## More data manipulation 
# data <- dcast.data.table(data, geographicAreaM49 + measuredItemCPC + timePointYears + group ~ measuredElementFS,
#                          value.var = "Value")
# 
# setnames(data, old=c("51", "61", "71", "91"),
#          new=c("production", "imports", "variationIntialExstenc", "exports"))
# 
# ## Checking
# 
# for(cname in c("production", "imports", "variationIntialExstenc", "exports")){
#   data[is.na(get(cname)), c(cname) := 0]
# }
# 
# data[, netTrade := imports - exports]
# data
# 
# ## Country groups
# dlpath <- file.path("C:", "Users", "caetano", "Documents", "Github", "faoswsFeed",
#                     "data-raw", "IR_factor", "class.csv")
# 
# countryGroup <- fread(dlpath)
# countryGroup[, .N, GroupName]
# 
# countryGroup[grepl("income", GroupName), .N, GroupName]
# 
# countryIncomeGroup <- countryGroup[GroupCode %in% c("HIC", "LIC", "UMC", "LMC"), ]
# # countryRegionGroup <- countryGroup[GroupCode %in% c("NAC", "LCN", "AFR", "EAS", "EUU"),]
# 
# # library(countrycode)
# # ?countrycode
# 
# countryIncomeGroup[, geographicAreaM49 := as.character(countrycode(CountryCode, "wb", "iso3n"))]
# 
# # Sudan has the wrong name (it should be former Sudan)
# countryIncomeGroup[geographicAreaM49 == "736", CountryName := "Sudan (former)"]
# # China should be 1248
# countryIncomeGroup[geographicAreaM49 == "156", geographicAreaM49 := "1248"]
# #Exclude Channel Islands and Kosovo (not separately recognised by the UN)
# countryIncomeGroup <- countryIncomeGroup[!is.na(geographicAreaM49)]
# countryIncomeGroup
# 
# # Merge
# 
# data <- merge(data, countryIncomeGroup[, c("geographicAreaM49", "GroupName"), with = F], 
#       by = "geographicAreaM49", all.x=T)
# 
# data <- data[!is.na(geographicAreaM49)]
# data <- data[!is.na(GroupName)]
# 
# tabGroup <- data[, list(production = sum(production, na.rm = T),
#             imports = sum(imports, na.rm = T),
#             exports = sum(exports, na.rm = T),
#             deltaStocks = sum(variationIntialExstenc, na.rm = T)),
#      by=list(GroupName, group, timePointYears)]
# 
# tabGroup <- tabGroup[, trade := imports - exports]

tabGroup = fread("C:/Users/caetano/Documents/Github/faoswsStock/sandbox/tabGroup.csv")

Abstract

This vignette provides analysis for Cereals, Pulses and Sugar.

1. Data

The data was pulled from the domain faostat_one and dataset FS1_SUA_UPD between 2005 and 2012 for all the countries.
We are interested in Cereals, Pulses and Sugar. The measuredElementFS pulled are:

kable(data.table(measuredElementFS = c("51", "61", "71", "91"),
label = c("Production (Mt)", "Imports (Mt)", "Variation Intial Exstenc (Mt)", "Exports (Mt)")), format = "markdown", padding = 0)

We are using the .csv file from the Feed Module called class. This file has information for income by country. After combine the table from the SWS and the class.csv we get this dataset.

head(tabGroup, 5)

2. Data Visualization

options(warn=-1)

tabGroup$GroupName <- factor(tabGroup$GroupName, 
                        levels = c("High income", "Upper middle income", 
                                   "Lower middle income", "Low income"))
ggplot(data=tabGroup,
       aes(x=as.numeric(timePointYears), y=production, group=GroupName, col = GroupName)) +
  geom_line(aes(), stat = "identity", position=position_dodge(), size=1) +
  facet_wrap(~ group
             , scales = "free"
  ) +
  theme(legend.title=element_blank(), strip.text = element_text(size=7)) +
  xlab('Year') + ylab('Production') +
  scale_x_continuous(lim=c(2005, 2012), breaks=seq(2005, 2012, 1)) +
 theme(
    axis.text.x = element_text(size = 8, face = "bold", angle = 45, vjust = .5),
    legend.position = "top"
  )
options(warn=-1)
ggplot(data=tabGroup,
       aes(x=as.numeric(timePointYears), y=trade, group=GroupName, col = GroupName)) +
  geom_line(aes(), stat = "identity", position=position_dodge(), size=1) +
  facet_wrap(~ group
             , scales = "free"
  ) +
  theme(legend.title=element_blank(), strip.text = element_text(size=7)) +
  xlab('Year') + ylab('Imports - Exports') +
  scale_x_continuous(lim=c(2005, 2012), breaks=seq(2005, 2012, 1)) +
  geom_hline(yintercept=0) +
   theme(
    axis.text.x = element_text(size = 8, face = "bold", angle = 45, vjust = .5),
    legend.position = "top"
  )


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