rm(list=ls(all=t))

Setup filenames

filename <- "ecsection0_relabelled" # !!!Update filename
functions_vers <-  "functions_1.7.R" # !!!Update helper functions file
source (functions_vers)
## --------
## This is sdcMicro v5.6.0.
## For references, please have a look at citation('sdcMicro')
## Note: since version 5.0.0, the graphical user-interface is a shiny-app that can be started with sdcApp().
## Please submit suggestions and bugs at: https://github.com/sdcTools/sdcMicro/issues
## --------
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## Loading required package: sp
## Checking rgeos availability: TRUE
## 
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
## 
##     select
## The following object is masked from 'package:sdcMicro':
## 
##     freq
## rgdal: version: 1.5-23, (SVN revision 1121)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/Usuario/Documents/R/win-library/3.6/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/Usuario/Documents/R/win-library/3.6/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-5
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
## Overwritten PROJ_LIB was C:/Users/Usuario/Documents/R/win-library/3.6/rgdal/proj
## Loading required package: spatstat.data
## Loading required package: spatstat.geom
## spatstat.geom 2.1-0
## 
## Attaching package: 'spatstat.geom'
## The following objects are masked from 'package:raster':
## 
##     area, rotate, shift
## Loading required package: spatstat.core
## Loading required package: nlme
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:raster':
## 
##     getData
## The following object is masked from 'package:dplyr':
## 
##     collapse
## Loading required package: rpart
## spatstat.core 2.1-2
## Loading required package: spatstat.linnet
## spatstat.linnet 2.1-1
## 
## spatstat 2.1-0       (nickname: 'Comedic violence') 
## For an introduction to spatstat, type 'beginner'
## rgeos version: 0.5-5, (SVN revision 640)
##  GEOS runtime version: 3.8.0-CAPI-1.13.1 
##  Linking to sp version: 1.4-5 
##  Polygon checking: TRUE
## 
## Spatial Point Pattern Analysis Code in S-Plus
##  
##  Version 2 - Spatial and Space-Time analysis
## 
## Attaching package: 'splancs'
## The following object is masked from 'package:raster':
## 
##     zoom
## The following object is masked from 'package:dplyr':
## 
##     tribble
## Loading required package: spam
## Loading required package: dotCall64
## Loading required package: grid
## Spam version 2.6-0 (2020-12-14) is loaded.
## Type 'help( Spam)' or 'demo( spam)' for a short introduction 
## and overview of this package.
## Help for individual functions is also obtained by adding the
## suffix '.spam' to the function name, e.g. 'help( chol.spam)'.
## 
## Attaching package: 'spam'
## The following objects are masked from 'package:base':
## 
##     backsolve, forwardsolve
## See https://github.com/NCAR/Fields for
##  an extensive vignette, other supplements and source code
## 
## Attaching package: 'geosphere'
## The following object is masked from 'package:spatstat.geom':
## 
##     perimeter
## 
## Attaching package: 'tibble'
## The following object is masked from 'package:splancs':
## 
##     tribble

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

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

mydata <- top_recode (variable="q009_chld_age", break_point=18, missing=NA)
## [1] "Frequency table before encoding"
## q009_chld_age. 009 Child age
##   9  10  11  12  13  14  15  16  17  18  21  25  26 
##   3   7  95 602 835 443 242 115  35   4   1   3   2

## [1] "Frequency table after encoding"
## q009_chld_age. 009 Child age
##          9         10         11         12         13         14         15         16 
##          3          7         95        602        835        443        242        115 
##         17 18 or more 
##         35         10

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

indirect_PII <- c("q003_villg_nm_chg",
                  "q005_chld_nm_chg",
                  "q007_hamlet_nm_chg",
                  "q008_urban")
capture_tables (indirect_PII)

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

#No Open-ends

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)