-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweeds_maize.R
4816 lines (3401 loc) · 178 KB
/
weeds_maize.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
library(dplyr)
library(data.table)
library(ggplot2)
library(raster)
library(sp)
library(sf)
library(rgdal)
library(RVenn)
library(rgbif)
library(devtools)
#install_github("xavi-rp/PreSPickR",
# ref = "v2",
# INSTALL_opts = c("--no-multiarch")) # https://github.com/rstudio/renv/issues/162
library(PreSPickR)
library(rvest)
library(ENMeval)
library(dismo)
library(virtualspecies)
#library(terra)
#sessionInfo()
if(Sys.info()[4] == "D01RI1700308") {
wd <- ""
}else if(Sys.info()[4] == "S-JRCIPRAP320P") {
wd <- ""
}else if(Sys.info()[4] %in% c("jeodpp-terminal-jd001-03",
"jeodpp-terminal-jd002-03",
"jeodpp-terminal-03",
"jeodpp-terminal-dev-12")) {
if(!dir.exists("/eos/jeodpp/home/users/rotllxa/weeds/"))
dir.create("/eos/jeodpp/home/users/rotllxa/weeds/")
wd <- "/eos/jeodpp/home/users/rotllxa/weeds/"
gbif_creds <- "/home/rotllxa/Documents/"
}else{
wd <- ""
gbif_creds <- ""
}
setwd(wd)
## Crop Map ####
list.files("/eos/jeodpp/home/users/rotllxa")
list.files("/eos/jeodpp/home/users/rotllxa/data")
list.files("/storage/rotllxa")
list.files("/storage/rotllxa/Documents")
getwd()
list.files("/home/rotllxa/")
list.files("/home/rotllxa/Documents/")
list.files("/eos/jeodpp/data/base/")
#list.files("/eos/jeodpp/data/base/", recursive = TRUE)
list.files("/mnt/cidstorage/cidportal/data/OpenData/EUCROPMAP/")
list.files("/mnt/cidstorage/cidportal/data/OpenData/EUCROPMAP/2018") ## EUCropMap
list.files("/eos/jeodpp/data/projects/REFOCUS/data/BIODIVERSITY/")
list.files("/eos/jeodpp/data/projects/REFOCUS/data/BIODIVERSITY/Rega") ## Agricultural Management Intensity Map (Rega et al., 2020)
list.files("/eos/jeodpp/data/projects/REFOCUS/data/BIODIVERSITY/DataRestoration")
list.files("/eos/jeodpp/data/projects/REFOCUS/data/BIODIVERSITY/DataRestoration/cropmap_res")
cropmap2018 <- raster("/mnt/cidstorage/cidportal/data/OpenData/EUCROPMAP/2018/EUCROPMAP_2018.tif") # at 10m
#sp:::CRS("+init=EPSG:3035")
cropmap2018
cropmap2018_vals <- getValues(cropmap2018)
sum(cropmap2018_vals == 216, na.rm = TRUE)
cat_coords <- c(3500000, 3800000, 1900000, 2300000) # Catalonia (LAEA, m) (xmin, xmax, ymin, ymax)
cropmap2018_cat <- crop(cropmap2018, extent(cat_coords),
filename = "cropmap2018_cat.tif",
overwrite = TRUE)
plot(cropmap2018_cat)
cropmap2018_cat <- raster("cropmap2018_cat.tif")
fr_coords <- c(3100000, 4300000, 2200000, 3400000) # France (LAEA, m) (xmin, xmax, ymin, ymax)
cropmap2018_fr <- crop(cropmap2018, extent(fr_coords),
filename = "cropmap2018_fr.tif",
overwrite = TRUE)
plot(cropmap2018_fr)
#pixac_v7_byte_masked <- brick("/mnt/cidstorage/cidportal/data/OpenData/EUCROPMAP/2018/pixac_v7_byte_masked.tif")
#pixac_v7_byte_masked
# CropMap classes
#library(readr)
#cropmap_classes_2018 <- read_file("/mnt/cidstorage/cidportal/data/OpenData/EUCROPMAP/2018/EuroCropMap.qml")
crops_categs <- c(100, 211, 212, 213, 214, 215, 216, 217, 218, 219, 221, 222, 223, 230, 231, 232, 233, 240, 250, 290, 300, 500, 600, 800)
crops_names <- c("Artificial", "Common wheat", "Durum wheat", "Barley", "Rye", "Oats", "Maize", "Rice", "Triticale", "Other cereals", "Potatoes", "Sugar beet", "Other root crops", "Other non permanent industrial crops", "Sunflower", "Rape and turnip rape", "Soya", "Dry pulses", "Fodder crops (cereals and leguminous)", "Bare arable land", "Woodland and Shrubland (incl. permanent crops)", "Grasslands", "Bare land", "Wetlands")
cropmap_classes <- data.frame("crop_categ" = crops_categs, "crop_names" = crops_names)
View(cropmap_classes)
# Maiz: 216
### Aggregating maiz to 1km or 10km ####
#cropmap2018_maiz <- cropmap2018
#cropmap2018_maiz[cropmap2018_maiz$EUCROPMAP_2018 != 216] <- 0
aggr_fun_1km <- function(x, ...) { # returns share of maize at 1km (0 to 1)
if (all(is.na(x))){
mz_share <- NA
}else{
mz_share <- sum(x == 216, na.rm = TRUE) / 10000
}
return(mz_share)
}
cropmap2018_maiz_1km <- aggregate(x = cropmap2018,
fact = 100, # 1km
fun = aggr_fun_1km,
expand = TRUE,
na.rm = TRUE,
#filename = "cropmap2018_maiz_1km.tif",
filename = "",
overwrite = TRUE)
cropmap2018_maiz_1km
plot(cropmap2018_maiz_1km)
#sf::st_crs(3035)
wkt <- sf::st_crs(3035)[[2]]
#sp::CRS(wkt)
crs(cropmap2018_maiz_1km) <- sp::CRS(wkt)
writeRaster(cropmap2018_maiz_1km, "cropmap2018_maiz_1km.tif", overwrite = TRUE)
cropmap2018_maiz_1km <- raster("cropmap2018_maiz_1km_cat.tif")
cropmap2018_maiz_1km <- raster("cropmap2018_maiz_1km_fr.tif")
cropmap2018_maiz_1km <- raster("cropmap2018_maiz_1km.tif")
#crs(cropmap2018_maiz_1km) <- sp::CRS(wkt)
cropmap2018_maiz_1km_vals <- getValues(cropmap2018_maiz_1km)
sum(!is.na(cropmap2018_maiz_1km_vals)) # for Cat, 31059 pixels with some maize. For EU 3500020
sum(cropmap2018_maiz_1km_vals > 0, na.rm = TRUE) # over a total of 61533 (not NA). For EU 4285358
(sum(cropmap2018_maiz_1km_vals > 0, na.rm = TRUE) / sum(!is.na(cropmap2018_maiz_1km_vals))) * 100 # Cat: 50.47%; EU: 81.67
cropmap2018_maiz_1km_vals <- cropmap2018_maiz_1km_vals[!is.na(cropmap2018_maiz_1km_vals)]
sum(cropmap2018_maiz_1km_vals > 0)
summary(cropmap2018_maiz_1km_vals[cropmap2018_maiz_1km_vals > 0])
quantile(cropmap2018_maiz_1km_vals[cropmap2018_maiz_1km_vals > 0], seq(0, 1, 0.1))
quantile(cropmap2018_maiz_1km_vals[cropmap2018_maiz_1km_vals > 0], 0.47) # 47% of "maize pixels have less than 1% of maize
quantile(cropmap2018_maiz_1km_vals[cropmap2018_maiz_1km_vals > 0], 0.37) # 37% of "maize pixels have less than 0.5% of maize
quantile(cropmap2018_maiz_1km_vals[cropmap2018_maiz_1km_vals > 0], 0.18) # 18% of "maize pixels have less than 0.1% of maize
## 10km
aggr_fun_10km <- function(x, ...) { # returns share of maize at 10km (0 to 1)
if (all(is.na(x))){
mz_share <- NA
}else{
mz_share <- sum(x == 216, na.rm = TRUE) / 10^6 # 10^6 10-m pixels in a 10-km pixel
}
return(mz_share)
}
t0 <- Sys.time()
cropmap2018_maiz_10km <- aggregate(x = cropmap2018,
fact = 1000, # 10km
fun = aggr_fun_10km, # 10km
expand = TRUE,
na.rm = TRUE,
#filename = "cropmap2018_maiz_10km.tif",
filename = "",
overwrite = TRUE)
Sys.time() - t0
cropmap2018_maiz_10km
writeRaster(cropmap2018_maiz_10km, "cropmap2018_maiz_10km.tif", overwrite = TRUE)
cropmap2018_maiz_10km <- raster("cropmap2018_maiz_10km.tif")
## 16km
aggr_fun_16km <- function(x, ...) { # returns share of maize at 16km (0 to 1)
if (all(is.na(x))){
mz_share <- NA
}else{
mz_share <- sum(x == 216, na.rm = TRUE) / 2560000 # 2560000 (1600*1600) 10-m pixels in a 16-km pixel
}
return(mz_share)
}
t0 <- Sys.time()
cropmap2018_maiz_16km <- aggregate(x = cropmap2018,
fact = 1600, # 16km
fun = aggr_fun_16km, # 16km
expand = TRUE,
na.rm = TRUE,
#filename = "cropmap2018_maiz_16km.tif",
filename = "",
overwrite = TRUE)
Sys.time() - t0
cropmap2018_maiz_16km
writeRaster(cropmap2018_maiz_16km, "cropmap2018_maiz_16km.tif", overwrite = TRUE)
cropmap2018_maiz_16km <- raster("cropmap2018_maiz_16km.tif")
cropmap2018_maiz_16km
### Aggregating Arable and Non-Arable Land to 1km or 10km ####
aggr_NonAL_1km <- function(x, ...) { # returns share of Non-Arable Land at 1km (0 to 1)
if (all(is.na(x))){
nal_share <- NA
}else{
nal_share <- sum(x %in% c(500, 600, 800), na.rm = TRUE) / 10000
}
return(nal_share)
}
aggr_ArabLand_1km <- function(x, ...) { # returns share of Arable Land at 1km (0 to 1)
if (all(is.na(x))){
al_share <- NA
}else{
al_share <- sum(!x %in% c(100, # artificial
300, # Woodland and Shrubland (incl. permanent crops)
500, # Grasslands
600, # Bare land
800 # Wetlands
) &
!is.na(x),
na.rm = TRUE) / 10000
}
return(al_share)
}
aggr_ArabLand_10km <- function(x, ...) { # returns share of Arable Land at 1km (0 to 1)
if (all(is.na(x))){
al_share <- NA
}else{
al_share <- sum(!x %in% c(100, # artificial
300, # Woodland and Shrubland (incl. permanent crops)
500, # Grasslands
600, # Bare land
800 # Wetlands
)&
!is.na(x),
na.rm = TRUE) / 10^6 # 10^6 10-m pixels in a 10-km pixel
}
return(al_share)
}
cropmap2018_arabland_1km <- aggregate(x = cropmap2018,
#x = cropmap2018_cat,
fact = 100, # 1km
fun = aggr_ArabLand_1km,
expand = TRUE,
na.rm = TRUE,
filename = "cropmap2018_ArableLand_1km.tif",
#filename = "",
overwrite = TRUE)
cropmap2018_arabland_1km
plot(cropmap2018_arabland_1km)
#sf::st_crs(3035)
wkt <- sf::st_crs(3035)[[2]]
#sp::CRS(wkt)
crs(cropmap2018_arabland_1km) <- sp::CRS(wkt)
#writeRaster(cropmap2018_arabland_1km, "cropmap2018_NonAL_1km.tif", overwrite = TRUE)
cropmap2018_nal_1km <- raster("cropmap2018_NonAL_1km_cat.tif")
cropmap2018_nal_1km <- raster("cropmap2018_NonAL_1km.tif")
cropmap2018_arabland_1km <- raster("cropmap2018_ArableLand_1km_cat.tif")
#cropmap2018_arabland_1km_kk <- raster("cropmap2018_ArableLand_1km.tif")
#writeRaster(cropmap2018_arabland_1km_kk, "cropmap2018_ArableLand_1km_kk.tif")
cropmap2018_arabland_1km <- raster("cropmap2018_ArableLand_1km.tif")
## Merging (rasters) maize share and arable land share
cropmap2018_maiz_1km$cropmap2018_arabland_1km <- getValues(cropmap2018_arabland_1km)
cropmap2018_maiz_1km
summary(cropmap2018_maiz_1km$cropmap2018_arabland_1km)
cropmap2018_arabland_10km <- aggregate(x = cropmap2018,
fact = 1000, # 10km
fun = aggr_ArabLand_10km,
expand = TRUE,
na.rm = TRUE,
filename = "cropmap2018_ArableLand_10km.tif",
#filename = "",
overwrite = TRUE)
cropmap2018_arabland_10km <- raster("cropmap2018_ArableLand_10km.tif")
## recall cropmap2018_arabland_10km from Raph's data set
library(RPostgreSQL)
pg <- list(host = 'jeodb01.cidsn.jrc.it', port = '54331', # PG server parameters
user = 'refocus_eucropmap_user', pwd = '8gsmuTJUj2xKrHbf',
db = 'refocus_eucropmap_db')
con <- dbDriver("PostgreSQL") %>% # connect
dbConnect(host=pg$host, port=pg$port, user=pg$user, password=pg$pwd, dbname=pg$db)
tmp <- paste0("SELECT * FROM \"cdiv\".","crop_div_10km_geom AS a")
cropmap2018_arabland_10km <- st_read(con, query = tmp)
cropmap2018_arabland_10km
summary(round((cropmap2018_arabland_10km$cropland_sum / 10^6), 3))
plot(cropmap2018_arabland_10km)
cropmap2018_arabland_10km_vals <- round((cropmap2018_arabland_10km$cropland_sum / 10^6), 3)
## Maize weeds ####
weeds_maize <- read.csv("weeds_maize_report_2011.csv", header = TRUE)
head(weeds_maize)
nrow(weeds_maize)
## families of the weeds
fams <- c()
for(sp in weeds_maize$Species){
fam1 <- as.data.frame(rgbif::name_backbone(name = sp))$family
fams <- c(fams, fam1)
}
unique(fams)
length(unique(fams))
sort(unique(fams))
fams
#
occs_all <- fread(paste0(getwd(), "/../exploring_lucas_data/D5_FFGRCC_gbif_occ/sp_records_20210709.csv"), header = TRUE)
if(nchar(occs_all$sp2[1]) == 7) occs_all[, sp2 := gsub(" ", "_", occs_all$species)]
occs_all
cols_order <- c("species", "decimalLatitude", "decimalLongitude", "gbifID", "countryCode", "year", "sp2")
occs_all <- occs_all[, .SD, .SDcols = cols_order]
occs_all <- occs_all[occs_all$species != "", ]
occs_all_2018 <- occs_all[occs_all$year == 2018, ] # 1591221 occs for 2018
nrow(occs_all_2018)
sum(occs_all_2018$species == "")
length(unique(occs_all_2018$species))
occs_2018_specie <- unique(occs_all_2018$species)
head(sort(occs_2018_specie), 50)
head(sort(weeds_maize$Species))
sum(occs_2018_specie %in% weeds_maize$Species) # 151 sp
sum(weeds_maize$Species %in% occs_2018_specie) # 151 sp
sum(!weeds_maize$Species %in% occs_2018_specie) # 53 sp (maize weeds) which we don't have in 2018
weeds_maiz_gbib <- sort(weeds_maize$Species[weeds_maize$Species %in% occs_2018_specie])
weeds_maiz_not_gbib <- sort(weeds_maize$Species[!weeds_maize$Species %in% occs_2018_specie])
occs_all_2018_maiz <- occs_all_2018[occs_all_2018$species %in% weeds_maiz_gbib, ]
nrow(occs_all_2018_maiz) # 158427 occurrences for 2018
nrow(occs_all_2018) # over 1591221 in total for 2018
occs_all_2018_maiz
sum(occs_all_2018_maiz$countryCode == "ES")
sum(occs_all_2018_maiz$countryCode == "FR")
occs_all_2018_maiz_fr <- occs_all_2018_maiz[occs_all_2018_maiz$countryCode == "FR", ]
occs_all_2018_maiz_fr
sort(table(occs_all_2018_maiz_fr$species))
sps_subset <- c("Digitaria sanguinalis", "Erodium cicutarium", "Beta vulgaris", "Phytolacca americana")
#occs_00_indicators <- fread("sp_indicators.csv", header = TRUE)
#sps_subset <- occs_00_indicators$Var1
sort(table(occs_all_2018_maiz_fr[occs_all_2018_maiz_fr$species %in% sps_subset, ]$species))
occs_all_2018_maiz_fr_subset <- occs_all_2018_maiz_fr[occs_all_2018_maiz_fr$species %in% sps_subset, ]
occs_all_2018_maiz_fr_subset
write.csv(occs_all_2018_maiz_fr_subset, "occurrences_weeds_gbif_FR_2018_subset.csv", row.names = FALSE)
setnames(occs_all_2018_maiz, c("decimalLongitude", "decimalLatitude"), c("x", "y"))
occs_all_2018_maiz <- occs_all_2018_maiz[, .SD, .SDcols = c("species", "x", "y", "gbifID", "countryCode", "year")]
occs_all_2018_maiz_sf <- st_as_sf(as.data.frame(occs_all_2018_maiz), coords = c("x", "y"), crs = 4326)#, agr = "constant")
occs_all_2018_maiz_sf
#sf::st_crs(3035)
wkt <- sf::st_crs(3035)[[2]]
#sp::CRS(wkt)
#occs_all_2018_maiz_sf[occs_all_2018_maiz_sf$countryCode == "ES", ]
occs_all_2018_maiz_sf_laea <- st_transform(occs_all_2018_maiz_sf, crs = sp::CRS(wkt))
occs_all_2018_maiz_sf_laea
occs_maizeShare <- as.data.table(extract(cropmap2018_maiz_1km, occs_all_2018_maiz_sf_laea, cellnumbers = TRUE))
occs_maizeShare
## all occurrences (not only maize weeds)
setnames(occs_all_2018, c("decimalLongitude", "decimalLatitude"), c("x", "y"))
occs_all_2018 <- occs_all_2018[, .SD, .SDcols = c("species", "x", "y", "gbifID", "countryCode", "year")]
occs_all_2018_sf <- st_as_sf(as.data.frame(occs_all_2018), coords = c("x", "y"), crs = 4326)#, agr = "constant")
occs_all_2018_sf
occs_all_2018_sf_laea <- st_transform(occs_all_2018_sf, crs = sp::CRS(wkt))
occs_all_2018_sf_laea
occs_all_maizeShare <- as.data.table(extract(cropmap2018_maiz_1km, occs_all_2018_sf_laea, cellnumbers = TRUE))
occs_all_maizeShare
# We keep pixels with some maize and at least one of the weeds !!!
occs_all_2018_maiz_sf_laea_dt <- as.data.table(occs_all_2018_maiz_sf_laea)
occs_maizeShare <- cbind(occs_all_2018_maiz_sf_laea_dt, occs_maizeShare)
occs_maizeShare
occs_maizeShare <- na.omit(occs_maizeShare)
setkeyv(occs_maizeShare, "cells")
occs_maizeShare
# Removing repeated occurrences (same sp) in the same pixel, because we want to work with Species Richness
occs_maizeShare_abundances <- occs_maizeShare
occs_maizeShare <- occs_maizeShare[!duplicated(occs_maizeShare[, c("species", "cells")]), ]
sum(occs_maizeShare$cropmap2018_maiz_1km == 0)
sum(occs_maizeShare$cropmap2018_maiz_1km != 0)
## We keep only pixels with at least 20% of arable land because we want to be focused on
# agricultural areas. (see https://besjournals.onlinelibrary.wiley.com/doi/full/10.1111/j.1365-2664.2005.01072.x
# for a reference for why 30%. But we need 20% to keep Galium tri)
occs_maizeShare <- occs_maizeShare[cropmap2018_arabland_1km >= 0.2, ]
range(occs_maizeShare$cropmap2018_arabland_1km)
## We also remove those pixels with less than 0.2% of maize share to avoid some noise produced by the CropMap
occs_maizeShare <- occs_maizeShare[cropmap2018_maiz_1km >= 0.002, ]
range(occs_maizeShare$cropmap2018_maiz_1km)
occs_maizeShare
length(unique(occs_maizeShare$cells)) # cells with one or more weeds
length(unique(occs_maizeShare[occs_maizeShare$cropmap2018_maiz_1km != 0, cells])) # cells with maize and one or more weeds
summary(occs_maizeShare$cropmap2018_maiz_1km)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 0.0020 0.0214 0.0753 0.1086 0.1639 0.8798
quantile(occs_maizeShare$cropmap2018_maiz_1km, seq(0, 1, 0.1))
quantile(occs_maizeShare$cropmap2018_maiz_1km, 0.45)
occs_maizeShare_kk <- occs_maizeShare[!duplicated(occs_maizeShare$cells), ]
occs_maizeShare_kk
quantile(occs_maizeShare_kk$cropmap2018_maiz_1km, seq(0, 1, 0.1))
quantile(occs_maizeShare_kk$cropmap2018_maiz_1km, 0.46)
# percentiles of shares for those "maize pixels" with one or more weeds
# e.g. around % of "maize pixels" with weeds have a maize share below 1%
# 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
# 0.00200 0.00970 0.02290 0.04370 0.06900 0.09750 0.12926 0.16610 0.20880 0.27970 0.87980
occs_maizeShare_aggr <- as.data.table(table(occs_maizeShare$cells))
occs_maizeShare_aggr <- occs_maizeShare_aggr[, lapply(.SD, as.numeric)]
str(occs_maizeShare_aggr)
occs_all_maizeShare_aggr <- as.data.table(table(occs_all_maizeShare$cells)) # all species
occs_all_maizeShare_aggr <- occs_all_maizeShare_aggr[, lapply(.SD, as.numeric)]
occs_all_maizeShare_aggr
#occs_maizeShare_1 <- occs_maizeShare[, 6:7]
occs_maizeShare_1 <- occs_maizeShare[, 6:8]
occs_maizeShare_1[!duplicated(occs_maizeShare_1$cells), ]
occs_maizeShare_1 <- unique(occs_maizeShare_1, by = "cells")
occs_maizeShare_1 <- merge(occs_maizeShare_1, occs_maizeShare_aggr, by.x = "cells", by.y = "V1", all.x = TRUE)
occs_maizeShare_1
summary(occs_maizeShare_1$cropmap2018_maiz_1km_cat)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
#
summary(occs_maizeShare_1$cropmap2018_fr)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
#
summary(occs_maizeShare_1$cropmap2018_maiz_1km)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 0.0020 0.0318 0.0975 0.1255 0.1868 0.8798
# merging all species
setnames(occs_all_maizeShare_aggr, "N", "N_all")
occs_maizeShare_1 <- merge(occs_maizeShare_1, occs_all_maizeShare_aggr, by.x = "cells", by.y = "V1", all.x = TRUE)
## Assessing correlations ####
png("scatterplot_SpRichness_maize.png",
width = 16, height = 15, units = "cm", res = 150)
plot(y = occs_maizeShare_1$N, # x
#x = occs_maizeShare_1$EUCROPMAP_2018, # y
#x = occs_maizeShare_1$cropmap2018_maiz_1km_cat, # y
x = occs_maizeShare_1$cropmap2018_maiz_1km, # y
main = "",
ylab = "Species richness",
xlab = "Maize share",
pch = 19)
abline(lm(occs_maizeShare_1$N ~ occs_maizeShare_1$cropmap2018_maiz_1km), col = "red") # regression line (y~x)
pears_cor <- cor(occs_maizeShare_1$N, occs_maizeShare_1$cropmap2018_maiz_1km, method = "pearson")
mtext(paste0("Pearson's r = ", round(pears_cor, 3)),
col = "black",
side = 1, line = 3,
adj = 1,
cex = 0.8)
lregr <- summary(lm(occs_maizeShare_1$N ~ occs_maizeShare_1$cropmap2018_maiz_1km))
lregr$adj.r.squared
#mtext(paste0("R-squared = ", round(lregr$r.squared, 3)),
# col = "red",
# side = 1, line = 2,
# adj = 1,
# cex = 0.8)
dev.off()
lregr
coef(lm(occs_maizeShare_1$N ~ occs_maizeShare_1$cropmap2018_maiz_1km))
# sp richness against arable land (to demonstrate that the change in sp richness
# is due to the share of maize, thus intensification, and not to the share of arable)
png("scatterplot_SpRichness_Arable.png",
width = 16, height = 15, units = "cm", res = 150)
plot(y = occs_maizeShare_1$N, # x
x = occs_maizeShare_1$cropmap2018_arabland_1km, # y
main = "",
ylab = "Species richness",
xlab = "Arable land share",
pch = 19)
abline(lm(occs_maizeShare_1$N ~ occs_maizeShare_1$cropmap2018_arabland_1km), col = "red") # regression line (y~x)
pears_cor <- cor(occs_maizeShare_1$N, occs_maizeShare_1$cropmap2018_arabland_1km, method = "pearson")
mtext(paste0("Pearson's r = ", round(pears_cor, 3)),
col = "black",
side = 1, line = 3,
adj = 1,
cex = 0.8)
lregr <- summary(lm(occs_maizeShare_1$N ~ occs_maizeShare_1$cropmap2018_arabland_1km))
lregr$adj.r.squared
#mtext(paste0("R-squared = ", round(lregr$r.squared, 3)),
# col = "red",
# side = 1, line = 2,
# adj = 1,
# cex = 0.8)
dev.off()
## Sp richness aggregated by maize share class
occs_maizeShare_1[, maize_share_class := cut(occs_maizeShare_1$cropmap2018_maiz_1km,
breaks = seq(0, 1, 0.1),
include.lowest = FALSE,
right = TRUE)]
sort(unique(occs_maizeShare_1$maize_share_class))
png("boxplot_SpRichness_maize.png",
width = 20, height = 10, units = "cm", res = 150)
boxplot(occs_maizeShare_1$N ~ occs_maizeShare_1$maize_share_class,
main = "",
xlab = "Maize shares",
ylab = "Species richness")
dev.off()
# rounding Maize shares
occs_maizeShare_2 <- occs_maizeShare_1
occs_maizeShare_2$EUCROPMAP_2018 <- round(occs_maizeShare_2$EUCROPMAP_2018, 1)
occs_maizeShare_2$cropmap2018_maiz_1km <- round(occs_maizeShare_2$cropmap2018_maiz_1km, 1)
plot(y = occs_maizeShare_2$N,
x = occs_maizeShare_2$cropmap2018_maiz_1km,
main = "",
ylab = "Number of occurrences",
xlab = "Maize share (rounded)",
pch = 19)
abline(lm(occs_maizeShare_2$N ~ occs_maizeShare_2$cropmap2018_maiz_1km), col = "red") # regression line (y~x)
dev.off()
# Linear Regression
summary(lm(occs_maizeShare_2$N ~ occs_maizeShare_2$cropmap2018_maiz_1km))
# Pearson Correlation
cor(occs_maizeShare_2$N, occs_maizeShare_2$cropmap2018_maiz_1km, method = "pearson") # Cat: -0.017; FR: -0.032; Eur: -0.1336785
# Outliers
boxplot(occs_maizeShare_1$EUCROPMAP_2018)
boxplot(occs_maizeShare_1$cropmap2018_maiz_1km)
boxplot(occs_maizeShare_1$N)
hist(occs_maizeShare_1$N, xlab = "Number of occurrences")
hist(occs_maizeShare_1$EUCROPMAP_2018)
hist(occs_maizeShare_1$cropmap2018_maiz_1km, xlab = "Maize share")
tail(sort(occs_maizeShare_1$N), 40)
summary(occs_maizeShare_1$N)
quantile(occs_maizeShare_1$N, c(0.95, 0.9772, 0.98, 0.99, 0.995, 0.997, 0.9999))
occs_maizeShare_2 <- occs_maizeShare_1
nrow(occs_maizeShare_1)
occs_maizeShare_2 <- occs_maizeShare_2[occs_maizeShare_2$N <= quantile(occs_maizeShare_2$N, 0.9999), ]
nrow(occs_maizeShare_2)
#pdf("Occs_MaizeShare_Eur_NoOutliers_9999.pdf")
plot(y = occs_maizeShare_2$N,
#x = occs_maizeShare_2$EUCROPMAP_2018,
x = occs_maizeShare_2$cropmap2018_maiz_1km,
main = "",
ylab = "Number of occurrences (<= 99.99th Percentile)",
xlab = "Maize share",
pch = 19)
dev.off()
abline(lm(occs_maizeShare_2$N ~ occs_maizeShare_2$cropmap2018_maiz_1km), col = "red") # regression line (y~x)
pears_cor <- cor(occs_maizeShare_2$N, occs_maizeShare_2$EUCROPMAP_2018, method = "pearson") # Eur: -0.031
pears_cor <- cor(occs_maizeShare_2$N, occs_maizeShare_2$cropmap2018_maiz_1km, method = "pearson") # Eur: -0.023
pears_cor
mtext(paste0("Pearson's r = ", round(pears_cor, 3)),
col = "red",
side = 1, line = 2.5,
adj = 1,
cex = 1)
# Linear Regression
summary(lm(occs_maizeShare_2$N ~ occs_maizeShare_2$cropmap2018_maiz_1km))
boxplot(occs_maizeShare_2$N ~ occs_maizeShare_2$cropmap2018_maiz_1km,
main = "",
xlab = "Maize shares",
ylab = "Number of occurrences")
## kk
occs_maizeShare_3 <- occs_maizeShare_1
nrow(occs_maizeShare_1)
occs_maizeShare_3 <- occs_maizeShare_3[occs_maizeShare_3$N <= quantile(occs_maizeShare_3$N, 0.9999), ]
nrow(occs_maizeShare_3)
#pdf("Occs_MaizeShare_Eur_NoOutliers.pdf")
plot(y = log10(occs_maizeShare_3$N),
x = log10(occs_maizeShare_3$cropmap2018_maiz_1km),
main = "",
#ylab = "Number of occurrences (5 <= N <= 99.99th Percentile)",
ylab = "log - Number of occurrences (<= 99.99th Percentile)",
xlab = "log - Maize share",
pch = 19)
abline(lm(log10(occs_maizeShare_3$N) ~ log10(occs_maizeShare_3$cropmap2018_maiz_1km)), col = "red") # regression line (y~x)
pears_cor <- cor(occs_maizeShare_3$N, occs_maizeShare_3$cropmap2018_maiz_1km, method = "pearson") # Eur: -0.023
pears_cor
mtext(paste0("Pearson's r = ", round(pears_cor, 3)),
col = "red",
side = 1, line = 2.5,
adj = 1,
cex = 1)
dev.off()
## Linear Regression
model1 <- lm(occs_maizeShare_3$N ~ occs_maizeShare_3$cropmap2018_maiz_1km)
coef(model1)
summary(model1)
## Non Linear (exponential) regression
# https://rpubs.com/mengxu/exponential-model
occs_maizeShare_3 <- occs_maizeShare_1
# Select an approximate $\theta$, since theta must be lower than min(y), and greater than zero
theta.0 <- min(occs_maizeShare_3$N) * 0.5
# Estimate the rest parameters using a linear model
model.0 <- lm(log(occs_maizeShare_3$N - theta.0) ~ occs_maizeShare_3$cropmap2018_maiz_1km)
coef(model.0)
alpha.0 <- exp(coef(model.0)[1])
beta.0 <- coef(model.0)[2]
# Starting parameters
start <- list(alpha = alpha.0, beta = beta.0, theta = theta.0)
start
# Fit the model
model <- nls(N ~ alpha * exp(beta * cropmap2018_maiz_1km) + theta, data = occs_maizeShare_3, start = start)
# Plot fitted curve
plot(occs_maizeShare_3$cropmap2018_maiz_1km, occs_maizeShare_3$N)
lines(occs_maizeShare_3$cropmap2018_maiz_1km, predict(model, list(x = occs_maizeShare_3$cropmap2018_maiz_1km)), col = 'skyblue', lwd = 3)
summary(model)
## Polynomial model
model2 <- lm(N ~ cropmap2018_maiz_1km + I(cropmap2018_maiz_1km^2), data = occs_maizeShare_3)
summary(model2) # only the first order of the function is significant... no non-linear relation!
## GAM
library(mgcv)
model <- gam(N ~ s(cropmap2018_maiz_1km), data = occs_maizeShare_3)
summary(model)
## Poisson
hist(occs_maizeShare_1$N)
hist(occs_maizeShare_1$cropmap2018_maiz_1km)
#hist(occs_maizeShare_1$adjusted_richness)
cor(occs_maizeShare_1$N, occs_maizeShare_1$cropmap2018_maiz_1km)
library(vcd)
freqs <- table(occs_maizeShare_1$N)
freqs <- as.data.frame(freqs)
freqs$Var1 <- as.numeric(freqs$Var1)
gf <- goodfit(freqs[, 2:1], "poisson")
plot(gf, type = "standing", scale = "raw")
var(occs_maizeShare_1$N)
mean(occs_maizeShare_1$N)
model <- glm(N ~ cropmap2018_maiz_1km, family = "poisson", data = occs_maizeShare_1)
summary(model)
1 - (model$deviance / model$null.deviance) # McFadden's pseudo-R2 (for lm/glm(family ="gaussian"), it is the one reported as Multiple R-squared)
## Zero-inflated poisson
library(pscl)
model <- zeroinfl(N ~ cropmap2018_maiz_1km, data = occs_maizeShare_1)
sum(occs_maizeShare_1$N == 1)
table(occs_maizeShare_1$N)
# Zeroes have been already removed from the study, as we are focused on "maize areas" where at least one of the weeds is present
# We have a lot of pixels with N = 1, making the distribution very skewed
## Bootstraping for correlation
# It is a way to estimate the distribution of some statistic (mean, standard error, Pearson's correlation coeff, etc),
# given only one sample. So if I want to estimate the mean of a population using bootstrap methods, I generate many
# bootstrap samples, compute the mean of each of these bootstrap samples, and then use the distribution of those values
# to deduce where the unknown population mean is likely to fall and compute a confidence interval for the statistic.
# https://stackoverflow.com/questions/58393608/bootstrapped-correlation-in-r
# https://www.datacamp.com/community/tutorials/bootstrap-r
library(boot)
foo <- function(data, indices = NULL, cor.type){
if(!is.null(indices)){
dt<-data[indices,]
}else{
dt <- data
}
c(cor(dt[,1], dt[,2], method=cor.type))
}
myBootstrap <- boot(occs_maizeShare_1[, .SD, .SDcols = c("cropmap2018_maiz_1km", "N")], foo,
sim = "balanced",
R=1000, cor.type='s')
myBootstrap
head(myBootstrap$t)
mean(myBootstrap$t)
myBootstrap$t0
plot(myBootstrap)
boot.ci(myBootstrap, type=c('basic', "perc"))
boot.ci(myBootstrap, type='norm')$norm
## Assess correlation between N and N_all (i.e. number of weeds vs number of all species -not including weeds)
occs_maizeShare_1$N_all_NoWeeds <- occs_maizeShare_1$N_all - occs_maizeShare_1$N
hist(occs_maizeShare_1$N_all_NoWeeds, xlab = "All sp (no weeds)")
plot(x = occs_maizeShare_1$N_all_NoWeeds,
y = occs_maizeShare_1$N,
main = "",
xlab = "Number of all species (except weeds; occs.)",
ylab = "Number of Weeds (occurrences)",
pch = 19
)
abline(lm(occs_maizeShare_1$N ~ occs_maizeShare_1$N_all_NoWeeds), col = "red") # regression line (y~x)
pears_cor_1 <- cor(occs_maizeShare_1$N, occs_maizeShare_1$N_all_NoWeeds, method = "pearson") # Eur: 0.50 (Not a very strong correlation, but still)
pears_cor_1
summary(lm(occs_maizeShare_1$N ~ occs_maizeShare_1$N_all_NoWeeds)) # significant; R-squared = 0.2556; but no normal variables
# GAM
library(mgcv)
summary(gam(N ~ s(N_all_NoWeeds), data = occs_maizeShare_1)) # R-sq.(adj) = 0.605
summary(gam(N ~ N_all_NoWeeds, data = occs_maizeShare_1)) # R-sq.(adj) = 0.495
# GLM
mdl1 <- glm(occs_maizeShare_1$N ~ occs_maizeShare_1$N_all_NoWeeds, family = "poisson")
summary(mdl1) #
coef(mdl1) # It gives a positive effect
aov(mdl1)
with(summary(mdl1), 1 - deviance/null.deviance) # goodness of fit # 0.18
## Adding number of occurrences of other species (not weeds) as a covariate
# although both variables are quite correlated
mdl <- lm(occs_maizeShare_1$N ~ occs_maizeShare_1$cropmap2018_maiz_1km + occs_maizeShare_1$N_all_NoWeeds)
summary(mdl) # The effect of N_all_NoWeeds is masking the effect of maize share
coef(mdl) # It gives a positive effect
mdl1 <- glm(occs_maizeShare_1$N ~ occs_maizeShare_1$cropmap2018_maiz_1km + occs_maizeShare_1$N_all_NoWeeds, family = "poisson")
summary(mdl1) # The effect of N_all_NoWeeds is masking the effect of maize share
coef(mdl1) # It gives a positive effect
aov(mdl1)
## Assessing the effect of Maize share over the total number of species (weeds + no weeds)
occs_maizeShare_1
plot(x = occs_maizeShare_1$cropmap2018_maiz_1km,
y = occs_maizeShare_1$N_all,
main = "",
xlab = "Maize share",
ylab = "Number of all species (occurrences)",
pch = 19
)
abline(lm(occs_maizeShare_1$N_all ~ occs_maizeShare_1$cropmap2018_maiz_1km), col = "red") # regression line (y~x)
modl <- lm(occs_maizeShare_1$N_all ~ occs_maizeShare_1$cropmap2018_maiz_1km)
summary(modl)
coef(modl)
pears_cor_2 <- cor(occs_maizeShare_1$N_all, occs_maizeShare_1$cropmap2018_maiz_1km, method = "pearson") # Eur: -0.042
pears_cor_2
## Sp Richness vs Maize share over Arable land
occs_maizeShare_1[, cropmap2018_maizArable_1km := round(cropmap2018_maiz_1km / cropmap2018_arabland_1km, 3)]
sum(is.na(occs_maizeShare_1$N))
sum(is.na(occs_maizeShare_1$cropmap2018_maizArable_1km))
sum(is.na(occs_maizeShare_1$cropmap2018_maiz_1km))
sum(is.na(occs_maizeShare_1$cropmap2018_arabland_1km))
occs_maizeShare_1[is.na(occs_maizeShare_1$cropmap2018_maizArable_1km), ]
sum(is.nan(occs_maizeShare_1$cropmap2018_maizArable_1k))
occs_maizeShare_1$cropmap2018_maizArable_1km[is.nan(occs_maizeShare_1$cropmap2018_maizArable_1km)] <- 0
occs_maizeShare_1[occs_maizeShare_1$cells == 14790069, ]
occs_maizeShare_1 <- occs_maizeShare_1[, c(1:4, 6)]
#pdf("scatterplot_SpRichness_maizeArable.pdf")
plot(y = occs_maizeShare_1$N, # x
#x = occs_maizeShare_1$EUCROPMAP_2018, # y
#x = occs_maizeShare_1$cropmap2018_maiz_1km_cat, # y
x = occs_maizeShare_1$cropmap2018_maizArable_1k, # y
main = "",
ylab = "Species richness",
xlab = "Maize share / Arable Land share",
pch = 19)
abline(lm(occs_maizeShare_1$N ~ occs_maizeShare_1$cropmap2018_maizArable_1km), col = "red") # regression line (y~x)
pears_cor <- cor(occs_maizeShare_1$N, occs_maizeShare_1$cropmap2018_maizArable_1km, method = "pearson")
mtext(paste0("Pearson's r = ", round(pears_cor, 3)),
col = "black",
side = 1, line = 3,
adj = 1,
cex = 0.8)
lregr <- summary(lm(occs_maizeShare_1$N ~ occs_maizeShare_1$cropmap2018_maizArable_1km))
lregr$adj.r.squared
mtext(paste0("R-squared = ", round(lregr$r.squared, 3)),
col = "red",
side = 1, line = 2,
adj = 1,
cex = 0.8)
dev.off()
coef(lm(occs_maizeShare_1$N ~ occs_maizeShare_1$cropmap2018_maizArable_1km))
## Downloading Zea mays occurrences ####
GetBIF(credentials = paste0(gbif_creds, "/gbif_credentials.RData"),
taxon_list = "Zea mays",
download_format = "SIMPLE_CSV",
download_years = c(2000, 2021),
download_coords = c(-12.69141, 42.71485, 33.4901, 71.9218), #order: xmin, xmax, ymin, ymax
download_coords_accuracy = c(0, 50),
rm_dupl = TRUE,
cols2keep = c("species", "decimalLatitude", "decimalLongitude", #"elevation",
"gbifID",
"coordinateUncertaintyInMeters",