rm(list=ls(all=t))

Setup filenames

filename <- "ecsection1" # !!!Update filename
functions_vers <-  "functions_1.7.R" # !!!Update helper functions file

Setup data, functions and create dictionary for dataset review

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("q002_blckid", "q003_vill_id") 
mydata <- encode_location (variables= locvars, missing=999999)
## [1] "Frequency table before encoding"
## q002_blckid. 002 Unique block ID
##   1   2   3   4   5   6   7   8   9 
## 206 167 188 412  96 192 158 424 544 
## [1] "Frequency table after encoding"
## q002_blckid. 002 Unique block ID
## 279 280 281 282 283 284 285 286 287 
## 206 544 167 424  96 188 412 158 192 
## [1] "Frequency table before encoding"
## q003_vill_id. 003 Village ID
##    1    2    3    4    5    6    7    8    9   10   11   12   13   15   16   17   18   19   20 
##   17   16   17   16   20   29   29   16   15   13   17   26   24   14   18   21   18   18   20 
##   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39 
##   30   23   18   18   32   25   27   17   14   13   24   26   21   16   28   19   15   22   27 
##   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58 
##   16   16   18   16   27   21   22   21   20   17   17   17   18   27   25   27   19   13   21 
##   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77 
##   12   24   19   17   19   18   30   16   19   21   25   13   16   21   16   23   22   18   23 
##   78   80   81   82   83   84   85   87   88   89   90   91   92   93   94   95   96   97   98 
##   30   30   16   21   17   17   13   18   22   16   19   20   18   20   14   20   24   28   21 
##   99  100  101  102  103  104  105  106  107  108  109  110  111  112  113  114  115  116  117 
##   26   17   25   20   15   19   16   31   13   28   22   17   21   27   15   24   20   14   24 
##  118  119  120  121  122 <NA> 
##   22   21   13   13   10    1 
## [1] "Frequency table after encoding"
## q003_vill_id. 003 Village ID
##  609  610  611  612  613  614  615  616  617  618  619  620  621  622  623  624  625  626  627 
##   18   16   24   20   21   16   27   13   20   18   19   17   23   17   10   21   19   18   24 
##  628  629  630  631  632  633  634  635  636  637  638  639  640  641  642  643  644  645  646 
##   24   16   27   23   32   21   28   19   16   16   30   25   21   20   27   21   28   18   29 
##  647  648  649  650  651  652  653  654  655  656  657  658  659  660  661  662  663  664  665 
##   25   25   16   17   20   24   17   31   15   21   17   13   26   26   22   30   12   14   18 
##  666  667  668  669  670  671  672  673  674  675  676  677  678  679  680  681  682  683  684 
##   27   14   15   17   23   21   24   22   22   20   21   21   27   17   16   30   13   24   19 
##  685  686  687  688  689  690  691  692  693  694  695  696  697  698  699  700  701  702  703 
##   20   18   17   16   16   29   16   21   22   18   16   14   16   19   18   17   28   19   25 
##  704  705  706  707  708  709  710  711  712  713  714  715  716  717  718  719  720  721  722 
##   13   13   21   13   17   15   20   22   27   30   22   16   13   14   20   26   17   19   18 
##  723  724  725  726  727 <NA> 
##   18   15   18   17   13    1

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

# Top code household composition variables with large and unusual numbers 

mydata <- top_recode ("q101_siblings_no", break_point=5, missing=NA) # Topcode cases with 5 or more  
## [1] "Frequency table before encoding"
## q101_siblings_no. 101 How many siblings do you have that with at-least one common parent but who d
##    0    1    2    3    4    5    6    7 
## 1617  423  193   91   41   12    7    3

## [1] "Frequency table after encoding"
## q101_siblings_no. 101 How many siblings do you have that with at-least one common parent but who d
##         0         1         2         3         4 5 or more 
##      1617       423       193        91        41        22

mydata <- top_recode ("q102_num_nonres_sib", break_point=5, missing=NA) # Topcode cases with 5 or more  
## [1] "Frequency table before encoding"
## q102_num_nonres_sib. 102 In order of age, what number are you considering all of your non-resident si
##    1    2    3    4    5    6    7    8   14 <NA> 
##   82  374  173   85   35    8    7    2    1 1620

## [1] "Frequency table after encoding"
## q102_num_nonres_sib. 102 In order of age, what number are you considering all of your non-resident si
##         1         2         3         4 5 or more      <NA> 
##        82       374       173        85        53      1620

mydata <- top_recode ("q103_num_nonres_femsib", break_point=5, missing=NA) # Topcode cases with 5 or more  
## [1] "Frequency table before encoding"
## q103_num_nonres_femsib. 103 In order of age, what number are you considering all of your non-resident fe
##    1    2    3    4    5    6    7 <NA> 
##  180  325  157   63   27    6    4 1625

## [1] "Frequency table after encoding"
## q103_num_nonres_femsib. 103 In order of age, what number are you considering all of your non-resident fe
##         1         2         3         4 5 or more      <NA> 
##       180       325       157        63        37      1625

mydata <- top_recode ("q104_sib_res_nonres", break_point=8, missing=NA) # Topcode cases with 8 or more  
## [1] "Frequency table before encoding"
## q104_sib_res_nonres. 104 How many siblings (resident and non-resident) do you have that share at-leas
##   0   1   2   3   4   5   6   7   8   9  10  11 
##  11  31 186 607 667 461 258  97  46  11   8   4

## [1] "Frequency table after encoding"
## q104_sib_res_nonres. 104 How many siblings (resident and non-resident) do you have that share at-leas
##         0         1         2         3         4         5         6         7 8 or more 
##        11        31       186       607       667       461       258        97        69

mydata <- top_recode ("q105_noconsd_res_nonres_sib", break_point=7, missing=NA) # Topcode cases with 7 or more  
## [1] "Frequency table before encoding"
## q105_noconsd_res_nonres_sib. 105 In Order of age, what number are you considering all of your resident as wel
##    1    2    3    4    5    6    7    8    9   10 <NA> 
##  615  593  541  337  153   73   41   16    4    2   12

## [1] "Frequency table after encoding"
## q105_noconsd_res_nonres_sib. 105 In Order of age, what number are you considering all of your resident as wel
##         1         2         3         4         5         6 7 or more      <NA> 
##       615       593       541       337       153        73        63        12

mydata <- top_recode ("q106_noconsd_re_nonres_femsib", break_point=6, missing=NA) # Topcode cases with 6 or more  
## [1] "Frequency table before encoding"
## q106_noconsd_re_nonres_femsib. 106 In Order of age, what number are you considering all of your resident as wel
##    1    2    3    4    5    6    7 <NA> 
## 1048  674  400  151   62   28   11   13

## [1] "Frequency table after encoding"
## q106_noconsd_re_nonres_femsib. 106 In Order of age, what number are you considering all of your resident as wel
##         1         2         3         4         5 6 or more      <NA> 
##      1048       674       400       151        62        39        13

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

# !!!No Indirect PII - Categorical

Matching and crosstabulations: Run automated PII check

# Not enough variables for matching possible

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

# !!! No open-ends

GPS data: Displace

# !!! No GPS

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)