-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIQ_Report.Rmd
785 lines (597 loc) · 25.6 KB
/
IQ_Report.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
---
date: "Last compiled on `r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
toc: TRUE
toc_float: TRUE
toc_depth: 3
dev: png
keep_md: yes
word_document:
toc: TRUE
toc_depth: 3
pdf_document:
toc: TRUE
toc_depth: 3
dev: png
extra_dependencies: ["float"]
keep_md: yes
urlcolor: blue
title: '`r paste0(h," Indicator Quantile Report")`'
subtitle: 'SEACAR Analysis'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
warning=FALSE,
message=FALSE,
echo=FALSE,
dpi=200
)
```
\newpage
# Overview
## Purpose
The purpose of the indicator quantiles is to flag records that are "unusual" relative to all of the data in the DDI for a given indicator in order to facilitate QA/QC.
They are not used to filter any of the data for SEACAR analyses, and the presence of a LowerQuantile or UpperQuantile flag on a DDI record
alone does not necessarily indicate there is any issue with the record (neither does the absence of a LowerQuantile or UpperQuantile flag
necessarily mean that a data record is correct).
## Relevant file locations
Current values can be found in the "LowQuantile" and "HighQuantile" columns of the "Ref_Parameters" worksheet.
The R script described below and the output file can be found in the
*FloridaSEACAR* *IndicatorQuantiles* repository on GitHub:
* [https://github.com/FloridaSEACAR/IndicatorQuantiles](https://github.com/FloridaSEACAR/IndicatorQuantiles)
## Process steps
**IQ_Report_Render.R & IQ_Report.Rmd**
1. The *IQ_Report_Render.R* script lists all files in a given directory and filters it to a list of DDI exports to
evaluate considering a list of parameters to skip (user-defined).
2. User sets the desired upper and lower quantile thresholds, as well as a number of standard deviations away from the mean to use for the calculations.
3. User sets the string value(s) in the DDI exports that should be considered as NA values.
4. The remainder of the script loops through the file list, returning the values listed below and binding them together by row into a
single Excel spreadsheet that is saved to the User's working directory.
5. For each habitat included in the User's working directory a PDF report will be created in the "output" folder using *IQ_Report.Rmd*,
which provides an overview of questionable / flagged values.
6. In addition to the PDF reports, each habitat will provide a .txt data output file in the "output/data" folder containing questionable values.
\newpage
# Summary
The following quantile thresholds are used for flagging "questionable" values:
* Lower quantile: **`r quant_low`**
* Upper quantile: **`r quant_high`**
```{r excluded_params, results='asis', eval=FALSE}
cat("The following parameters are being excluded from this analysis: ")
cat(" \n")
for(par in parstoskip){
cat(paste0("* *",par,"*"))
}
```
**Included Indicators and Parameters and the files used in this analysis:**
```{r, results='asis'}
cat(" \n\n")
if(h=="Water Column"){
for (type in names(data_directory[[h]])){
cat(paste0("**",type,"**"))
cat(" \n")
indicators <- names(data_directory[[h]][[type]])
for (ind in indicators){
if(type=="Nekton"){
ind_display <- paste0("*Indicator:* ",ind," - *",file_short_list[[type]],"*")
} else {
ind_display <- paste0("*Indicator:* ",ind)
}
cat(ind_display)
params <- names(data_directory[[h]][[type]][[ind]])
cat(" \n")
for (par in params){
if(type=="Nekton"){
par_display <- paste0("* ", par)
} else if(type=="Discrete WQ"){
if(str_detect(par, "Total Nitrogen")){
par_display <- paste0("* ", par, "\n * *", file_short_list[[type]][[ind]][["Total Nitrogen"]], "*")
} else {
par_display <- paste0("* ", par, "\n * *", file_short_list[[type]][[ind]][[par]], "*")
}
} else {
# Continuous file display
file <- str_replace(file_short_list[[type]][[ind]][[par]], "SW|SE|NW|NE", "*AllRegions*")
par_display <- paste0("* ", par, "\n * *", file[[1]], "*")
}
cat("\n")
cat(par_display)
cat("\n")
}
cat(" \n")
}
}
} else {
cat(" \n")
cat(paste0("*",file_short,"*"))
cat(" \n\n")
indicators <- names(data_directory[[h]])
for(ind in indicators){
params <- names(data_directory[[h]][[ind]])
cat(paste0("*Indicator:* ",ind))
cat(" \n")
for(par in params){
cat("\n")
cat(paste0("* ", par))
cat("\n")
}
cat(" \n")
}
}
```
```{r file_name display, results='asis', eval=FALSE}
## display all file names for WC, or just one filename for others
cat(" \n\n")
if(h=="Water Column"){
cat("The data files used for the analysis: \n\n")
for(type in names(file_short_list)){
cat(paste0("**",type,"**"))
cat(" \n")
if(type=="Nekton"){
cat("\n")
cat(paste0("* ", file_short_list[[type]]))
cat(" \n")
} else {
for(indicator in names(file_short_list[[type]])){
cat(paste0("Indicator: ","*",indicator,"*"))
cat(" \n")
for(file in file_short_list[[type]][[indicator]]){
cat("\n")
if(type=="Continuous WQ"){
file <- str_replace(file, "SW|SE|NW|NE", "*AllRegions*")[[1]]
}
cat(paste0("* ", file))
cat("\n")
}
cat(" \n")
}
}
}
} else {
cat(paste0("The data file used for the analysis: **",file_short,"**"))
cat("\n")
}
```
\newpage
# Summary Tables
*q_low*: Value corresponding to the qval_low quantile for the parameter in the DDI export.
*q_high*: Value corresponding to the qval_high quantile for the parameter in the DDI export.
*mean*: Mean value for the parameter in the DDI export.
*n_tot*: Total number of records in the DDI export for the parameter.
*n_q_low*: Number of records in the DDI export that are below q_low for the parameter.
*n_q_high*: Number of records in the DDI export that are above q_high for the parameter.
*pct_flagged*: Proportion of total records in the DDI export for the parameter which have been flagged as above q_high, or below q_low.
```{r table overview, results='asis', eval=TRUE}
options(knitr.kable.NA = "")
if(h=="Water Column"){
for(type in names(data_directory[[h]])){
type_title <- glue("## {type}")
cat("\n\n")
cat(type_title, "\n\n")
indicators <- names(data_directory[[h]][[type]])
for (ind in indicators){
cat(paste0("*Indicator*: **",ind,"**"))
cat("\n\n")
table_data <- water_column_summary_directory[[type]] %>%
filter(Habitat == h, IndicatorName == ind) %>%
select(ParameterName, q_low, q_high, mean, n_tot, n_q_low, n_q_high) %>%
mutate(`pct_flagged` = (n_q_low + n_q_high) / n_tot * 100) %>%
arrange(ParameterName)
names(table_data) <- gsub("_"," ",names(table_data))
table_display <- kable(table_data,
format="simple",
caption="Indicator Quantile Overview",
digits = 2) %>%
kable_styling(latex_options="scale_down",
position = "center")
print(table_display)
cat("\n\n")
}
}
} else {
# Latex format for longer oyster parameter names (quadsize included)
# table_format <- ifelse(h=="Oyster/Oyster Reef", "latex", "simple")
table_format <- "simple"
if(h=="Oyster/Oyster Reef"){
select_columns <- c("ParameterName", "QuadSize_m2", "q_low", "q_high", "mean", "n_tot", "n_q_low", "n_q_high")
} else {
select_columns <- c("ParameterName", "q_low", "q_high", "mean", "n_tot", "n_q_low", "n_q_high")
}
for (ind in indicators){
cat(paste0("*Indicator*: **",ind,"**"))
cat("\n\n")
table_data <- qs %>%
filter(Habitat == h, IndicatorName == ind) %>%
select(select_columns) %>%
mutate(`pct_flagged` = (n_q_low + n_q_high) / n_tot * 100) %>%
arrange(ParameterName)
names(table_data) <- gsub("_"," ",names(table_data))
table_display <- kable(table_data,
format=table_format,
caption="Indicator Quantile Overview",
digits = 2,
booktabs = T) %>%
kable_styling(latex_options=c("scale_down","HOLD_position"),
position = "center")
if(nrow(table_data)>0){
print(table_display)
}
cat("\n\n")
}
}
cat("\n")
```
```{r combined SAV 2, eval=FALSE}
flagged_combined_df <- flagged_combined_df %>%
mutate(
ParameterName = ifelse(
habitat == "Submerged Aquatic Vegetation" & ParameterName %in% c("Percent Occurrence", "Percent Cover"),
ifelse(
CommonIdentifier %in% c("Total seagrass", "Total_SAV"),
paste0(ParameterName, " - Total"),
paste0(ParameterName, " - Species")
),
ParameterName
)
)
```
```{r, quantile_table_display function, eval=TRUE}
quantile_table_display <- function(quantile, hab) {
if (hab == "Water Column"){
# List individual types (disc, cont, nekton) for Water Column habitat
types <- names(data_directory[[hab]])
for (type in types){
if(type == "Nekton"){
i <- "Nekton"
cat("\n\n")
# find parameters for each indicator
parameters <- names(data_directory[[hab]][[type]][[i]])
for (p in parameters) {
# produce flagged data tables for given quantile (high or low)
q_values <- data_directory[[hab]][[type]][[i]][[p]] %>% filter(q_subset == quantile)
if(p %in% parstoskip) next
if (nrow(q_values %>% filter(ParameterName == p)) > 0) {
subtitle <- glue("### {p}")
cat("\n\n")
cat(subtitle, "\n\n")
q_par_values <- q_values %>%
filter(ParameterName == p) %>%
arrange(ProgramID, ProgramLocationID, SampleDate)
# create mask for habitats that include speciesname(CommonID) info in table
spec_habitats <- c("Coral/Coral Reef", "Submerged Aquatic Vegetation")
if (h %in% spec_habitats) {
q_par_values <- q_par_values %>%
select(RowID, ProgramID, ProgramName, ProgramLocationID, SampleDate, CommonIdentifier, ResultValue)
} else {
q_par_values <- q_par_values %>%
select(RowID, ProgramID, ProgramName, ProgramLocationID, SampleDate, ResultValue)
}
flagged_program_id <- unique(q_par_values$ProgramID)
q_value_table <- q_par_values %>%
select(-ProgramName)
low_thres <- water_column_summary_directory[[type]][ParameterName==p, q_low]
high_thres <- water_column_summary_directory[[type]][ParameterName==p, q_high]
threshold <- ifelse(quantile == "low", low_thres, high_thres)
names(q_value_table) <- gsub("_"," ",names(q_value_table))
# Print data table
q_data_table <- kable(q_value_table,
format="simple",
digits = 2,
caption=paste0("Flagged Values - ", str_to_title(quantile), " Indicator Quantile: **",
round(threshold,5),"**")) %>%
kable_styling(latex_options="scale_down",
position = "center")
if(type=="Nekton"){
print(q_data_table)
}
# List programs with flagged data below, with their ID
cat("\n **Programs containing flagged data:** \n \n")
for (p_id in flagged_program_id) {
p_name <- unlist(unique(q_par_values %>% filter(ProgramID == p_id) %>% select(ProgramName)))
cat(paste0("*",p_id,"*", " - ",p_name, " \n"))
}
cat(" \n")
cat("\\newpage")
} else {
cat(" \n")
cat(paste0("There are no *", str_to_title(quantile) ,"* Quantile Flagged Values for ", p))
cat(" \n")
}
}
}
}
cat(" \n")
} else {
# List individual tables for each indicator
indicators <- names(data_directory[[hab]])
for (i in indicators){
indicator_title <- glue("## Indicator: {i}")
cat("\n\n")
cat(indicator_title, "\n\n")
# find parameters for each indicator
parameters <- names(data_directory[[hab]][[i]])
for (p in parameters) {
# produce flagged data tables for given quantile (high or low)
q_values <- data_directory[[hab]][[i]][[p]] %>% filter(q_subset == quantile)
if(p %in% parstoskip) next
# Oyster parameters contain quadsize as variable p so will not filter ParameterName
if(h=="Oyster/Oyster Reef"){
filter_vals <- q_values
} else {
filter_vals <- q_values %>% filter(ParameterName == p)
}
if(nrow(filter_vals) > 0) {
subtitle <- glue("### {p}")
cat("\n\n")
cat(subtitle, "\n\n")
# Oyster parameters contain quadsize as variable p so will not filter ParameterName
if(h=="Oyster/Oyster Reef"){
q_par_values <- q_values %>%
arrange(ProgramID, ProgramLocationID, SampleDate)
} else {
q_par_values <- q_values %>%
filter(ParameterName == p) %>%
arrange(ProgramID, ProgramLocationID, SampleDate)
}
# create mask for habitats that include speciesname(CommonID) info in table
spec_habitats <- c("Coral/Coral Reef", "Submerged Aquatic Vegetation")
if (h %in% spec_habitats) {
q_par_values <- q_par_values %>%
select(RowID, ProgramID, ProgramName, ProgramLocationID, SampleDate, CommonIdentifier, ResultValue)
} else {
q_par_values <- q_par_values %>%
select(RowID, ProgramID, ProgramName, ProgramLocationID, SampleDate, ResultValue)
}
flagged_program_id <- unique(q_par_values$ProgramID)
q_value_table <- q_par_values %>%
select(-ProgramName)
if(h=="Oyster/Oyster Reef"){
parameterID <- unique(q_values$ParameterID)
qSize <- unique(q_values$QuadSize_m2)
low_thres <- qs %>%
dplyr::filter(ParameterID == parameterID,
(QuadSize_m2 == qSize | is.na(QuadSize_m2) & is.na(qSize))) %>%
dplyr::pull(q_low)
high_thres <- qs %>%
dplyr::filter(ParameterID == parameterID,
(QuadSize_m2 == qSize | is.na(QuadSize_m2) & is.na(qSize))) %>%
dplyr::pull(q_high)
} else {
low_thres <- qs_dat %>% dplyr::filter(ParameterName==p) %>% dplyr::pull(q_low)
high_thres <- qs_dat %>% dplyr::filter(ParameterName==p) %>% dplyr::pull(q_high)
}
threshold <- ifelse(quantile == "low", low_thres, high_thres)
names(q_value_table) <- gsub("_"," ",names(q_value_table))
# Print data table
q_data_table <- kable(q_value_table,
format="simple",
digits = 2,
caption=paste0("Flagged Values - ", str_to_title(quantile), " Indicator Quantile: **",
round(threshold,5),"**")) %>%
kable_styling(latex_options="scale_down",
position = "center")
print(q_data_table)
# List programs with flagged data below, with their ID
cat("\n **Programs containing flagged data:** \n \n")
for (p_id in flagged_program_id) {
p_name <- unlist(unique(q_par_values %>% filter(ProgramID == p_id) %>% select(ProgramName)))
cat(paste0("*",p_id,"*", " - ",p_name, " \n"))
}
cat(" \n")
cat("\\newpage")
} else {
cat(" \n")
cat(paste0("There are no *", str_to_title(quantile) ,"* Quantile Flagged Values for ", p))
cat(" \n")
}
}
}
}
}
```
```{r, WC_table display}
wc_program_table <- function(hab, type, i, p){
# produce flagged data tables
q_values <- data_directory[[hab]][[type]][[i]][[p]]
# Check for Total Nitrogen, include sub_param if so
p <- ifelse(str_detect(p, "Total Nitrogen"), "Total Nitrogen", p)
sub_param <- unique(q_values$sub_parameter)
data_summ <- water_column_summary_directory[[type]]
if (nrow(q_values) > 0) {
subtitle <- glue("### {p}")
cat("\n\n")
cat(subtitle, "\n\n")
program_table <- q_values %>%
dplyr::group_by(ProgramID, ProgramName) %>%
dplyr::summarise(n_flagged = n(),
n_low = sum(q_subset=="low"),
n_high = sum(q_subset=="high"), .groups = "keep")
# List of programs to show below table
programs <- as.data.table(program_table[ , c("ProgramID", "ProgramName")])
if(is.null(sub_param)){
# Total data count for display in program_table
program_table$n_tot_all <- data_summ[ParameterName==p, n_tot]
# Low and high quantile values
low_thres <- data_summ[ParameterName==p, q_low]
high_thres <- data_summ[ParameterName==p, q_high]
} else {
program_table$n_tot_all <- data_summ[sub_parameter==sub_param, n_tot]
# Low and high quantile values
low_thres <- data_summ[ParameterName==p & sub_parameter==sub_param, q_low]
high_thres <- data_summ[ParameterName==p & sub_parameter==sub_param, q_high]
}
# Merge program data counts & create percentage (Program & All per parameter)
program_data <- merge(program_table, program_counts[ParameterName==p & typeName==type, ], by = "ProgramID")
program_data <- program_data %>%
mutate(pct_prog = round(((n_flagged / n_tot)*100),5),
pct_all = round(((n_flagged / n_tot_all)*100),5)) %>%
select(ProgramID, n_flagged, n_low, n_high, n_tot, n_tot_all, pct_prog, pct_all)
cat(paste0(
"Quantile Values: \n",
"*Low*: **", round(low_thres,5), "**\n",
"*High*: **", round(high_thres,5), "**\n",
" \n",
"Total data for *",p,"* across all programs: **", unique(program_data$n_tot_all),"**"
))
cat(" \n")
names(program_data) <- gsub("_"," ",names(program_data))
# Print data table
program_kable <- kable(program_data %>% select(-"n tot all"),
format="simple",
digits = 5,
caption=paste0("Flagged data, overview by program for **", p,"**")) %>%
kable_styling(latex_options="scale_down",
position = "center")
print(program_kable)
# List programs with flagged data below, with their ID
cat("\n **Programs containing flagged data:** \n \n")
for (p_id in unique(programs$ProgramID)) {
p_name <- programs[ProgramID==p_id, ProgramName]
cat(paste0("*",p_id,"*", " - ",p_name, " \n"))
}
# Add space between each parameter listing
cat(" \n")
cat(" \n")
cat(" \n")
} else {
cat(" \n")
cat(paste0("There are no *", str_to_title(quantile) ,"* Quantile Flagged Values for ", p))
cat(" \n")
}
}
```
\newpage
```{r WC Formatting, results='asis'}
if(h=="Water Column"){
for(type in names(data_directory[[h]])){
cat("\n\n")
cat("\\newpage")
cat(glue("# {type}"), "\n\n")
# Discrete & Continuous display Program tables
if(!type=="Nekton"){
for(i in names(data_directory[[h]][[type]])){
cat("\n\n")
cat(glue("## {type} - {i}"), "\n\n")
for(p in names(data_directory[[h]][[type]][[i]])){
cat("\n\n")
wc_program_table(h, type, i, p)
}
}
} else {
# Nekton
cat("## Low Quantile")
cat("\n\n")
quantile_table_display("low", h)
cat("\\newpage")
cat(" \n")
cat("## High Quantile")
cat("\n\n")
quantile_table_display("high", h)
}
}
}
```
```{r low quantiles, results='asis', eval=TRUE}
if(!h=="Water Column"){
cat("\\newpage")
cat("# Low Quantile")
cat("\n\n")
quantile_table_display("low", h)
}
```
```{r high quantiles, results='asis', eval=TRUE}
if(!h=="Water Column"){
cat("\\newpage")
cat("# High Quantile")
cat("\n\n")
quantile_table_display("high", h)
}
```
```{r data flag check, results='asis', eval=FALSE}
check_flag <- function(h){
data_combined_subset <- data.table()
if(!h=="Water Column"){
for(i in names(flagged_data_list)){
data_combined_subset <- rbind(data_combined_subset, plyr::rbind.fill(flagged_data_list[[i]]), fill=TRUE)
}
# Compute statistics grouped by ParameterName
flag_table <- data_combined_subset[,
.(n_high = sum(q_subset == "high"),
n_high_flagged = sum(q_subset=="high" & str_detect(SEACAR_QAQCFlagCode, "17Q")),
n_low = sum(q_subset == "low"),
n_low_flagged = sum(q_subset=="low" & str_detect(SEACAR_QAQCFlagCode, "16Q"))),
by = ParameterName]
} else {
for(type in names(data_directory[[h]])){
for(i in names(data_directory[[h]][[type]])){
data2 <- plyr::rbind.fill(data_directory[[h]][[type]][[i]])
data2$type_name <- type
data2$indicator <- i
data_combined_subset <- rbind(data_combined_subset, data2, fill=TRUE)
}
}
# Compute statistics grouped by ParameterName
flag_table <- data_combined_subset[,
.(n_high = sum(q_subset == "high"),
n_high_flagged = sum(q_subset=="high" & str_detect(SEACAR_QAQCFlagCode, "17Q")),
n_low = sum(q_subset == "low"),
n_low_flagged = sum(q_subset=="low" & str_detect(SEACAR_QAQCFlagCode, "16Q")),
indicator = unique(indicator)),
by = list(ParameterName, type_name)]
}
# add signifier to any entry that doesn't have correct flag
# Color coding cells
flag_table <- flag_table %>%
dplyr::rowwise() %>%
dplyr::mutate(cell_color_low = ifelse(n_low == n_low_flagged, "black", "red"),
cell_color_high = ifelse(n_high == n_high_flagged, "black", "red")) %>%
dplyr::mutate(n_low = cell_spec(n_low, color = cell_color_low),
n_low_flagged = cell_spec(n_low_flagged, color = cell_color_low),
n_high = cell_spec(n_high, color = cell_color_high),
n_high_flagged = cell_spec(n_high_flagged, color = cell_color_high)) %>%
select(-c(cell_color_high, cell_color_low))
names(flag_table) <- gsub("_", " ", names(flag_table))
# produce table where quantile flags may not be correctly applied
different_codes <- data_combined_subset[
(q_subset=="high" & !str_detect(SEACAR_QAQCFlagCode, "17Q")) |
(q_subset=="low" & !str_detect(SEACAR_QAQCFlagCode, "16Q")),
c("RowID","ProgramID","ParameterName","ResultValue","SEACAR_QAQCFlagCode","SEACAR_QAQC_Description","q_subset")]
flag_display_table <- kable(flag_table,
format="latex",
digits = 2,
escape = F,
booktabs=T) %>%
kable_styling(latex_options=c("scale_down", "HOLD_position"),
position = "center")
print(flag_display_table)
if(nrow(different_codes)>0){
cat(" \n")
cat("Entries where ResultValue is above or below quantile, but expected SEACAR_QAQCFlagCode is not being applied")
cat(" \n")
code_display_table <- kable(different_codes %>% select(-SEACAR_QAQC_Description) %>% arrange(ProgramID),
format="latex",
digits = 4,
caption="SEACAR QAQC Flag Code discrepancies",
longtable = T,
booktabs = T) %>%
kable_styling(latex_options=c("HOLD_position","repeat_header"),
position = "center",
font_size = 8)
print(code_display_table)
cat(" \n")
for(desc in unique(different_codes$SEACAR_QAQC_Description)){
cat(paste0("* ",desc))
cat(" \n")
}
}
}
cat("# QAQC Quantile Flag Check")
cat(" \n")
cat(paste0("* n_high is the amount of data above the quantile value. \n * n_high_flagged is the amount of data above the quantile value AND containing proper SEACARQAQCFlag of 17Q (ResultValue above quantile value)."))
cat(" \n")
cat(paste0("* n_low is the amount of data below the quantile value. \n * n_low_flagged is the amount of data below the quantile value AND containing proper SEACARQAQCFlag of 16Q (ResultValue below quantile value)."))
cat(" \n")
cat("* If everything is in order, these values should be the same. Any discrepancies therein should be fastidiously noted.")
cat(" \n")
check_flag(h)
```