-
Notifications
You must be signed in to change notification settings - Fork 2
/
Init.R
368 lines (270 loc) · 16.4 KB
/
Init.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# This file needs to be run once at the very beginning of an analysis
# rm(list=ls())
setwd("H:/MyDocuments/IO work/DLE_scripts") # Change if run from OneDrive "/IIASA/DLE - Documents/WS2 - Documents/Analysis/IO/DLE_scripts/"
#################
### Constants ###
#################
### EXIO country order ###
exio_ctys <- c("AT", "BE", "BG", "CY", "CZ", "DE", "DK", "EE", "ES", "FI",
"FR", "GR",
# "HR", # Added new at EXIO3
"HU", "IE", "IT", "LT", "LU", "LV", "MT", "NL",
"PL", "PT", "RO", "SE", "SI", "SK", "GB", "US", "JP", "CN",
"CA", "KR", "BR", "IN", "MX", "RU", "AU", "CH", "TR", "TW",
"NO", "ID", "ZA", "WA", "WL", "WE", "WF", "WM")
num.cty <- length(exio_ctys)
exio.len <- length(exio_ctys)*200
exio.fd.len <- length(exio_ctys)*7
n_draw <- 100
D_val_uncertainty <- 0 # or 1 : Whether to include uncertainty analysis for valuation mtx - margins and tax rates
draw_count <- 1
options(digits=3)
# DLE DB: 2010 $ PPP
# EXIO: XXXX EUR MER
CES.year <- 2010 # DLE DB is in 2010$ PPP.
IO.year <- 2010 # Pick one closest to the survey year. 2007 for EXIO2. 2010 for DLE NatENE.2015 for DLE scaleup
# DLE DB in PPP 2010$ (PPP in terms of private consumption)
# EXIO in MER 2007
# Need this PPP rate to go back to local currency in 2010
# [LCU/$]
PPP_cty = WDI(country = c("IN", "BR", "ZA"), indicator = c("PA.NUS.PPP", "PA.NUS.PRVT.PP"), start = CES.year, end = CES.year, extra = FALSE, cache = NULL)
PPP_IND <- as.numeric(PPP_cty %>% filter(country=="India") %>% select(PA.NUS.PPP))
PPP_BRA <- as.numeric(PPP_cty %>% filter(country=="Brazil") %>% select(PA.NUS.PPP))
PPP_ZAF <- as.numeric(PPP_cty %>% filter(country=="South Africa") %>% select(PA.NUS.PPP))
# Inflation
# Deflate currency in 2010 to 2007 (EXIO)
CPI <- WDI(country = c("IN", "BR", "FR", "ZA"), indicator = "FP.CPI.TOTL", start = 2007, end = 2015, extra = FALSE, cache = NULL)
CPI_ratio_IND <- as.numeric(CPI %>% filter(year==CES.year & iso2c=='IN') %>% select(FP.CPI.TOTL) / CPI %>% filter(year==IO.year & iso2c=='IN') %>% select(FP.CPI.TOTL))
CPI_ratio_BRA <- as.numeric(CPI %>% filter(year==CES.year & iso2c=='BR') %>% select(FP.CPI.TOTL) / CPI %>% filter(year==IO.year & iso2c=='BR') %>% select(FP.CPI.TOTL))
CPI_ratio_ZAF <- as.numeric(CPI %>% filter(year==CES.year & iso2c=='ZA') %>% select(FP.CPI.TOTL) / CPI %>% filter(year==IO.year & iso2c=='ZA') %>% select(FP.CPI.TOTL))
# Exchange rate (MER) [LCU/$]
EXR_EUR <- WDI(country = "XC", indicator = "PA.NUS.FCRF", start = IO.year, end = IO.year, extra = FALSE, cache = NULL)
EXR_EUR <- EXR_EUR %>% rename(r=PA.NUS.FCRF)
EXR_cty <- WDI(country = c("IN", "BR", "ZA"), indicator = "PA.NUS.FCRF", start = IO.year, end = IO.year, extra = FALSE, cache = NULL)
EXR_IND <- as.numeric(EXR_cty %>% filter(country=="India") %>% select(PA.NUS.FCRF))
EXR_BRA <- as.numeric(EXR_cty %>% filter(country=="Brazil") %>% select(PA.NUS.FCRF))
EXR_ZAF <- as.numeric(EXR_cty %>% filter(country=="South Africa") %>% select(PA.NUS.FCRF))
# HH Consumption in India 2007 [US$]
HH_CON <- WDI(country = c("IN", "BR", "ZA"),
indicator = c(#"NE.CON.PETC.CD",
"NE.CON.PRVT.CD",
# "NE.CON.PETC.CN",
"NE.CON.PRVT.KD"),
start = 2004, end = 2011, extra = FALSE, cache = NULL)
BRA_con_grwth <- as.numeric(HH_CON %>% filter(year==2008 & iso2c=='BR') %>% select(NE.CON.PRVT.KD) /
HH_CON %>% filter(year==IO.year & iso2c=='BR') %>% select(NE.CON.PRVT.KD))
IND_con_grwth <- as.numeric(HH_CON %>% filter(year==2011 & iso2c=='IN') %>% select(NE.CON.PRVT.KD) /
HH_CON %>% filter(year==IO.year & iso2c=='IN') %>% select(NE.CON.PRVT.KD))
ZAF_con_grwth <- as.numeric(HH_CON %>% filter(year==2010 & iso2c=='ZA') %>% select(NE.CON.PRVT.KD) /
HH_CON %>% filter(year==IO.year & iso2c=='ZA') %>% select(NE.CON.PRVT.KD))
# Imports/Exports of goods and services (% of GDP)
WDI(country = c("IN", "BR"), indicator = c("NE.IMP.GNFS.ZS", "NE.EXP.GNFS.ZS"), start = 2007, end = 2007, extra = FALSE, cache = NULL)
Popul <- WDI(country = c("IN", "BR", "FR", "ZA"), indicator = "SP.POP.TOTL", start = 2007, end = 2015, extra = FALSE, cache = NULL)
BRA_pop_io.yr <- Popul %>% rename(pop=SP.POP.TOTL) %>% filter(iso2c=="BR" & year==IO.year) %>% select(pop) %>% as.numeric()#1.9e8
IND_pop_io.yr <- Popul %>% rename(pop=SP.POP.TOTL) %>% filter(iso2c=="IN" & year==IO.year) %>% select(pop) %>% as.numeric()#1.159e9
ZAF_pop_io.yr <- Popul %>% rename(pop=SP.POP.TOTL) %>% filter(iso2c=="ZA" & year==IO.year) %>% select(pop) %>% as.numeric()#1.159e9
##############################################
### Read in EXIO matrices ###
##############################################
# Takes long time to run.
# Some .Rda files are already created to save time.
# source("EXIO_init.R")
# EXIO_init_load.R derives energy intensity matrices.
# But since EXIO3, these matrices are read in 'import_EXIO_FE_extension.R'
# Now these intensities are based on 2008 IEA balance and fed into dfei.exio and tfei.exio.
# Using 2008 instead of 2007 because the Indian final electricity consumption breakdown into industries doesn't exist until 2007.
source("EXIO_init_load.R") # EXIO2 read - Still necessary with EXIO3 for setting indexes, carrier names, etc.
source("DLE_integration_functions.R")
#########################################
### Get EXIO FD vectors for countries ###
#########################################
# Get IND final demand from EXIO [M.EUR to M.USD]
IND_place <- which(exio_ctys=="IN")
IND_idx_fd <- seq(7*(IND_place-1)+1, 7*IND_place) # 7 final demand columns per country
IND_idx_ex <- seq(200*(IND_place-1)+1, 200*IND_place) # 200 EXIO comodities
IND_idx_ex.i <- seq(163*(IND_place-1)+1, 163*IND_place) # 163 EXIO industries
IND_fd_ex <- matrix(final_demand[,IND_idx_fd[1]], nrow=200) / EXR_EUR$r # to M.USD (2007 MER)
IND_fd_exio <- rowSums(IND_fd_ex) # Sum all HH FD across countries
IND_fd_exio_imp <- rowSums(IND_fd_ex[,-IND_place]) # Sum all HH FD across countries
# Get BRA final demand from EXIO [M.EUR to M.USD]
BRA_place <- which(exio_ctys=="BR")
BRA_idx_fd <- seq(7*(BRA_place-1)+1, 7*BRA_place) # 7 final demand columns per country
BRA_idx_ex <- seq(200*(BRA_place-1)+1, 200*BRA_place) # 7 final demand columns per country
# Issue: This 'final_demand' for BRA gives too small values for electricity expenditure.
# Instead I can use the column from 'BR_output.xls' file.
# BRA_fd_ex <- matrix(final_demand[,BRA_idx_fd[1]], nrow=200)
# BRA_fd_exio <- rowSums(BRA_fd_ex) # Sum all HH FD across countries
# BRA_fd_exio_imp <- rowSums(BRA_fd_ex[,-BRA_place]) # Sum all HH FD across countries
BRA_fd_ex <- read_excel("../Valuation/BR_output.xls", sheet="usebptot", skip=14, col_names=FALSE)
# Issue: Brazil FD has zero education expediture. (reasons unknown)
# Simply replace the zero with the values found on actual BRA IO
BRA_fd_exio <- as.matrix(BRA_fd_ex[1:200,169])
BRA_fd_exio[174] <- 15600 # M Euro
BRA_fd_exio <- BRA_fd_exio / EXR_EUR$r # to M.USD 2007
# The value 15600 is from H:\MyDocuments\IO work\Bridging\CES-COICOP\BRA IO FD comparison.xlsx
# Get ZAF final demand from EXIO [M.EUR to M.USD]
ZAF_place <- which(exio_ctys=="ZA")
ZAF_idx_fd <- seq(7*(ZAF_place-1)+1, 7*ZAF_place) # 7 final demand columns per country
ZAF_idx_ex <- seq(200*(ZAF_place-1)+1, 200*ZAF_place) # 200 EXIO comodities
ZAF_idx_ex.i <- seq(163*(ZAF_place-1)+1, 163*ZAF_place) # 163 EXIO industries
ZAF_fd_ex <- matrix(final_demand[,ZAF_idx_fd[1]], nrow=200) / EXR_EUR$r # to M.USD (2007 MER)
ZAF_fd_exio <- rowSums(ZAF_fd_ex) # Sum all HH FD across countries
#########################################
### Read in COICOP-EXIO Qual mapping ### (replaced by ICP-EXIO below)
#########################################
# # Issue: This is to be replaced by 'bridge_ICP_EXIO_q' (currently in Map_CES_COICOP.R).
# # But bridge_COICOP_EXIO_q is used as a base for contructing bridge_ICP_EXIO_q.
# # I will move the scripts here (or call from here) once it is being used
# n_sector_coicop <- 109
#
# # Mapping <- system.file("COICOP3_EXIO_bridge.xlsx", package = "XLConnect")
# wb <- XLConnect::loadWorkbook("../Bridging/CES-COICOP/COICOIP_EXIO_Qual_UN_Edited.xlsx")
#
# # Qualitative mapping (0 or 1)
# bridge_COICOP_EXIO_q <- XLConnect::readWorksheet(wb, sheet="COICOIP_EXIO_Qual_UN", header=FALSE,
# startRow=2, endRow=1+n_sector_coicop, startCol=1, endCol= 201, forceConversion=T)
#
# # Final COICOP classification with 109 headings
# COICOP_catnames2 <- XLConnect::readWorksheet(wb, sheet="COICOIP_EXIO_Qual_UN", header=FALSE, startRow=2, endRow=1+n_sector_coicop, startCol=1, endCol=1)
# EX_catnames <- XLConnect::readWorksheet(wb, sheet="COICOIP_EXIO_Qual_UN", header=FALSE, startRow=1, endRow=1, startCol=2)
#
# # Issue: This qual mapping may change depending on countries, which we need to tackle then.
##############################################
### Read in ICP-EXIO Qual mapping ###
##############################################
# This is already excuted and saved in a file.
# Don't need to run everytime.
# source("Generate_base_ICP-EXIO_mapping.R")
n_sector_icp <- 151 # Num of ICP sectors
# This matrix is modified externally manually based on the resulting csv from running Generate_base_ICP-EXIO_mapping.R
# to fine-allocate mostly for food-subsectors.
# The result is in H:\MyDocuments\IO work\Bridging\CES-COICOP\ICP_EXIO_Qual_Edited.xlsx
# Manually changed cells are colored in green in the xlsx file.
# Two types of manual changes
# 1. ICP item disaggregation info further details (meat -> poultry)
# 2. Some positive EXIO FD values do not match to any ICP sectors. (e.g. stone from EXIO mapped to household maintenance in ICP)
# => can be checked by cbind(names(qual_map)[colConst_init!=0 & colSums(qual_map_init)==0], colConst_init[colConst_init!=0 & colSums(qual_map_init)==0])
wb <- XLConnect::loadWorkbook("../Bridging/CES-COICOP/ICP_EXIO_Qual_UN_Edited.xlsx")
bridge_ICP_EXIO_q <- XLConnect::readWorksheet(wb, "ICP_EXIO_Qual_UN2", header=TRUE,
forceConversion=T, endCol=201)
ICP_catnames <- bridge_ICP_EXIO_q[,1]
EX_catnames <- names(bridge_ICP_EXIO_q)[-1]
#####################################################
### Treating CES fuel sectors differently ###
#####################################################
wb <- XLConnect::loadWorkbook("../Bridging/CES-COICOP/CES_fuel_EXIO.xlsx")
bridge_fuel_EXIO_q <- XLConnect::readWorksheet(wb, "Sheet1", header=TRUE, forceConversion=T,
startRow=2, startCol=3, endCol=202)
DLE_fuel_sector_Q <- XLConnect::readWorksheet(wb, "Sheet2", header=TRUE, forceConversion=T,
startRow=2, startCol=3)
DLE_fuelnames_std <- XLConnect::readWorksheet(wb, "Sheet1", header=TRUE, forceConversion=T,
startRow=2, startCol=2, endCol=2)
DLE_fuel_sector_Q[is.na(DLE_fuel_sector_Q)] <- 0
bridge_fuel_EXIO_q[is.na(bridge_fuel_EXIO_q)] <- 0
names(bridge_fuel_EXIO_q) <- EX_catnames
row.names(bridge_fuel_EXIO_q) <- DLE_fuelnames_std[,1]
names(DLE_fuelnames_std) <- "item"
n_sector_icp_fuel <- n_sector_icp + dim(DLE_fuelnames_std)[1]
############################################################
### Read final demand vector from each country's CES DB ###
############################################################
source("P:/ene.general/DecentLivingEnergy/Surveys/Generic function to access database.R")
source("Read_final_demand_from_DB.R")
source("Read_direct_energy_from_DB.R")
# Read total FD for all population
# dim: n_CES_sector x 2 (or 11 for deciles)
# Comprehensive fuel sectors (union of all DBs)
DLE_fuel_types <- ConstructyFuelTypeSet() %>% arrange(fuel)
# Reading and constructing matrices
# source("Read_DLE_DB.R")
# Read pre-constructed raw DB data
# # IND
load(file=paste0(getwd(), "/Saved tables/IND_FD.Rda"))
load(file=paste0(getwd(), "/Saved tables/IND_HH.Rda"))
load(file=paste0(getwd(), "/Saved tables/IND_AllHHConsump.Rda"))
# load(file=paste0(getwd(), "/Saved tables/IND1_FUEL_Alldata.Rda")) # IND_FUEL_Alldata
# load(file=paste0(getwd(), "/Saved tables/IND_FD_harmonized.Rda")) # IND_FD_ICP_AllHH
#
# # ZAF
load(file=paste0(getwd(), "/Saved tables/ZAF_FD.Rda"))
load(file=paste0(getwd(), "/Saved tables/ZAF_HH.Rda"))
load(file=paste0(getwd(), "/Saved tables/ZAF_AllHHConsump.Rda"))
#
# # BRA
load(file=paste0(getwd(), "/Saved tables/BRA_FD.Rda"))
load(file=paste0(getwd(), "/Saved tables/BRA_HH.Rda"))
load(file=paste0(getwd(), "/Saved tables/BRA_AllHHConsump.Rda"))
#####################################################
### Read in (CES-Pseudo COICOP) mappings from WB ###
#####################################################
### Read in ICP heading number following NTNU 109 mapping (not 100%, some ICP headings are aggregated) ###
Mapping <- system.file("ICP_SEQ.xlsx", package = "XLConnect")
wb <- XLConnect::loadWorkbook("../Bridging/CES-COICOP/Worldbank/ICP_SEQ.xls")
# I added 'Sheet2' and fixed some mis-categorizations for my needs.
icp_seq <- XLConnect::readWorksheet(wb, sheet="Sheet2", header=TRUE, startRow=2, startCol=1, endCol=1, forceConversion=T)
icp_cat <- XLConnect::readWorksheet(wb, sheet="Sheet2", header=FALSE, startRow=3, startCol=3, endCol=4, forceConversion=T)
NTNU <- XLConnect::readWorksheet(wb, sheet="Sheet2", header=TRUE, startRow=2, startCol=7, endCol=8, forceConversion=T)
icp_ntnu <-cbind(icp_seq, icp_cat, NTNU)
names(icp_ntnu)[2:3] <- c("COICOP1","COICOP2")
names(icp_ntnu)[5] <- "ICP_Heading"
source("rIPFP - Process_WB.R") # Read in the function 'processWBscript' and resulting mtxs for 4 countries
# Issue: I still need to match with our CES DB and final NTNU 109 classification
# How to combine fuel consumption and other (food etc)
# -> We decided to follow ICP headings from the WB and bridge this ICP classification to EXIO.
##############################################
### Generate CES-ICP mapping ###
##############################################
# Read in CES code tables, fix some mis-mappings from WB, and create CES_ICP_IDN, CES_ICP_IND, etc.
# Then I can do
# IND_FD_ICP <- t(CES_ICP_IND) %*% as.matrix(IND_FD_code[,2])
# to get FD in ICP classification.
source("rIPFP - Map_CES_COICOP.R")
##############################################
### Load EXIOBASE3 ###
##############################################
# For EXIO3, L_inverse/final_demand/tot_demand should be updated.
# For now I only use 2007 and 2008
# This also overwrites EXIO FD vectors.
source("Import_EXIO3.R") # Incorporate EXIO3
# Need to come before "rIPFP - Valuation.R" because valuation data also changes
##########################################
### Read in function 'get_basic_price' ###
##########################################
source("rIPFP - Valuation.R")
# Run this once to generate and save those vectors
# But, better to use this (instead of the one below) to make sure which year XXX_FD_ICP_io.yr is based on.
source("Init_consumption_vectors.R")
# For a faster process
# source("Load_init_data.R") # Need 'get_purch_price'
#################################################
### Read in function 'Bridging_uncertainty.R' ###
#################################################
# The uniform random draw routine based on a qual mapping
source("rIPFP - Bridging_uncertainty.R")
#################################################
### Read in function 'Bridging_uncertainty.R' ###
#################################################
source("rIPFP - Functions_for_intensity_analysis.R")
##############################################
### Set up environment for RAS run ###
##############################################
source("rIPFP - Bridge_RAS.R")
##############################################
### Run analysis etc.! ###
##############################################
# Analysis_for_paper.R is the main file for analysis
ICP_food_idx <- 1:45
ICP_clth_idx <- 48:55
ICP_hous_idx <- c(56:58, 61)
ICP_watr_idx <- 59:60
ICP_ener_idx <- c(62:65, 103, 152:164)
ICP_hlth_idx <- 85:95
ICP_mobl_idx <- c(96:102, 104:112)
ICP_educ_idx <- 135
ICP_othr_idx <- c(46:47, 122:134, 136:151)
ICP_hhold_idx <- c(56:84, 138:151) # Household goods/services
ICP_svc_idx <- 85:137 # Health, Transport, Communication, Recreation
ICP_fuel_idx <-152:164
ICP_all_idx <- 1:164
# Food or clothing intensities and activities also can be done here
# source("DLE_integration_init.R") # Derive ICP intensities