06 March 2015

Exploring the structure of national consumption

This entry is a direct continuation of my first and second entry on the structure of national resource consumption

library(ggplot2);library(Hmisc)
# READING IN DATA

## SETTING DIRECTORY FOR EORA DATA ON LOCAL HARD DRIVE
wd<-"G:/Documents/PostDocKVA/Data/Eora" ### data directory
setwd(wd)

## READING IN DATA
### MATERIAL USE DATA - ENERGY DATASET
energy.df<-read.csv("TradeBalance_I-ENERGY.csv",header=TRUE)
### Reading in .csv file with annual gdp and population sizes
gdppop.df<-read.csv("gdppop.csv",header=TRUE,skip=1) #skipping the first line which includes a description of the file

## REMOVING NEGATIVE AND ZERO CONSUMPTION ENTRIES
energy.df<-energy.df[which(energy.df[,"Consumption"]>0),]

## REMOVING NEGATIVE AND ZERO CONSUMPTION ENTRIES
energy.df<-energy.df[-which(as.character(energy.df$Country)=="Former USSR"),]


## merging the gdp and population size data onto the energy consumption data frame
energy.df<-merge(energy.df,gdppop.df,by=c("CountryA3","y","Country"),all.x=TRUE)


## To make consumption more comparable let's calculate per capita consumption by associating population data

### calculate per capita consumption and gdp consumption intensity by associating population data
energy.df[,"Consum.pop.int"]<-energy.df[,"Consumption"]/energy.df[,"val"]
energy.df[,"Consum.gdp.int"]<-energy.df[,"Consumption"]/energy.df[,"GDP"]

### calculating scaled consumption intensitities with respect to population size and gdp
energy.df<-energy.df[order(energy.df[,"Country"],energy.df[,"y"]),]
energy.df[,"Consum.pop.int.scale"]<-unlist(by(energy.df,energy.df[,"Country"], function(x) scale(x[,"Consum.pop.int"],center=TRUE,scale=TRUE)))
energy.df[,"Consum.gdp.int.scale"]<-unlist(by(energy.df,energy.df[,"Country"], function(x) scale(x[,"Consum.gdp.int"],center=TRUE,scale=TRUE)))

energy.df<-energy.df[-which( energy.df[,"y"] %in% c(1991,2000,2011)),]

Now let’s look at the contribution of imports and domestic extration to national consumption. We will do this qucikly in the beginning since I’m running out of time today.

head(energy.df)
##    CountryA3    y     Country TerritorialEmissions Imports Exports
## 43       AFG 1970 Afghanistan               115043   15343    1343
## 44       AFG 1971 Afghanistan               115043   13345    1262
## 45       AFG 1972 Afghanistan               115043   11725    1465
## 46       AFG 1973 Afghanistan               115043    9633    1392
## 47       AFG 1974 Afghanistan               115043    8265    1213
## 48       AFG 1975 Afghanistan               115043    7828    1150
##    DirectEmissions Consumption     GDP      val Consum.pop.int
## 43           43301      129043 1277935 11839729    0.010899151
## 44           43301      127126 1362663 12138578    0.010472891
## 45           43301      125303 1168728 12449180    0.010065161
## 46           43301      123284 1266842 12760486    0.009661388
## 47           43301      122094 1567124 13058067    0.009350082
## 48           43301      121720 1722525 13328589    0.009132249
##    Consum.gdp.int Consum.pop.int.scale Consum.gdp.int.scale
## 43     0.10097775             1.651512             2.065308
## 44     0.09329233             1.524365             1.807681
## 45     0.10721314             1.402746             2.274329
## 46     0.09731600             1.282307             1.942561
## 47     0.07790960             1.189450             1.292028
## 48     0.07066371             1.124474             1.049134
### Subtracting exports from imports and domestic extraction in proportion to their size.

