-
Notifications
You must be signed in to change notification settings - Fork 0
/
05b_Bootstrap_main.R
1575 lines (1287 loc) · 86.6 KB
/
05b_Bootstrap_main.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#-----------------------------------------------------#
# #
# This program estimates the main model with boot- #
# strapped standard errors. #
# #
# Note: The bootstrap program (Section 1: Bootstrap #
# program) cannot be run because farm-level data are #
# confidential. The results of the bootstrap are #
# stored in lines 916, 931, and 946, allowing to #
# create the tables in Section 2 (Presentation of #
# results). #
# #
#-----------------------------------------------------#
# load packages
library(dplyr)
library(writexl)
library(ggplot2)
library(ggpattern)
library(systemfit)
library(doFuture)
library(doRNG)
# load data
load("rOutput/farm_ready.Rda")
# keep only variables needed
df_farm <- df_farm %>% select(key, year, nuts2,
iCereals, iProtein, iOilseed, iRoots, iCorn,
np_cereals, np_protein, np_oilseed, np_roots, np_corn, nw_fert,
k_land, k_labor, k_capital, trend, trend2,
lsh_cereals, lsh_protein, lsh_oilseed, lsh_roots,
gdd_1to3, prec_1to3, gddHigh_1to3, dd_1to3,
gdd_4to10, prec_4to10, gddHigh_4to10, dd_4to10,
np_cereals_fm, np_protein_fm, np_oilseed_fm, np_roots_fm, np_corn_fm, nw_fert_fm,
k_land_fm, k_labor_fm, k_capital_fm, trend_fm, trend2_fm,
lsh_cereals_fm, lsh_protein_fm, lsh_oilseed_fm, lsh_roots_fm,
gdd_1to3_fm, prec_1to3_fm, gddHigh_1to3_fm, dd_1to3_fm,
gdd_4to10_fm, prec_4to10_fm, gddHigh_4to10_fm, dd_4to10_fm,
qq_cereals, qq_protein, qq_oilseed, qq_roots, qq_corn, nx_fert, x_fert, x_otherinp,
gdd_obs, prec_obs, gddHigh_obs, dd_obs,
gdd_obs_1to3, prec_obs_1to3, gddHigh_obs_1to3, dd_obs_1to3,
gdd_obs_4to10, prec_obs_4to10, gddHigh_obs_4to10, dd_obs_4to10,
gdd_obs_fm, prec_obs_fm, gddHigh_obs_fm, dd_obs_fm,
gdd_obs_1to3_fm, prec_obs_1to3_fm, gddHigh_obs_1to3_fm, dd_obs_1to3_fm,
gdd_obs_4to10_fm, prec_obs_4to10_fm, gddHigh_obs_4to10_fm, dd_obs_4to10_fm)
#----------------------------#
#### 1) Bootstrap program ####
#----------------------------#
my.boot <- function(data, nrep, cluster, nCores, actual) {
# Set up the bootstrap
B <- nrep #Number of draws
registerDoFuture() # Initialize parallel computing
plan(multisession, workers = nCores) # Define parallel computing plan with number of cores
res <- foreach(1:B, .combine = rbind) %dorng% {
if (actual==FALSE) {
cluster_id <- unique(cluster)
sb_ID <- sample(cluster_id, replace = TRUE) # For clustering at farm-level, select "ID_farm". Otherwise, select "ID_nuts2"
data_boot <- list()
for (j in 1:length(sb_ID)) {
data_boot[[j]] <- df_farm[which(cluster == sb_ID[j]), ]
}
data_boot <- do.call(rbind, data_boot)
} else if (actual==TRUE) {
data_boot <- df_farm
}
tryCatch( { # in case there is an optimization error in one of the draws
# -------------------- #
# START OF ESTIMATIONS #
# -------------------- #
library(dplyr)
#calculate sample mean for elasticity evaluation
dat_sm <- dplyr::summarise_all(data_boot, mean)
#Define probit regressions
cereal.eq <- iCereals ~ np_cereals + np_protein + np_oilseed + np_roots + np_corn + nw_fert +
k_land + k_labor + k_capital + trend + trend2 +
lsh_cereals + lsh_protein + lsh_oilseed + lsh_roots +
gdd_1to3 + prec_1to3 + gddHigh_1to3 + dd_1to3 +
gdd_4to10 + prec_4to10 + gddHigh_4to10 + dd_4to10 +
np_cereals_fm + np_protein_fm + np_oilseed_fm + np_roots_fm + np_corn_fm + nw_fert_fm +
k_land_fm + k_labor_fm + k_capital_fm + trend_fm + trend2_fm +
lsh_cereals_fm + lsh_protein_fm + lsh_oilseed_fm + lsh_roots_fm +
gdd_1to3_fm + prec_1to3_fm + gddHigh_1to3_fm + dd_1to3_fm +
gdd_4to10_fm + prec_4to10_fm + gddHigh_4to10_fm + dd_4to10_fm
protein.eq <- iProtein ~ np_cereals + np_protein + np_oilseed + np_roots + np_corn + nw_fert +
k_land + k_labor + k_capital + trend + trend2 +
lsh_cereals + lsh_protein + lsh_oilseed + lsh_roots +
gdd_1to3 + prec_1to3 + gddHigh_1to3 + dd_1to3 +
gdd_4to10 + prec_4to10 + gddHigh_4to10 + dd_4to10 +
np_cereals_fm + np_protein_fm + np_oilseed_fm + np_roots_fm + np_corn_fm + nw_fert_fm +
k_land_fm + k_labor_fm + k_capital_fm + trend_fm + trend2_fm +
lsh_cereals_fm + lsh_protein_fm + lsh_oilseed_fm + lsh_roots_fm +
gdd_1to3_fm + prec_1to3_fm + gddHigh_1to3_fm + dd_1to3_fm +
gdd_4to10_fm + prec_4to10_fm + gddHigh_4to10_fm + dd_4to10_fm
oilseed.eq <- iOilseed ~ np_cereals + np_protein + np_oilseed + np_roots + np_corn + nw_fert +
k_land + k_labor + k_capital + trend + trend2 +
lsh_cereals + lsh_protein + lsh_oilseed + lsh_roots +
gdd_1to3 + prec_1to3 + gddHigh_1to3 + dd_1to3 +
gdd_4to10 + prec_4to10 + gddHigh_4to10 + dd_4to10 +
np_cereals_fm + np_protein_fm + np_oilseed_fm + np_roots_fm + np_corn_fm + nw_fert_fm +
k_land_fm + k_labor_fm + k_capital_fm + trend_fm + trend2_fm +
lsh_cereals_fm + lsh_protein_fm + lsh_oilseed_fm + lsh_roots_fm +
gdd_1to3_fm + prec_1to3_fm + gddHigh_1to3_fm + dd_1to3_fm +
gdd_4to10_fm + prec_4to10_fm + gddHigh_4to10_fm + dd_4to10_fm
roots.eq <- iRoots ~ np_cereals + np_protein + np_oilseed + np_roots + np_corn + nw_fert +
k_land + k_labor + k_capital + trend + trend2 +
lsh_cereals + lsh_protein + lsh_oilseed + lsh_roots +
gdd_1to3 + prec_1to3 + gddHigh_1to3 + dd_1to3 +
gdd_4to10 + prec_4to10 + gddHigh_4to10 + dd_4to10 +
np_cereals_fm + np_protein_fm + np_oilseed_fm + np_roots_fm + np_corn_fm + nw_fert_fm +
k_land_fm + k_labor_fm + k_capital_fm + trend_fm + trend2_fm +
lsh_cereals_fm + lsh_protein_fm + lsh_oilseed_fm + lsh_roots_fm +
gdd_1to3_fm + prec_1to3_fm + gddHigh_1to3_fm + dd_1to3_fm +
gdd_4to10_fm + prec_4to10_fm + gddHigh_4to10_fm + dd_4to10_fm
corn.eq <- iCorn ~ np_cereals + np_protein + np_oilseed + np_roots + np_corn + nw_fert +
k_land + k_labor + k_capital + trend + trend2 +
lsh_cereals + lsh_protein + lsh_oilseed + lsh_roots +
gdd_1to3 + prec_1to3 + gddHigh_1to3 + dd_1to3 +
gdd_4to10 + prec_4to10 + gddHigh_4to10 + dd_4to10 +
np_cereals_fm + np_protein_fm + np_oilseed_fm + np_roots_fm + np_corn_fm + nw_fert_fm +
k_land_fm + k_labor_fm + k_capital_fm + trend_fm + trend2_fm +
lsh_cereals_fm + lsh_protein_fm + lsh_oilseed_fm + lsh_roots_fm +
gdd_1to3_fm + prec_1to3_fm + gddHigh_1to3_fm + dd_1to3_fm +
gdd_4to10_fm + prec_4to10_fm + gddHigh_4to10_fm + dd_4to10_fm
#Define structural equations
eqQQcereals <- qq_cereals ~ PHI_cereals + I(PHI_cereals*np_cereals) + I(PHI_cereals*np_protein) + I(PHI_cereals*np_oilseed) + I(PHI_cereals*np_roots) + I(PHI_cereals*np_corn) + I(PHI_cereals*nw_fert) +
I(PHI_cereals*k_land) + I(PHI_cereals*k_labor) + I(PHI_cereals*k_capital) + I(PHI_cereals*trend) + I(PHI_cereals*trend2) +
I(PHI_cereals*gdd_obs) + I(PHI_cereals*prec_obs) + I(PHI_cereals*gddHigh_obs) + I(PHI_cereals*dd_obs) +
I(PHI_cereals*gdd_1to3) + I(PHI_cereals*prec_1to3) + I(PHI_cereals*gddHigh_1to3) + I(PHI_cereals*dd_1to3) +
I(PHI_cereals*gdd_4to10) + I(PHI_cereals*prec_4to10) + I(PHI_cereals*gddHigh_4to10) + I(PHI_cereals*dd_4to10) +
I(PHI_cereals*gdd_obs_1to3) + I(PHI_cereals*prec_obs_1to3) + I(PHI_cereals*gddHigh_obs_1to3) + I(PHI_cereals*dd_obs_1to3) +
I(PHI_cereals*gdd_obs_4to10) + I(PHI_cereals*prec_obs_4to10) + I(PHI_cereals*gddHigh_obs_4to10) + I(PHI_cereals*dd_obs_4to10) +
I(PHI_cereals*np_cereals_fm) + I(PHI_cereals*np_protein_fm) + I(PHI_cereals*np_oilseed_fm) + I(PHI_cereals*np_roots_fm) + I(PHI_cereals*np_corn_fm) + I(PHI_cereals*nw_fert_fm) +
I(PHI_cereals*k_land_fm) + I(PHI_cereals*k_labor_fm) + I(PHI_cereals*k_capital_fm) + I(PHI_cereals*trend_fm) + I(PHI_cereals*trend2_fm) +
I(PHI_cereals*gdd_obs_fm) + I(PHI_cereals*prec_obs_fm) + I(PHI_cereals*gddHigh_obs_fm) + I(PHI_cereals*dd_obs_fm) +
I(PHI_cereals*gdd_1to3_fm) + I(PHI_cereals*prec_1to3_fm) + I(PHI_cereals*gddHigh_1to3_fm) + I(PHI_cereals*dd_1to3_fm) +
I(PHI_cereals*gdd_4to10_fm) + I(PHI_cereals*prec_4to10_fm) + I(PHI_cereals*gddHigh_4to10_fm) + I(PHI_cereals*dd_4to10_fm) +
I(PHI_cereals*gdd_obs_1to3_fm) + I(PHI_cereals*prec_obs_1to3_fm) + I(PHI_cereals*gddHigh_obs_1to3_fm) + I(PHI_cereals*dd_obs_1to3_fm) +
I(PHI_cereals*gdd_obs_4to10_fm) + I(PHI_cereals*prec_obs_4to10_fm) + I(PHI_cereals*gddHigh_obs_4to10_fm) + I(PHI_cereals*dd_obs_4to10_fm) +
phi_cereals -1
eqQQprotein <- qq_protein ~ PHI_protein + I(PHI_protein*np_cereals) + I(PHI_protein*np_protein) + I(PHI_protein*np_oilseed) + I(PHI_protein*np_roots) + I(PHI_protein*np_corn) + I(PHI_protein*nw_fert) +
I(PHI_protein*k_land) + I(PHI_protein*k_labor) + I(PHI_protein*k_capital) + I(PHI_protein*trend) + I(PHI_protein*trend2) +
I(PHI_protein*gdd_obs) + I(PHI_protein*prec_obs) + I(PHI_protein*gddHigh_obs) + I(PHI_protein*dd_obs) +
I(PHI_protein*gdd_1to3) + I(PHI_protein*prec_1to3) + I(PHI_protein*gddHigh_1to3) + I(PHI_protein*dd_1to3) +
I(PHI_protein*gdd_4to10) + I(PHI_protein*prec_4to10) + I(PHI_protein*gddHigh_4to10) + I(PHI_protein*dd_4to10) +
I(PHI_protein*gdd_obs_1to3) + I(PHI_protein*prec_obs_1to3) + I(PHI_protein*gddHigh_obs_1to3) + I(PHI_protein*dd_obs_1to3) +
I(PHI_protein*gdd_obs_4to10) + I(PHI_protein*prec_obs_4to10) + I(PHI_protein*gddHigh_obs_4to10) + I(PHI_protein*dd_obs_4to10) +
I(PHI_protein*np_cereals_fm) + I(PHI_protein*np_protein_fm) + I(PHI_protein*np_oilseed_fm) + I(PHI_protein*np_roots_fm) + I(PHI_protein*np_corn_fm) + I(PHI_protein*nw_fert_fm) +
I(PHI_protein*k_land_fm) + I(PHI_protein*k_labor_fm) + I(PHI_protein*k_capital_fm) + I(PHI_protein*trend_fm) + I(PHI_protein*trend2_fm) +
I(PHI_protein*gdd_obs_fm) + I(PHI_protein*prec_obs_fm) + I(PHI_protein*gddHigh_obs_fm) + I(PHI_protein*dd_obs_fm) +
I(PHI_protein*gdd_1to3_fm) + I(PHI_protein*prec_1to3_fm) + I(PHI_protein*gddHigh_1to3_fm) + I(PHI_protein*dd_1to3_fm) +
I(PHI_protein*gdd_4to10_fm) + I(PHI_protein*prec_4to10_fm) + I(PHI_protein*gddHigh_4to10_fm) + I(PHI_protein*dd_4to10_fm) +
I(PHI_protein*gdd_obs_1to3_fm) + I(PHI_protein*prec_obs_1to3_fm) + I(PHI_protein*gddHigh_obs_1to3_fm) + I(PHI_protein*dd_obs_1to3_fm) +
I(PHI_protein*gdd_obs_4to10_fm) + I(PHI_protein*prec_obs_4to10_fm) + I(PHI_protein*gddHigh_obs_4to10_fm) + I(PHI_protein*dd_obs_4to10_fm) +
phi_protein -1
eqQQoilseed <- qq_oilseed ~ PHI_oilseed + I(PHI_oilseed*np_cereals) + I(PHI_oilseed*np_protein) + I(PHI_oilseed*np_oilseed) + I(PHI_oilseed*np_roots) + I(PHI_oilseed*np_corn) + I(PHI_oilseed*nw_fert) +
I(PHI_oilseed*k_land) + I(PHI_oilseed*k_labor) + I(PHI_oilseed*k_capital) + I(PHI_oilseed*trend) + I(PHI_oilseed*trend2) +
I(PHI_oilseed*gdd_obs) + I(PHI_oilseed*prec_obs) + I(PHI_oilseed*gddHigh_obs) + I(PHI_oilseed*dd_obs) +
I(PHI_oilseed*gdd_1to3) + I(PHI_oilseed*prec_1to3) + I(PHI_oilseed*gddHigh_1to3) + I(PHI_oilseed*dd_1to3) +
I(PHI_oilseed*gdd_4to10) + I(PHI_oilseed*prec_4to10) + I(PHI_oilseed*gddHigh_4to10) + I(PHI_oilseed*dd_4to10) +
I(PHI_oilseed*gdd_obs_1to3) + I(PHI_oilseed*prec_obs_1to3) + I(PHI_oilseed*gddHigh_obs_1to3) + I(PHI_oilseed*dd_obs_1to3) +
I(PHI_oilseed*gdd_obs_4to10) + I(PHI_oilseed*prec_obs_4to10) + I(PHI_oilseed*gddHigh_obs_4to10) + I(PHI_oilseed*dd_obs_4to10) +
I(PHI_oilseed*np_cereals_fm) + I(PHI_oilseed*np_protein_fm) + I(PHI_oilseed*np_oilseed_fm) + I(PHI_oilseed*np_roots_fm) + I(PHI_oilseed*np_corn_fm) + I(PHI_oilseed*nw_fert_fm) +
I(PHI_oilseed*k_land_fm) + I(PHI_oilseed*k_labor_fm) + I(PHI_oilseed*k_capital_fm) + I(PHI_oilseed*trend_fm) + I(PHI_oilseed*trend2_fm) +
I(PHI_oilseed*gdd_obs_fm) + I(PHI_oilseed*prec_obs_fm) + I(PHI_oilseed*gddHigh_obs_fm) + I(PHI_oilseed*dd_obs_fm) +
I(PHI_oilseed*gdd_1to3_fm) + I(PHI_oilseed*prec_1to3_fm) + I(PHI_oilseed*gddHigh_1to3_fm) + I(PHI_oilseed*dd_1to3_fm) +
I(PHI_oilseed*gdd_4to10_fm) + I(PHI_oilseed*prec_4to10_fm) + I(PHI_oilseed*gddHigh_4to10_fm) + I(PHI_oilseed*dd_4to10_fm) +
I(PHI_oilseed*gdd_obs_1to3_fm) + I(PHI_oilseed*prec_obs_1to3_fm) + I(PHI_oilseed*gddHigh_obs_1to3_fm) + I(PHI_oilseed*dd_obs_1to3_fm) +
I(PHI_oilseed*gdd_obs_4to10_fm) + I(PHI_oilseed*prec_obs_4to10_fm) + I(PHI_oilseed*gddHigh_obs_4to10_fm) + I(PHI_oilseed*dd_obs_4to10_fm) +
phi_oilseed -1
eqQQroots <- qq_roots ~ PHI_roots + I(PHI_roots*np_cereals) + I(PHI_roots*np_protein) + I(PHI_roots*np_oilseed) + I(PHI_roots*np_roots) + I(PHI_roots*np_corn) + I(PHI_roots*nw_fert) +
I(PHI_roots*k_land) + I(PHI_roots*k_labor) + I(PHI_roots*k_capital) + I(PHI_roots*trend) + I(PHI_roots*trend2) +
I(PHI_roots*gdd_obs) + I(PHI_roots*prec_obs) + I(PHI_roots*gddHigh_obs) + I(PHI_roots*dd_obs) +
I(PHI_roots*gdd_1to3) + I(PHI_roots*prec_1to3) + I(PHI_roots*gddHigh_1to3) + I(PHI_roots*dd_1to3) +
I(PHI_roots*gdd_4to10) + I(PHI_roots*prec_4to10) + I(PHI_roots*gddHigh_4to10) + I(PHI_roots*dd_4to10) +
I(PHI_roots*gdd_obs_1to3) + I(PHI_roots*prec_obs_1to3) + I(PHI_roots*gddHigh_obs_1to3) + I(PHI_roots*dd_obs_1to3) +
I(PHI_roots*gdd_obs_4to10) + I(PHI_roots*prec_obs_4to10) + I(PHI_roots*gddHigh_obs_4to10) + I(PHI_roots*dd_obs_4to10) +
I(PHI_roots*np_cereals_fm) + I(PHI_roots*np_protein_fm) + I(PHI_roots*np_oilseed_fm) + I(PHI_roots*np_roots_fm) + I(PHI_roots*np_corn_fm) + I(PHI_roots*nw_fert_fm) +
I(PHI_roots*k_land_fm) + I(PHI_roots*k_labor_fm) + I(PHI_roots*k_capital_fm) + I(PHI_roots*trend_fm) + I(PHI_roots*trend2_fm) +
I(PHI_roots*gdd_obs_fm) + I(PHI_roots*prec_obs_fm) + I(PHI_roots*gddHigh_obs_fm) + I(PHI_roots*dd_obs_fm) +
I(PHI_roots*gdd_1to3_fm) + I(PHI_roots*prec_1to3_fm) + I(PHI_roots*gddHigh_1to3_fm) + I(PHI_roots*dd_1to3_fm) +
I(PHI_roots*gdd_4to10_fm) + I(PHI_roots*prec_4to10_fm) + I(PHI_roots*gddHigh_4to10_fm) + I(PHI_roots*dd_4to10_fm) +
I(PHI_roots*gdd_obs_1to3_fm) + I(PHI_roots*prec_obs_1to3_fm) + I(PHI_roots*gddHigh_obs_1to3_fm) + I(PHI_roots*dd_obs_1to3_fm) +
I(PHI_roots*gdd_obs_4to10_fm) + I(PHI_roots*prec_obs_4to10_fm) + I(PHI_roots*gddHigh_obs_4to10_fm) + I(PHI_roots*dd_obs_4to10_fm) +
phi_roots -1
eqQQcorn <- qq_corn ~ PHI_corn + I(PHI_corn*np_cereals) + I(PHI_corn*np_protein) + I(PHI_corn*np_oilseed) + I(PHI_corn*np_roots) + I(PHI_corn*np_corn) + I(PHI_corn*nw_fert) +
I(PHI_corn*k_land) + I(PHI_corn*k_labor) + I(PHI_corn*k_capital) + I(PHI_corn*trend) + I(PHI_corn*trend2) +
I(PHI_corn*gdd_obs) + I(PHI_corn*prec_obs) + I(PHI_corn*gddHigh_obs) + I(PHI_corn*dd_obs) +
I(PHI_corn*gdd_1to3) + I(PHI_corn*prec_1to3) + I(PHI_corn*gddHigh_1to3) + I(PHI_corn*dd_1to3) +
I(PHI_corn*gdd_4to10) + I(PHI_corn*prec_4to10) + I(PHI_corn*gddHigh_4to10) + I(PHI_corn*dd_4to10) +
I(PHI_corn*gdd_obs_1to3) + I(PHI_corn*prec_obs_1to3) + I(PHI_corn*gddHigh_obs_1to3) + I(PHI_corn*dd_obs_1to3) +
I(PHI_corn*gdd_obs_4to10) + I(PHI_corn*prec_obs_4to10) + I(PHI_corn*gddHigh_obs_4to10) + I(PHI_corn*dd_obs_4to10) +
I(PHI_corn*np_cereals_fm) + I(PHI_corn*np_protein_fm) + I(PHI_corn*np_oilseed_fm) + I(PHI_corn*np_roots_fm) + I(PHI_corn*np_corn_fm) + I(PHI_corn*nw_fert_fm) +
I(PHI_corn*k_land_fm) + I(PHI_corn*k_labor_fm) + I(PHI_corn*k_capital_fm) + I(PHI_corn*trend_fm) + I(PHI_corn*trend2_fm) +
I(PHI_corn*gdd_obs_fm) + I(PHI_corn*prec_obs_fm) + I(PHI_corn*gddHigh_obs_fm) + I(PHI_corn*dd_obs_fm) +
I(PHI_corn*gdd_1to3_fm) + I(PHI_corn*prec_1to3_fm) + I(PHI_corn*gddHigh_1to3_fm) + I(PHI_corn*dd_1to3_fm) +
I(PHI_corn*gdd_4to10_fm) + I(PHI_corn*prec_4to10_fm) + I(PHI_corn*gddHigh_4to10_fm) + I(PHI_corn*dd_4to10_fm) +
I(PHI_corn*gdd_obs_1to3_fm) + I(PHI_corn*prec_obs_1to3_fm) + I(PHI_corn*gddHigh_obs_1to3_fm) + I(PHI_corn*dd_obs_1to3_fm) +
I(PHI_corn*gdd_obs_4to10_fm) + I(PHI_corn*prec_obs_4to10_fm) + I(PHI_corn*gddHigh_obs_4to10_fm) + I(PHI_corn*dd_obs_4to10_fm) +
phi_corn -1
eqNXfert <- nx_fert ~ np_cereals + np_protein + np_oilseed + np_roots + np_corn + nw_fert +
k_land + k_labor + k_capital + trend + trend2 +
gdd_obs + prec_obs + gddHigh_obs + dd_obs +
gdd_1to3 + prec_1to3 + gddHigh_1to3 + dd_1to3 +
gdd_4to10 + prec_4to10 + gddHigh_4to10 + dd_4to10 +
gdd_obs_1to3 + prec_obs_1to3 + gddHigh_obs_1to3 + dd_obs_1to3 +
gdd_obs_4to10 + prec_obs_4to10 + gddHigh_obs_4to10 + dd_obs_4to10 +
np_cereals_fm + np_protein_fm + np_oilseed_fm + np_roots_fm + np_corn_fm + nw_fert_fm +
k_land_fm + k_labor_fm + k_capital_fm + trend_fm + trend2_fm +
gdd_obs_fm + prec_obs_fm + gddHigh_obs_fm + dd_obs_fm +
gdd_1to3_fm + prec_1to3_fm + gddHigh_1to3_fm + dd_1to3_fm +
gdd_4to10_fm + prec_4to10_fm + gddHigh_4to10_fm + dd_4to10_fm +
gdd_obs_1to3_fm + prec_obs_1to3_fm + gddHigh_obs_1to3_fm + dd_obs_1to3_fm +
gdd_obs_4to10_fm + prec_obs_4to10_fm + gddHigh_obs_4to10_fm + dd_obs_4to10_fm
# Define system of equations
system <- list( QQcereals = eqQQcereals,
QQprotein = eqQQprotein,
QQoilseed = eqQQoilseed,
QQroots = eqQQroots,
QQcorn = eqQQcorn,
NXfert = eqNXfert)
# Define restrictions
restrict <- c( "QQcereals_I(PHI_cereals * np_protein) - QQprotein_I(PHI_protein * np_cereals) = 0",
"QQcereals_I(PHI_cereals * np_oilseed) - QQoilseed_I(PHI_oilseed * np_cereals) = 0",
"QQcereals_I(PHI_cereals * np_roots) - QQroots_I(PHI_roots * np_cereals) = 0",
"QQcereals_I(PHI_cereals * np_corn) - QQcorn_I(PHI_corn * np_cereals) = 0",
"QQcereals_I(PHI_cereals * nw_fert) - NXfert_np_cereals = 0",
"QQprotein_I(PHI_protein * np_oilseed) - QQoilseed_I(PHI_oilseed * np_protein) = 0",
"QQprotein_I(PHI_protein * np_roots) - QQroots_I(PHI_roots * np_protein) = 0",
"QQprotein_I(PHI_protein * np_corn) - QQcorn_I(PHI_corn * np_protein) = 0",
"QQprotein_I(PHI_protein * nw_fert) - NXfert_np_protein = 0",
"QQoilseed_I(PHI_oilseed * np_roots) - QQroots_I(PHI_roots * np_oilseed) = 0",
"QQoilseed_I(PHI_oilseed * np_corn) - QQcorn_I(PHI_corn * np_oilseed) = 0",
"QQoilseed_I(PHI_oilseed * nw_fert) - NXfert_np_oilseed = 0",
"QQroots_I(PHI_roots * np_corn) - QQcorn_I(PHI_corn * np_roots) = 0",
"QQroots_I(PHI_roots * nw_fert) - NXfert_np_roots = 0",
"QQcorn_I(PHI_corn * nw_fert) - NXfert_np_corn = 0")
#----------------------------------#
#### Step 1: Probit regressions ####
#----------------------------------#
# Obtain data mean for probit-variables (same for all probit regressions)
iCrop <- glm(cereal.eq, family = binomial(link = "probit"),
data = data_boot)
dat_prob <- dplyr::as_tibble(model.matrix(iCrop))
dat_sm_prob <- dat_prob %>%
dplyr::summarise_all(mean)
#set-up the loop over crops
list_crops <- data.frame(crops=c("Cereals","Protein","Oilseed","Roots","Corn"),
probit=c("cereal.eq","protein.eq","oilseed.eq","roots.eq","corn.eq"),
abbrev=c("cer","prot","oil","roots","corn"))
n_crops <- list_crops %>% count() %>% as.numeric() # number of crops
# Create list where the results will be stored
list_probit_act <- list(Cereals=list(linpred_sm="", PHI="", PHI_sm="", phi="", phi_sm="", probit_me=""),
Protein=list(linpred_sm="", PHI="", PHI_sm="", phi="", phi_sm="", probit_me=""),
Oilseed=list(linpred_sm="", PHI="", PHI_sm="", phi="", phi_sm="", probit_me=""),
Roots= list(linpred_sm="", PHI="", PHI_sm="", phi="", phi_sm="", probit_me=""),
Corn=list(linpred_sm="", PHI="", PHI_sm="", phi="", phi_sm="", probit_me=""))
# Loop over all crops and estimate probits
for (i in 1:n_crops) {
# Probit estimation
crop.eq <- get(list_crops[i,"probit"])
iCrop <- glm(crop.eq, family = binomial(link = "probit"),
data = data_boot)
# Linear prediction at the sample mean (=linpred_sm)
list_probit_act[[i]][[1]] <- iCrop_linpred_sm <- crossprod(matrix(iCrop$coefficients),
t(dat_sm_prob))
# PHI at the farm-level
list_probit_act[[i]][[2]] <- pnorm(predict(iCrop)) #note: predict(iCrop) is iCrop_linpred
# PHI at the sample mean
list_probit_act[[i]][[3]] <- pnorm(iCrop_linpred_sm)
# phi at the farm-level
list_probit_act[[i]][[4]] <- dnorm(predict(iCrop)) #note: predict(iCrop) is iCrop_linpred
# phi at the sample mean
list_probit_act[[i]][[5]] <- dnorm(iCrop_linpred_sm)
# Probit coefficients
if (i == 1) {
coef_iCereals <- iCrop$coefficients
} else if (i == 2) {
coef_iProtein <- iCrop$coefficients
} else if (i == 3) {
coef_iOilseed <- iCrop$coefficients
} else if (i == 4) {
coef_iRoots <- iCrop$coefficients
} else if (i == 5) {
coef_iCorn <- iCrop$coefficients
}
# Marginal effects (iCrop_me)
list_probit_act[[i]][[6]] <- dnorm(iCrop_linpred_sm)*coef(iCrop) #note: dnorm(iCrop_linpred_sm) is phi_crop_sm
}
# Restore the results estimated in the loop
# Probit linear prediction at the sample mean (for elasticity evaluation)
iCereals_linpred_sm <- as.numeric(do.call(c, list_probit_act[[1]][1]))
iProtein_linpred_sm <- as.numeric(do.call(c, list_probit_act[[2]][1]))
iOilseed_linpred_sm <- as.numeric(do.call(c, list_probit_act[[3]][1]))
iRoots_linpred_sm <- as.numeric(do.call(c, list_probit_act[[4]][1]))
iCorn_linpred_sm <- as.numeric(do.call(c, list_probit_act[[5]][1]))
# PHIs at the farm-level (for estimation of second stage)
data_boot$PHI_cereals <- as.numeric(do.call(c, list_probit_act[[1]][2]))
data_boot$PHI_protein <- as.numeric(do.call(c, list_probit_act[[2]][2]))
data_boot$PHI_oilseed <- as.numeric(do.call(c, list_probit_act[[3]][2]))
data_boot$PHI_roots <- as.numeric(do.call(c, list_probit_act[[4]][2]))
data_boot$PHI_corn <- as.numeric(do.call(c, list_probit_act[[5]][2]))
# PHIs at the sample mean (for elasticity evaluation)
PHI_cereals_sm <- as.numeric(do.call(c, list_probit_act[[1]][3]))
PHI_protein_sm <- as.numeric(do.call(c, list_probit_act[[2]][3]))
PHI_oilseed_sm <- as.numeric(do.call(c, list_probit_act[[3]][3]))
PHI_roots_sm <- as.numeric(do.call(c, list_probit_act[[4]][3]))
PHI_corn_sm <- as.numeric(do.call(c, list_probit_act[[5]][3]))
# phis at the farm-level (for estimation of second stage)
data_boot$phi_cereals <- as.numeric(do.call(c, list_probit_act[[1]][4]))
data_boot$phi_protein <- as.numeric(do.call(c, list_probit_act[[2]][4]))
data_boot$phi_oilseed <- as.numeric(do.call(c, list_probit_act[[3]][4]))
data_boot$phi_roots <- as.numeric(do.call(c, list_probit_act[[4]][4]))
data_boot$phi_corn <- as.numeric(do.call(c, list_probit_act[[5]][4]))
# phis at the sample mean (for elasticity evaluation)
phi_cereals_sm <- as.numeric(do.call(c, list_probit_act[[1]][5]))
phi_protein_sm <- as.numeric(do.call(c, list_probit_act[[2]][5]))
phi_oilseed_sm <- as.numeric(do.call(c, list_probit_act[[3]][5]))
phi_roots_sm <- as.numeric(do.call(c, list_probit_act[[4]][5]))
phi_corn_sm <- as.numeric(do.call(c, list_probit_act[[5]][5]))
# Probit marginal effects (for results presentation)
iCereals_me <- as.numeric(do.call(c, list_probit_act[[1]][6]))
iProtein_me <- as.numeric(do.call(c, list_probit_act[[2]][6]))
iOilseed_me <- as.numeric(do.call(c, list_probit_act[[3]][6]))
iRoots_me <- as.numeric(do.call(c, list_probit_act[[4]][6]))
iCorn_me <- as.numeric(do.call(c, list_probit_act[[5]][6]))
#----------------------------------------#
#### Step 2: Run Structural equations ####
#----------------------------------------#
## Regression with iterated SUR estimation
model_linear <- systemfit::systemfit( formula = system, method = "SUR",
data = data_boot, restrict.matrix = restrict,
maxit = 100 )
coef_linear <- coef(model_linear)
# "x times \beta" (needed for elasticities)
qx_pred <- predict(model_linear)
qq_cereals_pred_b <- mean(qx_pred$QQcereals.pred) - model_linear$coefficients["QQcereals_phi_cereals"]*mean(data_boot$phi_cereals)
qq_cereals_pred <- (qq_cereals_pred_b) / mean(data_boot$PHI_cereals)
qq_protein_pred_b <- mean(qx_pred$QQprotein.pred) - model_linear$coefficients["QQprotein_phi_protein"]*mean(data_boot$phi_protein)
qq_protein_pred <- (qq_protein_pred_b) / mean(data_boot$PHI_protein)
qq_oilseed_pred_b <- mean(qx_pred$QQoilseed.pred) - model_linear$coefficients["QQoilseed_phi_oilseed"]*mean(data_boot$phi_oilseed)
qq_oilseed_pred <- (qq_oilseed_pred_b) / mean(data_boot$PHI_oilseed)
qq_roots_pred_b <- mean(qx_pred$QQroots.pred) - model_linear$coefficients["QQroots_phi_roots"]*mean(data_boot$phi_roots)
qq_roots_pred <- (qq_roots_pred_b) / mean(data_boot$PHI_roots)
qq_corn_pred_b <- mean(qx_pred$QQcorn.pred) - model_linear$coefficients["QQcorn_phi_corn"]*mean(data_boot$phi_corn)
qq_corn_pred <- (qq_corn_pred_b) / mean(data_boot$PHI_corn)
nx_fert_pred <- mean(qx_pred$NXfert.pred)
# -------------------------------- #
#### Compute price elasticities ####
# ---------------------------------#
#set-up a data frame to store the results
elast_prices <- data.frame("Variables"=c("P Cereals", "P Protein", "P Oilseed", "P Roots", "P Corn", "W Fertilizer", "W Others"),
"Q Cereals"=rep(NA,7),
"Q Protein"=rep(NA,7),
"Q Oilseed"=rep(NA,7),
"Q Roots"=rep(NA,7),
"Q Corn"=rep(NA,7),
"X Fertilizer"=rep(NA,7),
"X Others"=rep(NA,7))
# ------------------------------ #
# Price elasticities for outputs #
# ------------------------------ #
#set-up the loop over crops
list_qcrops <- data.frame(crops=c("cereals","protein","oilseed","roots","corn"),
probit=c("iCereals","iProtein","iOilseed","iRoots","iCorn"),
struct=c("QQcereals","QQprotein", "QQoilseed", "QQroots", "QQcorn"))
n_qcrops <- list_qcrops %>% count() %>% as.numeric() # number of crops
list_prices <- data.frame(prices=c("np_cereals", "np_protein", "np_oilseed", "np_roots", "np_corn", "nw_fert"),
endswith=c("np_cereals)", "np_protein)", "np_oilseed)", "np_roots)", "np_corn)", "nw_fert)"))
n_prices <- list_prices %>% count() %>% as.numeric()
# Loop over all crops
for (i in 1:n_qcrops) {
coefs_struct_crop <- dplyr::as_tibble(t(coef_linear)) %>% select(starts_with(list_qcrops[i,"struct"]))
coefs_probit_crop <- dplyr::as_tibble(t(get(paste0("coef_",list_qcrops[i,"probit"]))))
coef_phi_struct <- coefs_struct_crop %>% select(ends_with(paste0("phi_",list_qcrops[i,"crops"]), ignore.case = FALSE)) # phi_cereals etc.
PHI <- get(paste0("PHI_",list_qcrops[i,"crops"],"_sm"))
phi <- get(paste0("phi_",list_qcrops[i,"crops"],"_sm"))
linpred <- get(paste0(list_qcrops[i,"probit"],"_linpred_sm"))
pred <- get(paste0("qq_",list_qcrops[i,"crops"],"_pred"))
mean_quantity <- dat_sm %>% select(paste0("qq_",list_qcrops[i,"crops"]))
for (j in 1:n_prices) {
coef_probit_p <- coefs_probit_crop %>% select(list_prices[j,"prices"])
coef_struct_p <- coefs_struct_crop %>% select(ends_with(list_prices[j,"endswith"]))
mean_price <- dat_sm %>% select(list_prices[j,"prices"])
elast_prices[j,1+i] <- (PHI * coef_struct_p + phi * pred * coef_probit_p -
coef_phi_struct * linpred * coef_probit_p * phi ) * (mean_price / mean_quantity )
}
# Price elasticity for numeraire adds up to 0
elast_prices[j+1,1+i] <- 0 - elast_prices[1,1+i] - elast_prices[2,1+i] - elast_prices[3,1+i] - elast_prices[4,1+i] - elast_prices[5,1+i] - elast_prices[6,1+i]
}
# ----------------------------- #
# Price elasticities for inputs #
# ----------------------------- #
# Fertilizer demand (note: There is no selection equation for fertilizer --> use regular derivative)
coef_fert <- dplyr::as_tibble(t(coef_linear)) %>% select(starts_with("NXfert"))
for (j in 1:n_prices) {
coef_struct_p <- coef_fert %>% select(ends_with(list_prices[j,"prices"]))
mean_price <- dat_sm %>% select(list_prices[j,"prices"])
elast_prices[j,7] <- coef_struct_p * (mean_price/dat_sm$nx_fert)
}
elast_prices[j+1,7] <- 0 - elast_prices[1,7] - elast_prices[2,7] - elast_prices[3,7] - elast_prices[4,7] - elast_prices[5,7] - elast_prices[6,7]
# Other input demand
list_prices <- data.frame(prices=c("np_cereals", "np_protein", "np_oilseed", "np_roots", "np_corn", "nw_fert"),
endswith=c("np_cereals)", "np_protein)", "np_oilseed)", "np_roots)", "np_corn)", "nw_fert)"),
quantities=c("qq_cereals", "qq_protein", "qq_oilseed", "qq_roots", "qq_corn", "x_fert")) # Note: Here I use x_fert instead of nx_fert so that I do not have to add a negative sign to X.Others_W.Fert
n_prices <- list_prices %>% count() %>% as.numeric()
for (j in 1:n_prices) {
elast_sum <- sum(elast_prices[1:n_prices,j+1]) # sum of all elasticities except for w_others
mean_quantity <- dat_sm %>% select(list_prices[j,"quantities"])
mean_price <- dat_sm %>% select(list_prices[j,"prices"])
elast_prices[j,8] <- (mean_quantity * mean_price) / dat_sm$x_otherinp *
(elast_sum)
}
elast_prices[j+1,8] <- 0 - elast_prices[1,8] - elast_prices[2,8] - elast_prices[3,8] - elast_prices[4,8] - elast_prices[5,8] - elast_prices[6,8]
# ------------------------------------- #
# Store price elasticities in bootstrap #
# --------------------------------------#
#q_cer
el_qcer_pcer <- c('el_qcer_pcer' = as.numeric(elast_prices[1,2]))
el_qcer_pprot <- c('el_qcer_pprot' = as.numeric(elast_prices[2,2]))
el_qcer_poil <- c('el_qcer_poil' = as.numeric(elast_prices[3,2]))
el_qcer_proots <- c('el_qcer_proots' = as.numeric(elast_prices[4,2]))
el_qcer_pcorn <- c('el_qcer_pcorn' = as.numeric(elast_prices[5,2]))
el_qcer_wfert <- c('el_qcer_wfert' = as.numeric(elast_prices[6,2]))
el_qcer_wotherinp <- c('el_qcer_wotherinp' = as.numeric(elast_prices[7,2]))
#q_prot
el_qprot_pcer <- c('el_qprot_pcer' = as.numeric(elast_prices[1,3]))
el_qprot_pprot <- c('el_qprot_pprot' = as.numeric(elast_prices[2,3]))
el_qprot_poil <- c('el_qprot_poil' = as.numeric(elast_prices[3,3]))
el_qprot_proots <- c('el_qprot_proots' = as.numeric(elast_prices[4,3]))
el_qprot_pcorn <- c('el_qprot_pcorn' = as.numeric(elast_prices[5,3]))
el_qprot_wfert <- c('el_qprot_wfert' = as.numeric(elast_prices[6,3]))
el_qprot_wotherinp <- c('el_qprot_wotherinp' = as.numeric(elast_prices[7,3]))
#q_oil
el_qoil_pcer <- c('el_qoil_pcer' = as.numeric(elast_prices[1,4]))
el_qoil_pprot <- c('el_qoil_pprot' = as.numeric(elast_prices[2,4]))
el_qoil_poil <- c('el_qoil_poil' = as.numeric(elast_prices[3,4]))
el_qoil_proots <- c('el_qoil_proots' = as.numeric(elast_prices[4,4]))
el_qoil_pcorn <- c('el_qoil_pcorn' = as.numeric(elast_prices[5,4]))
el_qoil_wfert <- c('el_qoil_wfert' = as.numeric(elast_prices[6,4]))
el_qoil_wotherinp <- c('el_qoil_wotherinp' = as.numeric(elast_prices[7,4]))
#qroots
el_qroots_pcer <- c('el_qroots_pcer' = as.numeric(elast_prices[1,5]))
el_qroots_pprot <- c('el_qroots_pprot' = as.numeric(elast_prices[2,5]))
el_qroots_poil <- c('el_qroots_poil' = as.numeric(elast_prices[3,5]))
el_qroots_proots <- c('el_qroots_proots' = as.numeric(elast_prices[4,5]))
el_qroots_pcorn <- c('el_qroots_pcorn' = as.numeric(elast_prices[5,5]))
el_qroots_wfert <- c('el_qroots_wfert' = as.numeric(elast_prices[6,5]))
el_qroots_wotherinp <- c('el_qroots_wotherinp' = as.numeric(elast_prices[7,5]))
#qcorn
el_qcorn_pcer <- c('el_qcorn_pcer' = as.numeric(elast_prices[1,6]))
el_qcorn_pprot <- c('el_qcorn_pprot' = as.numeric(elast_prices[2,6]))
el_qcorn_poil <- c('el_qcorn_poil' = as.numeric(elast_prices[3,6]))
el_qcorn_proots <- c('el_qcorn_proots' = as.numeric(elast_prices[4,6]))
el_qcorn_pcorn <- c('el_qcorn_pcorn' = as.numeric(elast_prices[5,6]))
el_qcorn_wfert <- c('el_qcorn_wfert' = as.numeric(elast_prices[6,6]))
el_qcorn_wotherinp <- c('el_qcorn_wotherinp' = as.numeric(elast_prices[7,6]))
#xfert
el_xfert_pcer <- c('el_xfert_pcer' = as.numeric(elast_prices[1,7]))
el_xfert_pprot <- c('el_xfert_pprot' = as.numeric(elast_prices[2,7]))
el_xfert_poil <- c('el_xfert_poils' = as.numeric(elast_prices[3,7]))
el_xfert_proots <- c('el_xfert_proots' = as.numeric(elast_prices[4,7]))
el_xfert_pcorn <- c('el_xfert_pcorn' = as.numeric(elast_prices[5,7]))
el_xfert_wfert <- c('el_xfert_wfert' = as.numeric(elast_prices[6,7]))
el_xfert_wotherinp <- c('el_xfert_woth' = as.numeric(elast_prices[7,7]))
#xother
el_xotherinp_pcer <- c('el_xotherinp_pcer' = as.numeric(elast_prices[1,8]))
el_xotherinp_pprot <- c('el_xotherinp_pprot' = as.numeric(elast_prices[2,8]))
el_xotherinp_poil <- c('el_xotherinp_poil' = as.numeric(elast_prices[3,8]))
el_xotherinp_proots <- c('el_xotherinp_proots' = as.numeric(elast_prices[4,8]))
el_xotherinp_pcorn <- c('el_xotherinp_pcorn' = as.numeric(elast_prices[5,8]))
el_xotherinp_wfert <- c('el_xotherinp_wfert' = as.numeric(elast_prices[6,8]))
el_xotherinp_wotherinp <- c('el_xotherinp_wotherinp' = as.numeric(elast_prices[7,8]))
# ---------------------------------- #
#### Compute weather elasticities ####
# ---------------------------------- #
# ------------------------------ #
# Weather elasticities for crops #
# ------------------------------ #
#set-up a data frame to store the results
elast_weather <- data.frame("Variables"=c("GDD_obs", "PREC_obs", "GDDHigh_obs", "DD_obs", "GDD_past", "PREC_past", "GDDHigh_past", "DD_past"),
"Q Cereals"=rep(NA,8),
"Q Protein"=rep(NA,8),
"Q Oilseed"=rep(NA,8),
"Q Roots"=rep(NA,8),
"Q Corn"=rep(NA,8),
"X Fertilizer"=rep(NA,8))
# Weather vars
list_weather <- data.frame(weather_obs= c("gdd_obs","prec_obs","gddHigh_obs","dd_obs"),
weather_1to3= c("gdd_1to3","prec_1to3","gddHigh_1to3","dd_1to3"),
weather_4to10=c("gdd_4to10","prec_4to10","gddHigh_4to10","dd_4to10"),
weather_obs_1to3 = c("gdd_obs_1to3","prec_obs_1to3","gddHigh_obs_1to3","dd_obs_1to3"),
weather_obs_4to10 = c("gdd_obs_4to10","prec_obs_4to10","gddHigh_obs_4to10","dd_obs_4to10"),
weather_obs_endwith= c("* gdd_obs)","* prec_obs)","* gddHigh_obs)","* dd_obs)"),
weather_1to3_endwith= c("* gdd_1to3)","* prec_1to3)","* gddHigh_1to3)","* dd_1to3)"),
weather_4to10_endwith=c("* gdd_4to10)","* prec_4to10)","* gddHigh_4to10)","* dd_4to10)"),
weather_obs_1to3_endwith = c("* gdd_obs_1to3)","* prec_obs_1to3)","* gddHigh_obs_1to3)","* dd_obs_1to3)"),
weather_obs_4to10_endwith = c("* gdd_obs_4to10)","* prec_obs_4to10)","* gddHigh_obs_4to10)","* dd_obs_4to10)"))
n_weather <- list_weather %>% count() %>% as.numeric()
# Loop over all crops
for (i in 1:n_qcrops) {
coefs_struct_crop <- dplyr::as_tibble(t(coef_linear)) %>% select(starts_with(list_qcrops[i,"struct"]))
coefs_probit_crop <- dplyr::as_tibble(t(get(paste0("coef_",list_qcrops[i,"probit"]))))
coef_phi_struct <- coefs_struct_crop %>% select(ends_with(paste0("phi_",list_qcrops[i,"crops"]), ignore.case = FALSE)) # phi_cereals etc.
PHI <- get(paste0("PHI_",list_qcrops[i,"crops"],"_sm"))
phi <- get(paste0("phi_",list_qcrops[i,"crops"],"_sm"))
linpred <- get(paste0(list_qcrops[i,"probit"],"_linpred_sm"))
pred <- get(paste0("qq_",list_qcrops[i,"crops"],"_pred"))
mean_quantity <- dat_sm %>% select(paste0("qq_",list_qcrops[i,"crops"]))
# Loop over all weather variables
for (j in 1:n_weather) {
# Weather-interaction terms
coef_struct_weather_obs_1to3 <- coefs_struct_crop %>% select(ends_with(list_weather[j,"weather_obs_1to3_endwith"]))
coef_struct_weather_obs_4to10 <- coefs_struct_crop %>% select(ends_with(list_weather[j,"weather_obs_4to10_endwith"]))
# Mean weather for derivatives
mean_weather_obs <- dat_sm %>% select(list_weather[j,"weather_obs"])
mean_weather_1to3 <- dat_sm %>% select(list_weather[j,"weather_1to3"])
mean_weather_4to10 <- dat_sm %>% select(list_weather[j,"weather_4to10"])
# Observed weather (note: observed weather only affects structural equations, not crop selection; also note that PHI_* is treated like a constant term )
coef_struct_weather <- coefs_struct_crop %>% select(ends_with(list_weather[j,"weather_obs_endwith"]))
elast_weather[j,i+1] <- PHI * (coef_struct_weather + coef_struct_weather_obs_1to3*mean_weather_1to3 + coef_struct_weather_obs_4to10*mean_weather_4to10) * (1/mean_quantity)
# Weather_1to3
coef_probit_weather <- coefs_probit_crop %>% select(list_weather[j,"weather_1to3"])
coef_struct_weather <- coefs_struct_crop %>% select(ends_with(list_weather[j,"weather_1to3_endwith"]))
elast_weather_1to3 <- (PHI * (coef_struct_weather + coef_struct_weather_obs_1to3*mean_weather_obs) + phi * pred * coef_probit_weather -
coef_phi_struct * linpred * coef_probit_weather * phi) * (1 / mean_quantity )
# Weather_4to10
coef_probit_weather <- coefs_probit_crop %>% select(list_weather[j,"weather_4to10"])
coef_struct_weather <- coefs_struct_crop %>% select(ends_with(list_weather[j,"weather_4to10_endwith"]))
elast_weather_4to10 <- (PHI * (coef_struct_weather + coef_struct_weather_obs_4to10*mean_weather_obs) + phi * pred * coef_probit_weather -
coef_phi_struct * linpred * coef_probit_weather * phi) * (1 / mean_quantity )
# Weather_past
elast_weather[j+4,i+1] <- elast_weather_1to3 + elast_weather_4to10
}
}
# ----------------------------------- #
# Weather elasticities for fertilizer #
# ----------------------------------- #
# Weather vars
list_weather <- data.frame(weather_obs= c("gdd_obs","prec_obs","gddHigh_obs","dd_obs"),
weather_1to3= c("gdd_1to3","prec_1to3","gddHigh_1to3","dd_1to3"),
weather_4to10=c("gdd_4to10","prec_4to10","gddHigh_4to10","dd_4to10"),
weather_obs_1to3 = c("gdd_obs_1to3","prec_obs_1to3","gddHigh_obs_1to3","dd_obs_1to3"),
weather_obs_4to10 = c("gdd_obs_4to10","prec_obs_4to10","gddHigh_obs_4to10","dd_obs_4to10"),
weather_obs_endwith= c("_gdd_obs","_prec_obs","_gddHigh_obs","_dd_obs"),
weather_1to3_endwith= c("_gdd_1to3","_prec_1to3","_gddHigh_1to3","_dd_1to3"),
weather_4to10_endwith=c("_gdd_4to10","_prec_4to10","_gddHigh_4to10","_dd_4to10"),
weather_obs_1to3_endwith = c("_gdd_obs_1to3","_prec_obs_1to3","_gddHigh_obs_1to3","_dd_obs_1to3"),
weather_obs_4to10_endwith = c("_gdd_obs_4to10","_prec_obs_4to10","_gddHigh_obs_4to10","_dd_obs_4to10"))
n_weather <- list_weather %>% count() %>% as.numeric()
coefs_fert <- dplyr::as_tibble(t(coef_linear)) %>% select(starts_with("NXfert"))
# Loop over all weather variables
for (j in 1:n_weather) {
# Weather-interaction terms
coef_fert_weather_obs_1to3 <- coefs_fert %>% select(ends_with(list_weather[j,"weather_obs_1to3_endwith"]))
coef_fert_weather_obs_4to10 <- coefs_fert %>% select(ends_with(list_weather[j,"weather_obs_4to10_endwith"]))
# Mean weather for derivatives
mean_weather_obs <- dat_sm %>% select(list_weather[j,"weather_obs"])
mean_weather_1to3 <- dat_sm %>% select(list_weather[j,"weather_1to3"])
mean_weather_4to10 <- dat_sm %>% select(list_weather[j,"weather_4to10"])
# Observed weather (note: observed weather only affects structural equations, not crop selection; also note that PHI_* is treated like a constant term )
coef_fert_weather <- coefs_fert %>% select(ends_with(list_weather[j,"weather_obs_endwith"]))
elast_weather[j,7] <- (coef_fert_weather + coef_fert_weather_obs_1to3*mean_weather_1to3 + coef_fert_weather_obs_4to10*mean_weather_4to10) * (1/dat_sm$nx_fert)
# Weather_1to3
coef_fert_weather <- coefs_fert %>% select(ends_with(list_weather[j,"weather_1to3_endwith"]))
elast_weather_1to3 <- (coef_fert_weather + coef_fert_weather_obs_1to3*mean_weather_obs) * (1/dat_sm$nx_fert)
# Weather_4to10
coef_fert_weather <- coefs_fert %>% select(ends_with(list_weather[j,"weather_4to10_endwith"]))
elast_weather_4to10 <- (coef_fert_weather + coef_fert_weather_obs_4to10*mean_weather_obs) * (1/dat_sm$nx_fert)
# Weather_past
elast_weather[j+4,7] <- elast_weather_1to3 + elast_weather_4to10
}
# --------------------------------------- #
# Store weather elasticities in bootstrap #
# ----------------------------------------#
# Rescale results to obtain the units days, mm, days, days
elast_weather[,2:7] <- elast_weather[,2:7]*100
#qcer
# obs
sel_qcer_gdd_obs <- c('sel_qcer_gdd_obs' = as.numeric(elast_weather[1,2]))
sel_qcer_prec_obs <- c('sel_qcer_prec_obs' = as.numeric(elast_weather[2,2]))
sel_qcer_gddHigh_obs <- c('sel_qcer_gddHigh_obs' = as.numeric(elast_weather[3,2]))
sel_qcer_dd_obs <- c('sel_qcer_dd_obs' = as.numeric(elast_weather[4,2]))
#past
sel_qcer_gdd_past <- c('sel_qcer_gdd_past' = as.numeric(elast_weather[5,2]))
sel_qcer_prec_past <- c('sel_qcer_prec_past' = as.numeric(elast_weather[6,2]))
sel_qcer_gddHigh_past <- c('sel_qcer_gddHigh_past' = as.numeric(elast_weather[7,2]))
sel_qcer_dd_past <- c('sel_qcer_dd_past' = as.numeric(elast_weather[8,2]))
#qprot
# obs
sel_qprot_gdd_obs <- c('sel_qprot_gdd_obs' = as.numeric(elast_weather[1,3]))
sel_qprot_prec_obs <- c('sel_qprot_prec_obs' = as.numeric(elast_weather[2,3]))
sel_qprot_gddHigh_obs <- c('sel_qprot_gddHigh_obs' = as.numeric(elast_weather[3,3]))
sel_qprot_dd_obs <- c('sel_qprot_dd_obs' = as.numeric(elast_weather[4,3]))
#past
sel_qprot_gdd_past <- c('sel_qprot_gdd_past' = as.numeric(elast_weather[5,3]))
sel_qprot_prec_past <- c('sel_qprot_prec_past' = as.numeric(elast_weather[6,3]))
sel_qprot_gddHigh_past <- c('sel_qprot_gddHigh_past' = as.numeric(elast_weather[7,3]))
sel_qprot_dd_past <- c('sel_qprot_dd_past' = as.numeric(elast_weather[8,3]))
#qoil
# obs
sel_qoil_gdd_obs <- c('sel_qoil_gdd_obs' = as.numeric(elast_weather[1,4]))
sel_qoil_prec_obs <- c('sel_qoil_prec_obs' = as.numeric(elast_weather[2,4]))
sel_qoil_gddHigh_obs <- c('sel_qoil_gddHigh_obs' = as.numeric(elast_weather[3,4]))
sel_qoil_dd_obs <- c('sel_qoil_dd_obs' = as.numeric(elast_weather[4,4]))
#past
sel_qoil_gdd_past <- c('sel_qoil_gdd_past' = as.numeric(elast_weather[5,4]))
sel_qoil_prec_past <- c('sel_qoil_prec_past' = as.numeric(elast_weather[6,4]))
sel_qoil_gddHigh_past <- c('sel_qoil_gddHigh_past' = as.numeric(elast_weather[7,4]))
sel_qoil_dd_past <- c('sel_qoil_dd_past' = as.numeric(elast_weather[8,4]))
#qroots
# obs
sel_qroots_gdd_obs <- c('sel_qroots_gdd_obs' = as.numeric(elast_weather[1,5]))
sel_qroots_prec_obs <- c('sel_qroots_prec_obs' = as.numeric(elast_weather[2,5]))
sel_qroots_gddHigh_obs <- c('sel_qroots_gddHigh_obs' = as.numeric(elast_weather[3,5]))
sel_qroots_dd_obs <- c('sel_qroots_dd_obs' = as.numeric(elast_weather[4,5]))
#past
sel_qroots_gdd_past <- c('sel_qroots_gdd_past' = as.numeric(elast_weather[5,5]))
sel_qroots_prec_past <- c('sel_qroots_prec_past' = as.numeric(elast_weather[6,5]))
sel_qroots_gddHigh_past <- c('sel_qroots_gddHigh_past' = as.numeric(elast_weather[7,5]))
sel_qroots_dd_past <- c('sel_qroots_dd_past' = as.numeric(elast_weather[8,5]))
#corn
# obs
sel_qcorn_gdd_obs <- c('sel_qcorn_gdd_obs' = as.numeric(elast_weather[1,6]))
sel_qcorn_prec_obs <- c('sel_qcorn_prec_obs' = as.numeric(elast_weather[2,6]))
sel_qcorn_gddHigh_obs <- c('sel_qcorn_gddHigh_obs' = as.numeric(elast_weather[3,6]))
sel_qcorn_dd_obs <- c('sel_qcorn_dd_obs' = as.numeric(elast_weather[4,6]))
#past
sel_qcorn_gdd_past <- c('sel_qcorn_gdd_past' = as.numeric(elast_weather[5,6]))
sel_qcorn_prec_past <- c('sel_qcorn_prec_past' = as.numeric(elast_weather[6,6]))
sel_qcorn_gddHigh_past <- c('sel_qcorn_gddHigh_past' = as.numeric(elast_weather[7,6]))
sel_qcorn_dd_past <- c('sel_qcorn_dd_past' = as.numeric(elast_weather[8,6]))
#xfert
# obs
sel_xfert_gdd_obs <- c('sel_xfert_gdd_obs' = as.numeric(elast_weather[1,7]))
sel_xfert_prec_obs <- c('sel_xfert_prec_obs' = as.numeric(elast_weather[2,7]))
sel_xfert_gddHigh_obs <- c('sel_xfert_gddHigh_obs' = as.numeric(elast_weather[3,7]))
sel_xfert_dd_obs <- c('sel_xfert_dd_obs' = as.numeric(elast_weather[4,7]))
#past
sel_xfert_gdd_past <- c('sel_xfert_gdd_past' = as.numeric(elast_weather[5,7]))
sel_xfert_prec_past <- c('sel_xfert_prec_past' = as.numeric(elast_weather[6,7]))
sel_xfert_gddHigh_past <- c('sel_xfert_gddHigh_past' = as.numeric(elast_weather[7,7]))
sel_xfert_dd_past <- c('sel_xfert_dd_past' = as.numeric(elast_weather[8,7]))
# ------------------------------- #
#### Store results of interest ####
# ------------------------------- #
result <- c(coef_iCereals, # Probit coefficients
coef_iProtein,
coef_iOilseed,
coef_iRoots,
coef_iCorn,
iCereals_me, # Probit marginal effects
iProtein_me,
iOilseed_me,
iRoots_me,
iCorn_me,
model_linear$coefficients, #structural coefficients
el_qcer_pcer, #price elasticities
el_qcer_pprot,
el_qcer_poil,
el_qcer_proots,
el_qcer_pcorn,
el_qcer_wfert,
el_qcer_wotherinp,
el_qprot_pcer,
el_qprot_pprot,
el_qprot_poil,
el_qprot_proots,
el_qprot_pcorn,
el_qprot_wfert,
el_qprot_wotherinp,
el_qoil_pcer,
el_qoil_pprot,
el_qoil_poil,
el_qoil_proots,
el_qoil_pcorn,
el_qoil_wfert,
el_qoil_wotherinp,
el_qroots_pcer,
el_qroots_pprot,
el_qroots_poil,
el_qroots_proots,
el_qroots_pcorn,
el_qroots_wfert,
el_qroots_wotherinp,
el_qcorn_pcer,
el_qcorn_pprot,
el_qcorn_poil,
el_qcorn_proots,
el_qcorn_pcorn,
el_qcorn_wfert,
el_qcorn_wotherinp,
el_xfert_pcer,
el_xfert_pprot,
el_xfert_poil,
el_xfert_proots,
el_xfert_pcorn,
el_xfert_wfert,
el_xfert_wotherinp,
el_xotherinp_pcer,
el_xotherinp_pprot,
el_xotherinp_poil,
el_xotherinp_proots,
el_xotherinp_pcorn,
el_xotherinp_wfert,
el_xotherinp_wotherinp,
sel_qcer_gdd_obs, #weather elasticities
sel_qcer_prec_obs,
sel_qcer_gddHigh_obs,
sel_qcer_dd_obs,
sel_qprot_gdd_obs,
sel_qprot_prec_obs,
sel_qprot_gddHigh_obs,
sel_qprot_dd_obs,
sel_qoil_gdd_obs,
sel_qoil_prec_obs,
sel_qoil_gddHigh_obs,
sel_qoil_dd_obs,
sel_qroots_gdd_obs,
sel_qroots_prec_obs,
sel_qroots_gddHigh_obs,
sel_qroots_dd_obs,
sel_qcorn_gdd_obs,
sel_qcorn_prec_obs,
sel_qcorn_gddHigh_obs,
sel_qcorn_dd_obs,
sel_xfert_gdd_obs,
sel_xfert_prec_obs,
sel_xfert_gddHigh_obs,
sel_xfert_dd_obs,
sel_qcer_gdd_past,
sel_qcer_prec_past,
sel_qcer_gddHigh_past,
sel_qcer_dd_past,
sel_qprot_gdd_past,
sel_qprot_prec_past,
sel_qprot_gddHigh_past,
sel_qprot_dd_past,
sel_qoil_gdd_past,
sel_qoil_prec_past,
sel_qoil_gddHigh_past,
sel_qoil_dd_past,
sel_qroots_gdd_past,
sel_qroots_prec_past,
sel_qroots_gddHigh_past,
sel_qroots_dd_past,
sel_qcorn_gdd_past,
sel_qcorn_prec_past,
sel_qcorn_gddHigh_past,
sel_qcorn_dd_past,
sel_xfert_gdd_past,
sel_xfert_prec_past,
sel_xfert_gddHigh_past,
sel_xfert_dd_past)
result
}, error=function(err) {rep(NA,1185)} ) # insert NA's if there is an optimization error
}
}
#-------------------------------------------#
#### Obtain and store the actual results ####
#-------------------------------------------#
# run one draw with the original data
system.time(boot_actresults <- my.boot(data=df_farm, nrep=1, cluster=df_farm$key, nCores=1, actual=TRUE))
# save actual result
save(boot_actresults, file="rOutput/bootresults_main_actual.Rda")
boot_actresults <- NULL
#------------------------------------------------#
#### Run bootstrap: Cluster at the farm-level ####
#------------------------------------------------#
# find number of cores
nCores <- parallel::detectCores() -1
# run the bootstrap
set.seed(1234)
system.time(boot_results <- my.boot(data=df_farm, nrep=1000, cluster=df_farm$key, nCores=nCores, actual=FALSE))
# save
save(boot_results, file="rOutput/bootresults_main_clustFarm.Rda")
boot_results <- NULL
#-------------------------------------------------#
#### Run bootstrap: Cluster at the nuts2-level ####
#-------------------------------------------------#
# find number of cores
nCores <- parallel::detectCores() -1
# run
set.seed(1234)
system.time(boot_results <- my.boot(data=df_farm, nrep=1000, cluster=df_farm$nuts2, nCores=nCores, actual=FALSE))
# save
save(boot_results, file="rOutput/bootresults_main_clustNuts2.Rda")
boot_results <- NULL
#----------------------------------#
#### 2) Presentation of results ####
#----------------------------------#
# Pick cluster at farm or nuts2 level
cluster <- "clustFarm"
#cluster <- "clustNuts2"
# Load bootstrap results
load(paste0("rOutput/bootresults_main_",cluster,".Rda"))
load("rOutput/bootresults_main_actual.Rda")
# Turn matrix into data frame
boot_results <- as.data.frame(boot_results)
summary(boot_results$sel_qcer_gdd_obs)
# --------------------------------- #
# Create list with original results #
# --------------------------------- #
names(boot_results)
boot_summary <- data.frame("Variable"=c(names(boot_results)),
"Original"=boot_actresults)
# ------------------------ #
# Add confidence intervals #
# ------------------------ #
# Confidence intervals
CIs <- t(sapply(boot_results,function(i) quantile(i,c(0.025, 0.975), na.rm=TRUE)))
CIs <- as.data.frame(CIs)
colnames(CIs) <- c("L95","U95")
# Add CIs to boot_summary
boot_summary <- cbind(boot_summary, CIs)
# Organize
coef_iCereals <- boot_summary[c(1:47),]
coef_iProtein <- boot_summary[c(48:94),]
coef_iOilseed <- boot_summary[c(95:141),]
coef_iRoots <- boot_summary[c(142:188),]
coef_iCorn <- boot_summary[c(189:235),]
iCereals_me <- boot_summary[c(236:282),]
iProtein_me <- boot_summary[c(283:329),]
iOilseed_me <- boot_summary[c(330:376),]
iRoots_me <- boot_summary[c(377:423),]
iCorn_me <- boot_summary[c(424:470),]
iCereals_me_extr <- boot_summary[c(471:517),]
iProtein_me_extr <- boot_summary[c(518:564),]