rm(list=ls(all=t))

Setup filenames

filename <- "ecsection5_relabelled" # !!!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

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

!!! No Indirect PII - Ordinal

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

indirect_PII <- c("q504_areu_currmarrd",
                  "q506o_others_entry",
                  "q506o_other_access")

capture_tables (indirect_PII)

# Recode those with very specific values. 

!!! No action, low risk

val_labels(mydata$q506o_others_entry)
## Motor cycle/ Bike      Water heater        Fan/cooler     Tape recorder           Tractor 
##                 1                 2                 3                 4                 5 
##             Boxes             Books    Not applicable    Not applicable 
##                 6                 7               999                NA
breaks <- c(1:7)
labels <- c("Small motor vehicle" = 1,
            "Water heater" = 2,
            "Fan/cooler" = 3,
            "Tape recorder" = 4,
            "Large Motor Vehicle" = 5,
            "Boxes" = 6,
            "Books" = 7)
mydata2 <- ordinal_recode (variable="q506o_others_entry", 
                          break_points=breaks, 
                          missing=999999, 
                          value_labels=labels)

## [1] "Frequency table before encoding"
## q506o_others_entry. 506o. Other item (specify)
## Motor cycle/ Bike      Water heater        Fan/cooler     Tape recorder           Tractor 
##                 3                 1                 4                 1                 4 
##             Boxes             Books    Not applicable 
##                 1                 1              2372 
##      recoded
##       [1,2) [2,3) [3,4) [4,5) [5,6) [6,7) [7,1e+06)
##   1       3     0     0     0     0     0         0
##   2       0     1     0     0     0     0         0
##   3       0     0     4     0     0     0         0
##   4       0     0     0     1     0     0         0
##   5       0     0     0     0     4     0         0
##   6       0     0     0     0     0     1         0
##   7       0     0     0     0     0     0         1
##   999     0     0     0     0     0     0      2372
## [1] "Frequency table after encoding"
## q506o_others_entry. 506o. Other item (specify)
## Small motor vehicle        Water heater          Fan/cooler       Tape recorder 
##                   3                   1                   4                   1 
## Large Motor Vehicle               Boxes               Books 
##                   4                   1                2373 
## [1] "Inspect value labels and relabel as necessary"
## Small motor vehicle        Water heater          Fan/cooler       Tape recorder 
##                   1                   2                   3                   4 
## Large Motor Vehicle               Boxes               Books 
##                   5                   6                   7

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

# !!!Identify open-end variables here: 

open_ends <- c("q509_watntobecome",
               "q512d_steps_nxtyr1",
               "q512d_steps_nxtyr2",
               "q512d_steps_nxtyr3",
               "q512g_knwtodo_1yr")
report_open (list_open_ends = open_ends)

# Review "verbatims.csv". Identify variables to be deleted or redacted and their row number 

!!! Drop all, as actually verbatim data in Hindi

drop_vars <- c("q512d_steps_nxtyr1",
               "q512d_steps_nxtyr2",
               "q512d_steps_nxtyr3",
               "q512g_knwtodo_1yr")
mydata <- mydata[!names(mydata) %in% drop_vars]

GPS data: Displace

!!! No GPS data

Save processed data in Stata and SPSS format

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)