-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrIPFP - Functions_for_intensity_analysis.R
365 lines (276 loc) · 14.9 KB
/
rIPFP - Functions_for_intensity_analysis.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
## Functions for intensity analysis ##
# Intensity under ICP classification (returns MJ/USD)
# In the end, I will replace the codes above with this function.
DeriveIntensities <- function(country='IND', type='final', final.intensity.mat=tfei.exio, pri.intensity.mat=tpei.USE) {
icp_fd_cty_usd <- eval(parse(text=paste0(country, "_FD_ICP_io.yr")))
list[result_all, NC_all, FD_adj] <- Run_rIPFP(bridge_ICP_EXIO_q[,-1], country)
final_alloc_list_all <- lapply(result_all, func1)
#### Tentatively inserted to generate average bridge mtx (for India collabo (Soumyajit) and Pm2.5) 5/20/2019
out <- data.frame(ICP=ICP_catnames, Reduce("+", final_alloc_list_all)/ length(final_alloc_list_all))
names(out)[-1] <- EX_catnames
write.xlsx(out,
paste0("C:/Users/min/IIASA/DLE - Documents/Air Pollution/IND.bridge.EXIO-COICOP.xlsx"))
####
if (class(final.intensity.mat)=="numeric") {
final.intensity.mat <- matrix(final.intensity.mat, nrow=1)
}
alloc_nonRAS <- get_bridge_COICOP_EXIO(bridge_ICP_EXIO_q[,-1], n_draw)
inten_RAS_all <- SetupSectorIntensities(final_alloc_list_all, NC_all, countrycode(country,"iso3c", "iso2c"), type, final.intensity.mat, pri.intensity.mat)
nonRAS_all <- SetupSectorIntensities(alloc_nonRAS, NC_all, countrycode(country,"iso3c", "iso2c"), type, final.intensity.mat, pri.intensity.mat)
no_expense <- which((rowSums(bridge_ICP_EXIO_q[,-1])!=0) & (icp_fd_cty_usd[,1]==0))
no_expense <- no_expense[!(no_expense %in% grep("UNBR", ICP_catnames))] # Remove UNBR items
# inten_comb_all <- inten_RAS_all
# inten_comb_all[,no_expense] <- nonRAS_all[,no_expense]
return(list(inten_RAS_all, final_alloc_list_all, NC_all, FD_adj))
}
# Allocation ratio in cells - summing up to 1 each row
func1 <- function (x) {
a <- diag(1/rowSums(x))
a[is.infinite(a)] <- 0
x <- a %*% x
}
GetHHSectoralEnergyPerCap <- function(idx, country='IND', fd_HH, int_sect) {
xlcFreeMemory()
scaler <- eval(parse(text=paste0("scaler_",country)))
eHH_sect <- t(int_sect[,idx, drop=FALSE] %*% fd_HH[idx, , drop=FALSE] / 1000) / scaler # MJ to GJ / HH
xlcFreeMemory()
eHH_sect <- data.table(hhid = colnames(fd_HH), eHH_sect)
idx_end <- dim(eHH_sect)[2]
setkey(eHH_sect, hhid)
xlcFreeMemory()
cty_HH <- eval(parse(text=paste0(country, "_HH")))
eHH_sect <- merge(eHH_sect, cty_HH, by="hhid")
setkey(eHH_sect, hhid, consumption)
# eHH.per.cap.by.decile <- eHH_sect %>% group_by(decile) %>% mutate(pop=weight*hh_size) %>% mutate_at(vars(V1:V500), funs(.*weight)) %>%
# summarise_at(vars(V1:V500,pop), sum) %>% mutate_at(vars(V1:V500), funs(./pop))
xlcFreeMemory()
# Data.table doesn't look reliable. The column division doesn't work!!! (WTF)
eHH_cap_sect <- eHH_sect
eHH_cap_sect <- eHH_cap_sect[, 2:idx_end := eHH_cap_sect[,2:idx_end, with=FALSE] / hh_size] # idx_end instead of (n_draw+1) because of no-converge runs
eHH_cap_sd <- data.table(hhid = colnames(fd_HH),
sd = apply(eHH_cap_sect[,2:idx_end, with=FALSE], 1, sd),
eHH_cap_sect[,(idx_end+1):dim(eHH_sect)[2], with=FALSE])
# eHH_cap_sect <- data.frame(eHH_sect)
# eHH_cap_sect[, 2:idx_end] <- eHH_cap_sect[, 2:idx_end] / eHH_cap_sect$hh_size # idx_end instead of (n_draw+1) because of no-converge runs
# eHH_cap_sd <- data.table(hhid = colnames(fd_HH),
# sd = apply(eHH_cap_sect[,2:idx_end], 1, sd),
# eHH_cap_sect[,(idx_end+1):dim(eHH_sect)[2]])
return(list(eHH_cap_sect, eHH_cap_sd))
}
IndirecIntensitiesByICPSect <- function(sect_idx, cty) {
# sect_idx <- grep(ICP_sect_name, ICP_catnames, ignore.case = TRUE)
print(ICP_catnames[sect_idx])
map_idx <- which(bridge_ICP_EXIO_q[sect_idx,-1]==1)
cty_alloc <- eval(parse(text=paste0(countrycode(cty,"iso2c", "iso3c"), "_alloc")))
sect_alloc <- do.call("rbind", lapply(cty_alloc, '[', sect_idx,))
cty_place <- which(exio_ctys==cty)
# cty_idx_fd <- seq(7*(cty_place-1)+1, 7*cty_place) # 7 final demand columns per country
# cty_idx <- seq(200*(cty_place-1)+1, 200*cty_place) # 200 EXIO commodities per country
all_bp_idx <- c(map_idx, trd_idx, trp_idx)
own_ex_idx <- (cty_place-1)*200 + all_bp_idx
alloc <- apply(sect_alloc[,all_bp_idx], 2, mean)
indirec.int <- colSums(indirect_E_int[,own_ex_idx]) * EXR_EUR$r # MJ/USD2007
direc.int <- colSums(energy_int[,own_ex_idx]) * EXR_EUR$r # MJ/USD2007
comp <- rbind(as.integer(all_bp_idx), indirec.int, direc.int, alloc)
print(map_idx)
print(comp) # MJ/Eur
return(comp)
}
# Get by-decile summary (mean,sd) of any eHH
SummarizeGJPerCapByDecile <- function(eHH) {
nColTot <- dim(eHH)[2]
nColAdd <- dim(BRA_HH)[2]-1 # we do this because # of drawn columns are not equal to n_draw because of no convergences in rIPFP
eHH_sum <- data.table(eHH[,1, with=FALSE],
avg = apply(eHH[,2:(nColTot-nColAdd), with=FALSE], 1, mean), # , with=FALSE all removed (data.table issue)
sd = apply(eHH[,2:(nColTot-nColAdd), with=FALSE], 1, sd),
eHH[,(nColTot-nColAdd+1):nColTot, with=FALSE])
# eHH_sum <- eHH_sum %>% group_by(decile) %>% summarise(u=weighted.mean(avg, weight), sd=weighted.mean(sd, weight))
eHH_sum <- eHH_sum %>% mutate(tothhE = avg*hh_size*weight, sdE = sd*hh_size*weight) %>% group_by(decile) %>%
summarise(u = sum(tothhE)/sum(weight*hh_size), sd=sum(sdE)/sum(weight*hh_size)) %>% arrange(decile) #, pop=sum(weight*hh_size)
return(eHH_sum[,-1])
}
SummarizeGJPerCap <- function(eHH) {
nColTot <- dim(eHH)[2]
nColAdd <- dim(BRA_HH)[2]-1 # we do this because # of drawn columns are not equal to n_draw because of no convergences in rIPFP
eHH_sum <- data.table(hhid = eHH[,1, with=FALSE],
avg = apply(eHH[,2:(nColTot-nColAdd), with=FALSE], 1, mean), # , with=FALSE all removed (data.table issue)
sd = apply(eHH[,2:(nColTot-nColAdd), with=FALSE], 1, sd),
eHH[,(nColTot-nColAdd+1):nColTot, with=FALSE])
# eHH_sum <- eHH_sum %>% group_by(decile) %>% summarise(u=weighted.mean(avg, weight), sd=weighted.mean(sd, weight))
eHH_sum <- eHH_sum %>% mutate(tothhE = avg*hh_size*weight, sdE = sd*hh_size*weight) %>%
summarise(u = sum(tothhE)/sum(weight*hh_size), sd=sum(sdE)/sum(weight*hh_size)) #, pop=sum(weight*hh_size)
return(eHH_sum)
}
cbind.dt.simple = function(...) {
x = c(...)
setattr(x, "class", c("data.table", "data.frame"))
ans = .Call(data.table:::Calloccolwrapper, x, max(100L, ncol(x) + 64L), FALSE)
.Call(data.table:::Csetnamed, ans, 0L)
}
###########################
### Plotting functions ###
###########################
PlotIntensityHist.decile <- function (intens_HH, name="V", xmax, bin_size=0.1, drawline=TRUE, linedata, ticksize=10) {
xlcFreeMemory()
opar <- par()
par(mfrow=c(10,1), oma = c(0, 0, 0, 0), mar= c(2, 0, 0, 0))
for (i in 1:10) {
a <- as.matrix(intens_HH %>% filter(decile==paste0("decile",i)) %>% select(starts_with(name)))
w0 <- intens_HH$weight[intens_HH$decile==paste0("decile",i)]
w1 <- w0[rep(1:length(w0), each=dim(a)[2])] # dim(a)[2] instead of n_draw because of non-converge runs
# weighted.hist(a, w1, seq(0, max(a)+bin_size, bin_size), xlim=c(0,xmax), main=NULL, xaxis = FALSE)
d <- density(a, weights = w1)
plot(d, axes=FALSE, xlim=c(1,xmax), main=' ')
axis(side = 1, at = seq(0,xmax,ticksize))
if (drawline) {
abline(v=linedata[i,1], col="red")
}
}
par(opar)
}
PlotIntensityHist.all <- function (intens_HH, name="V", xmax, bin_size=0.1, drawline=TRUE, linedata, ticksize=10) {
xlcFreeMemory()
a <- as.matrix(intens_HH %>% select(starts_with(name)))
w0 <- as.matrix(bind_cols(replicate(dim(a)[2], all_HH_f_IN %>% select(weight), simplify = FALSE)))
d <- density(a, weights = w0)
plot(d, axes=FALSE, xlim=c(1,xmax), main=' ')
axis(side = 1, at = seq(0,xmax,ticksize))
if (drawline) {
abline(v=linedata[i,1], col="red")
}
}
PlotIncomeByCountry <- function (f_HH, xmax, bin_size=0.1, tick_size = 1) { # bin/tick size in kilo USD
xlcFreeMemory()
opar <- par()
par(mfrow=c(10,1), oma = c(0, 0, 0, 0), mar= c(2, 0, 0, 0))
for (i in 1:10) {
a <- as.matrix(f_HH %>% filter(decile==paste0("decile",i)) %>% select(consumption)) / 1000 # 1000 USD/yr
w0 <- f_HH$weight[f_HH$decile==paste0("decile",i)]
w1 <- w0[rep(1:length(w0), each=dim(a)[2])] # dim(a)[2] instead of n_draw because of non-converge runs
weighted.hist(a, w1, seq(0, max(a)+bin_size, bin_size), xlim=c(0,xmax), main=NULL, xaxis = FALSE)
axis(side = 1, at = seq(0, xmax, tick_size), cex.axis=1.5)
}
par(opar)
}
PlotMainHistBRA <- function (intens_HH, name="V", xmax, bin_size=0.1, linedata, ticksize=10) {
xlcFreeMemory()
# opar <- par()
layout(matrix(1:11, ncol=1))
par(oma = c(0, 0, 2, 0), mar= c(2, 0, 0, 0))
for (i in 1:10) {
a <- as.matrix(intens_HH %>% filter(decile==paste0("decile",i)) %>% select(starts_with(name)))
w0 <- intens_HH$weight[intens_HH$decile==paste0("decile",i)]
w1 <- w0[rep(1:length(w0), each=dim(a)[2])] # dim(a)[2] instead of n_draw because of non-converge runs
# weighted.hist(a, w1, seq(0, max(a)+bin_size, bin_size), xlim=c(0,xmax), freq=FALSE, main=NULL, xaxis = FALSE)
d<- density(a, weights = w1); d$y <- d$y/sum(d$y)
plot(d, axes=FALSE, xlim=c(1,xmax), main=' ')
axis(side = 1, at = seq(0,xmax,ticksize))
axis(side = 2)
abline(v=(seq(0,250,10)), col="lightgray", lty="dotted")
m1 <- linedata[i,8]; m2 <- linedata[i,6]; m3 <- linedata[i,4];m4 <- linedata[i,2];
sd1 <- linedata[i,9]; sd2 <- linedata[i,7]; sd3 <- linedata[i,5]; sd4 <- linedata[i,3];
# Uncertainty bands
polygon(x=c(m1-2*sd1, m1+2*sd1, m1+2*sd1, m1-2*sd1), y=c(0,0,1,1), col=rgb(1,0,0,0.3), border=NA)
polygon(x=c(m2-2*sd2, m2+2*sd2, m2+2*sd2, m2-2*sd2), y=c(0,0,1,1), col=rgb(0,0,1,0.3), border=NA)
polygon(x=c(m3-2*sd3, m3+2*sd3, m3+2*sd3, m3-2*sd3), y=c(0,0,1,1), col=rgb(0.3,0.8,0.3,0.3), border=NA)
polygon(x=c(m4-2*sd4, m4+2*sd4, m4+2*sd4, m4-2*sd4), y=c(0,0,1,1), col=rgb(1,0,1,0.3), border=NA)
# Mean lines
abline(v=m1,lwd=1, col="red")
abline(v=m2,lwd=1, col="blue")
abline(v=m3,lwd=1, col=rgb(0.3,0.8,0.3,1))
abline(v=m4,lwd=1, col="purple")
}
plot.new()
legend(x="center", ncol=4, c("ownVal-adjFD", "defVal-adjFD", "ownVal-orgFD", "defVal-orgFD"),
lty = 1, lwd=2, col=c("red", "blue", rgb(0.3,0.8,0.3,1), "purple"),
y.intersp=0.4, cex = 1, bty = "n",
pt.bg = c(rgb(1,0,0,0.3),rgb(0,0,1,0.3),rgb(0.3,0.8,0.3,0.3),rgb(1,0,1,0.3)))
# par(opar)
}
PlotMainHistIND <- function (intens_HH, name="V", xmax, bin_size=0.1, linedata, ticksize=10) {
xlcFreeMemory()
layout(matrix(1:11, ncol=1))
par(oma = c(0, 0, 2, 0), mar= c(2, 0, 0, 0))
for (i in 1:10) {
a <- as.matrix(intens_HH %>% filter(decile==paste0("decile",i)) %>% select(starts_with(name)))
w0 <- intens_HH$weight[intens_HH$decile==paste0("decile",i)]
w1 <- w0[rep(1:length(w0), each=dim(a)[2])] # dim(a)[2] instead of n_draw because of non-converge runs
# weighted.hist(a, w1, seq(0, max(a)+bin_size, bin_size), xlim=c(0,xmax), freq=FALSE, main=NULL, xaxis = FALSE)
d<- density(a, weights = w1); d$y <- d$y/sum(d$y)
plot(d, axes=FALSE, xlim=c(1,xmax), main=' ')
axis(side = 1, at = seq(0,xmax,ticksize))
axis(side = 2)
abline(v=(seq(0,100,10)), col="lightgray", lty="dotted")
m1 <- linedata[i,12]; m2 <- linedata[i,10];
sd1 <- linedata[i,13]; sd2 <- linedata[i,11];
# Uncertainty bands
polygon(x=c(m1-2*sd1, m1+2*sd1, m1+2*sd1, m1-2*sd1), y=c(0,0,1,1), col=rgb(1,0,0,0.3), border=NA)
polygon(x=c(m2-2*sd2, m2+2*sd2, m2+2*sd2, m2-2*sd2), y=c(0,0,1,1), col=rgb(0,0,1,0.3), border=NA)
# Mean lines
abline(v=m1,lwd=1, col="red")
abline(v=m2,lwd=1, col="blue")
}
plot.new()
legend(x="center", ncol=2, c("adjFD", "orgFD"),
lty = 1, lwd=2, col=c("red", "blue"),
y.intersp=0.4, cex = 1, bty = "n",
pt.bg = c(rgb(1,0,0,0.3),rgb(0,0,1,0.3)))
}
PlotNonfuelIntensity <- function(intensity_mtx, noexp, ymax, titlename) {
divider <- c(2, 8, 14, 17, 22, 25, 28, 32, 37,
40, 47, 55, 65, 84, 95, 112, 116, 134, 135, 137)
idx_section_name <- c(divider)+1
section_name <- icp_ntnu$ICP_Heading[idx_section_name]
section_name <- gsub("UNBR ", "", section_name)
section_name[19] <- "Restaurants and hotels"
nonFuelIntensity <- intensity_mtx[,1:151]
n_sector_nonfuel <- 151
noexp_nonfuel <- noexp[noexp<=151]
# Non-fuel
boxplot(nonFuelIntensity, range=0, ylim=c(0, ymax), axes = FALSE, add=FALSE)
col_div <- c(par("usr")[1], divider+0.5, par("usr")[2])
# Paint alternating colors
for(i in 1:(length(col_div)-1)) {
color_bgn <- c("gray60", "gray15")[i %% 2]
rect(col_div[i], par("usr")[3], col_div[i+1], par("usr")[4],col = color_bgn, border=FALSE)
}
boxplot(nonFuelIntensity, ylab ="Primary energy intensity [MJ/2007USD]",
axes = FALSE, ylim=c(0, ymax), add=TRUE, cex.lab=1.3, range=0)
# axis(side = 1, at = seq(1,n_sector_icp,10))
title(xlab ="Consumption items", line=1, cex.lab=1.3)
axis(side = 2, at = seq(0,ymax,20), cex.axis=1.1)
idx_section <- c(divider)+1
text(idx_section-1, y=40, section_name, pos=4, offset=0.8, cex = 1, srt = 90)
text(1:n_sector_nonfuel+0.5, y=apply(nonFuelIntensity, 2, max), 1:n_sector_nonfuel, pos=3, offset=1, cex = 0.7, srt = 90)
text(noexp_nonfuel+0.9, y=apply(nonFuelIntensity[,noexp_nonfuel], 2, max), '+', pos=3, offset=2, cex = 1.2, srt = 90)
title(titlename)
pp <- recordPlot()
return(pp)
}
PlotFuelIntensity <- function(intensity_mtx, noexp, ymax) {
# Fuel
opar <- par()
par(mar=c(10,4,1,1))
FuelIntensity <- intensity_mtx[,152:164]
noexp_fuel <- noexp[noexp>151]-151
FuelLabel <- DLE_fuelnames_std$item
FuelLabel[2] <- "Charcoal/coal/brisquette"
FuelLabel[6] <- "Fuelwood"
FuelLabel[8] <- "Fuel oil"
boxplot(FuelIntensity, axes = FALSE, ylim=c(0, ymax), add=FALSE, cex.lab=1.3, range=0, ylab ="Primary energy intensity [MJ/2007USD]")
axis(side = 2, at = seq(0,ymax,50), cex.axis=1.1)
text(noexp_fuel, y=apply(FuelIntensity[,noexp_fuel], 2, max), '+', pos=3, offset=1, cex = 1.2, srt = 90)
axis(side = 1, at = 1:dim(DLE_fuelnames_std)[1], labels=FuelLabel, las=2, srt = 45)
# axis(side = 1, at = 1:dim(DLE_fuelnames_std)[1], labels=FALSE)
# text(x=1:dim(DLE_fuelnames_std)[1], y=par()$usr[3]-0.05*(par()$usr[4]-par()$usr[3]), cex = 0.8, labels=FuelLabel, srt = 45, xpd=TRUE, adj=1)
par(opar)
pp <- recordPlot()
return(pp)
}
# Build embodied intensity table after excluding certain sectors (e.g. transportation)
ConstructCustomTEI.EXIO <- function(dfei, idx_ex) { # dfei: n_carrier x (200*n_sect)
idx <- as.vector(sapply(seq(0,exio.len-200,200), function(x) x+idx_ex, simplify = "array"))
dfei[,idx] <- 0
tfei <- eigenMapMatMult(dfei, as.matrix(L_inverse)) # faster
return(tfei)
}