-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathR_Schreiner_Romania_analytics.Rmd
1763 lines (1393 loc) · 61.5 KB
/
R_Schreiner_Romania_analytics.Rmd
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
---
editor_options:
chunk_output_type: console
output:
html_document:
fig_caption: yes
figure_caption: yes
highlight: tango
number_sections: yes
toc: yes
toc_depth: 4
pdf_document:
toc: yes
toc_depth: '4'
word_document:
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Paradise lost? Pesticide pollution in a European region with considerable amount of traditional agriculture {-}
Verena C. Schreiner, Moritz Link, Stefan Kunz, Eduard Szöcs, Andreas Scharmüller, Bernadette Vogler, Birgit Beck, Karina P. Battes, Mirela Cimpean, Heinz P. Singer, Juliane Hollender and Ralf B. Schäfer
R script written by Verena C. Schreiner
checked by Moritz Link and Ralf B. Schäfer
University of Koblenz-Landau
Fortstrasse 7
76829 Landau
GERMANY
Corresponding author mail address: schreiner-verena'@'uni-landau.de
## project path
project path, change to the path you saved the rawfiles
```{r}
prj <- getwd()
```
# PART 1: calculate water concentrations from SDB disks
## Tools / Packages
```{r, message=FALSE, warning = FALSE}
require(data.table)
library(reshape)
```
## Load & Clean Data
### read data
```{r}
# results SDB disks
Results_SDB = read.table(file.path(prj,"Results_SDB.csv"),
header=TRUE, sep = ";")
# codes SDB disks
Code_SDB = read.table(file.path(prj, "Code_SDB.csv"), header=TRUE, sep = ";",
dec=".", na.string=c("NR", "NA", ""), check.names=FALSE)
Code_SDB = as.data.table(Code_SDB)
# sampling rates
Rs_known = read.table(file.path(prj,"Rs_known.csv"),
header=TRUE, sep = ";", dec=".", na.string=c("NR", "NA"), check.names=FALSE)
```
### clean results SDB
```{r}
names(Results_SDB) <- gsub("X", "CH", names(Results_SDB))
Results_SDB <- Results_SDB[,-3]
# remove duplicated compounds
Results_SDB <- Results_SDB[-c(60,259), ]
# remove compounds, where standards or spiking was not detectable
Not_worked = read.table(file.path(prj,"Not_worked.csv"),
header=TRUE, sep = ";")
Results_SDB <- Results_SDB[!Results_SDB$Compound %in% Not_worked$Compound, ]
# remove codes from compound names
out <- strsplit(as.character(Results_SDB$Compound),'_')
Results_SDB <- data.frame(do.call(rbind, out), Results_SDB[,-1])
Results_SDB <- Results_SDB[,-c(2:4)]
names(Results_SDB)[1] <- c("Compound")
```
### remove compounds that were not detected
```{r}
Results_SDB$detected <- ifelse(rowSums(Results_SDB[,c(3:94)], na.rm = TRUE) > 0, paste("yes"), paste("no"))
# only use those further that were detected
Results_SDB_det <- Results_SDB[Results_SDB$detected == "yes", ]
Results_SDB_det <- Results_SDB_det[,-96]
# as data table + melt dataframe
Results_SDB_det = as.data.table(Results_SDB_det)
Results_SDB_det = melt(Results_SDB_det, id.vars = c("Compound", "LOQ_L_rund", "LOQ_lower_round_E"), value.name = "Concentration", variable.name = "Code")
```
### deal with compounds that lacked calibration data
remove Carbendazim + Simazin-2-hydroxy
```{r}
Results_SDB_det_rem <- Results_SDB_det[! which(grepl( "^CH",Results_SDB_det$Code) & Results_SDB_det$Compound == "Carbendazim"),]
Results_SDB_det_rem <- Results_SDB_det_rem[! which(grepl( "^CH",Results_SDB_det_rem$Code) & Results_SDB_det_rem$Compound == "Simazin-2-hydroxy"),]
```
adding extrapolated data using external calibration data
```{r}
Cal_ext = read.table(file.path(prj,"Comp_Cal_ext.csv"),
header=TRUE, sep = ";")
names(Cal_ext) <- gsub("X", "CH", names(Cal_ext))
Cal_ext <- Cal_ext[,-c(2:4)]
names(Cal_ext)[2] <- c("LOQ_lower_round_E")
Cal_ext$LOQ_L_rund <- c(0.2, NA)
out <- strsplit(as.character(Cal_ext$Wirkstoff),'_')
Cal_ext <- data.frame(do.call(rbind, out), Cal_ext[,-1])
Cal_ext <- Cal_ext[,-c(2:3)]
names(Cal_ext)[1] <- c("Compound")
Cal_ext = as.data.table(Cal_ext)
Cal_ext = melt(Cal_ext, id.vars = c("Compound", "LOQ_L_rund", "LOQ_lower_round_E"), value.name = "Concentration", variable.name = "Code")
```
combine data again
```{r}
Results_SDB_det2 <- rbind(Results_SDB_det_rem, Cal_ext)
nrow(Results_SDB_det2) == nrow(Results_SDB_det) # TRUE = no information lost
```
### combine stereoisomers
merge results of 2,4-D and Dicamba, since we were not able to separate them reliably (stereoisomeres)
```{r}
Stereo <- c("2-4-D", "Dicamba")
Results_SDB_det2_Dic <- Results_SDB_det2[Results_SDB_det2$Compound %in% Stereo,]
Results_SDB_det2_wo_Dic <- Results_SDB_det2[!Results_SDB_det2$Compound %in% Stereo,]
nrow(Results_SDB_det2) == nrow(Results_SDB_det2_wo_Dic) + nrow(Results_SDB_det2_Dic)
# summing up values
# remove Dicamba of those samples with ID RO
Results_SDB_det2_Dic <- as.data.frame(Results_SDB_det2_Dic)
Results_SDB_det2_Dic <- Results_SDB_det2_Dic[- which(grepl( "^RO", Results_SDB_det2_Dic$Code) & Results_SDB_det2_Dic$Compound == "Dicamba"),]
Results_SDB_det2_Dic_cast <- data.frame(cast(Results_SDB_det2_Dic, Compound ~ Code, value = "Concentration", mean))
Results_SDB_det2_Dic_cast2 <- colSums(Results_SDB_det2_Dic_cast[,2:ncol(Results_SDB_det2_Dic_cast)], na.rm = TRUE)
Results_SDB_det2_Dic_cast2 <- as.data.frame(Results_SDB_det2_Dic_cast2)
Results_SDB_det2_Dic_cast2$Code <- rownames(Results_SDB_det2_Dic_cast2)
Results_SDB_det2_Dic_cast2$Compound <- c("2-4-D")
names(Results_SDB_det2_Dic_cast2)[1] <- c("Concentration")
# change 0 in NA
Results_SDB_det2_Dic_cast2$Concentration[Results_SDB_det2_Dic_cast2$Concentration == 0] <- NA
Results_SDB_det2_Dic_cast2$LOQ_L_rund <- unique(Results_SDB_det2_Dic[Results_SDB_det2_Dic$Compound == "2-4-D",]$LOQ_L_rund)
Results_SDB_det2_Dic_cast2$LOQ_lower_round_E <- unique(Results_SDB_det2_Dic[Results_SDB_det2_Dic$Compound == "2-4-D",]$LOQ_lower_round_E)
Results_SDB_det2_Dic_cast2 <- Results_SDB_det2_Dic_cast2[,c(3,4,5,2,1)]
Results_SDB_det2 <- rbind(Results_SDB_det2_wo_Dic, Results_SDB_det2_Dic_cast2)
```
remove dataframes that are not necessary anymore
```{r}
rm(Cal_ext, Not_worked, Results_SDB_det, Results_SDB_det_rem, Stereo, Results_SDB_det2_Dic,
Results_SDB_det2_wo_Dic, Results_SDB_det2_Dic_cast, Results_SDB_det2_Dic_cast2 )
gc()
```
### add codes to samples
```{r}
# merge masses + codes
Results_SDB2 = merge(Results_SDB_det2, Code_SDB, by = "Code")
# only use entries where compounds were not detected
Results_SDB2 <- Results_SDB2[!is.na(Results_SDB2$Concentration),]
```
## calculate masses from concentration
concentration [ug/L] in sample multiplied with injection Volume: 100 uL (0.0001 L) (gain ug)
this is multiplied with factor for injection:
total volume of evaporated sample (Vol_evap) [uL] / volume injected (100 uL)
this multiplied with factor of part used from sample
volume total sample (Vol_total) / volume used sample part (Vol_used)
divided by number of disks, since replicates at one site were sometimes merged
```{r}
Results_SDB2 = Results_SDB2[, list(Compound, Mass_ug =
((Concentration* 0.0001 * (Vol_evap)/100 * (Vol_total/
Vol_used))/No_disks),
site, Rainfallevent, duration,
LOQ_L = ((LOQ_L_rund *0.0001 * 500/100)) ,
LOQ_E = ((LOQ_lower_round_E*0.0001 * 500/100)),
not_using, comment),
by = Code]
Results_SDB2 <- as.data.frame(Results_SDB2)
```
exclude those that were measured, but which had issues (e.g. buried, flow too low)
```{r}
Results_SDB2 <- Results_SDB2[is.na(Results_SDB2$not_using), ]
```
add information on compound type
```{r}
Comp_type = read.table(file.path(prj,"Comp_type.csv"),
header=TRUE, sep = ";")
Results_SDB2 <- merge(Results_SDB2, Comp_type, by=c("Compound"), all.x=TRUE)
```
## calculate water concentrations
now only proceed with non-metabolites
```{r}
Results_SDB2_pest <- Results_SDB2[!Results_SDB2$type == "Metabolite",]
```
add sampling rates
```{r}
Results_Rs <- merge(Results_SDB2_pest, Rs_known, by="Compound", all.x=TRUE)
```
remove detected but not agricultural compounds/ pesticide
```{r}
Non_agri <- c("5-Chloro-2-methyl-4-isothiazolin-3-on-(CMI)", "Triclosan", "Piperonyl-butoxide", "Trinexapac-ethyl",
"N-N-diethyl-3-methylbenzamid-(DEET)", "2-n-Octyl-4-isothiazolin-3-on-(OIT)")
Results_Rs <- Results_Rs[!Results_Rs$Compound %in% Non_agri, ]
```
calculate water concentrations of detected compounds
use a timeframe of 2 for the rainfall event samples and the actual exposure time (6 days) for the samples withour rainfall event
```{r}
Results_Rs_conc_C <- Results_Rs[!is.na(Results_Rs$Rs),]
length(unique(Results_Rs_conc_C$Compound))
Results_Rs_conc <- Results_Rs_conc_C[,c(1,2,4,5)]
Results_Rs_conc$conc_water <- ifelse(Results_Rs_conc_C$Rainfallevent == "1",
Results_Rs_conc_C$Mass_ug/(Results_Rs_conc_C$Rs * 6),
Results_Rs_conc_C$Mass_ug/(Results_Rs_conc_C$Rs * 2))
```
calculate LOQs in stream water (based on LOQ in the lab and the sampling rates)
```{r}
Results_Rs_conc$LOQ_L <- Results_Rs_conc_C$LOQ_L/(Results_Rs_conc_C$Rs * 2)
Results_Rs_conc$LOQ_E <- Results_Rs_conc_C$LOQ_E/(Results_Rs_conc_C$Rs * 2)
```
write table
```{r}
write.table(Results_Rs_conc[,-c(6,7)], file = "Concentrations_SDB.csv", sep = ";")
```
## create a table with all detected compounds (also without sampling rate)
```{r}
All_det_SDB <- Results_SDB2_pest
All_det_SDB <- All_det_SDB[All_det_SDB$Mass_ug > "0",]
write.csv(All_det_SDB, "All_det_SDB.csv")
```
## incoorporating compounds where we lacked an experimental sampling rates
```{r}
Results_Rs_conc_miss_Rs <- Results_Rs[is.na(Results_Rs$Rs),]
# add a sampling rate from the lower range, to obtain high TWA concentrations
Results_Rs_conc_miss_Rs$Rs <- c(0.2)
Results_wo_Rs_conc <- Results_Rs_conc_miss_Rs[,c(1,2,4,5)]
Results_wo_Rs_conc$conc_water <- Results_Rs_conc_miss_Rs$Mass_ug/(Results_Rs_conc_miss_Rs$Rs * 2)
```
# PART 2: create pesticide gradient
## Tools / Packages
```{r, message=FALSE, warning = FALSE}
library(plyr)
```
## Load & Clean Data
```{r}
SDB <- read.table(file.path(prj, "Concentrations_SDB.csv"),
header = TRUE,
sep = ";",
na.strings = c("NR", "NA"))
SR <- read.table(file.path(prj, "Concentrations_PDMS.csv"),
header = TRUE,
sep = ";",
na.strings = c("NR", "NA"))
all_comp_SDB <- read.table(file.path(prj, "All_det_SDB.csv"),
header = TRUE,
sep = ",",
na.strings = c("NR", "NA"))
CAS_t_n <- read.table(file.path(prj, "CAS_type_name.csv"),
header = TRUE,
sep = ";",
na.strings = c("NR", "NA"))
EC50 <- read.table(file.path(prj, "EC50.csv"),
header = TRUE,
sep = ";",
na.strings = c("NR", "NA"))
names(EC50)[2] <- c("Compound")
CAS <- unique(EC50[, c(1,2)])
```
### Adjusting PDMS
getting the LOQ of the compounds measured via PDMS
values currently are in pg/L
```{r}
SR_LOQ <- SR[1,]
SR_LOQ <- as.data.frame(t(SR_LOQ))
SR_LOQ$Compound <- rownames(SR_LOQ)
SR_LOQ <- SR_LOQ[-c(1:3),]
names(SR_LOQ)[1] <- c("LOQ")
attr(SR_LOQ$LOQ, "ATT") <- NULL
SR_LOQ <- droplevels(SR_LOQ)
SR_LOQ$LOQ <- as.integer(SR_LOQ$LOQ)
SR_LOQ$LOQ <- (SR_LOQ$LOQ)/1E6
```
now adjust the SR dataframe to the SDB one
```{r}
SR <- SR[-1,]
# save data.table and long format
SR = as.data.table(SR)
SR = melt(SR, id.vars = c("Code", "site", "Rainfallevent"), value.name = "conc_water", variable.name = "Compound")
```
changing unit of concentration
```{r}
SR$conc_water <- (SR$conc_water)/1E6
```
remove non-detects + samples where no corresponding SDB disk is available
```{r}
SR <- SR[!is.na(SR$conc_water),]
SR <- SR[!is.na(SR$Code),]
```
counting how often the metabolite (Chlorpyrifos.methyl) was detected
```{r}
SR_met <- SR[SR$Compound == "Chlorpyrifos.methyl",]
count_SR_met <- data.frame(cast(SR_met, site ~ Rainfallevent, value = "conc_water", length))
# replace 0 with NA
count_SR_met[, 2:5][count_SR_met[, 2:5] == 0] <- NA
# was detected nearly everywhere
```
remove metabolite
```{r}
SR <- SR[!SR$Compound == "Chlorpyrifos.methyl",]
```
### merge both data frames
merge dataframes based on the concentrations that were detected
```{r}
SR <- SR[,c("Compound", "Code", "site", "Rainfallevent", "conc_water") ]
SR <- as.data.frame(SR)
SR$method <- c("PDSM")
SDB$method <- c("SDB")
conc <- rbind(SDB, SR)
conc$Compound <- gsub("lambda.Cyhalothrin", "Cyhalothrin", conc$Compound)
conc$Compound <- gsub("Cypermethrin.alpha", "alpha-Cypermethrin", conc$Compound)
conc$Compound <- gsub("2-4-D", "2,4-D", conc$Compound)
conc$Compound <- gsub("Terbutylazin", "Terbuthylazin", conc$Compound)
conc$Compound <- gsub("Fenamidone", "Fenamidon", conc$Compound)
```
merge dataframes based on the compounds that were detected
```{r}
SR2 <- SR[,c("Compound", "Code", "site", "Rainfallevent") ]
all_comp_SDB <- all_comp_SDB[,c("Compound", "Code", "site", "Rainfallevent") ]
comp_all <- rbind(all_comp_SDB, SR2)
# add information abot CAS and type
comp_all2 <- comp_all
comp_all2$Compound <- gsub("lambda.Cyhalothrin", "lambda-Cyhalothrin", comp_all2$Compound)
comp_all2$Compound <- gsub("Cypermethrin.alpha", "Cypermethrin-alpha", comp_all2$Compound)
comp_all_CAS <- merge(comp_all2, CAS_t_n, by = "Compound", all.x = TRUE)
# write.csv(comp_all_CAS, "comp_all_CAS.csv", row.names = FALSE)
```
add CAS number
```{r}
conc <- merge(conc, CAS, by = "Compound", all.x = TRUE)
```
Rename rainfall events
```{r}
conc$Rainfallevent <- gsub("1", "Sampling_1", conc$Rainfallevent)
conc$Rainfallevent <- gsub("2", "Sampling_2", conc$Rainfallevent)
conc$Rainfallevent <- gsub("3", "Sampling_3", conc$Rainfallevent)
conc$Rainfallevent <- gsub("B", "Sampling_4", conc$Rainfallevent)
comp_all$Rainfallevent <- gsub("1", "Sampling_1", comp_all$Rainfallevent)
comp_all$Rainfallevent <- gsub("2", "Sampling_2", comp_all$Rainfallevent)
comp_all$Rainfallevent <- gsub("3", "Sampling_3", comp_all$Rainfallevent)
comp_all$Rainfallevent <- gsub("B", "Sampling_4", comp_all$Rainfallevent)
```
## number of detected compounds per sample (all without metabolites)
based on all detected compounds
```{r}
comp_all$Value <- c("1")
count_all2 <- data.frame(cast(comp_all, site ~ Rainfallevent, value = "Value", length))
# replace 0 with NA
count_all2[, 2:5][count_all2[, 2:5] == 0] <- NA
names(count_all2)[2] <- c("N_det_Sampling_1")
names(count_all2)[3] <- c("N_det_Sampling_2")
names(count_all2)[4] <- c("N_det_Sampling_3")
names(count_all2)[5] <- c("N_det_Sampling_4")
```
calculate mean and median
```{r}
count_all2$mean_N_det <- rowMeans(count_all2[,2:5], na.rm = TRUE)
count_all2$median_N_det <- apply(count_all2[,2:5], 1, median, na.rm = TRUE)
```
overall detected compound (not regarding rainfall event)
```{r}
count_all_Nall2 <- data.frame(cast(count(comp_all, c("site", "Compound")), site ~ ., value = "Compound", length))
names(count_all_Nall2)[2] <- c("N_det_all")
count_all2 <- merge(count_all2, count_all_Nall2, by = c("site"))
```
## number of compounds considered for sum concentration per sample
based on compounds used to calculate concentrations (with sampling rates)
```{r}
count_all <- data.frame(cast(conc, site ~ Rainfallevent, value = "conc_water", length))
# replace 0 with NA
count_all[, 2:5][count_all[, 2:5] == 0] <- NA
names(count_all)[2] <- c("N_conc_Sampling_1")
names(count_all)[3] <- c("N_conc_Sampling_2")
names(count_all)[4] <- c("N_conc_Sampling_3")
names(count_all)[5] <- c("N_conc_Sampling_4")
```
calculate mean and median
```{r}
count_all$mean_N_conc <- rowMeans(count_all[,2:5], na.rm = TRUE)
count_all$median_N_conc <- apply(count_all[,2:5], 1, median, na.rm = TRUE)
```
## concentrations
### sum concentration per sample
```{r}
sum_conc_all <- data.frame(cast(conc, site ~ Rainfallevent, value = "conc_water", sum))
# replace 0 with NA
sum_conc_all[, 2:5][sum_conc_all[, 2:5] == 0] <- NA
names(sum_conc_all)[2] <- c("sum_conc_Sampling_1")
names(sum_conc_all)[3] <- c("sum_conc_Sampling_2")
names(sum_conc_all)[4] <- c("sum_conc_Sampling_3")
names(sum_conc_all)[5] <- c("sum_conc_Sampling_4")
```
calculate mean and median
```{r}
sum_conc_all$mean_sum_conc <- rowMeans(sum_conc_all[,2:5], na.rm = TRUE)
sum_conc_all$median_sum_conc <- apply(sum_conc_all[,2:5], 1, median, na.rm = TRUE)
sum_conc_all$max_sum_conc <- apply(sum_conc_all[,2:5], 1, max, na.rm = TRUE)
```
### maximum concentrations
#### per site and rainfall event
```{r}
conc2 <- as.data.table(conc)
max_sample <- conc2[conc2[ , .I[conc_water == max(conc_water)], by = c("site","Rainfallevent")]$V1]
# transpose
max_sample <- dcast(max_sample, site ~ Rainfallevent, value.var=c("conc_water", "Compound"))
names(max_sample)[2] <- c("max_conc_Sampling_1")
names(max_sample)[3] <- c("max_conc_Sampling_2")
names(max_sample)[4] <- c("max_conc_Sampling_3")
names(max_sample)[5] <- c("max_conc_Sampling_4")
names(max_sample)[6] <- c("which_max_conc_Sampling_1")
names(max_sample)[7] <- c("which_max_conc_Sampling_2")
names(max_sample)[8] <- c("which_max_conc_Sampling_3")
names(max_sample)[9] <- c("which_max_conc_Sampling_4")
```
#### per site over all samplings
```{r}
max_site <- conc2[conc2[ , .I[conc_water == max(conc_water)], by = c("site")]$V1]
max_site <- dcast(max_site, site ~ ., value.var=c("conc_water", "Compound"))
names(max_site)[2] <- c("max_conc_all")
names(max_site)[3] <- c("which_max_conc_all")
```
## Toxicity to invertebrate
filter for most sensitive freshwater invertebrates
```{r}
EC50_iv <- EC50[,c("casnr", "Compound","ec50f_ma4896_fin", "ec50f_ma4896_tax", "ec50f_ma4896_tax",
"ec50f_ma4896_src")]
names(EC50_iv)[3] <- c("EC50_iv")
```
### Calculate Toxicity
Merge EC_50 information to conc
```{r}
tox_iv <- merge(conc, EC50_iv[,c(1,3)], by="casnr", all.x=TRUE)
check_iv <- tox_iv[is.na(tox_iv$EC50_iv),] # two compounds, no EC50 Values available
tox_iv <- tox_iv[!is.na(tox_iv$EC50_iv),]
```
calculating the factor of toxicity
```{r}
tox_iv$tox_iv_fac <- tox_iv$conc_water / tox_iv$EC50_iv
```
### identifying most toxic compound
#### per site and rainfall event
```{r}
tox_iv <- as.data.table(tox_iv)
max_tox_sample_iv <- tox_iv[tox_iv[ , .I[tox_iv_fac == max(tox_iv_fac)],
by = c("site","Rainfallevent")]$V1]
# TU information for each compound!
# numeric value has to be log-transformed to calculate TU
tox_iv_log <- tox_iv
tox_iv_log$tox_iv_fac <- log10(tox_iv_log$tox_iv_fac)
names(tox_iv_log)[9] <- c("TU_iv")
# transpose
max_tox_sample_iv <- dcast(max_tox_sample_iv, site ~ Rainfallevent,
value.var=c("tox_iv_fac", "Compound"))
```
all numeric columns have to be log-transformed to calculate TU
```{r}
max_tox_sample_iv[,c(2:5)] <- log10(max_tox_sample_iv[,c(2:5)])
names(max_tox_sample_iv)[2] <- c("max_TU_iv_Sampling_1")
names(max_tox_sample_iv)[3] <- c("max_TU_iv_Sampling_2")
names(max_tox_sample_iv)[4] <- c("max_TU_iv_Sampling_3")
names(max_tox_sample_iv)[5] <- c("max_TU_iv_Sampling_4")
names(max_tox_sample_iv)[6] <- c("whichmax_TU_iv_Sampling_1")
names(max_tox_sample_iv)[7] <- c("whichmax_TU_iv_Sampling_2")
names(max_tox_sample_iv)[8] <- c("whichmax_TU_iv_Sampling_3")
names(max_tox_sample_iv)[9] <- c("whichmax_TU_iv_Sampling_4")
```
#### per site
```{r}
max_tox_site_iv <- tox_iv[tox_iv[ , .I[tox_iv_fac == max(tox_iv_fac)], by = c("site")]$V1]
max_tox_site_iv <- dcast(max_tox_site_iv, site ~ ., value.var=c("tox_iv_fac", "Compound"))
names(max_tox_site_iv)[2] <- c("max_TU_iv_all")
names(max_tox_site_iv)[3] <- c("whichmax_TU_iv_all")
```
numeric column has to be log-transformed to calculate TU
```{r}
max_tox_site_iv[,c(2)] <- log10(max_tox_site_iv[,c(2)])
```
#### list of most toxic compounds
```{r}
max_tox_all_iv <- tox_iv[order(-tox_iv$tox_iv_fac), ]
max_tox_all_iv_test <- as.data.frame(max_tox_all_iv$Compound)
max_tox_all_iv_test2 <- unique(max_tox_all_iv_test)
Top_10_iv <- as.data.frame(max_tox_all_iv_test2[c(1:10),])
names(Top_10_iv)[1] <- c("Compound")
```
### calculating sumTU per sample
```{r}
sumTU_iv <- data.frame(cast(tox_iv, site ~ Rainfallevent, value = "tox_iv_fac", sum))
# replace 0 with NA
sumTU_iv[, 2:5][sumTU_iv[, 2:5] == 0] <- NA
# rename the columns
names(sumTU_iv)[2] <- c("sumTU_iv_Sampling_1")
names(sumTU_iv)[3] <- c("sumTU_iv_Sampling_2")
names(sumTU_iv)[4] <- c("sumTU_iv_Sampling_3")
names(sumTU_iv)[5] <- c("sumTU_iv_Sampling_4")
```
numeric columns have to be log-transformed to calculate sumTU
```{r}
sumTU_iv[,c(2:5)] <- log10(sumTU_iv[,c(2:5)])
```
calculate mean + median
```{r}
sumTU_iv$mean_sumTU_iv <- rowMeans(sumTU_iv[,2:5], na.rm = TRUE)
sumTU_iv$median_sumTU_iv <- apply(sumTU_iv[,2:5], 1, median, na.rm = TRUE)
sumTU_iv$max_sumTU_iv <- apply(sumTU_iv[,2:5], 1, max, na.rm = TRUE)
```
### number of compounds contributing to 75 % of the toxicity
```{r}
tox_iv_count <- as.data.frame(tox_iv)
tox_iv_count <- dcast(tox_iv_count, Compound ~ Code, value.var=c("tox_iv_fac"))
names_tox_iv_count <- tox_iv_count[,1]
tox_iv_count <- tox_iv_count[,2:75]
tox_iv_count <- as.data.frame(t(t(tox_iv_count) / colSums(tox_iv_count, na.rm = TRUE)))
colSums(tox_iv_count, na.rm = TRUE)
# Now the values are in percent or rather a factor of 1
count_75 <- NULL
for(i in 1:ncol(tox_iv_count)) {
Test <- as.data.table(tox_iv_count[,i])
Test <- Test[order(-Test[,1]),]
Test <- cumsum(Test)
count_75[i] <- min(which(Test[,1] >= 0.75))
}
count_75 <- as.data.frame(t(as.data.frame.list((count_75))))
count_75 <- droplevels(count_75)
codes_samples <- unique(conc[,3:4])
N_75_iv <- cbind(codes_samples, count_75)
names(N_75_iv)[3] <- c("N_75_iv")
N_75_iv <- dcast(N_75_iv, site ~ Rainfallevent,
value.var=c("N_75_iv"))
names(N_75_iv)[2] <- c("N_75_iv_Sampling_1")
names(N_75_iv)[3] <- c("N_75_iv_Sampling_2")
names(N_75_iv)[4] <- c("N_75_iv_Sampling_3")
names(N_75_iv)[5] <- c("N_75_iv_Sampling_4")
contribution_iv <- cbind(names_tox_iv_count, tox_iv_count)
```
## Toxicity based on algae
now filter only to the EC50 values of algae
```{r}
EC50_al <- EC50[,c("casnr", "Compound", "ec50f_al4896_fin", "ec50f_al4896_tax", "ec50f_al4896_src")]
names(EC50_al)[3] <- c("EC50_al")
```
### calculate toxicity
Merge missing EC_50 information to conc
```{r}
tox_al <- merge(conc, EC50_al[,c(2,3)], by="Compound", all.x=TRUE)
check <- tox_al[is.na(tox_al$EC50_al),] # these compounds have EC50 values that were higher than the tested range
tox_al <- tox_al[!is.na(tox_al$EC50_al),]
```
calculating the factor of toxicity
```{r}
tox_al$tox_al_fac <- tox_al$conc_water / tox_al$EC50_al
```
### identifying most toxic compound
#### per site and rainfall event
```{r}
tox_al <- as.data.table(tox_al)
max_tox_sample_al <- tox_al[tox_al[ , .I[tox_al_fac == max(tox_al_fac)],
by = c("site","Rainfallevent")]$V1]
# TU information for each compound
# numeric value has to be log-transformed to calculate TU
tox_al_log <- tox_al
tox_al_log$tox_al_fac <- log10(tox_al_log$tox_al_fac)
names(tox_al_log)[9] <- c("TU_al")
# now transpose the table
max_tox_sample_al <- dcast(max_tox_sample_al, site ~ Rainfallevent,
value.var=c("tox_al_fac", "Compound"))
```
all numeric columns have to be log-transformed to calculate TU
```{r}
max_tox_sample_al[,c(2:5)] <- log10(max_tox_sample_al[,c(2:5)])
names(max_tox_sample_al)[2] <- c("max_TU_al_Sampling_1")
names(max_tox_sample_al)[3] <- c("max_TU_al_Sampling_2")
names(max_tox_sample_al)[4] <- c("max_TU_al_Sampling_3")
names(max_tox_sample_al)[5] <- c("max_TU_al_Sampling_4")
names(max_tox_sample_al)[6] <- c("whichmax_TU_al_Sampling_1")
names(max_tox_sample_al)[7] <- c("whichmax_TU_al_Sampling_2")
names(max_tox_sample_al)[8] <- c("whichmax_TU_al_Sampling_3")
names(max_tox_sample_al)[9] <- c("whichmax_TU_al_Sampling_4")
```
#### per site
```{r}
max_tox_site_al <- tox_al[tox_al[ , .I[tox_al_fac == max(tox_al_fac)], by = c("site")]$V1]
max_tox_site_al <- dcast(max_tox_site_al, site ~ ., value.var=c("tox_al_fac", "Compound"))
names(max_tox_site_al)[2] <- c("max_TU_al_all")
names(max_tox_site_al)[3] <- c("whichmax_TU_al_all")
```
numeric columns have to be log-transformed to calculate TU
```{r}
max_tox_site_al[,c(2)] <- log10(max_tox_site_al[,c(2)])
```
#### list of most toxic compounds
```{r}
max_tox_all_al <- tox_al[order(-tox_al$tox_al_fac), ]
max_tox_all_al_test <- as.data.frame(max_tox_all_al$Compound)
max_tox_all_al_test2 <- unique(max_tox_all_al_test)
Top_10_al <- as.data.frame(max_tox_all_al_test2[c(1:10),])
names(Top_10_al)[1] <- c("Compound")
# write.csv(Top_10_al, "Top_10_al.csv", row.names = FALSE)
```
### calculating sumTU per sample
```{r}
sumTU_al <- data.frame(cast(tox_al, site ~ Rainfallevent, value = "tox_al_fac", sum))
# both 0 values were substituted with NA
sumTU_al[, 2:5][sumTU_al[, 2:5] == 0] <- NA
# rename the columns
names(sumTU_al)[2] <- c("sumTU_al_Sampling_1")
names(sumTU_al)[3] <- c("sumTU_al_Sampling_2")
names(sumTU_al)[4] <- c("sumTU_al_Sampling_3")
names(sumTU_al)[5] <- c("sumTU_al_Sampling_4")
```
numeric columns have to be log-transformed to calculate TU
```{r}
sumTU_al[,c(2:5)] <- log10(sumTU_al[,c(2:5)])
```
calculate mean + median
```{r}
sumTU_al$mean_sumTU_al <- rowMeans(sumTU_al[,2:5], na.rm = TRUE)
sumTU_al$median_sumTU_al <- apply(sumTU_al[,2:5], 1, median, na.rm = TRUE)
sumTU_al$max_sumTU_al <- apply(sumTU_al[,2:5], 1, max, na.rm = TRUE)
```
### number of compounds contributing to 75% of the toxicity
```{r}
tox_al_count <- as.data.frame(tox_al)
tox_al_count <- dcast(tox_al_count, Compound ~ Code, value.var=c("tox_al_fac"))
tox_al_count <- tox_al_count[,2:75]
tox_al_count <- as.data.frame(t(t(tox_al_count) / colSums(tox_al_count, na.rm = TRUE)))
colSums(tox_al_count, na.rm = TRUE)
# Now the values are in percent or rather a factor of 1
count_75 <- NULL
for(i in 1:ncol(tox_al_count)) {
Test <- as.data.table(tox_al_count[,i])
Test <- Test[order(-Test[,1]),]
Test <- cumsum(Test)
count_75[i] <- min(which(Test[,1] >= 0.75))
}
count_75 <- as.data.frame(t(as.data.frame.list((count_75))))
count_75 <- droplevels(count_75)
codes_samples <- unique(conc[,3:4])
N_75_al <- cbind(codes_samples, count_75)
names(N_75_al)[3] <- c("N_75_al")
N_75_al <- dcast(N_75_al, site ~ Rainfallevent,
value.var=c("N_75_al"))
names(N_75_al)[2] <- c("N_75_al_Sampling_1")
names(N_75_al)[3] <- c("N_75_al_Sampling_2")
names(N_75_al)[4] <- c("N_75_al_Sampling_3")
names(N_75_al)[5] <- c("N_75_al_Sampling_4")
```
#### combine all dataframes
```{r}
Gradient <- cbind(count_all2, count_all[-1], sum_conc_all[-1] , max_sample[,-1], max_site[,-1],
max_tox_sample_iv[,-1], max_tox_site_iv[,-1], sumTU_iv[,-1], N_75_iv[,-1],
max_tox_sample_al[,-1], max_tox_site_al[,-1], sumTU_al[,-1], N_75_al[,-1])
write.csv(Gradient, file = "Pesticide_gradient.csv", row.names = FALSE)
```
# PART 3: statistics presented in the manuscript
## Tools / Packages
```{r, message=FALSE, warning = FALSE}
library(dplyr)
library(ggplot2)
library(cowplot)
library(glmnet)
library(foreach)
library(doParallel) # parallel backend for doParallel
library(plotmo) # for plot
library(tidyr)
library(scales)
library(matrixStats) # calculate standards deviation
library(effects) # calculate predictor effects (Figure S2)
```
## Load & Clean Data
### read in data with explanatory variables and toxicity (created earlier)
```{r}
flow_raw = read.table(file.path(prj, "flow_raw.csv"),
header = TRUE,
sep = ",",
na.strings = c("NR", "NA"))
temp_raw = read.table(file.path(prj, "temp_raw.csv"),
header = TRUE,
sep = ",",
na.strings = c("NR", "NA"))
catchment <- read.table(file.path(prj, "catchment.csv"),
header = TRUE,
sep = ";",
na.strings = c("NR", "NA"))
stream_substrate <- read.table(file.path(prj, "stream_substrate.csv"),
header = TRUE,
sep = ",",
na.strings = c("NR", "NA"))
field_size <- read.table(file.path(prj, "Fields_mean_median_geom_mean.csv"),
header = TRUE,
sep = ";",
na.strings = c("NR", "NA"))
field_size_factor <- read.table(file.path(prj, "field_size_factor.csv"), # factorial agricultural intensity
header = TRUE,
sep = ";",
na.strings = c("NR", "NA"))
habitat <- read.table(file.path(prj, "habitat.csv"),
header = TRUE,
sep = ",",
na.strings = c("NR", "NA"))
TU <- read.table(file.path(prj, "Pesticide_gradient.csv"),
header=TRUE, sep=",", dec=".",
na.string=c("NR", "NA"), check.names=TRUE)
agri_buffer = read.table(file.path(prj, "Rip_buffer.csv"),
header = TRUE,
sep = ",",
na.strings = c("NR", "NA"))
names(agri_buffer)[19] <- c("agri_buffer")
agri_buffer$agri_buffer <- agri_buffer$agri_buffer *100
buffer_width <- read.table(file.path(prj, "Buffer_width_June.csv"),
header = TRUE,
sep = ";",
na.strings = c("NR", "NA"))
# calculate minimum per site
buffer_width_min <- ddply(buffer_width, c("site"),summarise, min_buff = min(value))
conc_comp <- read.table(file.path(prj, "conc.csv"), # single concentrations
header = TRUE,
sep = ";",
na.strings = c("NR", "NA"))
```
### Merge the dataframes
```{r}
var_table <- merge(habitat, stream_substrate[-2], by = "site")
var_table <- merge(var_table, catchment, by = "site")
var_table <- merge(var_table, agri_buffer[,c(1,19)], by = "site")
var_table <- merge(var_table, field_size[,c(1,4)], by = "site")
var_table <- merge(var_table, buffer_width_min, by = "site")
var_table <- merge(var_table, field_size_factor, by = "site")
```
## calculate more necessary parameters
calculate width of shore (from height and distance to landscape level)
```{r}
var_table <- as.data.table(var_table)
var_table[, `:=`(shore = sqrt((dist_landscape_lev)^2 + (height_landscape_lev)^2))]
```
calculate ratio of fine material (smaller than 2 mm) as proxy for erosion
summing up psammal and argyllal
```{r}
var_table$fine_sub <- var_table$Psammal + var_table$Argyllal
```
calculate plant height of shore (average of the different vegetation types)
weighted to the appearance of the respective type of shore vegetation
```{r}
var_table$veg_height <- (var_table$bank_cover_forest* var_table$bank_height_forest +
var_table$bank_cover_reed* var_table$bank_height_reed +
var_table$bank_cover_shrubs* var_table$bank_height_shrubs +
var_table$bank_cover_forbs* var_table$bank_height_forbs +
var_table$bank_cover_meadow* var_table$bank_height_meadow +
var_table$bank_cover_agri* var_table$bank_height_agri +
var_table$bank_w.o_veg * 0 )/ (var_table$bank_cover_forest+
var_table$bank_cover_reed + var_table$bank_cover_shrubs +
var_table$bank_cover_forbs + var_table$bank_cover_meadow +
var_table$bank_cover_agri + var_table$bank_w.o_veg)
```
add toxicity data to table
```{r}
data_all <- merge(var_table, TU[,c("site", "max_sumTU_iv", "max_sumTU_al", "max_sum_conc", "sumTU_iv_Sampling_1", "sumTU_al_Sampling_1")], by="site")
```
## Identify drivers for toxicity
1. response variables:
* sum concentration
* sumTU invertebrates
* sumTU algae
2. explanatory variables:
* catchment size (catch_area)
* % agriculture in catchment (ratio_agri_catch)
* % agriculture in 200 m buffer (ratio_agri_buffer)
* size fields (field.size.geom.mean)
* distance between stream and field - buffer width (dist_field)
* height plants in buffer (veg_height)
* direct distance between stream and landscape level (shore)
* benthic substrate smaller than 2 mm (fine_sub)
### check relationships between response variables
```{r}
hist(data_all$max_sum_conc)
```
The response variable sum_conc needs to be log-transformed because of strong deviation from normal distribution and wide range
log-transformation
```{r}
data_all$max_sum_conc_log <- log10(data_all$max_sum_conc)
hist(data_all$max_sum_conc_log)
```
## correlation between sumTU_iv vs. sum concentration
```{r}
plot(data_all$max_sum_conc_log, data_all$max_sumTU_iv)
abline(lm(data_all$max_sumTU_iv ~ data_all$max_sum_conc_log))
cor.test(data_all$max_sum_conc_log, data_all$max_sumTU_iv)
```
both should be considered in model, cor = 0.476
## correlation between sumTU_al and sumTU_ms
```{r}
plot(data_all$max_sumTU_iv, data_all$max_sumTU_al)
abline(lm(data_all$max_sumTU_al ~ data_all$max_sumTU_iv))
cor.test(data_all$max_sumTU_iv, data_all$max_sumTU_al)
```
both should be considered in model, cor = 0.491
## correlation between sumTU_al and sumconc
```{r}
plot(data_all$max_sum_conc_log, data_all$max_sumTU_al)
abline(lm(data_all$max_sumTU_al ~ data_all$max_sum_conc_log))
cor.test(data_all$max_sum_conc_log, data_all$max_sumTU_al)
```
strong correlation, one variable sufficient; cor = 0.958