File size: 9,987 Bytes
caa8075 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
########################################################################
##### El Salvador - Migration Outcomes - Canton-Level - Pop Census #####
########################################################################
rm(list = ls()) # Clear variables
require(foreign)
require(ggplot2)
require(rgdal)
require(rgeos)
require(RColorBrewer) # creates nice color schemes
require(maptools) # loads sp library too
require(scales) # customize scales
require(gridExtra) # mutiple plots
require(plyr) # join function
require(dplyr)
require(mapproj) # projection tools
require(raster) # raster tools
require(ggvis) # visualize estimators
require(rdrobust) # rd estimation tools
require(stringdist) # approximate string matching
require(gdata)
require(rdd) # sorting tests
require(stargazer) # format tables
require(sandwich) # robust se's
require(zoo) # filling in
require(fuzzyjoin) # approximate string matching
require(haven)
require(stringi)
########################################
# Approximate String Matching Funtion
string_match <- function(string_to_match, options, smethod="osa") {
if(string_to_match!="") {
sdists <- stringdist(string_to_match, options, method=smethod)
ind <- which(sdists == min(sdists))
if(length(ind) != 1) {
ind <- ind[1] # Assumes first index is the most common string to match.
}
return(options[ind])
} else {
return("")
}
}
as.numeric.factor <- function(x) {as.numeric(levels(x))[x]} # Function to turn factor vars to numeric variables correctly.
########################################
## Read in Data:
cantons <- read_dta(file="./Output/cantons_wGeoCovariates.dta")
# Note: Data doesn't have main RD variables of interest, need to merge them in:
# Vars created in ESLR_AnalysisConflictData.R
canton_rd_vars <- read.csv(file="./Data/conflict_canton.csv", header=TRUE)
# canton_rd_vars <- read.csv(file="./R/Output/conflict_canton_subset.csv", header=TRUE)
# Keep Vars of Interest and Merge in:
canton_rd_vars <- dplyr::select(canton_rd_vars,CODIGO,num_holdings:max_above_500)
#cantons <- dplyr::select(cantons,-reform)
cantons <- left_join(cantons,canton_rd_vars, by="CODIGO")
cantons$CODIGO_NOM <- as.character(cantons$CODIGO_)
########################################
poblaccion_section <- read_sav(file = "./Data/poblacion.sav")
########################################
cantons_popcensus <- dplyr::select(poblaccion_section,
gender=S06P02,
age=S06P03A,
S06P07A, S06P08A1, S06P08A2,
DEPDSC, MUNDSC, CANDSC,
literate = S06P09,
educated = S06P10,
educ_level = S06P11A,
finished_hs = S06P11B,
S06P22)
cantons_popcensus <- mutate(cantons_popcensus,
born_same_as_mother= ifelse(S06P07A < 3,S06P07A,NA) ,
lived_canton_always = ifelse(S06P08A1 < 3,S06P08A1,NA),
lived_canton_year = ifelse(S06P08A2>0,S06P08A2,NA),
public_sector_worker = ifelse(S06P22 == 1, 1,
ifelse(is.na(S06P22) | S06P22==-2,NA, 0)),
pop = 1,
CODIGO_NOM = toupper(stri_trans_general(paste(DEPDSC, MUNDSC, CANDSC,sep=", "),"Latin-ASCII")))
cantons_popcensus <- mutate(cantons_popcensus,
born_same_as_mother= ifelse(born_same_as_mother ==2 ,0,born_same_as_mother) ,
lived_canton_always = ifelse(lived_canton_always ==2 ,0,lived_canton_always),
educ_yrs = 1*(educ_level==1)+6*(educ_level==2)+ 9*(educ_level==3)+
11*(educ_level==4)+13*(educ_level==5)+ 15*(educ_level==6)+
16*(educ_level==7)+ 17*(educ_level==8)+ 20*(educ_level==9))
cantons_popcensus <- filter(cantons_popcensus, CANDSC != "AREA URBANA")
# Summarise to make merging faster:
cantons_popcensus <- cantons_popcensus %>%
group_by(CODIGO_NOM) %>%
summarise_if(is.numeric, mean, na.rm = TRUE)
max.dist <- 15 # since there are errors in mun names + state names
# inds <- amatch(cantons_popcensus$CODIGO_NOM, cantons$CODIGO_NOM, maxDist=max.dist) # can try different maxDists and different methods (using levenstein right now as default i believe)
# # View(t(rbind(cantons_literacy$CODIGO_NOM,as.character(cantons$CODIGO_NOM[inds]))))
# cantons_popcensus$CODIGO <- cantons$CODIGO[inds]
# cantons <- left_join(cantons, cantons_popcensus, by="CODIGO")
max.dist <- 10 # since there are errors in mun names + state names
cantons <- stringdist_join(cantons, cantons_popcensus,
by = c("CODIGO_NOM" = "CODIGO_NOM"),
mode = "left",
method = "jw",
max_dist = max.dist,
distance_col = "dist")
cantons <- cantons %>%
group_by(CODIGO_NOM.x) %>%
top_n(1, -dist) %>% ungroup()
as.numeric.factor <- function(x) {as.numeric(levels(x))[x]}
as.numeric.factor.wcheck <- function(x) {if(class(x)=="factor") { return(as.numeric(levels(x))[x]) } else { return(x)}}
### Using Share Above 500
cantons$share_above500 <- cantons$num_above500/(cantons$num_above500 + cantons$num_below500)
## Same Canton Always:
b0 <- lm(lived_canton_always ~ share_above500 + gender + age + age^2 , data=cantons)
cov0 <- vcovHC(b0, type = "HC1")
robust.se0 <- sqrt(diag(cov0))
summary(b0)
## Same Canton Year:
b1 <- lm(lived_canton_year ~ share_above500 + gender + age + age^2, data=cantons)
cov1 <- vcovHC(b1, type = "HC1")
robust.se1 <- sqrt(diag(cov1))
summary(b1)
## Same Canton - Mother:
b2 <- lm(born_same_as_mother ~ share_above500 + gender + age + age^2, data=cantons)
cov2 <- vcovHC(b2, type = "HC1")
robust.se2 <- sqrt(diag(cov2))
summary(b2)
stargazer(b0,b1,b2,
type = "latex",
se = list(robust.se0, robust.se1,robust.se2),
keep = c("share_above500"),
digits = 4,
out="./Output/MigrationOutcomes_CantonLevel.tex")
########################################
## Now for highly educated sample
cantons_popcensus <- dplyr::select(poblaccion_section,
gender=S06P02,
age=S06P03A,
S06P07A, S06P08A1, S06P08A2,
DEPDSC, MUNDSC, CANDSC,
literate = S06P09,
educated = S06P10,
educ_level = S06P11A,
finished_hs = S06P11B)
cantons_popcensus <- mutate(cantons_popcensus,
born_same_as_mother= ifelse(S06P07A < 3,S06P07A,NA) ,
lived_canton_always = ifelse(S06P08A1 < 3,S06P08A1,NA),
lived_canton_year = ifelse(S06P08A2>0,S06P08A2,NA),
finished_hs = ifelse(finished_hs>0,finished_hs, NA),
CODIGO_NOM = toupper(stri_trans_general(paste(DEPDSC, MUNDSC, CANDSC,sep=", "),"Latin-ASCII")))
cantons_popcensus <- mutate(cantons_popcensus,
born_same_as_mother= ifelse(born_same_as_mother ==2 ,0,born_same_as_mother) ,
finished_hs = ifelse(finished_hs==2, 0, finished_hs),
lived_canton_always = ifelse(lived_canton_always ==2 ,0,lived_canton_always)
)
cantons_popcensus_educ <- filter(cantons_popcensus,
finished_hs==1)
cantons_popcensus_educ <- filter(cantons_popcensus_educ, CANDSC != "AREA URBANA")
# Summarise to make merging faster:
cantons_popcensus_educ <- cantons_popcensus_educ %>%
group_by(CODIGO_NOM) %>%
summarise_if(is.numeric, mean, na.rm = TRUE)
max.dist <- 15 # since there are errors in mun names + state names
cantons <- read_dta(file="./Output/cantons_wGeoCovariates.dta")
cantons <- left_join(cantons,canton_rd_vars, by="CODIGO")
cantons$CODIGO_NOM <- as.character(cantons$CODIGO_)
max.dist <- 10 # since there are errors in mun names + state names
cantons <- stringdist_join(cantons, cantons_popcensus_educ,
by = c("CODIGO_NOM" = "CODIGO_NOM"),
mode = "left",
method = "jw",
max_dist = max.dist,
distance_col = "dist")
cantons <- cantons %>%
group_by(CODIGO_NOM.x) %>%
top_n(1, -dist) %>% ungroup()
as.numeric.factor <- function(x) {as.numeric(levels(x))[x]}
as.numeric.factor.wcheck <- function(x) {if(class(x)=="factor") { return(as.numeric(levels(x))[x]) } else { return(x)}}
### Using Share Above 500
cantons$share_above500 <- cantons$num_above500/(cantons$num_above500 + cantons$num_below500)
## Same Canton Always:
b0 <- lm(lived_canton_always ~ share_above500 + gender + age + age^2 , data=cantons)
cov0 <- vcovHC(b0, type = "HC1")
robust.se0 <- sqrt(diag(cov0))
summary(b0)
## Same Canton Year:
b1 <- lm(lived_canton_year ~ share_above500 + gender + age + age^2, data=cantons)
cov1 <- vcovHC(b1, type = "HC1")
robust.se1 <- sqrt(diag(cov1))
summary(b1)
## Same Canton - Mother:
b2 <- lm(born_same_as_mother ~ share_above500 + gender + age + age^2, data=cantons)
cov2 <- vcovHC(b2, type = "HC1")
robust.se2 <- sqrt(diag(cov2))
summary(b2)
stargazer(b0,b1,b2,
type = "latex",
se = list(robust.se0, robust.se1,robust.se2),
keep = c("share_above500"),
digits = 4,
out="./Output/MigrationOutcomes_CantonLevel_CompletedHS.tex")
|