rm(list=ls(all=t))

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

filename <- "bhsections67" # !!!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("q006_block_id", "q007_vlg_id") 
mydata <- encode_location (variables= locvars, missing=999999)
## [1] "Frequency table before encoding"
## q006_block_id. 6 Block Code
##    1    2    3    4    5    6    7    8    9 <NA> 
##  194  155  195  407   98  190  143  422  516   33 
## [1] "Frequency table after encoding"
## q006_block_id. 6 Block Code
##  279  280  281  282  283  284  285  286  287 <NA> 
##  422  155  194  190  195  516  143   98  407   33 
## [1] "Frequency table before encoding"
## q007_vlg_id. 7 Village Code
##    1    2    3    4    5    6    7    9   10   11   12   13   15   16   17   18   19   20   21 
##   16   16   16   15   20   31   28   17   15   20   24   24   15   18   21   17   17   18   30 
##   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40 
##   22   18   17   32   27   26   18   14   15   24   24   22   16   29   18   17   22   27   17 
##   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59 
##   16   18   17   28   20   24   21   19   17   17   16   18   26   24   27   18   17   21   13 
##   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78 
##   24   22   16   18   18   29   16   18   21   25   13   16   19   16   23   23   17   22   29 
##   80   81   82   83   84   85   87   88   89   90   91   92   93   94   95   96   97   98   99 
##   30   17   22   17   17   13   16   22   15   19   19   19   21   13   17   22   28   21   25 
##  100  101  102  103  104  105  106  107  108  109  110  111  112  113  114  115  116  117  118 
##   18   24   21   15   19   14   31   16   27   21   17   21   26   14   24   19   16   21   22 
##  119 <NA> 
##   16   33 
## [1] "Frequency table after encoding"
## q007_vlg_id. 7 Village Code
##  265  266  267  268  269  270  271  272  273  274  275  276  277  278  279  280  281  282  283 
##   19   19   18   17   18   17   15   24   15   15   22   16   22   18   22   24   21   16   17 
##  284  285  286  287  288  289  290  291  292  293  294  295  296  297  298  299  300  301  302 
##   15   26   20   24   21   17   21   17   16   18   30   18   16   19   24   25   27   26   21 
##  303  304  305  306  307  308  309  310  311  312  313  314  315  316  317  318  319  320  321 
##   26   18   17   28   19   29   24   28   27   20   16   21   16   24   23   14   14   30   16 
##  322  323  324  325  326  327  328  329  330  331  332  333  334  335  336  337  338  339  340 
##   13   13   17   19   21   15   16   13   16   22   17   17   23   18   24   16   16   17   15 
##  341  342  343  344  345  346  347  348  349  350  351  352  353  354  355  356  357  358  359 
##   27   18   22   16   29   28   14   29   17   31   17   18   20   17   21   25   32   21   18 
##  360  361  362  363  364  365  366  367  368  369  370  371  372  373  374  375  376  377  378 
##   17   19   22   22   17   19   24   24   18   31   21   21   13   17   27   16   18   22   16 
##  379 <NA> 
##   22   33

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. 

mydata <- top_recode (variable="q704_distance", break_point=3, missing=NA)
## [1] "Frequency table before encoding"
## q704_distance. 704 Travel distance from residence to associated school (DISE Code) in question 
##    0    1    2    3    4    5    7   10   61 
## 1839  389   90   21    7    4    1    1    1

## [1] "Frequency table after encoding"
## q704_distance. 704 Travel distance from residence to associated school (DISE Code) in question 
##         0         1         2 3 or more 
##      1839       389        90        35

mydata <- top_recode (variable="q705_time", break_point=60, missing=NA)
## [1] "Frequency table before encoding"
## q705_time. 705 Travel time from residence to associated school (DISE Code) in question 001 
##   0   1   2   3   4   5   7   8  10  15  18  20  25  30  40  45  50  55  60  75  90 120 180 
##  40  22  20   6   2 362   4   2 715 457   3 223  38 356   7  18   2   2  63   4   3   3   1

## [1] "Frequency table after encoding"
## q705_time. 705 Travel time from residence to associated school (DISE Code) in question 001 
##          0          1          2          3          4          5          7          8 
##         40         22         20          6          2        362          4          2 
##         10         15         18         20         25         30         40         45 
##        715        457          3        223         38        356          7         18 
##         50         55 60 or more 
##          2          2         74

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

# !!! No Indirect PII categorical

dropvars <- c("q606_rel1", "q606_rel2") 
mydata <- mydata[!names(mydata) %in% dropvars]

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

mydata <- mydata[!names(mydata) %in% "q708_remarks"]

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)