rm(list=ls(all=t))

Setup filenames, data, functions and create dictionary for dataset review

filename <- "ehsection4" # !!!Update filename
functions_vers <-  "functions_1.7.R" # !!!Update helper functions file
source (functions_vers)

Visually inspect variables in "dictionary.csv" and flag for risk, using the following flags:

# Direct PII: Respondent Names, Addresses, Identification Numbers, Phone Numbers
# Direct PII-team: Interviewer Names, other field team names 
# Indirect PII-ordinal: Date of birth, Age, income, education, household composition. 
# Indirect PII-categorical: Gender, education, ethnicity, nationality,
# occupation, employer, head of household, marital status
# GPS: Longitude, Latitude
# Small Location: Location (<100,000) 
# Large Location (>100,000)
# Weight: weightVar
# Household ID:  hhId, 
# Open-ends: Review responses for any sensitive information, redact as necessary 

Direct PII: variables to be removed

# !!! No Direct PII

Direct PII-team: Encode field team names

# !!! No Direct PII-team

Small locations: Encode locations with pop <100,000 using random large numbers

!!!Include relevant variables, but check their population size first to confirm they are <100,000

dropvars <- c("dise") 
mydata <- mydata[!names(mydata) %in% dropvars]

locvars <- c("a006_a_block_id", "a007_a_vill_id") 
mydata <- encode_location (variables= locvars, missing=999999)
## [1] "Frequency table before encoding"
## a006_a_block_id. 006 Block ID
##   1   2   3   4   5   6   7   8   9 
## 212 171 196 416 100 197 158 432 552 
## [1] "Frequency table after encoding"
## a006_a_block_id. 006 Block ID
## 279 280 281 282 283 284 285 286 287 
## 212 432 171 197 196 552 158 100 416 
## [1] "Frequency table before encoding"
## a007_a_vill_id. 007 Village ID
##   1   2   3   4   5   6   7   8   9  10  11  12  13  15  16  17  18  19  20  21  22  23  24  25  26  27  28 
##  17  16  17  16  20  30  29  16  15  15  17  26  24  15  18  21  18  18  20  30  23  18  18  32  27  28  18 
##  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 
##  15  15  24  26  23  16  29  19  17  22  27  16  16  18  16  28  21  24  21  20  18  17  17  18  27  25  27 
##  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  80  81  82  83 
##  19  16  22  13  24  21  18  19  18  30  16  19  22  25  14  16  21  16  24  23  18  23  30  31  16  22  17 
##  84  85  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 
##  18  13  18  22  16  19  20  19  20  14  21  24  29  22  26  19  25  21  16  19  16  31  14  28  22  17  21 
## 112 113 114 115 116 117 118 119 120 121 122 
##  27  15  24  20  16  24  23  22  13  13  10 
## [1] "Frequency table after encoding"
## a007_a_vill_id. 007 Village ID
## 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 
##  16  22  15  17  31  20  16  21  30  17  21  18  22  28  27  17  19  22  16  17  32  17  13  26  15  31  28 
## 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 
##  15  14  27  16  27  18  16  16  30  20  26  18  24  23  18  24  15  16  15  16  20  18  18  18  18  30  17 
## 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 
##  16  18  18  27  17  21  29  26  19  19  23  16  22  29  23  23  19  18  24  16  19  29  24  20  28  10  21 
## 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 
##  20  18  25  22  22  17  24  24  21  25  20  30  22  19  21  13  19  14  19  16  21  21  16  16  16  25  18 
## 717 718 719 720 721 722 723 724 725 726 727 
##  27  16  14  22  13  18  23  24  15  24  13

Indirect PII - Ordinal: Global recode or Top/bottom coding for extreme values

# Focus on variables with a "Lowest Freq" in dictionary of 30 or less. 

break_age <- c(0,5,10,15,20)
labels_age <- c("0-4" =1, 
                "5-9" =2, 
                "10-14" =3, 
                "15-19"=4, 
                "20 or older"=5)

mydata <- ordinal_recode (variable="a404_old_girl_die_", break_points=break_age, missing=999999, value_labels=labels_age)

## [1] "Frequency table before encoding"
## a404_old_girl_die_. 404. How old was the girl when she died?
##    1    2    3    4    7    8    9   10   11   12   13   15   16 <NA> 
##    5    3    1    5    5    1    4    5    1    8    1    1    1 2393 
##     recoded
##      [0,5) [5,10) [10,15) [15,20) [20,1e+06)
##   1      5      0       0       0          0
##   2      3      0       0       0          0
##   3      1      0       0       0          0
##   4      5      0       0       0          0
##   7      0      5       0       0          0
##   8      0      1       0       0          0
##   9      0      4       0       0          0
##   10     0      0       5       0          0
##   11     0      0       1       0          0
##   12     0      0       8       0          0
##   13     0      0       1       0          0
##   15     0      0       0       1          0
##   16     0      0       0       1          0
## [1] "Frequency table after encoding"
## a404_old_girl_die_. 404. How old was the girl when she died?
##   0-4   5-9 10-14 15-19  <NA> 
##    14    10    15     2  2393 
## [1] "Inspect value labels and relabel as necessary"
##         0-4         5-9       10-14       15-19 20 or older 
##           1           2           3           4           5
mydata <- ordinal_recode (variable="a405_stop_liv_resid_", break_points=break_age, missing=999999, value_labels=labels_age)

