forked from impact-initiatives/cleaningtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
574 lines (487 loc) · 18.9 KB
/
README.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
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
devtools::load_all()
deletion <- cleaningtools::cleaningtools_cleaning_log |> dplyr::filter(change_type == "remove_survey")
cleaning_log2 <- cleaningtools::cleaningtools_cleaning_log |> dplyr::filter(change_type != "remove_survey")
options(scipen = 999)
```
## cleaningtools
<!-- badges: start -->
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](code_of_conduct.md)
[![R-CMD-check](https://github.com/impact-initiatives/cleaningtools/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/impact-initiatives/cleaningtools/actions/workflows/R-CMD-check.yaml)
[![codecov](https://codecov.io/gh/impact-initiatives/cleaningtools/branch/master/graph/badge.svg?token=SOH3NGXQDU)](https://codecov.io/gh/impact-initiatives/cleaningtools)
<!-- badges: end -->
## Overview
The `cleaningtools` package focuses on cleaning, and has three components: <p>
**1. Check**, which includes a set of functions that flag values, such as check_outliers and check_logical.
<br> **2. Create**, which includes a set of functions to create different items for use in cleaning, such as the cleaning log from the checks, clean data, and enumerator performance.
<br> **3. Review**, which includes a set of functions to review the cleaning, such as reviewing the cleaning.
## Installation
You can install the development version from [GitHub](https://github.com/) with:
``` {r, eval=FALSE}
# install.packages("devtools")
devtools::install_github("impact-initiatives/cleaningtools")
```
## Template
You can use the R studio wizard to create project template.
![](man/figures/template_wizard.png)
## Examples
### 1. Checks on the dataset
#### 1.1 Check of PII
`check_pii()` function takes raw data (input can be dataframe or list. However incase of list, you must specify the element name in `element_name` parameter!) and looks for potential PII in the dataset. By default, the function will look for following words but you can also add additional words to look by using `words_to_look` parameter.The default words are-`c("telephone","contact","name","gps","neighbourhood","latitude","logitude","contact","nom","gps","voisinage")`.
The function will give a list with two element. One will be the data and second one will be the list of potential PII
* Using dataframe as input
``` {r, eval=T}
output_from_data <- cleaningtools::check_pii(dataset = cleaningtools::cleaningtools_raw_data, words_to_look = "date", uuid_column = "X_uuid")
output_from_data$potential_PII |> head()
```
* Using list as input
``` {r, eval=T}
### from list
df_list <- list(raw_data = cleaningtools::cleaningtools_raw_data)
output_from_list <- cleaningtools::check_pii(dataset = df_list, element_name = "raw_data", words_to_look = "date", uuid_column = "X_uuid")
output_from_list$potential_PII |> head()
```
#### 1.2 Check of duration from audits
##### 1.2.1 Reading the audits files
It will read only the compressed file.
```{r, eval = FALSE}
my_audit_list <- cleaningtools::create_audit_list(audit_zip_path = "audit_for_tests_100.zip")
```
##### 1.2.2 Adding the duration to the dataset
Once you have read your audit file from the zip, you will get a list of audit.
You can use this list to calculate and add the duration. You have 2 options with
a start and end question or summing all the durations.
```{r}
list_audit <- list(
uuid1 = data.frame(
event = c("form start", rep("question", 5)),
node = c("", paste0("/xx/question", 1:5)),
start = c(
1661415887295, 1661415887301,
1661415890819, 1661415892297,
1661415893529, 1661415894720
),
end = c(
NA, 1661415890790, 1661415892273,
1661415893506, 1661415894703,
1661415896452
)
),
uuid2 = data.frame(
event = c("form start", rep("question", 5)),
node = c("", paste0("/xx/question", 1:5)),
start = c(1661415887295, 1661415887301, 1661415890819, 1661415892297, 1661415893529, 1661415894720),
end = c(NA, 1661415890790, 1661415892273, 1661415893506, 1661415894703, 1661415896452)
)
)
some_dataset <- data.frame(
X_uuid = c("uuid1", "uuid2"),
question1 = c("a", "b"),
question2 = c("a", "b"),
question3 = c("a", "b"),
question4 = c("a", "b"),
question5 = c("a", "b")
)
```
If you want to sum all the duration.
```{r}
cleaningtools::add_duration_from_audit(some_dataset, uuid_column = "X_uuid", audit_list = list_audit)
```
If you want to use calculate duration between 2 questions.
```{r}
cleaningtools::add_duration_from_audit(some_dataset,
uuid_column = "X_uuid", audit_list = list_audit,
start_question = "question1",
end_question = "question3",
sum_all = F
)
```
If you want to do both.
```{r}
cleaningtools::add_duration_from_audit(some_dataset,
uuid_column = "X_uuid", audit_list = list_audit,
start_question = "question1",
end_question = "question3",
sum_all = T
)
```
##### 1.2.3 checking the duration of the dataset
Once you have added the duration to the dataset, you can check if duration are between the threshold you are looking for.
``` {r, eval=T}
testdata <- data.frame(
uuid = c(letters[1:7]),
duration_audit_start_end_ms = c(
2475353, 375491, 2654267, 311585, 817270,
2789505, 8642007
),
duration_audit_start_end_minutes = c(41, 6, 44, 5, 14, 46, 144)
)
cleaningtools::check_duration(testdata, column_to_check = "duration_audit_start_end_minutes") |> head()
cleaningtools::check_duration(
testdata,
column_to_check = "duration_audit_start_end_ms",
lower_bound = 375490,
higher_bound = 8642000
) |> head()
testdata %>%
cleaningtools::check_duration(column_to_check = "duration_audit_start_end_minutes") %>%
check_duration(
column_to_check = "duration_audit_start_end_ms",
log_name = "duration_in_ms",
lower_bound = 375490,
higher_bound = 8642000
) |>
head()
```
#### 1.3 Check outliers
`check_outliers()` takes raw data set and look for potential outlines. It can both data frame or list. However you must specify the element name (name of your data set in the given list) in `element_name` parameter!
``` {r, eval=T}
set.seed(122)
### from list
df_outlier <- data.frame(
uuid = paste0("uuid_", 1:100),
one_value = c(round(runif(90, min = 45, max = 55)), round(runif(5)), round(runif(5, 99, 100))),
expense = c(sample(200:500, replace = T, size = 95), c(600, 100, 80, 1020, 1050)),
income = c(c(60, 0, 80, 1020, 1050), sample(20000:50000, replace = T, size = 95)),
yy = c(rep(100, 99), 10)
)
outliers <- cleaningtools::check_outliers(dataset = df_outlier, uuid_column = "uuid")
outliers$potential_outliers |> head()
```
#### 1.4 Check for value
`check_value()` function look for specified value in the given data set and return in a cleaning log format. The function can take a data frame or a list as input.
``` {r, eval=T}
set.seed(122)
df <- data.frame(
X_uuid = paste0("uuid_", 1:100),
age = c(sample(18:80, replace = T, size = 96), 99, 99, 98, 88),
gender = c("99", sample(c("male", "female"), replace = T, size = 95), "98", "98", "88", "888")
)
output <- cleaningtools::check_value(dataset = df, uuid_column = "X_uuid", element_name = "checked_dataset", values_to_look = c(99, 98, 88, 888))
output$flaged_value |> head()
```
#### 1.5 Check logics
`check_logical()` takes a regular expression, as it is how it will be read from Excel `check_logical_with_list()`.
```{r}
test_data <- data.frame(
uuid = c(1:10) %>% as.character(),
today = rep("2023-01-01", 10),
location = rep(c("villageA", "villageB"), 5),
distance_to_market = c(rep("less_30", 5), rep("more_30", 5)),
access_to_market = c(rep("yes", 4), rep("no", 6)),
number_children_05 = c(rep(c(0, 1), 4), 5, 6)
)
cleaningtools::check_logical(test_data,
uuid_column = "uuid",
check_to_perform = "distance_to_market == \"less_30\" & access_to_market == \"no\"",
columns_to_clean = "distance_to_market, access_to_market",
description = "distance to market less than 30 and no access"
) |> head()
```
```{r}
test_data <- data.frame(
uuid = c(1:10) %>% as.character(),
distance_to_market = rep(c("less_30", "more_30"), 5),
access_to_market = c(rep("yes", 4), rep("no", 6)),
number_children_05 = c(rep(c(0, 1), 4), 5, 6),
number_children_618 = c(rep(c(0, 1), 4), 5, 6)
)
check_list <- data.frame(
name = c("logical_xx", "logical_yy", "logical_zz"),
check = c(
"distance_to_market == \"less_30\" & access_to_market == \"no\"",
"number_children_05 > 3",
"rowSums(dplyr::across(starts_with(\"number\")), na.rm = T) > 9"
),
description = c(
"distance to market less than 30 and no access",
"number of children under 5 seems high",
"number of children very high"
),
variables_to_clean = c(
"distance_to_market, access_to_market",
"number_children_05",
""
)
)
cleaningtools::check_logical_with_list(test_data,
uuid_column = "uuid",
list_of_check = check_list,
check_id_column = "name",
check_to_perform_column = "check",
columns_to_clean_column = "variables_to_clean",
description_column = "description"
) |> head()
```
#### 1.6 Check for duplicates
##### 1.6.1 With one or several variables
```{r}
testdata <- data.frame(
uuid = c(letters[1:4], "a", "b", "c"),
col_a = runif(7),
col_b = runif(7)
)
cleaningtools::check_duplicate(testdata)
```
Or you can check duplicate for a specific variable or combination of variables.
```{r}
testdata2 <- data.frame(
uuid = letters[c(1:7)],
village = paste("village", c(1:3, 1:3, 4)),
ki_identifier = paste0("xx_", c(1:5, 3, 4))
)
check_duplicate(testdata2, columns_to_check = "village")
check_duplicate(testdata2, columns_to_check = c("village", "ki_identifier"))
```
##### 1.6.2 With the gower distance (soft duplicates)
To use it with the complete dataset:
```{r}
soft_duplicates <- check_soft_duplicates(
dataset = cleaningtools_raw_data,
kobo_survey = cleaningtools_survey,
uuid_column = "X_uuid",
idnk_value = "dont_know",
sm_separator = ".",
log_name = "soft_duplicate_log",
threshold = 7
)
soft_duplicates[["soft_duplicate_log"]] %>% head()
soft_duplicates <- check_soft_duplicates(
dataset = cleaningtools_raw_data,
kobo_survey = cleaningtools_survey,
uuid_column = "X_uuid",
idnk_value = "dont_know",
sm_separator = ".",
log_name = "soft_duplicate_log",
threshold = 7,
return_all_results = TRUE
)
soft_duplicates[["soft_duplicate_log"]] %>% head()
```
To use it grouping by enumerator:
```{r}
group_by_enum_raw_data <- cleaningtools_raw_data %>%
dplyr::group_by(enumerator_num)
soft_per_enum <- group_by_enum_raw_data %>%
dplyr::group_split() %>%
purrr::map(~ check_soft_duplicates(
dataset = .,
kobo_survey = cleaningtools_survey,
uuid_column = "X_uuid", idnk_value = "dont_know",
sm_separator = ".",
log_name = "soft_duplicate_log",
threshold = 7,
return_all_results = TRUE
))
soft_per_enum %>%
purrr::map(~ .[["soft_duplicate_log"]]) %>%
purrr::map2(
.y = dplyr::group_keys(group_by_enum_raw_data) %>% unlist(),
~ dplyr::mutate(.x, enum = .y)
) %>%
do.call(dplyr::bind_rows, .) %>%
head()
```
#### 1.7 Check the food consumption score
The `check_fcs()` function verifies whether all the food consumption components have identical values or not. It will flag the UUIDs where all the values are the same.
```{r warning=TRUE}
cleaningtools::check_fcs(
dataset = cleaningtools::cleaningtools_food_consumption_df,
uuid_column = "X_uuid",
cereals_column = "cereals_grains_roots_tubers",
pulses_column = "beans_legumes_pulses_nuts",
dairy_column = "milk_dairy_products",
meat_column = "meat_fish_eggs",
vegetables_column = "vegetables",
fruits_column = "fruite",
oil_column = "oil_fat_butter",
sugar_column = "sugar_sugary_food"
) |> head()
```
#### 1.8 Check others values
The `check_others()` function generate a log for other follow up questions
```{r}
output <- cleaningtools::check_others(
dataset = cleaningtools::cleaningtools_clean_data,
uuid_column = "X_uuid",
columns_to_check = names(cleaningtools::cleaningtools_clean_data |>
dplyr::select(ends_with("_other")) |>
dplyr::select(-contains(".")))
)
output$other_log |> head()
```
#### 1.9 Check percentage of missing values.
##### 1.9.1 Add the percentage missing
The `add_percentage_missing()` adds the percentage of missing values per row.
```{r warning=FALSE}
data_example <- data.frame(
uuid = letters[1:3],
col_1 = c(1, NA, 3),
col_2 = c(NA, NA, "expenditures"),
col_3 = c("with need", NA, "with need"),
col_4 = c("food health school", NA, "food"),
col_4.food = c(1, NA, 1),
col_4.health = c(1, NA, 0),
col_4.school = c(1, NA, 0)
)
data_example <- data_example %>% cleaningtools::add_percentage_missing()
data_example |> head()
```
##### 1.9.2 add_percentage_missing()
The `add_percentage_missing()` function will flag if a survey for its missing values. The missing values column can be created with add_percentage_missing and the values are flagged with check_outliers.
```{r}
data_example %>%
cleaningtools::check_percentage_missing() |>
head()
```
### 2. Exporting the flags in excel.
#### 2.1 create_combined_log()
The function `create_combined_log()` takes the cleaning logs as input and returns a list with two elements: the dataset and the combined cleaning log.
```{r message=FALSE}
list_log <- cleaningtools::cleaningtools_raw_data |>
check_pii(uuid_column = "X_uuid") |>
check_duplicate(uuid_column = "X_uuid") |>
check_value(uuid_column = "X_uuid") |>
create_combined_log()
list_log$cleaning_log |> head(6)
```
#### 2.2 `add_info_to_cleaning_log()`
The function `add_info_to_cleaning_log()` is designed to add information from the dataset into the cleaning log.
```{r message=FALSE}
add_with_info <- list_log |> add_info_to_cleaning_log(dataset_uuid_column = "X_uuid")
add_with_info$cleaning_log |> head()
```
#### 2.3 `create_xlsx_cleaning_log()`
The function `add_info_to_cleaning_log()` is designed to add information from the dataset into the cleaning log.
```{r eval = FALSE}
add_with_info |>
create_xlsx_cleaning_log(
kobo_survey = cleaningtools_survey,
kobo_choices = cleaningtools_choices,
use_dropdown = TRUE,
output_path = "mycleaninglog.xlsx"
)
```
### 3. Create a clean data
We are creating a dataset and cleaning log for the example
``` {r, eval=T, message=FALSE}
test_data <- data.frame(
uuid = paste0("uuid", 1:4),
age = c(180, 23, 45, 67),
gender = c("male", "female", "male", "female"),
pop_group = c("idp", "refugee", "host", "idp"),
strata = c("a", "b", "c", "d")
)
test_data
```
``` {r, eval=T, message=FALSE}
cleaning_log_test <- data.frame(
uuid = paste0("uuid", 1:4),
question = c("age", "gender", "pop_group", "strata"),
change_type = c("blank_response", "no_change", "Delete", "change_res"),
new_value = c(NA_character_, NA_character_, NA_character_, "st-a")
)
cleaning_log_test
```
#### 3.1 Check the cleaning log
After obtaining both the cleaning log and dataset, it is considered good practice to utilize the review_cleaning_log() function to ensure the consistency between the cleaning log and the dataset. It is highly recommended to perform this check on a daily basis, enabling you to promptly identify any issues right from the outset.
``` {r, eval=T, message=TRUE}
cleaningtools::review_cleaning_log(
raw_dataset = test_data,
raw_data_uuid_column = "uuid",
cleaning_log = cleaning_log_test,
cleaning_log_change_type_column = "change_type",
change_response_value = "change_res",
cleaning_log_question_column = "question",
cleaning_log_uuid_column = "uuid",
cleaning_log_new_value_column = "new_value"
)
```
#### 3.2 Create the clean data from the raw data and cleaning log
Once you have a perfect cleaning log and the raw dataset, you can create clean data by applying`create_clean_data()` function.
``` {r, eval=T, message=FALSE}
cleaningtools::create_clean_data(
raw_dataset = test_data,
raw_data_uuid_column = "uuid",
cleaning_log = cleaning_log_test,
cleaning_log_change_type_column = "change_type",
change_response_value = "change_res",
NA_response_value = "blank_response",
no_change_value = "no_change",
remove_survey_value = "Delete",
cleaning_log_question_column = "question",
cleaning_log_uuid_column = "uuid",
cleaning_log_new_value_column = "new_value"
)
```
#### 3.3 Recreate parent column for choice multiple
`recreate_parent_column()` recreates the concerted columns for select multiple questions
``` {r, eval=T}
test_data <- dplyr::tibble(
uuid = paste0("uuid_", 1:6),
gender = rep(c("male", "female"), 3),
reason = c(
"xx,yy", "xx,zy",
"zy", "xx,xz,zy",
NA_character_, "xz"
),
reason.x.x. = c(0, 1, 0, 1, 0, 0),
reason.yy = c(1, 0, 0, 0, 1, 0),
reason.x.z = c(0, 0, 0, 1, 0, 1),
reason.zy = c(0, 1, 1, 1, 0, 0),
reason_zy = c(NA_character_, "A", "B", "C", NA_character_, NA_character_)
)
cleaningtools::recreate_parent_column(dataset = test_data, uuid_column = "uuid", sm_separator = ".") |> head()
```
### 4. Review of the cleaning
#### 4.1 Review cleaning log with clean data and raw data
`review_cleaning` function takes raw data, clean data and cleaning log as inputs, and it first creates the cleaning log by comparing raw data and clean data, then compares it with the user-provided cleaning log. Finally, flagged the discrepancies between them (if any).
``` {r, eval=T, message=FALSE}
compared_df <- review_cleaning(
raw_dataset = cleaningtools::cleaningtools_raw_data,
raw_dataset_uuid_column = "X_uuid",
clean_dataset = cleaningtools::cleaningtools_clean_data,
clean_dataset_uuid_column = "X_uuid",
cleaning_log = cleaning_log2,
cleaning_log_uuid_column = "X_uuid",
cleaning_log_question_column = "questions",
cleaning_log_new_value_column = "new_value",
cleaning_log_old_value_column = "old_value",
deletion_log = deletion,
deletion_log_uuid_column = "X_uuid",
check_for_deletion_log = T
)
compared_df |> head()
```
#### 4.2 Example:: The `review_others()` function reviews discrepancy between kobo relevancies and the dataset
```{r , warning=F}
review_others(
dataset = cleaningtools::cleaningtools_clean_data,
uuid_column = "X_uuid", kobo_survey = cleaningtools::cleaningtools_survey
) |> head()
```
#### 4.3 Example:: `review_sample_frame_with_dataset()`
`review_sample_frame_with_dataset()` compares the sample frame with dataset and provide the overview of completed and remaining surveys.
``` {r, eval=T,warning =F}
review_output <- cleaningtools::review_sample_frame_with_dataset(
sample_frame = cleaningtools::cleaningtools_sample_frame,
sampling_frame_strata_column = "Neighbourhood",
sampling_frame_target_survey_column = "Total.no.of.HH",
clean_dataset = cleaningtools::cleaningtools_clean_data,
clean_dataset_strata_column = "neighbourhood",
consent_column = "consent_remote",
consent_yes_value = "yes"
)
review_output |> head()
```
## Code of Conduct
Please note that the cleaningtools project is released with a [Contributor Code of Conduct](https://impact-initiatives.github.io/cleaningtools/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.