-
Notifications
You must be signed in to change notification settings - Fork 1
/
QAQC.Rmd
270 lines (209 loc) · 11 KB
/
QAQC.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
---
title: "`r paste0(name.i, ' - Chemistry QAQC results')`"
date: "`r Sys.Date()`"
output:
html_document:
toc: true
toc_depth: 6
toc_float: true
##
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
#file.copy("sections/images", ".", recursive = TRUE)
```
## QAQC overview
The components of our quality review are:
* Laboratory QAQC
+ Checking that laboratory analyses met all internal QAQC checks.
* Accuracy
+ Assessing the bias of a method in a given sample matrix Using a sample matrix spike.
* Precision
+ Assessing method precision using duplicate samples analyzed from the same date/location.
* Equipment Blank exceedances
+ Assessing the influence of field errors using equipment blank samples.
* Parameter Pairs
+ Flagging samples where component parameters exceed total concentrations.
* Holding time exceedances
+ Flagging samples where holding times were exceeded.
The accuracy, precision, and equipment blank assessments are performed on a subset of samples. The errors discovered with these QC assessments are applied to all normal samples. This is done by associating normal samples to those QC samples closest in date/time to each sample.
## QAQC Samples Overview
```{r}
#associate the data with the parameter info file
rlimits<-read.csv("sections/data/param_pairs_accuracy_v2.csv") %>%
mutate(chemical_name = tolower(chemical_name)) %>%
mutate(smaller = tolower(smaller))
data <- data %>%
mutate(chemical_name = tolower(chemical_name)) %>%
merge(. ,rlimits,by=c('chemical_name'),all.x = TRUE)
if (sum(is.na(data$accuracy)) > 0) {
# cat("NOT ALL PARAMS JOINED WITH HOLDING TIMES")
stop("NOT ALL PARAMETERS JOINED WITH ACCURACY/PARAM PAIRS TABLE")
}
data <- data %>%
mutate(short = case_when(
# fraction %in% "T" ~ paste0(as.character(short), " (total)"),
fraction %in% "D" ~ paste0(as.character(short), " (dissolved)"),
TRUE ~ as.character(short)
)) %>%
mutate(short = factor(short, sort(unique(short), decreasing = TRUE)))
```
```{r, echo=FALSE}
# Convert dates from factors to POSIXct objects
library(lubridate)
if (all(stringr::str_count(data$sample_date, ':') == 2)){
data$sample_date <- mdy_hms(data$sample_date)
data$analysis_date <- mdy_hms(data$analysis_date)
} else{
data$sample_date <- mdy_hm(data$sample_date)
data$analysis_date <- mdy_hm(data$analysis_date)
}
#pulling spike dataset
spike<-data[data$sample_type_code=="MS",]
spike<-spike %>%
select(sys_sample_code,lab_sdg,sample_date,chemical_name,short,fraction,result_value,result_unit,quantitation_limit,method_detection_limit,detection_limit_unit,lab_qualifiers,qc_original_conc,qc_spike_added,qc_spike_measured,accuracy) %>% distinct() %>%
filter(!is.na(spike$chemical_name))
#for all other data sets I need to first restrict the raw data to only that listed as "field"
data$sample_source <- tolower(data$sample_source)
data<-data[data$sample_source=="field",]
data<-data[!is.na(data$chemical_name),]
#pulling equipment blank data set
EB<-data[data$DEC_sample_type=="EB",]
EB<-unique(EB[c('sys_sample_code','lab_sdg','sample_date','chemical_name','short','fraction','result_value','result_unit','quantitation_limit','method_detection_limit','detection_limit_unit','lab_qualifiers')])
EB<-EB[!is.na(EB$chemical_name),]
#pulling duplicate data set
dup<-data[data$DEC_sample_type=="DUP"|data$DEC_sample_type=="N_DUPPARENT",]
dup<-unique(dup[c('DEC_sample_type','sys_sample_code','lab_sdg','sample_date','chemical_name','short','fraction','result_value','result_unit','quantitation_limit','method_detection_limit','detection_limit_unit','lab_qualifiers')])
#separate the parent and duplicate data sets
dupparent<-dup[dup$DEC_sample_type=="N_DUPPARENT",]
dup<-dup[dup$DEC_sample_type=="DUP",]
#remove -DUP from sys sample code for merge, including all instances of upper and lower case.
### ACCOUNT FOR DUPs labeled as "QC"? doesn't the new QAPP say to use this instead??
dup$sys_sample_code<-gsub("-[Dd][Uu][Pp]","",dup$sys_sample_code)
dup$sys_sample_code<-gsub(" [Dd][Uu][Pp]","",dup$sys_sample_code)
dup$sys_sample_code<-gsub("WS[Dd][Uu][Pp]","WS",dup$sys_sample_code)
dup$sys_sample_code<-gsub("W[Dd][Uu][Pp]","W",dup$sys_sample_code)
# For SMAS data, which uses SEQ as the field replicate identified in the sys_sample_code. Will not affect data not using this convention.
dup$sys_sample_code<-gsub("-[Ss][Ee][Qq]","",dup$sys_sample_code)
dup$sys_sample_code<-gsub(" [Ss][Ee][Qq]","",dup$sys_sample_code)
dup$sys_sample_code<-gsub("WS[Ss][Ee][Qq]","WS",dup$sys_sample_code)
dup$sys_sample_code<-gsub("W[Ss][Ee][Qq]","W",dup$sys_sample_code)
#convert both sys_sample_code fields to character
dup$sys_sample_code<-as.character(dup$sys_sample_code)
dupparent$sys_sample_code<-as.character(dupparent$sys_sample_code)
#rename duplicate fields so can merge the two tables
names(dupparent)[names(dupparent)=='result_value']<-'result_value.parent'
names(dupparent)[names(dupparent)=='result_unit']<-'result_unit.parent'
names(dupparent)[names(dupparent)=='sample_date']<-'sample_date.parent'
names(dupparent)[names(dupparent)=='quantitation_limit']<-'quantitation_limit.parent'
names(dupparent)[names(dupparent)=='method_detection_limit']<-'method_detection_limit.parent'
names(dupparent)[names(dupparent)=='detection_limit_unit']<-'detection_limit_unit.parent'
names(dupparent)[names(dupparent)=='lab_qualifiers']<-'lab_qualifiers.parent'
names(dup)[names(dup)=='result_value']<-'result_value.dup'
names(dup)[names(dup)=='result_unit']<-'result_unit.dup'
names(dup)[names(dup)=='sample_date']<-'sample_date.dup'
names(dup)[names(dup)=='quantitation_limit']<-'quantitation_limit.dup'
names(dup)[names(dup)=='method_detection_limit']<-'method_detection_limit.dup'
names(dup)[names(dup)=='detection_limit_unit']<-'detection_limit_unit.dup'
names(dup)[names(dup)=='lab_qualifiers']<-'lab_qualifiers.dup'
#remove DEC_sample_type for merge
dup<-unique(dup[c('sys_sample_code','lab_sdg','chemical_name','short','sample_date.dup','result_value.dup','result_unit.dup','quantitation_limit.dup','method_detection_limit.dup','detection_limit_unit.dup','lab_qualifiers.dup')])
dupparent<-unique(dupparent[c('sys_sample_code','lab_sdg','chemical_name','short','sample_date.parent','result_value.parent','result_unit.parent','quantitation_limit.parent','method_detection_limit.parent','detection_limit_unit.parent','lab_qualifiers.parent')])
# Fill result field for nondetects with the method detection limits
# Determined unneccesary as per Jason Fagel on 2/4/2020
# dup$result_value.dup <-
# ifelse(dup$lab_qualifiers.dup %in% "U", dup$method_detection_limit.dup, dup$result_value.dup)
# dupparent$result_value.parent <-
# ifelse(dupparent$lab_qualifiers.parent %in% "U", dupparent$method_detection_limit.parent, dupparent$result_value.parent)
#merge the two into one data set
dup<-merge(dup,dupparent,by=c('sys_sample_code','lab_sdg','chemical_name','short'),all=TRUE)
# rm(dupparent)
#remove qc from complete data set
data<-data[data$DEC_sample_type %in% c("N", "N_DUPPARENT"),]
#creating a holding time subset
HT<-unique(data[c('sys_sample_code','lab_sdg','sample_date','chemical_name','short','analysis_date')])
```
<!-- Plotting the sample dates to understand how best to associate them. -->
```{r, echo=FALSE, warning=FALSE}
samples<-unique(data[c('sample_date','lab_sdg')])
samples$freq<-1
samples$type<-"standard samples"
samples$sample_date<-as.Date(samples$sample_date,"%m/%d/%Y")
blanksamples<-unique(EB[c('sample_date','lab_sdg')])
blanksamples$freq<-2
blanksamples$type<-"equipment blanks"
blanksamples$sample_date<-as.Date(blanksamples$sample_date,"%m/%d/%Y")
# if (nrow(dup) > 0 ) {
# print("1")
# }else{
# print("2")
# }
if (nrow(dup) > 0 ) {
dupsamples<-unique(dup[c('sample_date.parent','lab_sdg')])
dupsamples$freq<-3
dupsamples$type<-"duplicates"
names(dupsamples)[names(dupsamples)=="sample_date.parent"]<-"sample_date"
dupsamples$sample_date<-as.Date(dupsamples$sample_date,"%m/%d/%Y")
}
spikesamples<-unique(spike[c('sample_date','lab_sdg')])
spikesamples$freq<-4
spikesamples$type<-"spike samples"
spikesamples$sample_date<-as.Date(spikesamples$sample_date,"%m/%d/%Y")
#merge together
samples<-merge(samples,blanksamples,all=TRUE)
if(nrow(dup) > 0){
samples<-merge(samples,dupsamples,all=TRUE)
}
samples<-merge(samples,spikesamples,all=TRUE)
library(ggplot2)
print(ggplot() +
geom_point(data=samples,aes(sample_date,freq,color=type)) +
ylab("sample types")+
xlab("sample date"))
rm(list=c('samples','blanksamples','dupsamples','spikesamples'))
```
```{r, child = 'sections/Lab.Rmd'}
```
```{r}
bind_nearest_date <- function(sample.df, match.df, match.col) {
final.df <- lapply(unique(match.df$chemical_name), function(param.i) {
sample.param.i <- sample.df[sample.df$chemical_name == param.i, ]
match.param.i <- match.df[match.df$chemical_name == param.i,]
site.df <- lapply(unique(sample.param.i$sys_sample_code), function(site.i) {
sample.sub <- sample.param.i[sample.param.i$sys_sample_code == site.i, ]
match.param.i$abs <- abs(match.param.i$sample_date - sample.sub$sample_date)
match.param.i$min <- min(abs(match.param.i$sample_date - sample.sub$sample_date))
match.param.i <- match.param.i[which(match.param.i$abs == match.param.i$min), ]
sample.sub[, match.col] <- match.param.i[, match.col][1]
return(sample.sub)
}) %>%
dplyr::bind_rows()
}) %>%
dplyr::bind_rows()
return(final.df)
}
```
```{r, child = 'sections/Accuracy.Rmd'}
```
```{r, child = 'sections/Precision.Rmd'}
```
```{r, child = 'sections/Equipment.Blanks.Rmd'}
```
```{r, child = 'sections/Parameter.pairs.Rmd'}
```
```{r, child = 'sections/HT.Rmd'}
```
```{r, child = 'sections/Conclusions.Rmd'}
```
```{r, echo=FALSE}
#creating final data set
if("SITE_ID" %in% colnames(data)){
forprint<-unique(data[c('project_name','SITE_ID','SITE_ID_CORR_IND','sys_sample_code','sample_delivery_group','chemical_name','sample_date','cas_rn','fraction','result_value','result_unit','lab_anl_method_name','method_detection_limit','detection_limit_unit','quantitation_limit','lab_qualifiers','validator_qualifiers','interpreted_qualifiers','qaqc_date')])
# Temp block for running data without project_name and SITE_ID_CORR_IND
# if("SITE_ID" %in% colnames(data)){
# forprint<-unique(data[c('SITE_ID','sys_sample_code','sample_delivery_group','chemical_name','sample_date','cas_rn','fraction','result_value','result_unit','method_detection_limit','detection_limit_unit','quantitation_limit','lab_qualifiers','validator_qualifiers','interpreted_qualifiers','qaqc_date')])
# sys_sample_code chemical_name sample_date cas_rn fraction result_value result_unit method_detection_limit detection_limit_unit quantitation_limit lab_qualifiers validator_qualifiers interpreted_qualifiers qaqc_date
} else{
forprint<-unique(data[c('sys_sample_code','chemical_name','sample_date','cas_rn','lab_anl_method_name','fraction','result_value','result_unit','method_detection_limit','detection_limit_unit','quantitation_limit','lab_qualifiers','validator_qualifiers','interpreted_qualifiers','qaqc_date','lab_sdg')])
}
```