This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midscalculator.Rmd
228 lines (184 loc) · 5.51 KB
/
midscalculator.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
---
title: "GBIF dataset MIDS calculator"
author: "Mathias Dillen"
date: "10/14/2020"
output: html_document
---
```{r setup, include=F}
knitr::opts_chunk$set(echo = T)
```
```{r packages,include=F}
library(tidyverse)
library(jsonlite)
library(magrittr)
```
## Set working directory
```{r setwd}
setwd("D:/apm/mids/gbifmids")
```
## Read occurrence file
Download a GBIF archive to your working directory, unzip and import the occurrence file.
```{r import}
occ = read_tsv("occurrence.txt",
col_types = cols(.default = "c"),
quote="")
```
## Parse informationWithheld to columns
```{r infwith, include=F}
#infwith is a function to set value to known:withheld if its DwC name is present
#in the dwc:informationWithheld field
#behold the hideous syntax
infwith <- function(df,
colname) {
df %<>%
mutate("{colname}" :=
ifelse(grepl(colname,
informationWithheld),
"known:withheld",
.data[[colname]])
)
return(df)
}
unksub <- function(df,
colname) {
unks = c("unknown",
"unknown:undigitized")
df %<>%
mutate("{colname}" :=
ifelse(.data[[colname]]%in%unks,
NA,
.data[[colname]])
)
return(df)
}
```
Set the value for the property of a record to `known:withheld` if its Darwin Core property name is present in the `dwc:informationWithheld` field. Also set values of `unknown` or `unknown:undigitized` as blanks. These are considered equivalent to undigitized data, which may or may not be available. Contrast with unknown:missing or unknown:undecipherable, which indicate digitization of these data has at least been attempted.
This is currently done for the following Darwin Core properties, using a custom function:
```{r infowithheld}
usedproperties = c("locality",
"verbatimLocality",
"decimalLatitude",
"decimalLongitude",
"county",
"recordedBy",
"recordedByID",
"eventDate",
"year",
"verbatimEventDate")
for (i in 1:length(usedproperties)) {
occ = infwith(occ,
usedproperties[i])
occ = unksub(occ,
usedproperties[i])
}
```
## MIDS level -1
Some specimens may not even achieve MIDS level 0. This happens if they have no physical identifier or if they have no value to indicate where they are curated.
```{r minus}
occminus = occ %>%
filter((is.na(catalogNumber)&
is.na(otherCatalogNumbers)&
is.na(materialSampleID))|
(is.na(institutionCode)&
is.na(collectionCode)&
is.na(ownerInstitutionCode)&
is.na(institutionID)&
is.na(collectionID)&
is.na(rightsHolder)))
occminus$mids = -1
occ %<>%
filter(!occurrenceID%in%occminus$occurrenceID)
```
## MIDS level 0
Specimens are stuck at level 0 mainly if they have no (proper) scientific name. This can be checked by using the Taxon Name Resolution Service (http://tnrs.iplantc.org/). GBIF performs a validation step during ingestion, so we can take the output of this by removing records flagged with `TAXON_MATCH_NONE`.
```{r zero}
occ0 = occ %>%
filter(is.na(scientificName)|
grepl("TAXON_MATCH_NONE",
issue))
occ0$mids = 0
occ %<>%
filter(!occurrenceID%in%occ0$occurrenceID)
```
## MIDS level 1
Specimens at level 1 have a proper scientific name, but are missing one of the following:
* No value for `dwc:county`, `dwc:locality`, `dwc:verbatimLocality` and decimal coordinates.
* No value for `dwc:recordedBy`. No value for `gbif:recordedByID`.
* No value for `dwc:eventDate`, `dwc:year` and `dwc:verbatimEventDate`.
```{r one}
occ1 = occ %>%
filter(
(is.na(county)&
is.na(locality)&
is.na(verbatimLocality)&
is.na(decimalLatitude)&
is.na(decimalLongitude))|
(is.na(recordedBy)&
is.na(recordedByID))|
(is.na(eventDate)&
is.na(year)&
is.na(verbatimEventDate))
)
occ1$mids = 1
occ %<>%
filter(!occurrenceID%in%occ1$occurrenceID)
```
## MIDS level 2
Specimens are at level 2 if they have no image or if GBIF flagged them with a certain issue.
```{r two}
occ2 = occ %>%
filter(is.na(mediaType),
!is.na(issue))
occ2$mids = 2
occ %<>%
filter(!occurrenceID%in%occ2$occurrenceID)
```
## MIDS level 3
Everything remaining. Summary stats:
```{r three}
occ$mids = 3
summ= tibble(n = c(
dim(occminus)[1],
dim(occ0)[1],
dim(occ1)[1],
dim(occ2)[1],
dim(occ)[1]))
summ %<>%
mutate(perc = round(100*n/sum(n),
digits=2))
print(
paste0("Number of records at -1: ",
dim(occminus)[1],
" or ",
summ$perc[1],
"%")
)
print(
paste0("Number of records at 0: ",
dim(occ0)[1],
" or ",
summ$perc[2],
"%")
)
print(
paste0("Number of records at 1: ",
dim(occ1)[1],
" or ",
summ$perc[3],
"%")
)
print(
paste0("Number of records at 2: ",
dim(occ2)[1],
" or ",
summ$perc[4],
"%")
)
print(
paste0("Number of records at 3: ",
dim(occ)[1],
" or ",
summ$perc[5],
"%")
)
```