-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code_WheatQuali.R
2945 lines (2209 loc) · 167 KB
/
Code_WheatQuali.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
# =================================================================================================
# =================================================================================================
# -------------------------------------------------------------------------------------------------
# Publication: Precipitation causes quality losses of large economic relevance in wheat production
# Authors: Janic Bucheli, Margot Visse-Mansiaux, Juan Herrera, Lilia Levy Häner, Jesse Tack
# and Robert Finger
# Published in Q-Open in 2024.
# -------------------------------------------------------------------------------------------------
# =================================================================================================
# =================================================================================================
# Download required packages
library(readxl)
library(plyr)
library(dplyr)
library(ggplot2)
library(cowplot)
library(fixest)
library(reshape2)
library(lmtest)
library(sandwich)
library(DescTools)
# =================================================================================================
# -------------------------------------------------------------------------------------------------
# 1) Read and prepare the varietal trial data
# -------------------------------------------------------------------------------------------------
# =================================================================================================
setwd("H:/Working Papers/Wheat Quality Paper/Raw_Data")
cols_interest <- c("site", "siteId","weather_station","variety","plotArea_m2","plot","sitePostalCode","sowingDate", "harvestDate", "year", "yield_dtPerHa","timeToFallDry_sec","heading_days","varietyAlternativeName")
# -----------------------------
# 2008 to 2012
# -----------------------------
filenames0812 <- list.files(path="2008-2012", full.names = T)
temp1 <- as.data.frame(read_excel(filenames0812[1]))
temp1 <- temp1[,cols_interest]
temp1$heading_days <- as.numeric(temp1$heading_days)
temp1$sowingDate <- as.numeric(temp1$sowingDate)
temp1$harvestDate <- as.numeric(temp1$harvestDate)
for(f in 2:length(filenames0812)){
temp <- as.data.frame(read_excel(filenames0812[f]))
# if condition because there are different column names in raw data files
if ("variety" %in% colnames(temp)){
temp$heading_days <- as.numeric(temp$heading_days)
temp$yield_dtPerHa <- as.numeric(temp$yield_dtPerHa)
temp$proteinYield_dtPerHa <- as.numeric(temp$proteinYield_dtPerHa)
temp$humidityPercentageInGrain <- as.numeric(temp$humidityPercentageInGrain)
temp$timeToFallDry_sec <- as.numeric(temp$timeToFallDry_sec)
temp$sowingDate <- as.numeric(temp$sowingDate)
temp$harvestDate <- as.numeric(temp$harvestDate)
temp$testWeightMittel_kg <- as.numeric(temp$testWeightMittel_kg)
} else{
temp$heading_days <- as.numeric(temp$heading_days)
temp$yield_dtPerHa <- as.numeric(temp$yield_dtPerHa)
temp$proteinYield_dtPerHa <- as.numeric(temp$proteinYield_dtPerHa)
temp$humidityPercentageInGrain <- as.numeric(temp$humidityPercentageInGrain)
temp$timeToFallDry_sec <- as.numeric(temp$timeToFallDry_sec)
temp$sowingDate <- as.numeric(temp$sowingDate)
temp$harvestDate <- as.numeric(temp$harvestDate)
temp$testWeightMittel_kg <- as.numeric(temp$testWeightMittel_kg)
colnames(temp)[which(colnames(temp) == "variety...11")] <- "variety"
}
temp <- temp[,cols_interest]
temp1 <- bind_rows(temp1,temp)
rm(temp)
}
temp_0812 <- temp1
rm(temp1,f,filenames0812)
# -----------------------------
# 2013 to 2015
# -----------------------------
filenames1315 <- list.files(path="2013-2015", full.names = T)
temp1 <- as.data.frame(read_excel(filenames1315[1]))
temp1 <- temp1[,cols_interest]
temp1$heading_days <- as.numeric(temp1$heading_days)
temp1$sowingDate <- as.numeric(temp1$sowingDate)
temp1$harvestDate <- as.numeric(temp1$harvestDate)
temp1$testWeightMittel_kg <- as.numeric(temp1$testWeightMittel_kg)
for(f in 2:length(filenames1315)){
temp <- as.data.frame(read_excel(filenames1315[f]))
if ("variety" %in% colnames(temp)){
temp$heading_days <- as.numeric(temp$heading_days)
temp$yield_dtPerHa <- as.numeric(temp$yield_dtPerHa)
temp$proteinYield_dtPerHa <- as.numeric(temp$proteinYield_dtPerHa)
temp$humidityPercentageInGrain <- as.numeric(temp$humidityPercentageInGrain)
temp$timeToFallDry_sec <- as.numeric(temp$timeToFallDry_sec)
temp$sowingDate <- as.numeric(temp$sowingDate)
temp$harvestDate <- as.numeric(temp$harvestDate)
temp$testWeightMittel_kg <- as.numeric(temp$testWeightMittel_kg)
} else{
temp$heading_days <- as.numeric(temp$heading_days)
temp$yield_dtPerHa <- as.numeric(temp$yield_dtPerHa)
temp$proteinYield_dtPerHa <- as.numeric(temp$proteinYield_dtPerHa)
temp$humidityPercentageInGrain <- as.numeric(temp$humidityPercentageInGrain)
temp$timeToFallDry_sec <- as.numeric(temp$timeToFallDry_sec)
temp$sowingDate <- as.numeric(temp$sowingDate)
temp$harvestDate <- as.numeric(temp$harvestDate)
temp$testWeightMittel_kg <- as.numeric(temp$testWeightMittel_kg)
colnames(temp)[which(colnames(temp) == "variety...11")] <- "variety"
}
temp <- temp[,cols_interest]
temp1 <- bind_rows(temp1,temp)
rm(temp)
}
temp_1315 <- temp1
rm(temp1,f, filenames1315)
# -----------------------------
# 2016 to 2019
# -----------------------------
filenames1619 <- list.files(path="2016-2019", full.names = T)
temp1 <- as.data.frame(read_excel(filenames1619[1]))
temp1 <- temp1[,cols_interest]
temp1$heading_days <- as.numeric(temp1$heading_days)
temp1$sowingDate <- as.numeric(temp1$sowingDate)
temp1$harvestDate <- as.numeric(temp1$harvestDate)
temp1$plotArea_m2 <- as.numeric(temp1$plotArea_m2)
for(f in 2:length(filenames1619)){
temp <- as.data.frame(read_excel(filenames1619[f]))
if ("variety" %in% colnames(temp)){
temp$heading_days <- as.numeric(temp$heading_days)
temp$yield_dtPerHa <- as.numeric(temp$yield_dtPerHa)
temp$proteinYield_dtPerHa <- as.numeric(temp$proteinYield_dtPerHa)
temp$humidityPercentageInGrain <- as.numeric(temp$humidityPercentageInGrain)
temp$timeToFallDry_sec <- as.numeric(temp$timeToFallDry_sec)
temp$sowingDate <- as.numeric(temp$sowingDate)
temp$harvestDate <- as.numeric(temp$harvestDate)
temp$plotArea_m2 <- as.numeric(temp$plotArea_m2)
temp$testWeightMittel_kg <- as.numeric(temp$testWeightMittel_kg)
} else{
temp$heading_days <- as.numeric(temp$heading_days)
temp$yield_dtPerHa <- as.numeric(temp$yield_dtPerHa)
temp$proteinYield_dtPerHa <- as.numeric(temp$proteinYield_dtPerHa)
temp$humidityPercentageInGrain <- as.numeric(temp$humidityPercentageInGrain)
temp$timeToFallDry_sec <- as.numeric(temp$timeToFallDry_sec)
temp$sowingDate <- as.numeric(temp$sowingDate)
temp$harvestDate <- as.numeric(temp$harvestDate)
temp$plotArea_m2 <- as.numeric(temp$plotArea_m2)
temp$testWeightMittel_kg <- as.numeric(temp$testWeightMittel_kg)
colnames(temp)[which(colnames(temp) == "variety...11")] <- "variety"
}
temp <- temp[,cols_interest]
temp1 <- bind_rows(temp1,temp)
rm(temp)
}
temp_1619 <- temp1
rm(temp1,f,filenames1619)
# -----------------------------
# Bring all data together
# -----------------------------
data_final <- bind_rows(temp_0812, temp_1315, temp_1619)
rm(temp_0812, temp_1315, temp_1619, filenames0812, filenames1315, filenames1619, cols_interest)
# Consistent labelling of variety names
data_final[data_final == "CH CLARO"] <- "CH_CLARO"
data_final[data_final == "CH CAMEDO"] <- "CH_CAMEDO"
data_final[data_final == "CH NARA"] <- "CH_NARA"
data_final[data_final == "BOCKRIS_(2)"] <- "BOCKRIS"
data_final[data_final == "ARNOLD_(BLE)"] <- "ARNOLD"
data_final[data_final == "ARNOLD (BLE)"] <- "ARNOLD"
data_final[data_final == "GENIUS (2)"] <- "GENIUS"
data_final[data_final == "GENIUS_(2)"] <- "GENIUS"
data_final[which(data_final$variety == "DILLAGO"),"variety"] <- as.character("DILAGO")
data_final[which(data_final$variety == "DILAGGO"),"variety"] <- as.character("DILAGO")
# Replace NA with the alternative variety name
data_final[which(data_final$variety == "NA"),"variety"] <- data_final[which(data_final$variety == "NA"),"varietyAlternativeName"]
# Drop observations for which the variety is unknown (these varieties are not introduced onto the market)
# We need to know the variety to allocate it to the quality class
data_final <- data_final[which(data_final$variety != "NA"),]
# -----------------------------
# Add sowing and havrest dates
# -----------------------------
data_sowing <- read.csv("Meta/Sowing_dates.csv", sep=";")
colnames(data_sowing) <- c("site", seq(2008,2020,1))
for(i in 1:nrow(data_final)){
data_final[i,"sowingDate"] <- as.character(data_sowing[which(data_sowing$site == data_final[i,"site"]),which(colnames(data_sowing) == as.character(data_final[i,"year"]))])
}
data_final$sowingDate <- as.Date(data_final$sowingDate, format="%Y-%m-%d")
rm(data_sowing,i)
data_harvest <- read.csv("Meta/Harvest_dates.csv", sep=";")
colnames(data_harvest) <- c("site", seq(2008,2020,1))
for(i in 1:nrow(data_final)){
data_final[i,"harvestDate"] <- as.character(data_harvest[which(data_harvest$site == data_final[i,"site"]),which(colnames(data_harvest) == as.character(data_final[i,"year"]))])
}
data_final$harvestDate <- as.Date(data_final$harvestDate, format="%Y-%m-%d")
rm(data_harvest,i)
# -----------------------------
# Downgrading
# -----------------------------
downgrading_threshold <- 220
data_final$downgrading <- NA
# 1/0 for occurrence of declassification
for(i in 1:nrow(data_final)){
if(is.na(data_final[i,"timeToFallDry_sec"])){
data_final[i,"downgrading"] <- NA
} else if(data_final[i,"timeToFallDry_sec"] <= downgrading_threshold){
data_final[i,"downgrading"] <- 1
} else (data_final[i,"downgrading"] <- 0)
}
rm(i, downgrading_threshold)
# -----------------------------
# Match variety with price class
# -----------------------------
# We derive the classifications from officially published lists of recommended varieties
data_classification <- read.csv("Meta/classification.csv", sep=";")
colnames(data_classification) <- c("variety", seq(2022,2008,-1))
data_classification[data_classification == ""] <- NA
# Use only rows with at least one record, i.e. no NA in each year
# This removes varieties that were not introduced to the list of recommended varieties
# In this list, there are 15 years, i.e. 15 NAs per row show that the variety is not in the list of recommended varieties
# Note that the quality class (economic class) may change when the variety shows lower relative performance
data_downgrading <- data_classification[which(rowSums(is.na(data_classification[,-1])) != 15),]
# Check whether there are spelling errors
# These are presumably the varieties that were not introduced to the list of recommended varieties
sort(unique(data_final$variety)[which(unique(data_final$variety) %in% data_classification$variety != T)])
# We match by variety and year
data_final$class <- NA
for (i in 1:nrow(data_final)){
if (data_final[i,"variety"] %in% data_downgrading$variety && !is.na(data_downgrading[which(data_downgrading$variety == data_final[i,"variety"]), which(colnames(data_downgrading) == as.character(data_final[i,"year"]))])){
data_final[i,"class"] <- data_downgrading[which(data_downgrading$variety == data_final[i,"variety"]), which(colnames(data_downgrading) == as.character(data_final[i,"year"]))]
} else if (data_final[i,"variety"] %in% data_downgrading$variety && is.na(data_downgrading[which(data_downgrading$variety == data_final[i,"variety"]), which(colnames(data_downgrading) == as.character(data_final[i,"year"]))])){
# Varieties are not immediately in list. Pick the classification from the year of market introduction
temp1 <- data_downgrading[which(data_downgrading$variety == data_final[i,"variety"]), -1]
data_final[i,"class"] <- temp1[!is.na(temp1)][length(temp1[!is.na(temp1)])]
} else {data_final[i,"class"] <- NA}
}
rm(i,temp1)
# This vector shows the varieties that are not in the list of recommended varieties
missing_varieties <- sort(unique(data_final[is.na(data_final$class),"variety"]))
# We drop observations for the class "Bio" because this class was introduced after 2019 and observations that are not in the list of recommended varieties
# We continue with the dataset agronomic_data that contains all the relevant information to answer our research questions
#Allow here yields with NA -> only interested in modelling weather effects on probability of a downgrading
downgrading_data <- data_final[which(!data_final$class %in% c("Bio", "Futterweizen") & !is.na(data_final$class) & !is.na(data_final$sowingDate) & !is.na(data_final$harvestDate) & !is.na(data_final$downgrading)),]
rm(data_final, missing_varieties, data_classification, data_downgrading)
# =================================================================================================
# -------------------------------------------------------------------------------------------------
# 2) Read and prepare meteorological data
# -------------------------------------------------------------------------------------------------
# =================================================================================================
# The maximum number of days prior to harvest for our weather variables of interest
number_days_prior_harvest <- 60
data_harvest <- read.csv("Meta/Harvest_dates.csv", sep=";")
colnames(data_harvest) <- c("site", seq(2008,2020,1))
data_sowing <- read.csv("Meta/Sowing_dates.csv", sep=";")
colnames(data_sowing) <- c("site", seq(2007,2019,1))
# -----------------------------
# 2.1 Precipitation
# -----------------------------
load("Weather/list_weather_data2.RData")
precipitation_data <- list_weather_data[[1]]
# Precipitation amounts d days prior to harvest
# Precipitation array with the structure: [number of days prior to harvest, location, year]
array_cumulative_precipitation_main <- array(dim=c(number_days_prior_harvest,length(unique(downgrading_data$site)),length(seq(2008,2019,1))))
dimnames(array_cumulative_precipitation_main)[[1]] <- seq(1,number_days_prior_harvest,1)
dimnames(array_cumulative_precipitation_main)[[2]] <- unique(downgrading_data$site)
dimnames(array_cumulative_precipitation_main)[[3]] <- seq(2008,2019,1)
for (d in 1:number_days_prior_harvest){
for (i in 1:length(unique(downgrading_data$site))){
for (t in 1: length(seq(2008,2019,1))){
if(is.na(data_harvest[which(data_harvest$site == dimnames(array_cumulative_precipitation_main)[[2]][i]),as.character(seq(2008,2019,1)[t])])){
array_cumulative_precipitation_main[d,i,t] <- NA
} else{
temp1 <- precipitation_data[which(row.names(precipitation_data) == dimnames(array_cumulative_precipitation_main)[[2]][i]),(which(colnames(precipitation_data) == data_harvest[which(data_harvest$site == dimnames(array_cumulative_precipitation_main)[[2]][i]),as.character(seq(2008,2019,1)[t])])-number_days_prior_harvest):which(colnames(precipitation_data) == data_harvest[which(data_harvest$site == dimnames(array_cumulative_precipitation_main)[[2]][i]),as.character(seq(2008,2019,1)[t])])]
array_cumulative_precipitation_main[d,i,t] <- sum(temp1[(length(temp1)-d):(length(temp1)-1)])
rm(temp1)}
}
}
}
rm(d,i,t)
# Precipitation amounts before the d days prior to harvest and from the sowing date (control variable)
array_cumulative_precipitation_control <- array(dim=c(number_days_prior_harvest,length(unique(downgrading_data$site)),length(seq(2008,2019,1))))
dimnames(array_cumulative_precipitation_control)[[1]] <- seq(1,number_days_prior_harvest,1)
dimnames(array_cumulative_precipitation_control)[[2]] <- unique(downgrading_data$site)
dimnames(array_cumulative_precipitation_control)[[3]] <- seq(2008,2019,1)
for (d in 1:number_days_prior_harvest){
for (i in 1:length(unique(downgrading_data$site))){
for (t in 1: length(seq(2008,2019,1))){
# Start date is the sowing date
temp_start <- as.character(data_sowing[which(data_sowing$site == dimnames(array_cumulative_precipitation_control)[[2]][i]),as.character(seq(2007,2018,1)[t])])
temp_end <- as.character(as.Date(data_harvest[which(data_harvest$site == dimnames(array_cumulative_precipitation_control)[[2]][i]),as.character(seq(2008,2019,1)[t])])-(d+1))
if(is.na(temp_start)){
array_cumulative_precipitation_control[d,i,t] <- NA
} else if (is.na(temp_end)){
array_cumulative_precipitation_control[d,i,t] <- NA
} else{
temp1 <- precipitation_data[which(row.names(precipitation_data) == dimnames(array_cumulative_precipitation_control)[[2]][i]),which(colnames(precipitation_data) == temp_start): which(colnames(precipitation_data) == temp_end)]
array_cumulative_precipitation_control[d,i,t] <- sum(temp1)
rm(temp1)}
rm(temp_start,temp_end)
}
}
}
rm(precipitation_data,d,i,t)
# -----------------------------
# 2.2 Temperature
# -----------------------------
Tmax_data <- list_weather_data[[2]]
Tmin_data <- list_weather_data[[3]]
# Baseline temperature for growing degree-days (GDD)
baseT <- 5
# We will loop over various critical temperature thresholds (from 18 to 32)
heat_threshold_range <- seq(25,32)
# Each element contains results for a specific heat threshold
list_GDD_array_cumulative_GDD_HDD_main <- list()
list_HDD_array_cumulative_GDD_HDD_main <- list()
list_GDD_array_cumulative_GDD_HDD_control <- list()
list_HDD_array_cumulative_GDD_HDD_control <- list()
for (h in 1:length(heat_threshold_range)){
# Temperature threshold from which we measure heat stress
heat_threshold <- heat_threshold_range[h]
# Array for growing degree-days (GDD)
GDD_array_cumulative_GDD_HDD_main <- array(dim=c(number_days_prior_harvest,length(unique(downgrading_data$site)),length(seq(2008,2019,1))))
dimnames(GDD_array_cumulative_GDD_HDD_main)[[1]] <- seq(1,number_days_prior_harvest,1)
dimnames(GDD_array_cumulative_GDD_HDD_main)[[2]] <- unique(downgrading_data$site)
dimnames(GDD_array_cumulative_GDD_HDD_main)[[3]] <- seq(2008,2019,1)
# Array for heat degree-days (HDD) with temperature exposure above the heat threshold
HDD_array_cumulative_GDD_HDD_main <- array(dim=c(number_days_prior_harvest,length(unique(downgrading_data$site)),length(seq(2008,2019,1))))
dimnames(HDD_array_cumulative_GDD_HDD_main)[[1]] <- seq(1,number_days_prior_harvest,1)
dimnames(HDD_array_cumulative_GDD_HDD_main)[[2]] <- unique(downgrading_data$site)
dimnames(HDD_array_cumulative_GDD_HDD_main)[[3]] <- seq(2008,2019,1)
for (d in 1:number_days_prior_harvest){
for (i in 1:length(unique(downgrading_data$site))){
for (t in 1: length(seq(2008,2019,1))){
if(is.na(data_harvest[which(data_harvest$site == dimnames(GDD_array_cumulative_GDD_HDD_main)[[2]][i]),as.character(seq(2008,2019,1)[t])])){
GDD_array_cumulative_GDD_HDD_main[d,i,t] <- NA
} else{
# Get the daily min and max temperature data
temp_Tmax <- Tmax_data[which(row.names(Tmax_data) == dimnames(GDD_array_cumulative_GDD_HDD_main)[[2]][i]),(which(colnames(Tmax_data) == data_harvest[which(data_harvest$site == dimnames(GDD_array_cumulative_GDD_HDD_main)[[2]][i]),as.character(seq(2008,2019,1)[t])])-number_days_prior_harvest):which(colnames(Tmax_data) == data_harvest[which(data_harvest$site == dimnames(GDD_array_cumulative_GDD_HDD_main)[[2]][i]),as.character(seq(2008,2019,1)[t])])]
temp_Tmin <- Tmin_data[which(row.names(Tmin_data) == dimnames(GDD_array_cumulative_GDD_HDD_main)[[2]][i]),(which(colnames(Tmin_data) == data_harvest[which(data_harvest$site == dimnames(GDD_array_cumulative_GDD_HDD_main)[[2]][i]),as.character(seq(2008,2019,1)[t])])-number_days_prior_harvest):which(colnames(Tmin_data) == data_harvest[which(data_harvest$site == dimnames(GDD_array_cumulative_GDD_HDD_main)[[2]][i]),as.character(seq(2008,2019,1)[t])])]
# Daily temperature load for 5-30?C
temp_daily_load_GDD <- vector(length=d)
# Daily temperature load for > 30?C
temp_daily_load_HDD <- vector(length=d)
# a) Tmax < baseT
# b) Tmax < heat threshold
# b.1) Tmin > baseT
# b.2) Tmin < baseT
# c) Tmax > heat threshold
# c.1) HDD + Tmin > baseT
# c.2) HDD + Tmin < baseT
for (l in 1:length(temp_daily_load_GDD)){
# Get the values to calculate daily temperature temperature curves
temp_Tmax_dayd <- temp_Tmax[length(temp_Tmax)-l]
temp_Tmin_dayd <- temp_Tmin[length(temp_Tmax)-l]
temp_Tmin_nextday <- temp_Tmin[length(temp_Tmax)-l+1]
# ----------------------------------------------------------
# HDD & GDD for first half day (from Tmin_dayd to Tmax_dayd)
# ----------------------------------------------------------
GDD_firsthalf <- NA
HDD_firsthalf <- NA
# a) Tmax < baseT
if (temp_Tmax_dayd < baseT){
GDD_firsthalf <- 0
HDD_firsthalf <- 0
# b) Tmax < heat threshold
} else if (temp_Tmax_dayd < heat_threshold){
HDD_firsthalf <- 0
# b.1) Tmin > baseT
if (temp_Tmin_dayd > baseT){
temp_meanT_firsthalf <- (temp_Tmax_dayd + temp_Tmin_dayd) / 2
temp_amplitude_firsthalf <- (temp_Tmax_dayd - temp_Tmin_dayd) / 2
temp_integrand_firsthalf <- function(x){temp_meanT_firsthalf + temp_amplitude_firsthalf * sin(x)}
GDD_firsthalf <- integrate(temp_integrand_firsthalf, lower = (-pi/2), upper = (pi/2))$value - baseT * ((pi/2) - (-pi/2))
rm(temp_meanT_firsthalf, temp_amplitude_firsthalf, temp_integrand_firsthalf)
} else {
# b.2) Tmin < baseT
temp_meanT_firsthalf <- (temp_Tmax_dayd + temp_Tmin_dayd) / 2
temp_amplitude_firsthalf <- (temp_Tmax_dayd - temp_Tmin_dayd) / 2
temp_integrand_firsthalf <- function(x){temp_meanT_firsthalf + temp_amplitude_firsthalf * sin(x)}
# Get lower bound of integral (crossing of baseT and function)
lower_integral_bound <- asin((baseT - temp_meanT_firsthalf) / temp_amplitude_firsthalf)
# Get GDD from first halfday
GDD_firsthalf <- integrate(temp_integrand_firsthalf, lower = lower_integral_bound, upper = (pi/2))$value - (baseT * ((pi/2) - lower_integral_bound))
rm(temp_meanT_firsthalf, temp_amplitude_firsthalf, lower_integral_bound)
}
# c) Tmax > heat threshold
} else {
temp_meanT_firsthalf <- (temp_Tmax_dayd + temp_Tmin_dayd) / 2
temp_amplitude_firsthalf <- (temp_Tmax_dayd - temp_Tmin_dayd) / 2
temp_integrand_firsthalf <- function(x){temp_meanT_firsthalf + temp_amplitude_firsthalf * sin(x)}
# HDD
lower_integral_bound_HDD <- asin((heat_threshold - temp_meanT_firsthalf) / temp_amplitude_firsthalf)
HDD_firsthalf <- integrate(temp_integrand_firsthalf, lower = lower_integral_bound_HDD, upper = (pi/2))$value - (heat_threshold * ((pi/2) - lower_integral_bound_HDD))
# c.1) HDD + Tmin > baseT
if (temp_Tmin_dayd > baseT){
GDD_firsthalf <- integrate(temp_integrand_firsthalf, lower = (-pi/2), upper = lower_integral_bound_HDD)$value - (baseT * (lower_integral_bound_HDD - (-pi/2)))
rm(temp_meanT_firsthalf, temp_amplitude_firsthalf,temp_integrand_firsthalf, lower_integral_bound_HDD)
} else{
# c.2) Tmin < baseT
lower_integral_bound <- asin((baseT - temp_meanT_firsthalf) / temp_amplitude_firsthalf)
GDD_firsthalf <- integrate(temp_integrand_firsthalf, lower = lower_integral_bound, upper = lower_integral_bound_HDD)$value - (baseT * (lower_integral_bound_HDD - lower_integral_bound))
rm(temp_meanT_firsthalf, temp_amplitude_firsthalf,temp_integrand_firsthalf, lower_integral_bound_HDD, lower_integral_bound)
}
}
# -------------------------------------------------------------
# HDD & GDD for second half day (from Tmax_dayd to Tmin_nextday)
# -------------------------------------------------------------
GDD_secondhalf <- NA
HDD_secondhalf <- NA
# a) Tmax < baseT
if (temp_Tmax_dayd < baseT){
GDD_secondhalf <- 0
HDD_secondhalf <- 0
# b) Tmax < heat threshold
} else if (temp_Tmax_dayd < heat_threshold){
HDD_secondhalf <- 0
# b.1) temp_Tmin_nextday > baseT
if (temp_Tmin_nextday > baseT){
temp_meanT_secondhalf <- (temp_Tmax_dayd + temp_Tmin_nextday) / 2
temp_amplitude_secondhalf <- (temp_Tmax_dayd - temp_Tmin_nextday) / 2
temp_integrand_secondhalf <- function(x){temp_meanT_secondhalf + temp_amplitude_secondhalf * sin(x)}
GDD_secondhalf <- integrate(temp_integrand_secondhalf, lower = (pi/2), upper = (3*pi/2))$value - baseT * ((3*pi/2) - (pi/2))
rm(temp_meanT_secondhalf, temp_amplitude_secondhalf, temp_integrand_secondhalf)
# b.2) temp_Tmin_nextday < baseT
} else {
temp_meanT_secondhalf <- (temp_Tmax_dayd + temp_Tmin_nextday) / 2
temp_amplitude_secondhalf <- (temp_Tmax_dayd - temp_Tmin_nextday) / 2
temp_integrand_secondhalf <- function(x){temp_meanT_secondhalf + temp_amplitude_secondhalf * sin(x)}
temp1 <- asin((baseT - temp_meanT_secondhalf) / temp_amplitude_secondhalf)
# temp1 shows the first intersection of baseT and sine curve. We need the second one.
# We get the second intersection by adding the absolute time difference between midday and first intersection to midday (pi/2)
upper_integral_bound <- (pi/2) + abs((pi/2) - temp1)
GDD_secondhalf <- integrate(temp_integrand_secondhalf, lower = pi/2, upper = (upper_integral_bound))$value - baseT * ((upper_integral_bound) - (pi/2))
rm (temp_integrand_secondhalf, upper_integral_bound, temp1, temp_amplitude_secondhalf, temp_meanT_secondhalf)
}
# c) Tmax > heat threshold
} else {
temp_meanT_secondhalf <- (temp_Tmax_dayd + temp_Tmin_nextday) / 2
temp_amplitude_secondhalf <- (temp_Tmax_dayd - temp_Tmin_nextday) / 2
temp_integrand_secondhalf <- function(x){temp_meanT_secondhalf + temp_amplitude_secondhalf * sin(x)}
# HDD
temp1 <- asin((heat_threshold - temp_meanT_secondhalf) / temp_amplitude_secondhalf)
upper_integral_bound_HDD <- (pi/2) + abs((pi/2) - temp1)
HDD_secondhalf <- integrate(temp_integrand_secondhalf, lower = (pi/2), upper = (upper_integral_bound_HDD))$value - (heat_threshold * (upper_integral_bound_HDD-(pi/2)))
rm(temp1)
# c.1) HDD + Tmin > baseT
if (temp_Tmin_nextday > baseT){
GDD_secondhalf <- integrate(temp_integrand_secondhalf, lower = (upper_integral_bound_HDD), upper = (3*pi/2))$value - (baseT * ((3*pi/2) - (upper_integral_bound_HDD)))
rm(temp_meanT_secondhalf, temp_amplitude_secondhalf,temp_integrand_secondhalf, upper_integral_bound_HDD)
# c.2) HDD + Tmin < baseT
}else {
temp1 <- asin((baseT - temp_meanT_secondhalf) / temp_amplitude_secondhalf)
upper_integral_bound <- (pi/2) + abs((pi/2) - temp1)
GDD_secondhalf <- integrate(temp_integrand_secondhalf, lower = (upper_integral_bound_HDD), upper = upper_integral_bound)$value - (baseT * (upper_integral_bound - upper_integral_bound_HDD))
rm(temp1)
}
}
temp_daily_load_GDD[l] <- GDD_firsthalf + GDD_secondhalf
temp_daily_load_HDD[l] <- HDD_firsthalf + HDD_secondhalf
rm(GDD_firsthalf, GDD_secondhalf,HDD_firsthalf, HDD_secondhalf, temp_Tmax_dayd, temp_Tmin_dayd, temp_Tmin_nextday)
}
GDD_array_cumulative_GDD_HDD_main[d,i,t] <- sum(temp_daily_load_GDD)
HDD_array_cumulative_GDD_HDD_main[d,i,t] <- sum(temp_daily_load_HDD)
rm(temp_daily_load_GDD, temp_daily_load_HDD,temp_Tmax, temp_Tmin)
}
}
}
}
rm(d,i,t,l)
GDD_array_cumulative_GDD_HDD_control <- array(dim=c(number_days_prior_harvest,length(unique(downgrading_data$site)),length(seq(2008,2019,1))))
dimnames(GDD_array_cumulative_GDD_HDD_control)[[1]] <- seq(1,number_days_prior_harvest,1)
dimnames(GDD_array_cumulative_GDD_HDD_control)[[2]] <- unique(downgrading_data$site)
dimnames(GDD_array_cumulative_GDD_HDD_control)[[3]] <- seq(2008,2019,1)
# Array for heat degree-days (HDD) with temperature exposure above the heat threshold
HDD_array_cumulative_GDD_HDD_control <- array(dim=c(number_days_prior_harvest,length(unique(downgrading_data$site)),length(seq(2008,2019,1))))
dimnames(HDD_array_cumulative_GDD_HDD_control)[[1]] <- seq(1,number_days_prior_harvest,1)
dimnames(HDD_array_cumulative_GDD_HDD_control)[[2]] <- unique(downgrading_data$site)
dimnames(HDD_array_cumulative_GDD_HDD_control)[[3]] <- seq(2008,2019,1)
for (d in 1:number_days_prior_harvest){
for (i in 1:length(unique(downgrading_data$site))){
for (t in 1: length(seq(2008,2019,1))){
if(is.na(data_harvest[which(data_harvest$site == dimnames(GDD_array_cumulative_GDD_HDD_control)[[2]][i]),as.character(seq(2008,2019,1)[t])])){
GDD_array_cumulative_GDD_HDD_control[d,i,t] <- NA
} else{
# Get the daily min and max temperature data
temp_start <- as.character(data_sowing[which(data_sowing$site == dimnames(GDD_array_cumulative_GDD_HDD_control)[[2]][i]),as.character(seq(2007,2018,1)[t])])
temp_end <- as.character(as.Date(data_harvest[which(data_harvest$site == dimnames(GDD_array_cumulative_GDD_HDD_control)[[2]][i]),as.character(seq(2008,2019,1)[t])])-(d))
temp_Tmax <- Tmax_data[which(row.names(Tmax_data) == dimnames(GDD_array_cumulative_GDD_HDD_control)[[2]][i]),which(colnames(Tmax_data) == temp_start): which(colnames(Tmax_data) == temp_end)]
temp_Tmin <- Tmin_data[which(row.names(Tmin_data) == dimnames(GDD_array_cumulative_GDD_HDD_control)[[2]][i]),which(colnames(Tmin_data) == temp_start): which(colnames(Tmin_data) == temp_end)]
# Daily temperature load for 5-30?C
temp_daily_load_GDD <- vector(length=length(temp_Tmax)-1)
# Daily temperature load for > 30?C
temp_daily_load_HDD <- vector(length=length(temp_Tmax)-1)
# a) Tmax < baseT
# b) Tmax < heat threshold
# b.1) Tmin > baseT
# b.2) Tmin < baseT
# c) Tmax > heat threshold
# c.1) HDD + Tmin > baseT
# c.2) HDD + Tmin < baseT
for (l in 1:length(temp_daily_load_GDD)){
# Get the values to calculate daily temperature temperature curves
temp_Tmax_dayd <- temp_Tmax[l]
temp_Tmin_dayd <- temp_Tmin[l]
temp_Tmin_nextday <- temp_Tmin[l+1]
# ----------------------------------------------------------
# HDD & GDD for first half day (from Tmin_dayd to Tmax_dayd)
# ----------------------------------------------------------
GDD_firsthalf <- NA
HDD_firsthalf <- NA
# a) Tmax < baseT
if (temp_Tmax_dayd < baseT){
GDD_firsthalf <- 0
HDD_firsthalf <- 0
# b) Tmax < heat threshold
} else if (temp_Tmax_dayd < heat_threshold){
HDD_firsthalf <- 0
# b.1) Tmin > baseT
if (temp_Tmin_dayd > baseT){
temp_meanT_firsthalf <- (temp_Tmax_dayd + temp_Tmin_dayd) / 2
temp_amplitude_firsthalf <- (temp_Tmax_dayd - temp_Tmin_dayd) / 2
temp_integrand_firsthalf <- function(x){temp_meanT_firsthalf + temp_amplitude_firsthalf * sin(x)}
GDD_firsthalf <- integrate(temp_integrand_firsthalf, lower = (-pi/2), upper = (pi/2))$value - baseT * ((pi/2) - (-pi/2))
rm(temp_meanT_firsthalf, temp_amplitude_firsthalf, temp_integrand_firsthalf)
} else {
# b.2) Tmin < baseT
temp_meanT_firsthalf <- (temp_Tmax_dayd + temp_Tmin_dayd) / 2
temp_amplitude_firsthalf <- (temp_Tmax_dayd - temp_Tmin_dayd) / 2
temp_integrand_firsthalf <- function(x){temp_meanT_firsthalf + temp_amplitude_firsthalf * sin(x)}
# Get lower bound of integral (crossing of baseT and function)
lower_integral_bound <- asin((baseT - temp_meanT_firsthalf) / temp_amplitude_firsthalf)
# Get GDD from first halfday
GDD_firsthalf <- integrate(temp_integrand_firsthalf, lower = lower_integral_bound, upper = (pi/2))$value - (baseT * ((pi/2) - lower_integral_bound))
rm(temp_meanT_firsthalf, temp_amplitude_firsthalf, lower_integral_bound)
}
# c) Tmax > heat threshold
} else {
temp_meanT_firsthalf <- (temp_Tmax_dayd + temp_Tmin_dayd) / 2
temp_amplitude_firsthalf <- (temp_Tmax_dayd - temp_Tmin_dayd) / 2
temp_integrand_firsthalf <- function(x){temp_meanT_firsthalf + temp_amplitude_firsthalf * sin(x)}
# HDD
lower_integral_bound_HDD <- asin((heat_threshold - temp_meanT_firsthalf) / temp_amplitude_firsthalf)
HDD_firsthalf <- integrate(temp_integrand_firsthalf, lower = lower_integral_bound_HDD, upper = (pi/2))$value - (heat_threshold * ((pi/2) - lower_integral_bound_HDD))
# c.1) HDD + Tmin > baseT
if (temp_Tmin_dayd > baseT){
GDD_firsthalf <- integrate(temp_integrand_firsthalf, lower = (-pi/2), upper = lower_integral_bound_HDD)$value - (baseT * (lower_integral_bound_HDD - (-pi/2)))
rm(temp_meanT_firsthalf, temp_amplitude_firsthalf,temp_integrand_firsthalf, lower_integral_bound_HDD)
} else{
# c.2) Tmin < baseT
lower_integral_bound <- asin((baseT - temp_meanT_firsthalf) / temp_amplitude_firsthalf)
GDD_firsthalf <- integrate(temp_integrand_firsthalf, lower = lower_integral_bound, upper = lower_integral_bound_HDD)$value - (baseT * (lower_integral_bound_HDD - lower_integral_bound))
rm(temp_meanT_firsthalf, temp_amplitude_firsthalf,temp_integrand_firsthalf, lower_integral_bound_HDD, lower_integral_bound)
}
}
# -------------------------------------------------------------
# HDD & GDD for second half day (from Tmax_dayd to Tmin_nextday)
# -------------------------------------------------------------
GDD_secondhalf <- NA
HDD_secondhalf <- NA
# a) Tmax < baseT
if (temp_Tmax_dayd < baseT){
GDD_secondhalf <- 0
HDD_secondhalf <- 0
# b) Tmax < heat threshold
} else if (temp_Tmax_dayd < heat_threshold){
HDD_secondhalf <- 0
# b.1) temp_Tmin_nextday > baseT
if (temp_Tmin_nextday > baseT){
temp_meanT_secondhalf <- (temp_Tmax_dayd + temp_Tmin_nextday) / 2
temp_amplitude_secondhalf <- (temp_Tmax_dayd - temp_Tmin_nextday) / 2
temp_integrand_secondhalf <- function(x){temp_meanT_secondhalf + temp_amplitude_secondhalf * sin(x)}
GDD_secondhalf <- integrate(temp_integrand_secondhalf, lower = (pi/2), upper = (3*pi/2))$value - baseT * ((3*pi/2) - (pi/2))
rm(temp_meanT_secondhalf, temp_amplitude_secondhalf, temp_integrand_secondhalf)
# b.2) temp_Tmin_nextday < baseT
} else {
temp_meanT_secondhalf <- (temp_Tmax_dayd + temp_Tmin_nextday) / 2
temp_amplitude_secondhalf <- (temp_Tmax_dayd - temp_Tmin_nextday) / 2
temp_integrand_secondhalf <- function(x){temp_meanT_secondhalf + temp_amplitude_secondhalf * sin(x)}
temp1 <- asin((baseT - temp_meanT_secondhalf) / temp_amplitude_secondhalf)
# temp1 shows the first intersection of baseT and sine curve. We need the second one.
# We get the second intersection by adding the absolute time difference between midday and first intersection to midday (pi/2)
upper_integral_bound <- (pi/2) + abs((pi/2) - temp1)
GDD_secondhalf <- integrate(temp_integrand_secondhalf, lower = pi/2, upper = (upper_integral_bound))$value - baseT * ((upper_integral_bound) - (pi/2))
rm (temp_integrand_secondhalf, upper_integral_bound, temp1, temp_amplitude_secondhalf, temp_meanT_secondhalf)
}
# c) Tmax > heat threshold
} else {
temp_meanT_secondhalf <- (temp_Tmax_dayd + temp_Tmin_nextday) / 2
temp_amplitude_secondhalf <- (temp_Tmax_dayd - temp_Tmin_nextday) / 2
temp_integrand_secondhalf <- function(x){temp_meanT_secondhalf + temp_amplitude_secondhalf * sin(x)}
# HDD
temp1 <- asin((heat_threshold - temp_meanT_secondhalf) / temp_amplitude_secondhalf)
upper_integral_bound_HDD <- (pi/2) + abs((pi/2) - temp1)
HDD_secondhalf <- integrate(temp_integrand_secondhalf, lower = (pi/2), upper = (upper_integral_bound_HDD))$value - (heat_threshold * (upper_integral_bound_HDD-(pi/2)))
rm(temp1)
# c.1) HDD + Tmin > baseT
if (temp_Tmin_nextday > baseT){
GDD_secondhalf <- integrate(temp_integrand_secondhalf, lower = (upper_integral_bound_HDD), upper = (3*pi/2))$value - (baseT * ((3*pi/2) - (upper_integral_bound_HDD)))
rm(temp_meanT_secondhalf, temp_amplitude_secondhalf,temp_integrand_secondhalf, upper_integral_bound_HDD)
# c.2) HDD + Tmin < baseT
}else {
temp1 <- asin((baseT - temp_meanT_secondhalf) / temp_amplitude_secondhalf)
upper_integral_bound <- (pi/2) + abs((pi/2) - temp1)
GDD_secondhalf <- integrate(temp_integrand_secondhalf, lower = (upper_integral_bound_HDD), upper = upper_integral_bound)$value - (baseT * (upper_integral_bound - upper_integral_bound_HDD))
rm(temp1)
}
}
temp_daily_load_GDD[l] <- GDD_firsthalf + GDD_secondhalf
temp_daily_load_HDD[l] <- HDD_firsthalf + HDD_secondhalf
rm(GDD_firsthalf, GDD_secondhalf,HDD_firsthalf, HDD_secondhalf, temp_Tmax_dayd, temp_Tmin_dayd, temp_Tmin_nextday)
}
GDD_array_cumulative_GDD_HDD_control[d,i,t] <- sum(temp_daily_load_GDD)
HDD_array_cumulative_GDD_HDD_control[d,i,t] <- sum(temp_daily_load_HDD)
rm(temp_daily_load_GDD, temp_daily_load_HDD,temp_Tmax, temp_Tmin, temp_start,temp_end)
} # close else loop
}
}
}
list_GDD_array_cumulative_GDD_HDD_main[[h]] <- GDD_array_cumulative_GDD_HDD_main
list_HDD_array_cumulative_GDD_HDD_main[[h]] <- HDD_array_cumulative_GDD_HDD_main
list_GDD_array_cumulative_GDD_HDD_control[[h]] <- GDD_array_cumulative_GDD_HDD_control
list_HDD_array_cumulative_GDD_HDD_control[[h]] <- HDD_array_cumulative_GDD_HDD_control
rm(GDD_array_cumulative_GDD_HDD_main, HDD_array_cumulative_GDD_HDD_main,GDD_array_cumulative_GDD_HDD_control, HDD_array_cumulative_GDD_HDD_control)
print(h / length(heat_threshold_range))
}
rm(h,i,l,d,t,Tmax_data, Tmin_data)
rm(data_harvest, data_sowing, heat_threshold)
# =================================================================================================
# -------------------------------------------------------------------------------------------------
# 3) Core model: Precipitation effects
# -------------------------------------------------------------------------------------------------
# =================================================================================================
# We use region x year clusters to account for spatial and temporal autocorrelation
west <- c("Changins","Moudon","Grangeneuve","Zollikofen","Courtemelon")
east <- c("Riedholz","Liebegg","Neuhausen","Lindau","Salenstein")
downgrading_data$cluster_region <- NA
for(i in 1:nrow(downgrading_data)){
if (downgrading_data[i,"site"] %in% west){
downgrading_data[i,"cluster_region"] <- "west"
} else if (downgrading_data[i,"site"] %in% east){
downgrading_data[i,"cluster_region"] <- "east"
} else ( downgrading_data[i,"cluster_region"] <- NA)
}
downgrading_data$cluster_regionXyear <- paste(downgrading_data$cluster_region,"X",downgrading_data$year, sep="")
downgrading_data$cluster_siteXyear <- paste(downgrading_data$site,"X",downgrading_data$year, sep="")
# -----------------------------
# 3.1 Core model
# -----------------------------
# Precipitation, GDD, HDD in period 1 & 2.
# Location and year FE
RSS_reg_core <- matrix(NA, nrow = number_days_prior_harvest, ncol = length(heat_threshold_range))
colnames( RSS_reg_core) <- heat_threshold_range
reg_input_core <- downgrading_data[,c("year","site","variety","downgrading","class","cluster_regionXyear","cluster_siteXyear")]
# First, grid search for parameters
for (h in 1:length(heat_threshold_range)){
for (d in 1:number_days_prior_harvest){
reg_input_core$precip_main <- NA
reg_input_core$precip_control <- NA
reg_input_core$GDD_main <- NA
reg_input_core$HDD_main <- NA
reg_input_core$GDD_control <- NA
reg_input_core$HDD_control <- NA
for(i in 1:nrow(reg_input_core)){
reg_input_core[i,"precip_main"] <-array_cumulative_precipitation_main[d, which(dimnames(array_cumulative_precipitation_main)[[2]] == reg_input_core[i,"site"]), which(dimnames(array_cumulative_precipitation_main)[[3]] == reg_input_core[i,"year"])]
reg_input_core[i,"precip_control"] <- array_cumulative_precipitation_control[d, which(dimnames(array_cumulative_precipitation_control)[[2]] == reg_input_core[i,"site"]), which(dimnames(array_cumulative_precipitation_control)[[3]] == reg_input_core[i,"year"])]
reg_input_core[i,"GDD_main"] <- list_GDD_array_cumulative_GDD_HDD_main[[h]] [d, which(dimnames(list_GDD_array_cumulative_GDD_HDD_main[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_GDD_array_cumulative_GDD_HDD_main[[h]])[[3]] == reg_input_core[i,"year"])]
reg_input_core[i,"HDD_main"] <- list_HDD_array_cumulative_GDD_HDD_main[[h]] [d, which(dimnames(list_HDD_array_cumulative_GDD_HDD_main[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_HDD_array_cumulative_GDD_HDD_main[[h]])[[3]] == reg_input_core[i,"year"])]
reg_input_core[i,"GDD_control"] <- list_GDD_array_cumulative_GDD_HDD_control[[h]][d, which(dimnames(list_GDD_array_cumulative_GDD_HDD_control[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_GDD_array_cumulative_GDD_HDD_control[[h]])[[3]] == reg_input_core[i,"year"])]
reg_input_core[i,"HDD_control"] <-list_HDD_array_cumulative_GDD_HDD_control[[h]][d, which(dimnames(list_HDD_array_cumulative_GDD_HDD_control[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_HDD_array_cumulative_GDD_HDD_control[[h]])[[3]] == reg_input_core[i,"year"])]
}
temp <- feols(downgrading ~ precip_main + GDD_main + HDD_main + precip_control + GDD_control + HDD_control| variety+site, cluster = ~ cluster_regionXyear, data = reg_input_core)
RSS_reg_core[d,h] <- sum(resid(temp)^2)
rm(temp)
}
print(h/length(heat_threshold_range))
}
which(RSS_reg_core == min(RSS_reg_core), arr.ind = TRUE)
rm(h,d,i)
# Heat threshold (h) and number of days prior to harvest (d) maximizing goodness of fit
h = which(RSS_reg_core == min(RSS_reg_core), arr.ind = TRUE)[2]
d = which(RSS_reg_core == min(RSS_reg_core), arr.ind = TRUE)[1]
rm(RSS_reg_core)
# Second, re-estimation of model with largest goodness of fit
reg_input_core$precip_main <- NA
reg_input_core$precip_control <- NA
reg_input_core$GDD_main <- NA
reg_input_core$HDD_main <- NA
reg_input_core$GDD_control <- NA
reg_input_core$HDD_control <- NA
for (i in 1:nrow(reg_input_core)){
reg_input_core[i,"precip_main"] <-array_cumulative_precipitation_main[d, which(dimnames(array_cumulative_precipitation_main)[[2]] == reg_input_core[i,"site"]), which(dimnames(array_cumulative_precipitation_main)[[3]] == reg_input_core[i,"year"])]
reg_input_core[i,"precip_control"] <- array_cumulative_precipitation_control[d, which(dimnames(array_cumulative_precipitation_control)[[2]] == reg_input_core[i,"site"]), which(dimnames(array_cumulative_precipitation_control)[[3]] == reg_input_core[i,"year"])]
reg_input_core[i,"GDD_main"] <- list_GDD_array_cumulative_GDD_HDD_main[[h]] [d, which(dimnames(list_GDD_array_cumulative_GDD_HDD_main[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_GDD_array_cumulative_GDD_HDD_main[[h]])[[3]] == reg_input_core[i,"year"])]
reg_input_core[i,"HDD_main"] <- list_HDD_array_cumulative_GDD_HDD_main[[h]] [d, which(dimnames(list_HDD_array_cumulative_GDD_HDD_main[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_HDD_array_cumulative_GDD_HDD_main[[h]])[[3]] == reg_input_core[i,"year"])]
reg_input_core[i,"GDD_control"] <- list_GDD_array_cumulative_GDD_HDD_control[[h]][d, which(dimnames(list_GDD_array_cumulative_GDD_HDD_control[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_GDD_array_cumulative_GDD_HDD_control[[h]])[[3]] == reg_input_core[i,"year"])]
reg_input_core[i,"HDD_control"] <-list_HDD_array_cumulative_GDD_HDD_control[[h]][d, which(dimnames(list_HDD_array_cumulative_GDD_HDD_control[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_HDD_array_cumulative_GDD_HDD_control[[h]])[[3]] == reg_input_core[i,"year"])]
}
core_model <- feols(downgrading ~ precip_main + HDD_main + GDD_main + precip_control + GDD_control + HDD_control| variety+site, cluster = ~ cluster_regionXyear, data = reg_input_core)
sum(resid(core_model)^2)
AIC(core_model)
BIC(core_model)
core_model_cluster_locXyear <- feols(downgrading ~ precip_main + HDD_main + GDD_main + precip_control + GDD_control + HDD_control| variety+site, cluster = ~ cluster_siteXyear, data = reg_input_core)
core_model_cluster_year <- feols(downgrading ~ precip_main + HDD_main + GDD_main + precip_control + GDD_control + HDD_control| variety+site, cluster = ~ year, data = reg_input_core)
core_model_cluster_none <- feols(downgrading ~ precip_main + HDD_main + GDD_main + precip_control + GDD_control + HDD_control| variety+site, data = reg_input_core)
# -----------------------------
# 3.2 Additional core models
# -----------------------------
# 3.2.1 No temperature control
# 3.2.2 No precipitation in period 2
# 3.2.3 No control variables
# ----------------------------------
# 3.2.1 No precipitation in period 2
# ----------------------------------
RSS_reg_core_M2 <- matrix(NA, nrow = number_days_prior_harvest, ncol = length(heat_threshold_range))
colnames(RSS_reg_core_M2) <- heat_threshold_range
reg_input_core_M2 <- downgrading_data[,c("year","site","variety","downgrading","class","cluster_regionXyear")]
# First, grid search for parameters
for (h in 1:length(heat_threshold_range)){
for (d in 1:number_days_prior_harvest){
reg_input_core_M2$precip_main <- NA
reg_input_core_M2$GDD_main <- NA
reg_input_core_M2$HDD_main <- NA
reg_input_core_M2$GDD_control <- NA
reg_input_core_M2$HDD_control <- NA
for(i in 1:nrow(reg_input_core_M2)){
reg_input_core_M2[i,"precip_main"] <-array_cumulative_precipitation_main[d, which(dimnames(array_cumulative_precipitation_main)[[2]] == reg_input_core_M2[i,"site"]), which(dimnames(array_cumulative_precipitation_main)[[3]] == reg_input_core_M2[i,"year"])]
reg_input_core_M2[i,"GDD_main"] <- list_GDD_array_cumulative_GDD_HDD_main[[h]] [d, which(dimnames(list_GDD_array_cumulative_GDD_HDD_main[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_GDD_array_cumulative_GDD_HDD_main[[h]])[[3]] == reg_input_core[i,"year"])]
reg_input_core_M2[i,"HDD_main"] <- list_HDD_array_cumulative_GDD_HDD_main[[h]] [d, which(dimnames(list_HDD_array_cumulative_GDD_HDD_main[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_HDD_array_cumulative_GDD_HDD_main[[h]])[[3]] == reg_input_core[i,"year"])]
reg_input_core_M2[i,"GDD_control"] <- list_GDD_array_cumulative_GDD_HDD_control[[h]][d, which(dimnames(list_GDD_array_cumulative_GDD_HDD_control[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_GDD_array_cumulative_GDD_HDD_control[[h]])[[3]] == reg_input_core[i,"year"])]
reg_input_core_M2[i,"HDD_control"] <-list_HDD_array_cumulative_GDD_HDD_control[[h]][d, which(dimnames(list_HDD_array_cumulative_GDD_HDD_control[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_HDD_array_cumulative_GDD_HDD_control[[h]])[[3]] == reg_input_core[i,"year"])]
}
temp <- feols(downgrading ~ precip_main + HDD_main + GDD_main + GDD_control + HDD_control | variety+site, cluster = ~ cluster_regionXyear, data = reg_input_core_M2)
RSS_reg_core_M2[d,h] <- sum(resid(temp)^2)
rm(temp)
}
print(h/length(heat_threshold_range))
}
rm(h,d,i)
# Heat threshold (h) and number of days prior to harvest (d) maximizing goodness of fit
h = which(RSS_reg_core_M2 == min(RSS_reg_core_M2), arr.ind = TRUE)[2]
d = which(RSS_reg_core_M2 == min(RSS_reg_core_M2), arr.ind = TRUE)[1]
rm(RSS_reg_core_M2)
# Second, re-estimation of model with largest goodness of fit
reg_input_core_M2$precip_main <- NA
reg_input_core_M2$GDD_main <- NA
reg_input_core_M2$HDD_main <- NA
reg_input_core_M2$GDD_control <- NA
reg_input_core_M2$HDD_control <- NA
for (i in 1:nrow(reg_input_core_M2)){
reg_input_core_M2[i,"precip_main"] <-array_cumulative_precipitation_main[d, which(dimnames(array_cumulative_precipitation_main)[[2]] == reg_input_core_M2[i,"site"]), which(dimnames(array_cumulative_precipitation_main)[[3]] == reg_input_core_M2[i,"year"])]
reg_input_core_M2[i,"GDD_main"] <- list_GDD_array_cumulative_GDD_HDD_main[[h]] [d, which(dimnames(list_GDD_array_cumulative_GDD_HDD_main[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_GDD_array_cumulative_GDD_HDD_main[[h]])[[3]] == reg_input_core[i,"year"])]
reg_input_core_M2[i,"HDD_main"] <- list_HDD_array_cumulative_GDD_HDD_main[[h]] [d, which(dimnames(list_HDD_array_cumulative_GDD_HDD_main[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_HDD_array_cumulative_GDD_HDD_main[[h]])[[3]] == reg_input_core[i,"year"])]
reg_input_core_M2[i,"GDD_control"] <- list_GDD_array_cumulative_GDD_HDD_control[[h]][d, which(dimnames(list_GDD_array_cumulative_GDD_HDD_control[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_GDD_array_cumulative_GDD_HDD_control[[h]])[[3]] == reg_input_core[i,"year"])]
reg_input_core_M2[i,"HDD_control"] <-list_HDD_array_cumulative_GDD_HDD_control[[h]][d, which(dimnames(list_HDD_array_cumulative_GDD_HDD_control[[h]])[[2]] == reg_input_core[i,"site"]), which(dimnames(list_HDD_array_cumulative_GDD_HDD_control[[h]])[[3]] == reg_input_core[i,"year"])]
}
core_model_M2 <- feols(downgrading ~ precip_main + HDD_main + GDD_main + GDD_control + HDD_control| variety+site, cluster = ~ cluster_regionXyear, data = reg_input_core_M2)
sum(resid(core_model_M2)^2)
AIC(core_model_M2)
BIC(core_model_M2)
# -----------------------------
# 3.2.2 No temperature control
# -----------------------------
RSS_reg_core_M3 <- matrix(NA, nrow = number_days_prior_harvest, ncol = length(heat_threshold_range))
colnames( RSS_reg_core_M3) <- heat_threshold_range
reg_input_core_M3 <- downgrading_data[,c("year","site","variety","downgrading","class","cluster_regionXyear")]
# First, grid search for parameters
for (h in 1:length(heat_threshold_range)){
for (d in 1:number_days_prior_harvest){
reg_input_core_M3$precip_main <- NA
reg_input_core_M3$precip_control <- NA
for(i in 1:nrow(reg_input_core_M3)){
reg_input_core_M3[i,"precip_main"] <-array_cumulative_precipitation_main[d, which(dimnames(array_cumulative_precipitation_main)[[2]] == reg_input_core_M3[i,"site"]), which(dimnames(array_cumulative_precipitation_main)[[3]] == reg_input_core_M3[i,"year"])]
reg_input_core_M3[i,"precip_control"] <- array_cumulative_precipitation_control[d, which(dimnames(array_cumulative_precipitation_control)[[2]] == reg_input_core_M3[i,"site"]), which(dimnames(array_cumulative_precipitation_control)[[3]] == reg_input_core_M3[i,"year"])]
}
temp <- feols(downgrading ~ precip_main + precip_control | variety+site, cluster = ~ cluster_regionXyear, data = reg_input_core_M3)
RSS_reg_core_M3[d,h] <- sum(resid(temp)^2)
rm(temp)