-
Notifications
You must be signed in to change notification settings - Fork 1
/
the_final.R
377 lines (226 loc) · 10.9 KB
/
the_final.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
369
370
371
372
373
374
375
376
377
#working directory ----
setwd(getwd())
#used packages ----
library(ggplot2)
library(readr)
library(decisionSupport)
library(vctrs)
library(tidyverse)
library(dplyr)
# loading area dataframe ----
input_estimates_project <- read_delim("real_input_estimates_project.csv", delim = ";", escape_double = FALSE, trim_ws = TRUE)
climate_cka <- read.csv("weather_cka.csv")
# Build function ----
agrivp_vs_agroforestry_function_two <- function(x, varnames) {
# Agroforestry = af ----
# Photovoltaic = pv ----
# ex-ante risks: impact the implementation of interventions ----
intervention_No_Pop_backup_af <- chance_event(chance = 0.01, 1, 0, n = 1)
intervention_No_Pop_backup_pv <- chance_event(chance = 0.1, 1, 0, n = 1)
Risk_low_quality_panels <- chance_event(chance = 0.15, 1, 0, n = 1)
# baseline mono culture benefits ----
Yield_crop = vv(var_mean = crop_Yield, var_CV, n_years) *
vv(var_mean = crop_price, var_CV, n_years)
af_area <- vv(var_mean = area_af, var_CV, n_years) #changing all area vars into these two
pv_area <- vv(var_mean = area_pv, var_CV, n_years) #baseline vv() based on pure af and pv area
field_preparation_monoculture <- field_preparation_mono
No_Intervention <- Yield_crop * crop_area - field_preparation_monoculture
# Precalc Yield wheat in intercropping systems
af_wheat <-
(crop_area -
af_area) *
Yield_crop -
field_preparation_monoculture
pv_wheat <-
(crop_area -
pv_area) *
Yield_crop -
field_preparation_monoculture
# Intervention cases ----
# Intervention agroforestry
for (decision_af in c(FALSE, TRUE)) {
if (decision_af) {
intervention_af_gain <- TRUE
intervention_af_cost <- TRUE
intervention_af_risks <- TRUE
}
else {
intervention_af_gain <- FALSE
intervention_af_cost <- FALSE
intervention_af_risks <- FALSE
}
# Costs agroforestry
if (intervention_af_cost) {
fixed_af_costs <- #fix cost variable gedebuggt
field_preparation_af +
planting_density_af *
price_cutting_af +
planting_density_af *
planting_cost_af +
harvesting_cost_af
}
else {
fixed_af_costs <- 0 # <--- damit es gleich is mit dem
}
# Gain agroforestry
if (intervention_af_gain) {
gain_af =
af_area *
vv(var_mean = yield_wood_af, var_CV, n_years) *
vv(var_mean = price_wood_af, var_CV, n_years) + #hier überall area_af mit af_area ausgetauscht
af_area *
subsidies_af +
ecosystem_service_af *
af_area
if (intervention_No_Pop_backup_af) {
af_area*0.1
}
}
else {
gain_af <- 0 #hier gain_af und oben precalc_af[...] ausgetauscht
}
# Benefits af_intercropping
af_benefits <- as.numeric(gain_af) + af_wheat #hier die Kosten gedoppeltgemoppelt gewesen
}
# Intervention Photovoltaic
for (decision_pv in c(FALSE, TRUE)) {
if (decision_pv) {
intervention_pv_gain <- TRUE
intervention_pv_costs <- TRUE
intervention_pv_risks <- TRUE
}
else {
intervention_pv_gain <- FALSE
intervention_pv_costs <- FALSE
intervention_pv_risks <- FALSE
}
# Costs pv
if (intervention_pv_costs) {
fixed_pv_costs <-
annual_cost_pv +
investment_cost_pv
}
if (intervention_No_Pop_backup_pv){
pv_area*0.1
}
else {
fixed_pv_costs <- 0
}
# Gain pv
if (intervention_pv_gain) {
Anual_average_solar_radiaton <- climate_cka$`mean_Globalstrahlung..Wh.m².`*365 #adjust to colname to kWh
panel_area <- vv(var_mean = area_pv, var_CV, n_years)/10000 # ha transformation in m^2
power_density <- vv(var_mean = power_density, var_CV, n_years)
solar_panel_efficiency <- vv(var_mean = solar_panel_efficiency, var_CV, n_years)
gain_pv = (Anual_average_solar_radiaton*panel_area*power_density*solar_panel_efficiency*energy_market_price)*10000 # qm in ha und
#Performance_ratio_panels als leerer Wert hat gebuggt => ausgetauscht mit 'solar_panel_efficiency'
if (Risk_low_quality_panels){
solar_panel_efficiency*0.1
}
}
else {
gain_pv <- 0
}
# Benefits pv_intercropping
pv_benefits <- as.numeric(gain_pv) + pv_wheat
}
# Net present value (NPV) calculation
if (decision_af) {
net_benefits_af <- af_benefits - fixed_af_costs
result_af_intervention <- net_benefits_af
result_af_n_intervention <- No_Intervention
}
if (decision_pv) {
net_benefits_pv <- pv_benefits - fixed_pv_costs
result_pv_intervention <- net_benefits_pv
result_pv_n_intervention <- No_Intervention
}
NPV_af <- discount(result_af_intervention, discount_rate, calculate_NPV = TRUE)
NPV_pv <- discount(result_pv_intervention, discount_rate, calculate_NPV = TRUE)
NPV_no <- discount(No_Intervention, discount_rate, calculate_NPV = TRUE)
return(list(
Gain_photovoltaic = NPV_pv,
Gain_agroforestry = NPV_af,
Gain_Crop_monoculture = NPV_no,
Cashflow_decison_do_pv = result_pv_intervention, #- result_pv_n_intervention,
Cashflow_decison_do_af = result_af_intervention, #- result_af_n_intervention,
Cashflow_single_mono = No_Intervention #- (result_af_intervention + result_pv_intervention)
))
} # End function
# Monte simulation ----
example_mc_simulation <- mcSimulation(estimate = as.estimate(input_estimates_project),
model_function = agrivp_vs_agroforestry_function_two,
numberOfModelRuns =30002,
functionSyntax = "plainNames")
# Monte plots ----
# All interventions
plot_distributions(mcSimulation_object = example_mc_simulation,
vars = c("Gain_photovoltaic", "Gain_agroforestry","Gain_Crop_monoculture"),
method = 'smooth_simple_overlay',
old_names = c("Gain_photovoltaic", "Gain_agroforestry","Gain_Crop_monoculture"),
new_names = c("Photovoltaic", "Agroforestry","Crop rotation"),
colors = c("green", "orange", "blue2"),
base_size = 25)+
theme(legend.position = c(0.3,0.8))
# Baseline + agroforestry
plot_distributions(mcSimulation_object = example_mc_simulation,
vars = c( "Gain_agroforestry","Gain_Crop_monoculture"),
method = 'smooth_simple_overlay',
old_names = c("Gain_agroforestry","Gain_Crop_monoculture"),
new_names = c("Agroforestry","Crop rotation"),
colors = c("green", "orange"),
base_size = 25)+
theme(legend.position = c(0.6,0.8))
# Baseline + photovoltaic
plot_distributions(mcSimulation_object = example_mc_simulation,
vars = c( "Gain_photovoltaic","Gain_Crop_monoculture"),
method = 'smooth_simple_overlay',
old_names = c("Gain_photovoltaic","Gain_Crop_monoculture"),
new_names = c("Photovoltaic","Crop rotation"),
colors = c("orange", "blue2"),
base_size = 25)+
theme(legend.position = c(0.6,0.8))
# Comparison both interventions
decisionSupport::plot_distributions(mcSimulation_object = example_mc_simulation,
vars = c("Gain_photovoltaic", "Gain_agroforestry"),
method = 'boxplot_density',
old_names = c("Gain_photovoltaic","Gain_agroforestry"),
new_names = c("Photovoltaic","Agroforestry"),
colors = c ("blue2", "green"),
base_size = 25)
# Boxplot density Agroforestry
decisionSupport::plot_distributions(mcSimulation_object = example_mc_simulation,
vars = "Gain_agroforestry",
method = 'boxplot_density',
old_names = "Gain_agroforestry",
new_names = "Agroforestry",
colors = "green",
base_size = 25)
decisionSupport::plot_distributions(mcSimulation_object = example_mc_simulation,
vars = "Gain_photovoltaic",
method = 'boxplot_density',
old_names = "Gain_photovoltaic",
new_names = "Photovoltaic",
colors = "blue2",
base_size = 25)
#PLS Analyse
pls_result <- plsr.mcSimulation(object = example_mc_simulation,
resultName = names(example_mc_simulation$y)[4], ncomp = 1)
plot_pls(pls_result, input_table = input_estimates_project, threshold = 0.1, cut_off_line = 1, base_size = 25)
#Value of Information (VoI) analysis ####
mcSimulation_table <- data.frame(example_mc_simulation$x, example_mc_simulation$y[1:3])
mcSimulation_table
#EVPI ####
evpi1 <- multi_EVPI(mc = mcSimulation_table, first_out_var = "Gain_agroforestry")
evpi2 <- multi_EVPI(mc = mcSimulation_table, first_out_var = "Gain_photovoltaic")
evpi3 <- multi_EVPI(mc = mcSimulation_table, first_out_var = "Gain_Crop_monoculture")
plot_evpi(evpi1, decision_vars = "Gain_agroforestry")
plot_evpi(evpi2, decision_vars = "Gain_photovoltaic")
plot_evpi(evpi3, decision_vars = "Gain_Crop_monoculture")
#Cashflow ####
cash <- plot_cashflow(mcSimulation_object = example_mc_simulation,
cashflow_var_name = c("Cashflow_decison_do_af", "Cashflow_decison_do_pv", "Cashflow_single_mono"),
facet_labels = c("Cashflow Agroforestry", "Cashflow Agrivoltaics", "Cashflow Crop Rotation"),
base_size= 25)
cash + scale_y_continuous(breaks = c(30000,10000,3000,0,-10000,-20000,-30000,-35000))
#End