-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1_Processing_raw_data_quality_test.Rmd
More file actions
executable file
·294 lines (207 loc) · 9.15 KB
/
Copy path1_Processing_raw_data_quality_test.Rmd
File metadata and controls
executable file
·294 lines (207 loc) · 9.15 KB
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
---
title: "Data exploration and cleaning"
output:
html_document:
df_print: paged
word_document: default
pdf_document: default
---
This notebook looks for common sources of error and flags those records manual revision by the assessors who capture data from each country.
The output are:
* a report of the quality check
* A .csv file (called ´kobo_output_tocheck.csv´) showing **the records that need manual review or corrections**, if any.
* A .csv file (called ´kobo_output_clean.csv´) with the data after processing (**records flagged in the previous file may or may not be included according to user choice**).
## Variables that need to be set by the user
Output from KoboToolbox in .csv format as downloaded using [these instructions](https://ccgenetics.github.io/guidelines-genetic-diversity-indicators/docs/5_Data_collection/Kobo_toolbox_help.html).
```{r}
kobo_file="International_Genetic_Indicator_testing_FOR_Ginkotests_noJapan.csv"
```
Should records that need manual review and potentially correction be removed or kept in the exported "clean" data?. True to keep, false to filter them. Defaults to FALSE
```{r}
keep_to_check=FALSE #true to keep, false to filter them. Defaults to FALSE
```
## Libraries and functions
Load required libraries:
```{r, warning=FALSE, message=FALSE}
library(tidyr)
library(dplyr)
library(utile.tools)
library(stringr)
library(ggplot2)
```
Useful custom functions
```{r}
# not in
"%!in%" <- function(x, y)!('%in%'(x,y))
```
## Get data
Get Kobo raw output data:
```{r}
#kobo_file variable is defined at the start of this script, it should be csv file
kobo_output<-read.csv(file=kobo_file, sep=";", header=TRUE) %>%
## add taxon column
mutate(taxon=(utile.tools::paste(genus, species, subspecies_variety, na.rm=TRUE))) %>%
# remove white space at the end of the name
mutate(taxon=str_trim(taxon, "right"))
```
Filter out records which were marked as "not_approved" in the manual Kobo validation interface (this means country assessors determined the is something wrong with that particular record).
```{r}
# check if any species is flagged as "validation_status_not_approved"
kobo_output %>%
filter(X_validation_status=="validation_status_not_approved") %>%
select(country_assessment, name_assessor, taxon)
# omit those records from data:
kobo_output<- kobo_output %>%
filter(X_validation_status!="validation_status_not_approved")
```
### Filter out any sort of tests
```{r}
# select likely columns to say "test"
cols= c("name_assessor", "email_assessor", "genus", "species", "subspecies_variety",
"scientific_authority", "common_name", "GBIF_taxonID", "NCBI_taxonID", "time_populations")
# check for "test" or "template" on any of them
kobo_output %>%
filter(if_any(all_of(cols), ~ grepl("test", .)) |
if_any(all_of(cols), ~ grepl("Template", .))) %>%
select(country_assessment, name_assessor, genus, species)
# filter them out of dataset
kobo_output<- kobo_output %>%
filter(if_any(all_of(cols), ~ !grepl("test", .))) %>%
filter(genus!="Template")
```
## Check for common data capture errors
### Number of populations
In the form, -999 was used to mark taxa with unknown number of extant populations. This was used because answering the question was mandatory, so leaving it blank wasn't possible. We have to change -999 to NA.
```{r}
kobo_output<-kobo_output %>%
mutate(n_extant_populations= na_if(n_extant_populations, -999))
```
We can now explore how many populations per species are still extant (still existing! NOT extinct!)?
```{r}
summary(kobo_output$n_extant_populations)
table(kobo_output$n_extant_populations)
```
Plot histogram
```{r}
ggplot(kobo_output, aes(x=n_extant_populations))+
geom_histogram()
```
Zoom Plot histogram
```{r}
kobo_output %>%
filter(n_extant_populations>=0, n_extant_populations<25) %>%
ggplot(., aes(x=n_extant_populations))+
geom_histogram()
```
Once -999 was replaced by NA there should be no negative number of populations (if they are, they are typos that need to be corrected).
```{r}
kobo_output %>%
filter(n_extant_populations<0) %>%
select(country_assessment, taxon, name_assessor, n_extant_populations, n_extinct_populations)
```
Show which taxa (if any) have 0 (zero) extant populations. **Is this correct? needs to be manually checked**
```{r}
kobo_output %>%
filter(n_extant_populations==0) %>%
select(country_assessment, taxon, name_assessor, n_extant_populations, n_extinct_populations)
```
Show which species (if any) have 999 extant populations. **Should this be -999? OR n_extinct pops??**
```{r}
kobo_output %>%
filter(n_extant_populations==999) %>%
select(country_assessment, taxon, name_assessor, n_extant_populations, n_extinct_populations)
```
Show which species (if any) have 999 EXTINCT populations. **Should this be -999?**
```{r}
kobo_output %>%
filter(n_extinct_populations==999) %>%
select(country_assessment, taxon, name_assessor, n_extant_populations, n_extinct_populations)
```
Put all taxa with weird number of populations that need to be checked together:
```{r}
check_n_pops <- kobo_output %>%
# variables of interest
select(country_assessment, name_assessor, taxon, n_extant_populations, n_extinct_populations) %>%
# same filters that discussed above
filter(n_extant_populations<0 |
n_extant_populations==0 |
n_extant_populations==999 |
n_extinct_populations==999) %>%
# add a column stating what needs to be checked:
mutate(need_to_check="check number of extant or extint populations. Are 0 correct? should 999 be -999? are extant/extint confused?")
```
### GBIF ID codes
Check GBIF
```{r}
# check IDs
head(kobo_output$GBIF_taxonID)
```
GBIF IDs tend to be 7 characters long. Some can be larger or shorter, but these seem to be exceptions. Therefore let's flag any records where the GBIF Id is =/= 7 to manually check if it is correct.
```{r}
kobo_output %>%
filter(nchar(GBIF_taxonID)>0, nchar(GBIF_taxonID)!=7) %>%
# show only relevant columns
select(country_assessment, name_assessor, taxon, GBIF_taxonID)
```
Put them in their own happy df with a column stating what is the likely problem:
```{r}
check_GBIF <- kobo_output %>%
filter(nchar(GBIF_taxonID)>0, nchar(GBIF_taxonID)!=7) %>%
# show only relevant columns
select(country_assessment, name_assessor, taxon, GBIF_taxonID) %>%
# add a column stating what needs to be checked:
mutate(need_to_check="check the GBIF taxonID. Either it looks plain different, or has more or less than 7 digits (most ids are 7 digits long, and this isn't, it could be an exception, or a mistake).")
```
### Species names
Genus, species and subspecies should be a single word, check if there are cases where it isn't. Only exception would be "var." or "subsp." in the subspecies_variety field:
```{r}
kobo_output %>%
filter(grepl(" ", genus) |
grepl(" ", species) |
grepl(" ", subspecies_variety)) %>%
filter(!grepl("var.", subspecies_variety)) %>%
filter(!grepl("subsp.", subspecies_variety)) %>%
# show only relevant columns
# show only relevant columns
select(country_assessment, name_assessor, taxon, genus, species, subspecies_variety)
```
Put them in their own happy df with a column stating what is the likely problem:
```{r}
check_taxon_names <- kobo_output %>%
filter(grepl(" ", genus) |
grepl(" ", species) |
grepl(" ", subspecies_variety)) %>%
filter(!grepl("var.", subspecies_variety)) %>%
filter(!grepl("subsp.", subspecies_variety)) %>%
# show only relevant columns
select(country_assessment, name_assessor, taxon, genus, species, subspecies_variety) %>%
mutate(need_to_check="check genus, species or subspecies_variety, we are targeting to have single words in each field, except in the ifraspecific names, where 'var.' and 'subsp.' (only) would be accepted. Other details or taxonomic notes should be added in the comments.")
```
## Create a single file for assessors review:
```{r}
to_check<-full_join(check_n_pops, check_GBIF) %>% full_join(check_taxon_names) %>%
# show columns in desired order:
select(country_assessment, name_assessor, taxon, need_to_check, n_extant_populations,
n_extinct_populations, GBIF_taxonID, genus, species, subspecies_variety)
# save file:
write.csv(to_check, "kobo_output_tocheck.csv", row.names = FALSE, fileEncoding = "UTF-8")
```
## Save the clean koboutput version, removing any taxa with issues:
Asks the user if she/he wants to keep the taxa flagged in the previous step, or if they should be filtered out.
```{r}
# keep_to_check variable is defined at the start of the script. It should be TRUE/FALSE
if(keep_to_check==FALSE){
# Remove from the clean version any remaining taxa with issues
kobo_clean<-kobo_output %>%
filter(taxon %!in% to_check$taxon)
print(paste("A total of", nrow(to_check), "records were excluded from the cleaned data"))
}
```
Export clean version
```{r}
write.csv(kobo_clean, "kobo_output_clean.csv", row.names = FALSE, fileEncoding = "UTF-8")
```
Session Info for reproducibility purposes:
```{r}
sessionInfo()
```