rm(list=ls(all=t))

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

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

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="q404_ch_age", break_points=break_age, missing=999999, value_labels=labels_age)

## [1] "Frequency table before encoding"
## q404_ch_age. 404 How old was the girl when she died?
##    0    1    2    3    4    5    6    7    8    9   11   12   14 <NA> 
##    1    4    2    2    2    6    2    2    4    3    2    2    1 2394 
##     recoded
##      [0,5) [5,10) [10,15) [15,20) [20,1e+06)
##   0      1      0       0       0          0
##   1      4      0       0       0          0
##   2      2      0       0       0          0
##   3      2      0       0       0          0
##   4      2      0       0       0          0
##   5      0      6       0       0          0
##   6      0      2       0       0          0
##   7      0      2       0       0          0
##   8      0      4       0       0          0
##   9      0      3       0       0          0
##   11     0      0       2       0          0
##   12     0      0       2       0          0
##   14     0      0       1       0          0
## [1] "Frequency table after encoding"
## q404_ch_age. 404 How old was the girl when she died?
##   0-4   5-9 10-14  <NA> 
##    11    17     5  2394 
## [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="q405_age", break_points=break_age, missing=999999, value_labels=labels_age)

## [1] "Frequency table before encoding"
## q405_age. 405 How old was the girl when her mother stopped living in the same residence as
##    0    1    2    3    4    5    6    7    8    9   10   11   12   14 <NA> 
##    7   21   14    7    5   13    5    7    9   17   12    7    6    1 2296 
##     recoded
##      [0,5) [5,10) [10,15) [15,20) [20,1e+06)
##   0      7      0       0       0          0
##   1     21      0       0       0          0
##   2     14      0       0       0          0
##   3      7      0       0       0          0
##   4      5      0       0       0          0
##   5      0     13       0       0          0
##   6      0      5       0       0          0
##   7      0      7       0       0          0
##   8      0      9       0       0          0
##   9      0     17       0       0          0
##   10     0      0      12       0          0
##   11     0      0       7       0          0
##   12     0      0       6       0          0
##   14     0      0       1       0          0
## [1] "Frequency table after encoding"
## q405_age. 405 How old was the girl when her mother stopped living in the same residence as
##   0-4   5-9 10-14  <NA> 
##    54    51    26  2296 
## [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="q411_age_died", break_points=break_age, missing=999999, value_labels=labels_age)

## [1] "Frequency table before encoding"
## q411_age_died. 411 How old was the girl when he died?
##    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   38 <NA> 
##   11   13    9   13    8   13   12   10   17   14   14    7    2    1    2    1 2280 
##     recoded
##      [0,5) [5,10) [10,15) [15,20) [20,1e+06)
##   0     11      0       0       0          0
##   1     13      0       0       0          0
##   2      9      0       0       0          0
##   3     13      0       0       0          0
##   4      8      0       0       0          0
##   5      0     13       0       0          0
##   6      0     12       0       0          0
##   7      0     10       0       0          0
##   8      0     17       0       0          0
##   9      0     14       0       0          0
##   10     0      0      14       0          0
##   11     0      0       7       0          0
##   12     0      0       2       0          0
##   13     0      0       1       0          0
##   14     0      0       2       0          0
##   38     0      0       0       0          1
## [1] "Frequency table after encoding"
## q411_age_died. 411 How old was the girl when he died?
##         0-4         5-9       10-14 20 or older        <NA> 
##          54          66          26           1        2280 
## [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="q411_age_died", break_points=break_age, missing=999999, value_labels=labels_age)

## [1] "Frequency table before encoding"
## q411_age_died. 411 How old was the girl when he died?
##         0-4         5-9       10-14 20 or older        <NA> 
##          54          66          26           1        2280 
##    recoded
##     [0,5) [5,10) [10,15) [15,20) [20,1e+06)
##   1    54      0       0       0          0
##   2    66      0       0       0          0
##   3    26      0       0       0          0
##   5     0      1       0       0          0
## [1] "Frequency table after encoding"
## q411_age_died. 411 How old was the girl when he died?
##  0-4  5-9 <NA> 
##  146    1 2280 
## [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="q412_age", break_points=break_age, missing=999999, value_labels=labels_age)

## [1] "Frequency table before encoding"
## q412_age. 412 How old was the girl when her father stopped living in the same residence as
##    0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15 <NA> 
##   40   46   13    5    9   19   17   12   21   28   28   23   14    3    4    2 2143 
##     recoded
##      [0,5) [5,10) [10,15) [15,20) [20,1e+06)
##   0     40      0       0       0          0
##   1     46      0       0       0          0
##   2     13      0       0       0          0
##   3      5      0       0       0          0
##   4      9      0       0       0          0
##   5      0     19       0       0          0
##   6      0     17       0       0          0
##   7      0     12       0       0          0
##   8      0     21       0       0          0
##   9      0     28       0       0          0
##   10     0      0      28       0          0
##   11     0      0      23       0          0
##   12     0      0      14       0          0
##   13     0      0       3       0          0
##   14     0      0       4       0          0
##   15     0      0       0       2          0
## [1] "Frequency table after encoding"
## q412_age. 412 How old was the girl when her father stopped living in the same residence as
##   0-4   5-9 10-14 15-19  <NA> 
##   113    97    72     2  2143 
## [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("q407_emp_status",
                  "q414_emp_status",
                  "q415_rel",
                  "q418i_othr_allowed",
                  "q429_obstacles",
                  "q430g_sharecar_cur",
                  "q430j_other",
                  "q430j_other_specify",
                  "q430j_other_cur",
                  "q430j_other_fut",
                  "q431h_othr_nec")
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("q415_rel_othr",
               "q422_change",
               "q423_future_plans",
               "q431h_othr_specify")
report_open (list_open_ends = open_ends)
mydata <- mydata[!names(mydata) %in% open_ends] # 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)