-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetBitacoras.R
173 lines (148 loc) · 7.76 KB
/
getBitacoras.R
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
library(tidyverse)
library(stringr)
library(readxl)
######################
### READ NEW CASES ###
######################
cases <- readxl::read_xlsx("data/Las víctimas de María (Responses).xlsx",col_types = 'text')
cases <-
cases %>%
mutate(DeathDate = as.character(as.Date(as.numeric(`Fecha del fallecimiento`), origin="1899-12-30"))) %>%
mutate(
case_id = row_number(),
Name = tolower(str_split(`Nombre del fallecido:`,' ',simplify = T)[,1]),
LastName = chartr('ÁáàéÉíÍóÓúÚüÜñÑ', 'aaaeeiioouuuunn', tolower(str_split(`Apellidos:`,' ',simplify = T)[,1])),
DeathMunicipality=chartr('ÁáàéÉíÍóÓúÚüÜñÑ', 'aaaeeiioouuuunn', str_squish(tolower(`Lugar del fallecimiento:`))),
ResidenceMunicipality=chartr('ÁáàéÉíÍóÓúÚüÜñÑ', 'aaaeeiioouuuunn', str_squish(tolower(`Lugar de residencia:`))),
SecondLastName = chartr('ÁáàéÉíÍóÓúÚüÜñÑ', 'aaaeeiioouuuunn', tolower(str_split(`Apellidos:`,' ',simplify = T)[,2])))
#####################
### CASE MATCHING ###
#####################
govt <- read_csv('data/govt_091817_061218.csv')
matches <- cases %>%
select(-Name) %>%
# match on LastName & SecondLastName
left_join(govt) %>%
# Age differences <=5
filter(as.numeric(`Edad del fallecido:`) <= (Age+5) & as.numeric(`Edad del fallecido:`) >= (Age-5)) %>%
# Matching death municipality or residence municipality
filter(DeathMunicipality == MunicipalityDeathPlace | ResidenceMunicipality == ResidencePlace) %>%
# Matching death date: smaller than 10 day difference, OR matching DeathFacility:
filter(
(abs(as.numeric(as.Date(paste(DeathDate_Year, DeathDate_Month, DeathDate_Day, sep='-')) - as.Date(DeathDate)))<=10)
| ((DeathPlace == 'HOSPITALIZADO' | DeathPlace == 'AMBULATORIO/SALA DE EMERGENCIA') & `¿Dónde murió la persona?`=='Hospital/Hospital')
| (DeathPlace == 'RESIDENCIA DE LA PERSONA FALLECIDA' & `¿Dónde murió la persona?`=='Residencia privada')
) %>%
select(case_id, DN, DeathNumber)
matches <- cases %>%
filter(!(case_id %in% matches$case_id))%>%
# Match on Name,LastName
select(-SecondLastName) %>%
left_join(govt) %>%
# Age differences <=5
filter(as.numeric(`Edad del fallecido:`) <= as.numeric(Age+5) & as.numeric(`Edad del fallecido:`) >= as.numeric(Age-5)) %>%
# Matching death municipality or residence municipality
filter(DeathMunicipality == MunicipalityDeathPlace | ResidenceMunicipality == ResidencePlace) %>%
# Matching death date: smaller than 10 day difference, OR matching DeathFacility:
filter(
(abs(as.numeric(as.Date(paste(DeathDate_Year, DeathDate_Month, DeathDate_Day, sep='-')) - as.Date(DeathDate)))<=10)
| ((DeathPlace == 'HOSPITALIZADO' | DeathPlace == 'AMBULATORIO/SALA DE EMERGENCIA') & `¿Dónde murió la persona?`=='Hospital/Hospital')
| (DeathPlace == 'RESIDENCIA DE LA PERSONA FALLECIDA' & `¿Dónde murió la persona?`=='Residencia privada')
) %>%
select(case_id, DN, DeathNumber) %>%
rbind(matches)
matches <- cases %>%
filter(!(case_id %in% matches$case_id))%>%
# Match on Name, SecondLastName
select(-LastName) %>%
left_join(govt) %>%
# Age differences <=5
filter(as.numeric(`Edad del fallecido:`) <= as.numeric(Age+5) & as.numeric(`Edad del fallecido:`) >= as.numeric(Age-5)) %>%
# Matching death municipality or residence municipality
filter(DeathMunicipality == MunicipalityDeathPlace | ResidenceMunicipality == ResidencePlace) %>%
# Matching death date: smaller than 10 day difference, OR matching DeathFacility:
filter(
(abs(as.numeric(as.Date(paste(DeathDate_Year, DeathDate_Month, DeathDate_Day, sep='-')) - as.Date(DeathDate)))<=10)
| ((DeathPlace == 'HOSPITALIZADO' | DeathPlace == 'AMBULATORIO/SALA DE EMERGENCIA') & `¿Dónde murió la persona?`=='Hospital/Hospital')
| (DeathPlace == 'RESIDENCIA DE LA PERSONA FALLECIDA' & `¿Dónde murió la persona?`=='Residencia privada')
) %>%
select(case_id, DN, DeathNumber) %>%
rbind(matches)
matches <- cases %>%
filter(!(case_id %in% matches$case_id))%>%
# Match on SecondLastName alone
select(-LastName, -Name) %>%
left_join(govt) %>%
# Age differences <=5
filter(as.numeric(`Edad del fallecido:`) <= as.numeric(Age+2) & as.numeric(`Edad del fallecido:`) >= as.numeric(Age-2)) %>%
# Matching death municipality or residence municipality
filter(DeathMunicipality == MunicipalityDeathPlace | ResidenceMunicipality == ResidencePlace) %>%
# Matching death date: smaller than 10 day difference, OR matching DeathFacility:
filter(
(abs(as.numeric(as.Date(paste(DeathDate_Year, DeathDate_Month, DeathDate_Day, sep='-')) - as.Date(DeathDate)))<=10)
| ((DeathPlace == 'HOSPITALIZADO' | DeathPlace == 'AMBULATORIO/SALA DE EMERGENCIA') & `¿Dónde murió la persona?`=='Hospital/Hospital')
| (DeathPlace == 'RESIDENCIA DE LA PERSONA FALLECIDA' & `¿Dónde murió la persona?`=='Residencia privada')
) %>%
select(case_id, DN, DeathNumber) %>%
rbind(matches)
matches <- cases %>%
filter(!(case_id %in% matches$case_id))%>%
# Match on LastName alone
select(-SecondLastName, -Name) %>%
left_join(govt) %>%
# Age differences <=5
filter(as.numeric(`Edad del fallecido:`) <= as.numeric(Age+2) & as.numeric(`Edad del fallecido:`) >= as.numeric(Age-2)) %>%
# Matching death municipality or residence municipality
filter(DeathMunicipality == MunicipalityDeathPlace | ResidenceMunicipality == ResidencePlace) %>%
# Matching death date: smaller than 10 day difference, OR matching DeathFacility:
filter(
(abs(as.numeric(as.Date(paste(DeathDate_Year, DeathDate_Month, DeathDate_Day, sep='-')) - as.Date(DeathDate)))<=10)
| ((DeathPlace == 'HOSPITALIZADO' | DeathPlace == 'AMBULATORIO/SALA DE EMERGENCIA') & `¿Dónde murió la persona?`=='Hospital/Hospital')
| (DeathPlace == 'RESIDENCIA DE LA PERSONA FALLECIDA' & `¿Dónde murió la persona?`=='Residencia privada')
) %>%
select(case_id, DN, DeathNumber) %>%
rbind(matches)
matches <- cases %>%
filter(!(case_id %in% matches$case_id))%>%
# Match on Name alone
select(-SecondLastName, -LastName) %>%
left_join(govt) %>%
# Age differences <=5
filter(as.numeric(`Edad del fallecido:`) <= as.numeric(Age+2) & as.numeric(`Edad del fallecido:`) >= as.numeric(Age-2)) %>%
# Matching death municipality or residence municipality
filter(DeathMunicipality == MunicipalityDeathPlace | ResidenceMunicipality == ResidencePlace) %>%
# Matching death date: smaller than 10 day difference, OR matching DeathFacility:
filter(
(abs(as.numeric(as.Date(paste(DeathDate_Year, DeathDate_Month, DeathDate_Day, sep='-')) - as.Date(DeathDate)))<=10)
| ((DeathPlace == 'HOSPITALIZADO' | DeathPlace == 'AMBULATORIO/SALA DE EMERGENCIA') & `¿Dónde murió la persona?`=='Hospital/Hospital')
| (DeathPlace == 'RESIDENCIA DE LA PERSONA FALLECIDA' & `¿Dónde murió la persona?`=='Residencia privada')
) %>%
select(case_id, DN, DeathNumber) %>%
rbind(matches)
matched_cases <- cases %>%
filter(case_id %in% matches$case_id) %>%
left_join(matches) %>%
select(-Name,-LastName,-SecondLastName) %>%
left_join(govt)
###########################
### TAKE OUT DUPLICATES ###
###########################
# load the existing cases
index <- read_csv('data/case_index.csv')
# cases not existing in the current database
matched_cases <- matched_cases %>%
filter(!DN %in% index$DN)
# cases do not replicate themselves
# -- if there are duplicated responses, just pick one
matched_cases <- matched_cases %>%
arrange(DeathNumber) %>%
mutate(last_case = lag(DeathNumber)) %>%
filter(is.na(last_case) | (last_case != DeathNumber)) %>%
select(-last_case)
##########################
### GENERATE BITACORAS ###
##########################
matched_cases <- matched_cases %>%
mutate(id = max(max(index$id),3000) + row_number())
source("fillingBitacoras.R")
## ~~ reporters to process the bitacoras