-
Notifications
You must be signed in to change notification settings - Fork 0
/
crop rotation decision (3 scenarios).R
225 lines (147 loc) · 9.47 KB
/
crop rotation decision (3 scenarios).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
library(tidyverse)
library(decisionSupport)
library(ggplot2)
read.csv("new_variable_estimates.csv", sep=";")
crop_rotation_decision <- function(){
# Estimate the income of rice in a normal season
rice_income <- vv(rice_yield * rice_price, n=n_year, var_CV=100)
# Estimate the income of soybean in a normal season
soybean_income <- vv(soybean_yield * soybean_price, n=n_year, var_CV=100)
# Estimate the income of chili in a normal season
chili_income <- vv(chili_yield * chili_price, n=n_year, var_CV=100)
#Estimate the cost of rice farm in a normal season
rice_cost_precal <- sum(rice_land_rental_cost, rice_seeds_cost, rice_fertilizer_cost,
rice_pesticide_cost, rice_machinery_cost, rice_harvesting_cost)
rice_cost <- vv(rice_cost_precal, n=n_year, var_CV=100)
#Estimate the cost of soybean farm in a normal season
soybean_cost_precal <- sum(soybean_land_rental_cost, soybean_seeds_cost, soybean_fertilizer_cost,
soybean_pesticide_cost, soybean_machinery_cost, soybean_harvesting_cost)
soybean_cost <- vv(soybean_cost_precal, n=n_year, var_CV=100)
#Estimate the cost in a normal season
chili_cost_precal <- sum(chili_land_rental_cost, chili_seeds_cost, chili_fertilizer_cost,
chili_pesticide_cost, chili_machinery_cost, chili_harvesting_cost)
chili_cost <- vv(chili_cost_precal, n=n_year, var_CV=100)
# Estimate the profit
rice_profit <- vv(rice_income - rice_cost, n=n_year, var_CV=100)
soybean_profit <- vv(soybean_income - soybean_cost, n=n_year, var_CV=100)
chili_profit <- vv(chili_income - chili_cost, n=n_year, var_CV=100)
# Final result
#assuming rice cultivation is 3 times per year
rice_cultivation_result = vv(rice_profit*3, n=n_year, var_CV=100)
#crop rotation decision scenario
#if crop rotation of 3 crops is done in one year
crop_rotation_result = vv(rice_profit + soybean_profit + chili_profit, n=n_year, var_CV=100)
#if crop rotation of rice and soybean is done in one year (rice-soybean-rice)
rice_soybean_result = vv((rice_profit*2) + soybean_profit, n=n_year, var_CV=100)
#if crop rotation of rice and chili is done in one year (rice-chili)
rice_chili_result = vv(rice_profit + chili_profit, n=n_year, var_CV=100)
# NPV
NPV_rice <- discount(rice_cultivation_result, discount_rate, calculate_NPV = TRUE)
NPV_crop_rotation <- discount(crop_rotation_result, discount_rate, calculate_NPV = TRUE)
NPV_rice_soybean <- discount(rice_soybean_result, discount_rate, calculate_NPV = TRUE)
NPV_rice_chili <- discount(rice_chili_result, discount_rate, calculate_NPV = TRUE)
# Cashflow
cashflow_crop_rotation <- crop_rotation_result - rice_cultivation_result
cashflow_rice_soybean <- rice_soybean_result - rice_cultivation_result
cashflow_rice_chili <- rice_chili_result - rice_cultivation_result
# Generate the list of outputs from the Monte Carlo simulation
return(list(Rice_NPV = NPV_rice,
crop_rotation_NPV = NPV_crop_rotation,
rice_soybean_NPV = NPV_rice_soybean,
rice_chili_NPV= NPV_rice_chili,
NPV_decision_crop_rotation = NPV_crop_rotation - NPV_rice,
NPV_decision_rice_soybean = NPV_rice_soybean - NPV_rice,
NPV_decision_rice_chili = NPV_rice_chili - NPV_rice,
cashflow_crop_rotation = cashflow_crop_rotation,
cashflow_rice_soybean = cashflow_rice_soybean,
cashflow_rice_chili = cashflow_rice_chili
))
}
make_variables<-function(est,n=1)
{ x<-random(rho=est, n=n)
for(i in colnames(x)) assign(i, as.numeric(x[1,i]),envir=.GlobalEnv)}
make_variables(estimate_read_csv("new_variable_estimates.csv"))
# Run the Monte Carlo simulation using the model function
input_estimates <- read.csv("new_variable_estimates.csv", sep=";")
crop_rotation_mc_simulation <- mcSimulation(estimate = as.estimate(input_estimates),
model_function = crop_rotation_decision,
numberOfModelRuns = 1000,
functionSyntax = "plainNames")
# plot NPV distribution analysis
#if rice with soybean and chili (rice-soybean-chili)
decisionSupport::plot_distributions(mcSimulation_object = crop_rotation_mc_simulation,
vars = c("crop_rotation_NPV", "Rice_NPV"),
method = 'smooth_simple_overlay')
decisionSupport::plot_distributions(mcSimulation_object = crop_rotation_mc_simulation,
vars = c("crop_rotation_NPV", "Rice_NPV"),
method = 'boxplot')
decisionSupport::plot_distributions(mcSimulation_object = crop_rotation_mc_simulation,
vars = c("NPV_decision_crop_rotation"),
method = 'boxplot_density')
#if rice with soybean (rice-soybean-rice)
decisionSupport::plot_distributions(mcSimulation_object = crop_rotation_mc_simulation,
vars = c("rice_soybean_NPV","Rice_NPV"),
method = 'smooth_simple_overlay')
decisionSupport::plot_distributions(mcSimulation_object = crop_rotation_mc_simulation,
vars = c("rice_soybean_NPV","Rice_NPV"),
method = 'boxplot')
decisionSupport::plot_distributions(mcSimulation_object = crop_rotation_mc_simulation,
vars = c("NPV_decision_rice_soybean"),
method = 'boxplot_density')
#if rice with chili (rice-chili)
decisionSupport::plot_distributions(mcSimulation_object = crop_rotation_mc_simulation,
vars = c("rice_chili_NPV","Rice_NPV"),
method = 'smooth_simple_overlay')
decisionSupport::plot_distributions(mcSimulation_object = crop_rotation_mc_simulation,
vars = c("rice_chili_NPV","Rice_NPV"),
method = 'boxplot')
decisionSupport::plot_distributions(mcSimulation_object = crop_rotation_mc_simulation,
vars = c("NPV_decision_rice_chili"),
method = 'boxplot_density')
# cashflow analysis
#with crop rotation of 3 crops
plot_cashflow(mcSimulation_object = crop_rotation_mc_simulation, cashflow_var_name = "cashflow_crop_rotation")
#with crop rotation of rice and soybean (rice-soybean-rice)
plot_cashflow(mcSimulation_object = crop_rotation_mc_simulation, cashflow_var_name = "cashflow_rice_soybean")
#with crop rotation of rice and chili (rice-chili)
plot_cashflow(mcSimulation_object = crop_rotation_mc_simulation, cashflow_var_name = "cashflow_rice_chili")
# VoI analysis
mcSimulation_table <- data.frame(crop_rotation_mc_simulation$x, crop_rotation_mc_simulation$y[1:7])
evpi_crop_rotation <- multi_EVPI(mc = mcSimulation_table, first_out_var = "crop_rotation_NPV")
plot_evpi(evpi_crop_rotation, decision_vars = "NPV_decision_crop_rotation")
evpi_rice_soybean <- multi_EVPI(mc = mcSimulation_table, first_out_var = "rice_soybean_NPV")
plot_evpi(evpi_rice_soybean, decision_vars = "NPV_decision_rice_soybean")
evpi_rice_chili <- multi_EVPI(mc = mcSimulation_table, first_out_var = "rice_chili_NPV")
plot_evpi(evpi_rice_chili, decision_vars = "NPV_decision_rice_chili")
# Projection to Latent Structures (PLS) analysis
#with crop rotation of rice, soybean, and chili
pls_result_crop_rotation <- plsr.mcSimulation(object = crop_rotation_mc_simulation,
resultName = names(crop_rotation_mc_simulation$y)[5], ncomp = 1)
plot_pls(pls_result_crop_rotation, threshold = 0)
#with crop rotation of rice and soybean (rice-soybean-rice)
pls_result_rice_soybean <- plsr.mcSimulation(object = crop_rotation_mc_simulation,
resultName = names(crop_rotation_mc_simulation$y)[6], ncomp = 1)
plot_pls(pls_result_rice_soybean, threshold = 0)
#with crop rotation of rice and chili (rice-chili)
pls_result_rice_chili <- plsr.mcSimulation(object = crop_rotation_mc_simulation,
resultName = names(crop_rotation_mc_simulation$y)[7], ncomp = 1)
plot_pls(pls_result_rice_chili, threshold = 0)
# the plots
#with crop rotation of 3 crops
compound_figure(mcSimulation_object = crop_rotation_mc_simulation,
input_table = input_estimates, plsrResults = pls_result_crop_rotation,
EVPIresults = evpi_crop_rotation, decision_var_name = "NPV_decision_crop_rotation",
cashflow_var_name = "cashflow_crop_rotation",
base_size = 7)
#with crop rotation of rice and soybean (rice-soybean-rice)
compound_figure(mcSimulation_object = crop_rotation_mc_simulation,
input_table = input_estimates, plsrResults = pls_result_rice_soybean,
EVPIresults = evpi_rice_soybean, decision_var_name = "NPV_decision_rice_soybean",
cashflow_var_name = "cashflow_rice_soybean",
base_size = 7)
#with crop rotation of rice and chili (rice-chili)
compound_figure(mcSimulation_object = crop_rotation_mc_simulation,
input_table = input_estimates, plsrResults = pls_result_rice_chili,
EVPIresults = evpi_rice_chili, decision_var_name = "NPV_decision_rice_chili",
cashflow_var_name = "cashflow_rice_chili",
base_size = 7)