-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlook_up_tables_for_SI.r
139 lines (115 loc) · 5.67 KB
/
look_up_tables_for_SI.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
## R package built for this paper:
library(TFPtoCO2)
library(dplyr)
## get emissions for all the countries (takes < 1.40 sec.):
data(elasticities)
countries <- unique(elasticities$i)
system.time(
pe.responses.all.i <- data.table::rbindlist(
lapply( countries , function(x){
r <- get.responses(ctry=x, boot = FALSE)
return(r)
}))
)
## Prepare matrix for supporting information spreadsheet:
pe.responses.all.i$Country.i <- countrycode::countrycode(pe.responses.all.i$i, 'iso3c',
'country.name')
pe.responses.all.i$Country.k <- countrycode::countrycode(pe.responses.all.i$k, 'iso3c',
'country.name')
emission.matrix <- reshape2::acast( pe.responses.all.i,
Country.i ~ Country.k, value.var = "emissions_GtCO2")
## get bootstrapped emissions for all the countries (took about 21
## seconds to complete)::
system.time(
responses.all.i <- data.table::rbindlist(
lapply( countries , function(x){
r <- get.responses(ctry=x, boot = TRUE)
return(r)
}))
)
## Post-processing of results: Table with domestic, foreign and total
## emissions from domestic TFP for SI spreadsheet:
alpha <- 0.1
table.totals.ci <- lapply( countries, function(x){
d <- subset(responses.all.i, i == x)
own <- aggregate(emissions_GtCO2 ~ k,
subset(d, k == x),
function(x) quantile(x,
probs=c( alpha/2,0.5,(1-alpha/2)),
names = TRUE))
own <- data.frame(effect = 'Domestic',
stat=c('lower.bound','median','upper.bound'),
Emissions_GtCO2= t(own[,-1]))
foreign <- data.frame(effect = 'Foreign',
stat=c('lower.bound','median','upper.bound'),
Emissions_GtCO2=quantile(d$emissions_GtCO2[d$k !=x],
probs=c( alpha/2,0.5,(1-alpha/2)),
names = TRUE))
total <- data.frame(effect = 'Total',
stat = c('lower.bound','median','upper.bound'),
Emissions_GtCO2=quantile(d$emissions_GtCO2,
probs=c( alpha/2,0.5,(1-alpha/2)),
names = TRUE) )
results <- rbind( own, foreign, total)
results$iso <- x
results
})
table.totals.ci <- data.table::rbindlist(table.totals.ci)
table.totals.ci$Country <- countrycode::countrycode(table.totals.ci$iso, 'iso3c',
'country.name')
table.totals.ci <- reshape2::acast( table.totals.ci,
Country ~ effect + stat,
value.var = "Emissions_GtCO2")
xlsx::write.xlsx(table.totals.ci, file ="../manuscript/TabulatedEffects_SI.xlsx",
sheetName = "Global Emissions",
col.names = TRUE, row.names = TRUE, append = FALSE)
xlsx::write.xlsx(emission.matrix, file ="../manuscript/TabulatedEffects_SI.xlsx",
sheetName = "Country Emissions",
col.names = TRUE, row.names = TRUE, append = TRUE)
## Write emission factors to SI
data(emission_data.ave)
?emission_data.ave
head(emission_data.ave)
EF.SI <- emission_data.ave[,c("iso","EF")]
EF.SI$EF <- EF.SI$EF*1e9 # convert to tons
EF.SI$country <- countrycode::countrycode(EF.SI$iso, "iso3c", "country.name")
EF.SI <- EF.SI[,c("country", "EF")]
xlsx::write.xlsx(EF.SI, file ="../manuscript/TabulatedEffects_SI.xlsx",
sheetName = "Emission Factors",
col.names = TRUE, row.names = TRUE, append = TRUE)
## 11/29/2022
## Domestic elasticities:
elasticities.boot.samples <- data.table::rbindlist(
lapply( countries , function(x){
r <- bootstrap.elasticities(x)
return(r)
}))
dom.elasticities <- elasticities.boot.samples %>%
filter(i==k) %>%
group_by(i) %>%
summarise( e.ik = quantile(e.ik, c(0.05, .5, .95)), q=c(0.05, .5, .95) )
dom.elasticities <- as.data.frame(reshape2::acast(dom.elasticities, i ~ q, value.var="e.ik"))
dom.elasticities$Country <- countrycode::countrycode(row.names(dom.elasticities), 'iso3c',
'country.name')
dom.elasticities <- dom.elasticities %>%relocate(Country)
xlsx::write.xlsx(dom.elasticities, file ="../manuscript/TabulatedEffects_SI.xlsx",
sheetName = "Domestic Elasticities",
col.names = TRUE, row.names = FALSE, append = TRUE)
## Foreign (bilateral) elasticities:
foreign.elasticities <- elasticities.boot.samples %>%
# filter(i!=k) %>%
group_by(i,k) %>%
summarise( e.ik = mean(e.ik) )
head(foreign.elasticities)
## Prepare matrix for supporting information spreadsheet:
foreign.elasticities$Country.i <- countrycode::countrycode(foreign.elasticities$i, 'iso3c',
'country.name')
foreign.elasticities$Country.k <- countrycode::countrycode(foreign.elasticities$k, 'iso3c',
'country.name')
foreign.elasticities.matrix <- reshape2::acast( foreign.elasticities,
Country.i ~ Country.k, value.var = "e.ik")
head(foreign.elasticities.matrix)
foreign.elasticities.matrix[is.na(foreign.elasticities.matrix)] <- 0
xlsx::write.xlsx(foreign.elasticities.matrix, file ="../manuscript/TabulatedEffects_SI.xlsx",
sheetName = "Bilateral Elasticities",
col.names = TRUE, row.names = TRUE, append = TRUE)