## create R program in working directory ## knitr::purl('sandbox/fo_licht/description_fo_lichts.Rmd') ## 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)
dlpath <- file.path("C:", "Users", "caetano", "Documents", "Github", "faoswsStock", "sandbox", "fo_licht", "sugar_prod_trade_stocks_fo_lichts2016_06_08.csv") sugarData <- fread(dlpath) sugarData[, supply := production + imports - exports + opening_stocks] sugarData[, utilization := consumption + ending_stocks] sugarData[, delta_stocks := ending_stocks - opening_stocks] sugarData[, stocks_percent := ending_stocks/supply] sugarData[, delta_total_supply := diff(c(NA, supply)), by=list(country_group)] sugarData[, netTrade := imports - exports]
This vignette provides a simple analysis of the F.O.Lichts refined sugar data for the stock module.
The data below was pulled from the publication in May/2016. The variables studied are: Opening Stocks, Production, Trade, Consumption and Closing Stocks for industrialised and developing countries.
head(sugarData[, c("timePointYears", "country_group", "opening_stocks", "production", "imports", "consumption", "exports", "ending_stocks"), with = F], 3)
We computed the Supply, Utilization, Delta Stocks, % of Stocks and Delta Total Supply.
head(sugarData, 3)
options(warn=-1) plotcolors <- colorRampPalette(c("blue", "black")) ggplot(data=sugarData, aes(x=as.factor(timePointYears), y=netTrade, group=country_group, col = country_group)) + geom_line(aes(), stat = "identity", position=position_dodge(), size=1) + # facet_wrap(~ itemGroup # , scales = "free" # ) + scale_color_manual(values = plotcolors(length(unique(sugarData$country_group)))) + # theme(strip.text.x = element_text(size = 10, colour = "black", angle = 0)) + theme(legend.title=element_blank(), strip.text = element_text(size=7)) + geom_hline(yintercept=0, col = "black") + theme(legend.title=element_blank(), strip.text = element_text(size=7)) + xlab('Year') + ylab('Imports - Exports') + ggtitle("Net Trade") + #scale_x_continuous(lim=c(2005, 2012), breaks=seq(2005, 2012, 1)) + 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) ggplot(data=sugarData, aes(x=timePointYears, y=stocks_percent, group=country_group, col = country_group)) + geom_line(aes(), stat = "identity", position=position_dodge(), size=1) + # facet_wrap(~ countryRegionName, scales = "free") + # facet_grid( . ~ countryRegionName, scales = "free") + # facet_grid( # country_group ~ . # # , scales = "free" # ) + scale_y_continuous(lim=c(0, 0.35), breaks=seq(0, 0.35, 0.05), labels = percent) + #scale_colour_brewer(palette="Blues") + scale_color_manual( values = plotcolors(length(unique(sugarData$country_group)))) + # theme(legend.title=element_blank(), strip.text = element_text(size=7)) + ylab('% stocks') + xlab('Year') + ggtitle("% Stocks by Year") + #scale_x_continuous(lim=c(2000, 2016), breaks=seq(2000, 2016, 4)) + 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) ggplot(data = sugarData, aes(x=delta_total_supply, y=delta_stocks, group=1)) + geom_point(aes(), stat = "identity", position=position_dodge(), size=1.5) + facet_wrap(~ country_group #, scales = "free" ) + theme_bw() + theme(legend.title=element_blank(), strip.text = element_text(size=7)) + ggtitle("Refined Sugar \n Delta Total Supply vs. Delta Stocks \n (1,000 tonnes, raw value)") + ylab('Delta Stocks') + xlab('Delta Total Supply') + geom_smooth(method=lm)
tabCor <- sugarData[, list(corr = 100 * round(cor(delta_stocks, delta_total_supply, use = "pairwise.complete.obs"), 3)), by=list(country_group)]
kable(tabCor, format = "markdown", padding = 0)
sugarData[, country_group := as.factor(country_group)] sugarModel <- lm(formula = ending_stocks ~ -1 + supply + country_group * supply - country_group, data = sugarData) # summary(sugarModel) # # windows() # plot(fitted(sugarModel),residuals(sugarModel),xlab="Fitted Values",ylab="Residuals") # abline(h=0) # sugarModel <- lm(delta_stocks ~ delta_total_supply + country_group, data = sugarData)
A linear regression was fitted:
The coefficients are:
kable(data.frame(sugarModel$coefficients[1:2]), format = "markdown", padding = 0)
The R Square is r paste0(100*round(summary(sugarModel)$r.squared, 3), "%")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.