rm(list=ls(all=t))

Setup filenames

filename <- "Section_12" # !!!Update filename
functions_vers <-  "functions_1.8.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

dropvars <- c("m_s12q5") 
mydata <- mydata[!names(mydata) %in% dropvars]

mydata$household_id <- zap_labels(mydata$household_id)

Direct PII-team: Encode field team names

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

locvars <- c("m_s12q9", "m_s12q20") 
mydata <- encode_location (variables= locvars, missing=999999)
## [1] "Frequency table before encoding"
## m_s12q9. sJq15: ${s12q6}'s municipality of residence:  Munisipyo ng tirahan ni ${s12q6}
##                     Polangui                       Abucay                    Mariveles                  San Nicolas                       Enrile 
##                            2                            4                            7                            1                            2 
##                    Calabanga                      Canaman              Jose Panganiban                      Magarao                    Naga City 
##                            4                            1                            1                            1                            4 
##                      Pasacao                     Tinambac     General Emilio Aguinaldo                        Jones                         Anda 
##                            3                            1                            2                            2                            2 
##                   Candelaria                     Sampaloc                      Pililla                    San Mateo                        Tanay 
##                            1                            1                            1                            1                            1 
##                        Pilar Other municipality - specify                         Lian                         <NA> 
##                            2                           19                            1                         2221 
## [1] "Frequency table after encoding"
## m_s12q9. sJq15: ${s12q6}'s municipality of residence:  Munisipyo ng tirahan ni ${s12q6}
##  864  865  866  867  868  869  870  871  872  873  874  875  876  877  878  879  880  881  882  883  884  885  886 <NA> 
##    1   19    4    1    2    2    3    1    1    1    2    1    4    2    1    7    4    1    2    1    2    1    1 2221 
## [1] "Frequency table before encoding"
## m_s12q20. sJq28: ${s12q17}'s municipality of residence:  Munisipyo ng tirahan ni ${s12q1
##                      Malinao                       Manito                       Abucay                    Mariveles                       Enrile 
##                            1                            1                            2                            7                            1 
##                         Labo                    Naga City                       Ocampo                    Pagsanjan                         Bani 
##                            1                            1                            1                            2                            1 
##                   Candelaria                    Jala-Jala                      Pililla                        Tanay                        Pilar 
##                            1                            2                            1                            1                            1 
## Other municipality - specify                         <NA> 
##                           10                         2251 
## [1] "Frequency table after encoding"
## m_s12q20. sJq28: ${s12q17}'s municipality of residence:  Munisipyo ng tirahan ni ${s12q1
## 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 <NA> 
##   10    1    2    1    7    1    1    1    2    1    1    1    1    2    1    1 2251

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

# !!!No Indirect PII - Categorical

Matching and crosstabulations: Run automated PII check

# !!!Insufficient demographic data

# !!! Identify open-end variables here: 
open_ends <- c("m_s12q8_other",
              "m_s12q10",
              "m_s12q21")

report_open (list_open_ends = open_ends)

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

mydata$m_s12q8_other[1636] <- "[small location]"
mydata$m_s12q10[1056] <- "[small location]"
mydata$m_s12q10[1059] <- "[small location]"
mydata$m_s12q10[1067] <- "[small location]"
mydata$m_s12q10[1143] <- "[small location]"
mydata$m_s12q10[1225] <- "[small location]"
mydata$m_s12q10[1231] <- "[small location]"
mydata$m_s12q10[1249] <- "[small location]"
mydata$m_s12q10[1276] <- "[small location]"
mydata$m_s12q10[1306] <- "[small location]"
mydata$m_s12q10[1317] <- "[small location]"
mydata$m_s12q10[1328] <- "[small location]"
mydata$m_s12q10[1392] <- "[small location]"
mydata$m_s12q10[1562] <- "[small location]"
mydata$m_s12q10[1608] <- "[small location]"
mydata$m_s12q10[1636] <- "[small location]"
mydata$m_s12q10[1971] <- "[small location]"
mydata$m_s12q10[2015] <- "[small location]"
mydata$m_s12q10[2082] <- "[small location]"
mydata$m_s12q10[2201] <- "[small location]"
mydata$m_s12q21[948] <- "[small location]"
mydata$m_s12q21[990] <- "[small location]"
mydata$m_s12q21[1059] <- "[small location]"
mydata$m_s12q21[1119] <- "[small location]"
mydata$m_s12q21[1231] <- "[small location]"
mydata$m_s12q21[1249] <- "[small location]"
mydata$m_s12q21[1495] <- "[small location]"
mydata$m_s12q21[1529] <- "[small location]"
mydata$m_s12q21[1683] <- "[small location]"
mydata$m_s12q21[2274] <- "[small location]"

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)