### This is the conceptual code
#### consum.extract <- extraction - (export * extraction/(import+extraction))
#### consum.import <- import - (export * import/(import+extraction))

energy.df[,"Extraction"]<- (energy.df[,"TerritorialEmissions"] + energy.df[,"DirectEmissions"]) ## These two categories both refer to domestically extracted resources, i thik. Check up with Wiedmann dataset.

energy.df[,"Extraction"]<- energy.df[,"TerritorialEmissions"]

energy.df[,"Consum.extract"]<- (energy.df[,"Extraction"]) - 
    (energy.df[,"Exports"] * ((energy.df[,"Extraction"]) /
       (energy.df[,"Extraction"] + energy.df[,"Imports"])))

energy.df[,"Consum.import"]<- (energy.df[,"Imports"]) - 
    (energy.df[,"Exports"] * ((energy.df[,"Imports"]) /
       (energy.df[,"Extraction"] + energy.df[,"Imports"])))


energy.df[,"Consum.balance"]<-(energy.df[,"Consum.extract"]-energy.df[,"Consum.import"])/energy.df[,"Consumption"]

energy.df[order(energy.df[,"Consum.balance"],decreasing=TRUE)[1:20],c("Country","y","Consum.balance")]
##           Country    y Consum.balance
## 2987    Hong Kong 1975      1.8921038
## 3992      Liberia 1972      1.7620715
## 3993      Liberia 1973      1.3574014
## 5360       Norway 1997      1.2404283
## 3197    Indonesia 1975      1.2249416
## 7594     Viet Nam 1975      1.1396350
## 3996      Liberia 1976      1.1056177
## 4059        Libya 1997      1.0502739
## 826       Bahrain 1997      1.0426134
## 5631  Philippines 1975      1.0392322
## 5044     Malaysia 1975      1.0354441
## 7054 Turkmenistan 1981      1.0192899
## 1476        China 1975      1.0022135
## 947       Belarus 1992      0.9992896
## 945       Belarus 1990      0.9991773
## 944       Belarus 1989      0.9990176
## 943       Belarus 1988      0.9989872
## 935       Belarus 1980      0.9989454
## 942       Belarus 1987      0.9989381
## 936       Belarus 1981      0.9989218
energy.df<-energy.df[-which(energy.df[,"Consum.balance"]>1),] ## removing entries that are above 1 (i.e. all resources being domestically extracted)

energy.df<-energy.df[order(energy.df[,"Country"],energy.df[,"y"]),]
energy.df[,"mean.gdp"]<-unlist(tapply(energy.df[,"GDP"],energy.df[,"Country"],function(x) rep(mean(x,na.rm=TRUE),length(x))))
energy.df[,"mean.val"]<-unlist(tapply(energy.df[,"val"],energy.df[,"Country"],function(x) rep(mean(x,na.rm=TRUE),length(x))))


ggplot(energy.df[which(energy.df[,"y"]<2008),],aes(x=y,y=Consum.balance)) + geom_line(aes(Group=Country,alpha=mean.val,size=mean.gdp/mean.val))

val.membership.df<-data.frame("mean.val"=unique(energy.df[,"mean.val"]),"val.membership"=NA)

val.membership.df[,"val.membership"]<-as.numeric(cut2(val.membership.df$mean.val,m=20))


energy.df<-merge(energy.df,val.membership.df,by="mean.val",all.x=TRUE)

ggplot(energy.df[which(energy.df[,"y"]<2008 & is.na(energy.df[,"val.membership"])==FALSE),],aes(x=y,y=Consum.balance)) + geom_line(aes(Group=Country,alpha=mean.gdp/mean.val)) + facet_wrap(~val.membership)

I will comment further on these plots later. However, it is interesting to see that the darkest lines (highest gdp per capita) in each facet plot (1 (lowest) - 9 (highest) national population size) tends to have the strongest trends toward lower extraction vs import balance. No surprise, but good to be able to see this trend in the data.