rm(list=ls(all=t))

Setup filenames

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

# !!!No Small locations

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="age0", 80, missing=NA)
## [1] "Frequency table before encoding"
## age0. Age at Baseline, Corrected
##    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22   23 
##  386  258  288  297  318  755  821  159   92  616  611  610  689  563  615  551  472  411  323  270  221  202  152 
##   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46 
##  106   90   75   81   66   73   95   88  104  124  131  162  183  163  213  188  211  174  226  219  159  218  167 
##   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69 
##  176  158  159  144  101  124  109  102   86   71   57   70   57   41   25   33   21   16   21   27   20   17   18 
##   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   88   89   90   91   92 <NA> 
##   14    7   10   13    6   10   10    9   11    6    3    8    6    4    3    3    4    2    2    2    1    1  118

## [1] "Frequency table after encoding"
## age0. Age at Baseline, Corrected
##          1          2          3          4          5          6          7          8          9         10 
##        386        258        288        297        318        755        821        159         92        616 
##         11         12         13         14         15         16         17         18         19         20 
##        611        610        689        563        615        551        472        411        323        270 
##         21         22         23         24         25         26         27         28         29         30 
##        221        202        152        106         90         75         81         66         73         95 
##         31         32         33         34         35         36         37         38         39         40 
##         88        104        124        131        162        183        163        213        188        211 
##         41         42         43         44         45         46         47         48         49         50 
##        174        226        219        159        218        167        176        158        159        144 
##         51         52         53         54         55         56         57         58         59         60 
##        101        124        109        102         86         71         57         70         57         41 
##         61         62         63         64         65         66         67         68         69         70 
##         25         33         21         16         21         27         20         17         18         14 
##         71         72         73         74         75         76         77         78         79 80 or more 
##          7         10         13          6         10         10          9         11          6         39 
##       <NA> 
##        118

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

# !!!No Indirect PII - Categorical

Matching and crosstabulations: Run automated PII check

# Based on dictionary inspection, select variables for creating sdcMicro object
# See: https://sdcpractice.readthedocs.io/en/latest/anon_methods.html
# All variable names should correspond to the names in the data file
# selected categorical key variables: gender, occupation/education and age
selectedKeyVars = c('c_gender', 'age0') ##!!! Replace with candidate categorical demo vars

# weight variable (add if available)
# selectedWeightVar = c('projwt') ##!!! Replace with weight var

# household id variable (cluster)
# selectedHouseholdID = c('wpid') ##!!! Replace with household id

# creating the sdcMicro object with the assigned variables
sdcInitial <- createSdcObj(dat = mydata, keyVars = selectedKeyVars)
sdcInitial
## The input dataset consists of 14902 rows and 8 variables.
##   --> Categorical key variables: c_gender, age0
## ----------------------------------------------------------------------
## Information on categorical key variables:
## 
## Reported is the number, mean size and size of the smallest category >0 for recoded variables.
## In parenthesis, the same statistics are shown for the unmodified data.
## Note: NA (missings) are counted as seperate categories!
##  Key Variable Number of categories      Mean size            Size of smallest (>0)    
##      c_gender                    4  (4)  3725.500 (3725.500)                     3 (3)
##          age0                   81 (81)   184.800  (184.800)                     6 (6)
## ----------------------------------------------------------------------
## Infos on 2/3-Anonymity:
## 
## Number of observations violating
##   - 2-anonymity: 0 (0.000%)
##   - 3-anonymity: 2 (0.013%)
##   - 5-anonymity: 3 (0.020%)
## 
## ----------------------------------------------------------------------
# !!!Any observations violating 2-anonymity

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

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)