-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.Rhistory
More file actions
344 lines (344 loc) · 12.8 KB
/
Copy path.Rhistory
File metadata and controls
344 lines (344 loc) · 12.8 KB
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
getwd()
anfavea_fls <- list.files(file.path('~/input'),
pattern = 'siteauto',
full.names = T)
anfavea_fls <- list.files(file.path('./input'),
pattern = 'siteauto',
full.names = T)
anfavea_fls <- list.files(file.path('/input'),
pattern = 'siteauto',
full.names = T)
# ------------------------------------------------------------------------
# Projects 2050 fleet based on ENE scenario
#
#
# ------------------------------------------------------------------------
# input -------------------------------------------------------------------
load(file.path('./tmp', '3_compute_fleet.rda'))
# Plot Directory
plot.dir <- './output/plots'
# Inertial Growth Scenario ------------------------------------------------
# Computing shares of sales
regis_fleet_shares <- regis_fleet %>%
mutate(across(starts_with('Registration'), ~ .x / Registration, .names = 'Shr{col}'))
# ------------------------------------------------------------------------
# Master file: Projects BEV fleet (2050)
#
# Julio Barros
# 25/07/2023
# ------------------------------------------------------------------------
# preamble ----------------------------------------------------------------
rm(list = ls())
gc()
# packages ----------------------------------------------------------------
library(tidyverse)
library(readxl)
library(zoo)
library("ggsci")
library("ggthemes")
source('./code/1_anfavea_registration_yearly_files.R')
source('./code/2_anfavea_registration.R')
source('./code/3_compute_fleet.R')
source('./code/4_projection_inertial.R')
source('./code/5_projection_optimist.R')
source('./code/6_projection_biofuels.R')
source('./code/7_ancillary_graphs.R')
# ------------------------------------------------------------------------
# Master file: Projects BEV fleet (2050)
#
# Julio Barros
# 25/07/2023
# ------------------------------------------------------------------------
# preamble ----------------------------------------------------------------
rm(list = ls())
gc()
# packages ----------------------------------------------------------------
library(tidyverse)
library(readxl)
library(zoo)
library("ggsci")
library("ggthemes")
# run files ---------------------------------------------------------------
source('./code/1_anfavea_registration_yearly_files.R')
source('./code/2_anfavea_registration.R')
source('./code/3_compute_fleet.R')
source('./code/4_projection_inertial.R')
source('./code/5_projection_optimist.R')
source('./code/6_projection_biofuels.R')
source('./code/7_ancillary_graphs.R')
# ------------------------------------------------------------------------
# Master file: Projects BEV fleet (2050)
#
# Julio Barros
# 25/07/2023
# ------------------------------------------------------------------------
# preamble ----------------------------------------------------------------
rm(list = ls())
gc()
# packages ----------------------------------------------------------------
library(tidyverse)
library(readxl)
library(zoo)
library("ggsci")
library("ggthemes")
# run files ---------------------------------------------------------------
source('./code/1_anfavea_registration_yearly_files.R')
source('./code/2_anfavea_registration.R')
source('./code/3_compute_fleet.R')
source('./code/4_projection_inertial.R')
source('./code/5_projection_optimist.R')
source('./code/6_projection_biofuels.R')
source('./code/7_ancillary_graphs.R')
# ------------------------------------------------------------------------
# Projects 2050 fleet based on base scenario (centro clima)
#
#
# ------------------------------------------------------------------------
# input -------------------------------------------------------------------
load(file.path('./tmp', '3_compute_fleet.rda'))
# Plot Directory
plot.dir <- './output/plots'
# Inertial Growth Scenario ------------------------------------------------
# Computing shares of sales
regis_fleet_shares <- regis_fleet %>%
mutate(across(starts_with('Registration'), ~ .x / Registration, .names = 'Shr{col}'))
# Sales Expansion Rate (year over year):
sales_growth <- 1.044
# 2023 - 2030
regis_fleet_23_30 <- regis_fleet_shares %>%
add_row(Year = 2023:2030) %>%
filter(Year >= 2022) %>%
mutate(Registration = accumulate(Registration, ~ .x*sales_growth)) %>%
mutate(ShrRegistrationGasoline = case_when(Year < 2030 ~ accumulate(ShrRegistrationGasoline, ~.x -0.0031),
TRUE ~ 0)) %>%
mutate(ShrRegistrationFlex = accumulate(ShrRegistrationFlex, ~.x -0.035)) %>%
mutate(ShrRegistrationDiesel = accumulate(ShrRegistrationDiesel, ~.x - 0.006)) %>%
mutate(ShrRegistrationHybrid = accumulate(ShrRegistrationHybrid, ~.x + 0.04)) %>%
mutate(ShrRegistrationBEV = accumulate(ShrRegistrationBEV, ~.x + 0.004)) %>%
mutate(ShrRegistrationEthanol = replace_na(ShrRegistrationEthanol, 0)) %>%
filter(Year >= 2023)
# 2030 - 2045
regis_fleet_31_45 <- regis_fleet_23_30 %>%
add_row(Year = 2031:2045) %>%
filter(Year >= 2030) %>%
mutate(Registration = accumulate(Registration, ~ .x*sales_growth)) %>%
mutate(ShrRegistrationGasoline = replace_na(ShrRegistrationGasoline, 0)) %>%
mutate(ShrRegistrationFlex = case_when( Year < 2045 ~ accumulate(ShrRegistrationFlex, ~.x -0.037),
TRUE ~ 0)) %>%
mutate(ShrRegistrationDiesel = case_when( Year < 2045 ~ accumulate(ShrRegistrationDiesel, ~.x - 0.0046),
TRUE ~0 )) %>%
mutate(ShrRegistrationHybrid = accumulate(ShrRegistrationHybrid, ~.x + 0.037)) %>%
mutate(ShrRegistrationBEV = accumulate(ShrRegistrationBEV, ~.x + 0.0046)) %>%
mutate(ShrRegistrationEthanol = replace_na(ShrRegistrationEthanol, 0)) %>%
filter(Year >= 2031)
# 2045 - 2050
regis_fleet_46_50 <- regis_fleet_31_45 %>%
add_row(Year = 2046:2050) %>%
filter(Year >= 2045) %>%
mutate(Registration = accumulate(Registration, ~ .x*sales_growth)) %>%
mutate(ShrRegistrationHybrid = accumulate(ShrRegistrationHybrid, ~.x - 0.016)) %>%
mutate(ShrRegistrationBEV = accumulate(ShrRegistrationBEV, ~.x + 0.016)) %>%
mutate(across(c(ShrRegistrationGasoline,
ShrRegistrationEthanol,
ShrRegistrationFlex,
ShrRegistrationDiesel), ~ replace_na(.x, 0))) %>%
filter(Year >= 2046)
# Bind all into one dataframe:
# 2023 - 2050, computing registry:
regis_fleet_23_50 <- bind_rows(regis_fleet_23_30,
regis_fleet_31_45,
regis_fleet_46_50) %>%
mutate(RegistrationGasoline = ShrRegistrationGasoline * Registration,
RegistrationEthanol = ShrRegistrationEthanol * Registration,
RegistrationFlex = ShrRegistrationFlex * Registration,
RegistrationDiesel = ShrRegistrationDiesel * Registration,
RegistrationHybrid = ShrRegistrationHybrid * Registration,
RegistrationBEV = ShrRegistrationBEV * Registration)
# 1957 - 2050
regis_fleet_57_50 <- bind_rows(regis_fleet, regis_fleet_23_50)
# Compute Fleet -----------------------------------------------------------
# compute exit function
sucateamento_otto <- function(yr, registration) {
age <- max(yr) - yr
exit <- exp(-exp(1.798-0.137*age))
shr_available <- 1 - exit
shr_available * registration
}
sucateamento_diesel <- function(yr, registration) {
age <- max(yr) - yr
exit <- (1/(1 + exp(0.17*(age - 15.3)))) + (1/(1+exp(0.17*(age + 15.3))))
shr_available <- 1 - exit
shr_available * registration
}
# Yearly dataframes in a list:
regis_ls <- map(.x = regis_fleet_57_50$Year,
~ regis_fleet_57_50 %>% filter(Year <= .x))
# Pass exit function to compute each year's contribution to net of fleet
yearly_net <- map(.x = regis_ls,
~ .x %>% mutate( FleetGasoline = sucateamento_otto(Year, RegistrationGasoline),
FleetEthanol = sucateamento_otto(Year, RegistrationEthanol),
FleetFlex = sucateamento_otto(Year, RegistrationFlex),
FleetDiesel = sucateamento_diesel(Year, RegistrationDiesel),
FleetHybrid = sucateamento_otto(Year, RegistrationHybrid),
FleetBEV = sucateamento_otto(Year, RegistrationBEV)))
# Summation for each year
yearly_fleet <- map_df(.x = yearly_net,
~ .x %>%
mutate(across(starts_with("Fleet"), ~ sum(.x))) %>%
filter(Year == max(Year)) %>%
select(c(Year, starts_with("Fleet")))) %>%
mutate(ModeledFleet = rowSums(select(.,starts_with("Fleet"))))
View(yearly_fleet)
View(yearly_net)
View(yearly_fleet)
# save fleet data
write_excel_csv2(yearly_fleet, file = file.path('.', 'cenario_base.csv'))
# save fleet data
write_excel_csv2(yearly_fleet, file = file.path('./output', 'cenario_base.csv'))
# ------------------------------------------------------------------------
# Master file: Projects BEV fleet (2050)
#
# Julio Barros
# 25/07/2023
# ------------------------------------------------------------------------
# preamble ----------------------------------------------------------------
rm(list = ls())
gc()
# packages ----------------------------------------------------------------
library(tidyverse)
library(readxl)
library(zoo)
library("ggsci")
library("ggthemes")
# run files ---------------------------------------------------------------
source('./code/1_anfavea_registration_yearly_files.R')
source('./code/2_anfavea_registration.R')
source('./code/3_compute_fleet.R')
source('./code/4_projection_inertial.R')
source('./code/5_projection_optimist.R')
source('./code/6_projection_biofuels.R')
source('./code/7_ancillary_graphs.R')
wd()
getwd()
# preamble ----------------------------------------------------------------
rm(list = ls())
gc()
# packages ----------------------------------------------------------------
library(tidyverse)
library(readxl)
library(zoo)
library("ggsci")
library("ggthemes")
# run files ---------------------------------------------------------------
source('./code/1_anfavea_registration_yearly_files.R')
source('./code/2_anfavea_registration.R')
source('./code/3_compute_fleet.R')
source('./code/4_projection_inertial.R')
source('./code/5_projection_optimist.R')
source('./code/6_projection_biofuels.R')
source('./code/7_ancillary_graphs.R')
rm(list = ls())
gc()
# ------------------------------------------------------------------------
# Master file: Projects BEV fleet (2050)
#
# Julio Barros
# 25/07/2023
# ------------------------------------------------------------------------
# packages ----------------------------------------------------------------
library(tidyverse)
library(readxl)
library(zoo)
library("ggsci")
library("ggthemes")
# run files ---------------------------------------------------------------
source('./code/1_anfavea_registration_yearly_files.R')
source('./code/2_anfavea_registration.R')
source('./code/3_compute_fleet.R')
source('./code/4_projection_inertial.R')
source('./code/5_projection_optimist.R')
source('./code/6_projection_biofuels.R')
source('./code/7_ancillary_graphs.R')
# finish ------------------------------------------------------------------
rm(list = ls())
gc()
# ------------------------------------------------------------------------
# Master file: Projects BEV fleet (2050)
#
# Julio Barros
# 25/07/2023
# ------------------------------------------------------------------------
# packages ----------------------------------------------------------------
library(tidyverse)
library(readxl)
library(zoo)
library("ggsci")
library("ggthemes")
# run files ---------------------------------------------------------------
source('./code/1_anfavea_registration_yearly_files.R')
source('./code/2_anfavea_registration.R')
source('./code/3_compute_fleet.R')
source('./code/4_projection_inertial.R')
source('./code/5_projection_optimist.R')
source('./code/6_projection_biofuels.R')
source('./code/7_ancillary_graphs.R')
# finish ------------------------------------------------------------------
rm(list = ls())
gc()
library(tidyverse)
library(readxl)
library(zoo)
library("ggsci")
library("ggthemes")
library(rgee)
ee_Initialize()
ee_install_upgrade()
library(tidyverse)
library(readxl)
library(zoo)
library("ggsci")
library("ggthemes")
library(rgee)
ee_Initialize()
ee_Initialize()
install.packages(c("remotes", "googledrive"))
remotes::install_github("r-spatial/rgee")
library(rgee)
# Get the username
HOME <- Sys.getenv("HOME")
# 1. Install miniconda
reticulate::install_miniconda()
# 2. Install Google Cloud SDK
system("curl -sSL https://sdk.cloud.google.com | bash")
# 3 Set global parameters
Sys.setenv("RETICULATE_PYTHON" = sprintf("%s/.local/share/r-miniconda/bin/python3", HOME))
Sys.setenv("EARTHENGINE_GCLOUD" = sprintf("%s/google-cloud-sdk/bin/", HOME))
# 4 Install rgee Python dependencies
ee_install()
rm(rs = list())
rm(list = rs())
rm(list = ls())
quit()
# ------------------------------------------------------------------------
# Master file: Projects BEV fleet (2050)
#
# Julio Barros
# 25/07/2023
# ------------------------------------------------------------------------
# packages ----------------------------------------------------------------
library(tidyverse)
library(readxl)
library(zoo)
library("ggsci")
library("ggthemes")
# run files ---------------------------------------------------------------
source('./code/1_anfavea_registration_yearly_files.R')
source('./code/2_anfavea_registration.R')
source('./code/3_compute_fleet.R')
source('./code/4_projection_inertial.R')
source('./code/5_projection_optimist.R')
source('./code/6_projection_biofuels.R')
source('./code/7_ancillary_graphs.R')
# finish ------------------------------------------------------------------
rm(list = ls())
gc()