## [1] "Frequency table before encoding"
## a405_stop_liv_resid_. 405. How old was the girl when her mother stopped living in the same residence?
##    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   17 <NA> 
##   26   16   10   14    8    1    3    6    4   12    9    5    2    3    1    1 2313 
##     recoded
##      [0,5) [5,10) [10,15) [15,20) [20,1e+06)
##   1     26      0       0       0          0
##   2     16      0       0       0          0
##   3     10      0       0       0          0
##   4     14      0       0       0          0
##   5      0      8       0       0          0
##   6      0      1       0       0          0
##   7      0      3       0       0          0
##   8      0      6       0       0          0
##   9      0      4       0       0          0
##   10     0      0      12       0          0
##   11     0      0       9       0          0
##   12     0      0       5       0          0
##   13     0      0       2       0          0
##   14     0      0       3       0          0
##   15     0      0       0       1          0
##   17     0      0       0       1          0
## [1] "Frequency table after encoding"
## a405_stop_liv_resid_. 405. How old was the girl when her mother stopped living in the same residence?
##   0-4   5-9 10-14 15-19  <NA> 
##    66    22    31     2  2313 
## [1] "Inspect value labels and relabel as necessary"
##         0-4         5-9       10-14       15-19 20 or older 
##           1           2           3           4           5
mydata <- ordinal_recode (variable="a411_old_girl_die_", break_points=break_age, missing=999999, value_labels=labels_age)

## [1] "Frequency table before encoding"
## a411_old_girl_die_. 411. How old was the girl when he died?
##    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16 <NA> 
##   21   12   13   10   13   10   13   22    8   21   14   22    7    9    6    3 2230 
##     recoded
##      [0,5) [5,10) [10,15) [15,20) [20,1e+06)
##   1     21      0       0       0          0
##   2     12      0       0       0          0
##   3     13      0       0       0          0
##   4     10      0       0       0          0
##   5      0     13       0       0          0
##   6      0     10       0       0          0
##   7      0     13       0       0          0
##   8      0     22       0       0          0
##   9      0      8       0       0          0
##   10     0      0      21       0          0
##   11     0      0      14       0          0
##   12     0      0      22       0          0
##   13     0      0       7       0          0
##   14     0      0       9       0          0
##   15     0      0       0       6          0
##   16     0      0       0       3          0
## [1] "Frequency table after encoding"
## a411_old_girl_die_. 411. How old was the girl when he died?
##   0-4   5-9 10-14 15-19  <NA> 
##    56    66    73     9  2230 
## [1] "Inspect value labels and relabel as necessary"
##         0-4         5-9       10-14       15-19 20 or older 
##           1           2           3           4           5
mydata <- ordinal_recode (variable="a412_stop_liv_resid_", break_points=break_age, missing=999999, value_labels=labels_age)

## [1] "Frequency table before encoding"
## a412_stop_liv_resid_. 412. How old was the girl when her father stopped living in the same residence?
##    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   17 <NA> 
##   36   13    8   13   13    6    5    5    3   20   12    8   12    3    3    1 2273 
##     recoded
##      [0,5) [5,10) [10,15) [15,20) [20,1e+06)
##   1     36      0       0       0          0
##   2     13      0       0       0          0
##   3      8      0       0       0          0
##   4     13      0       0       0          0
##   5      0     13       0       0          0
##   6      0      6       0       0          0
##   7      0      5       0       0          0
##   8      0      5       0       0          0
##   9      0      3       0       0          0
##   10     0      0      20       0          0
##   11     0      0      12       0          0
##   12     0      0       8       0          0
##   13     0      0      12       0          0
##   14     0      0       3       0          0
##   15     0      0       0       3          0
##   17     0      0       0       1          0
## [1] "Frequency table after encoding"
## a412_stop_liv_resid_. 412. How old was the girl when her father stopped living in the same residence?
##   0-4   5-9 10-14 15-19  <NA> 
##    70    32    55     4  2273 
## [1] "Inspect value labels and relabel as necessary"
##         0-4         5-9       10-14       15-19 20 or older 
##           1           2           3           4           5

Indirect PII - Categorical: Recode, encode, or Top/bottom coding for extreme values

# !!!Include relevant variables in list below (Indirect PII - Categorical, and Ordinal if not processed yet)

indirect_PII <- c("a415a_guardn_child_", 
                  "a430l_oth_",
                  "a430l_oth_entry_",
                  "a430la_current_")
capture_tables (indirect_PII)

Matching and crosstabulations: Run automated PII check

# !!! No direct demographic variables available in dataset

Open-ends: review responses for any sensitive information, redact as necessary

open_ends <- c("a415a_guardn_child_",
               "a418k_other_entry_",
               "a422_life_change_",
               "a423_plan_future_",
               "a428_obstacle_girl_",
               "a429_gto_school_",
               "a429_gto_school_oth_",
               "a435_situations_",
               "a437_advantages_",
               "a438_good_match_")
report_open (list_open_ends = open_ends)

dropvars <- c("a418k_other_entry_",
              "a422_life_change_",
              "a423_plan_future_",
              "a429_gto_school_oth_")

mydata <- mydata[!names(mydata) %in% dropvars] # Drop as actually verbatim data in local language

GPS data: Displace

# !!! No GPS data

Save processed data in Stata and SPSS format

Adds "_PU" (Public Use) to the end of the name

haven::write_dta(mydata, paste0(filename, "_PU.dta"))
haven::write_sav(mydata, paste0(filename, "_PU.sav"))

# Add report title dynamically
title_var <- paste0("DOL-ILAB SDC - ", filename)