rm(list=ls(all=t))

Setup filenames

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

# !!!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

locvars <- c("s0q4",
             "s0q5") 
mydata <- encode_location (variables= locvars, missing=999999)
## [1] "Frequency table before encoding"
## s0q4. Baseline Municipality
##                  Malinao                   Manito                 Polangui                   Abucay 
##                      168                       42                       56                      126 
##                Mariveles              San Nicolas                  Amulung                   Enrile 
##                      224                       28                       70                       70 
##                Calabanga                Camaligan                  Canaman          Jose Panganiban 
##                       70                       14                       42                       70 
##                     Labo                 Libmanan                  Magarao                Naga City 
##                       56                       14                       42                       28 
##                   Ocampo                  Pasacao                 Tinambac General Emilio Aguinaldo 
##                       14                       42                       14                       42 
##             Cauayan City                    Jones                Pagsanjan                     Pila 
##                       56                      252                      126                       14 
##                     Agno                     Anda                     Bani                 Bautista 
##                       56                       42                       28                       14 
##                 Bugallon                 Calasiao          San Carlos City                     Sual 
##                       56                       14                       14                       14 
##               Urbiztondo               Candelaria                 Sampaloc                Jala-Jala 
##                       14                       28                       56                       42 
##                  Pililla                San Mateo                    Tanay                    Pilar 
##                       42                       42                       56                       70 
##            Sorsogon City 
##                       28 
## [1] "Frequency table after encoding"
## s0q4. Baseline Municipality
## 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 
##  56  56  14  42  28 168  42 126 252  14  42  42  70  14  56 126  42  42  28  56  28  14  14  42  70  28  56 
## 815 816 817 818 819 820 821 822 823 824 825 826 827 828 
##  28  70  14 224  70  56  14  14  56  42  70  14  42  14 
## [1] "Frequency table before encoding"
## s0q5. Baseline Barangay
##                     Wawa               Calaycayan                    Gabon                  Bangkal 
##                       14                       14                       14                       14 
##                 Mabatang                   Salian                    Omboy                     Laon 
##                       14                       14                       14                       14 
##               Capitangan               Macaboboni                  Aloleng                    Boboy 
##                       14                       14                       14                       14 
##               Bangan-Oda                    Goran               Alituntung                  Cordova 
##                       14                       14                       14                       14 
##       Casingsingan Norte                Annafatan                    Carot                   Tondol 
##                       14                       14                       14                       14 
##                  Mal-Ong                Dacap Sur               Quinaoayan                     Diaz 
##                       14                       14                       14                       14 
##                Poblacion                 Hacienda                  Umanday           Laguit Padilla 
##                       42                       14                       14                       14 
##                  Harobay                Dominirog         Bonot, Sta. Rosa                San Lucas 
##                       14                       14                       14                       14 
##              Sta. Isabel               San Miguel                San Roque              San Agustin 
##                       14                       14                       28                       14 
##            San Francisco               San Andres               San Isidro                    Union 
##                       14                       14                       56                       14 
##                 Sinippil                San Pablo                Casalatan                    Lanna 
##                       14                       14                       14                       14 
##                Liwan Sur                 Roma Sur               Lemu Norte         Maddarulug Norte 
##                       14                       14                       14                       14 
##                   Tabora             Poblacion Iv           CastaãOs Lejos                    Punta 
##                       14                       14                       14                       14 
##                   Bayugo    Third District (Pob.)                  Diarao,               Linamanan, 
##                       14                       14                       14                       14 
##                  Abulan,             San Vicente,              Papan Este,                  Brgy. 1 
##                       14                       14                       14                       14 
##                  Abdulam                 Namnama,               Dalibubon,              San Isidro, 
##                       14                       14                       14                       14 
##                 Dibuluan                  Palagao                   Lacab,              Pungpongan, 
##                       14                       14                       14                       14 
##                Malannit,           San Sebastian,                  Minuri,               Napallong, 
##                       14                       14                       14                       14 
##                 Nakalaya             Luklukan Sur          North Poblacion                   Calero 
##                       14                       14                       14                       14 
##                   Osmena                    Benit                    Dalas                   Napaod 
##                       14                       14                       14                       14 
##                  Exciban                     Bell               Carangcang               Sta. Lucia 
##                       14                       14                       14                       14 
##                     Ogob                   Sugcad                  Payahan           Quinarabasahan 
##                       14                       14                       14                       14 
##                   Baybay                   Bulang                    Balza                    Bariw 
##                       14                       14                       14                       14 
##                 Tagaytay                  Tanawan                  Malolos                    Jonop 
##                       14                       14                       14                       14 
##                    Cawit                     Buyo                  Cawayan                 Maligaya 
##                       14                       14                       14                       14 
##              Balon-Anito                    Alion                     Ipag                   Malaya 
##                       14                       14                       14                       14 
##                    Biaan                 Cabcaben                 Townsite              Bayangas Ii 
##                       14                       14                       14                       14 
##                   Baseco                  Lucanin               San Carlos                Alas-Asin 
##                       14                       14                       14                       14 
##                 Mt. View        Concepcion Grande              Del Rosario                 Guinaban 
##                       14                       14                       28                       14 
##              Pinagsanjan                    Buboy                    BiãAn                 Magdapio 
##                       14                       14                       14                       14 
##                 Maulawin               Cabanbanan                   Sabang                   Dingin 
##                       14                       14                       14                       14 
##                  Caranan               San Cirilo                Sta. Rosa   Santa Clara Sur (Pob.) 
##                       14                       14                       14                       14 
##                Salvacion                    Inang                 Mercedes                   Putiao 
##                       14                       14                       14                       14 
##                   Quisao              Hulo (Pob.)        Bagumbayan (Pob.)                    Danao 
##                       14                       14                       14                       14 
##                    Alnay                 Cotnogan                   Balaba                    Banot 
##                       14                       14                       14                       14 
##                  Bilucao                 Bayongon                   Bataan                   Bacnar 
##                       14                       14                       14                       14 
##  Guitnang Bayan I (Pob.) Guitnang Bayan Ii (Pob.)                 Malanday              Pulang-Bato 
##                       14                       14                       14                       14 
##                  Alas-As                  Bulabog                   Balogo     Tandang Kutyo (Pob.) 
##                       14                       14                       14                       14 
##              Wawa (Pob.)                 Daraitan                 Sampaloc                    Sogod 
##                       14                       14                       14                       14 
##              Dalangiring 
##                       14 
## [1] "Frequency table after encoding"
## s0q5. Baseline Barangay
## 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 
##  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14 
## 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 
##  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14 
## 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 
##  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14 
## 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 
##  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14 
## 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 
##  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14 
## 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 
##  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14  14 
## 996 997 
##  14  14

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

# Recode those with very specific values. 
# !!!No specific values

Matching and crosstabulations: Run automated PII check

# !!! Insufficient demographic data

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)