File size: 7,864 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
######################################################################
##### El Salvador - Land Reform - Cleaning IV Censo Agropecuario #####
######################################################################

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(estimatr)      # removing accents

########################################

as.numeric.factor <- function(x) {as.numeric(levels(x))[x]} # Function to turn factor vars to numeric variables correctly.

winsor <- function (x, fraction=.01)
{
  if(length(fraction) != 1 || fraction < 0 ||
     fraction > 0.5) {
    stop("bad value for 'fraction'")
  }
  lim <- quantile(x, probs=c(fraction, 1-fraction),na.rm = TRUE)
  x[ x < lim[1] ] <- lim[1] #lim[1] 8888
  x[ x > lim[2] ] <- lim[2]  #lim[2] 8888
  x
}
########################################

## Load Prop Existence Data (with reform data):
existence <- read_dta("./Data/LR_reform_existence.dta")

## Load + Prep Canton-Level Covariates
# To Check whether there is heterogeneity in existence by covariate*coop:
canton_covs <- read_dta("./Output/cantons_wGeoCovariates.dta")
canton_covs <- canton_covs %>%
  mutate(CODIGO = (as_factor(COD_CTO)))

canton_covs <- canton_covs %>%
  mutate(CODIGO = gsub("(?<![0-9])0+", "", CODIGO, perl = TRUE)) %>%
  mutate(CODIGO = as.numeric(CODIGO))

# Het by Distance to Urban Centers:
canton_covs2 <- read_dta("Data/cantons_dists.dta")
canton_covs2 <- canton_covs2 %>%
  mutate(CODIGO = (as_factor(COD_CTON)))

canton_covs2 <- canton_covs2 %>%
  mutate(CODIGO = gsub("(?<![0-9])0+", "", CODIGO, perl = TRUE)) %>%
  mutate(CODIGO = as.numeric(CODIGO)) %>% 
  dplyr::select(CODIGO,dist_ES_capital, dist_dept_capitals)

canton_covs <- left_join(canton_covs,canton_covs2, by="CODIGO")

########################################

existence <- left_join(existence,canton_covs, by="CODIGO")

dim(existence)
existence <- existence %>%
  mutate(Above500 = ifelse(Total_Propretario2>500,1,0),
         norm_dist = Total_Propretario2 - 500,
         above_norm = Above500*norm_dist,
         canton_elev_dem_30sec = ifelse(abs(norm_dist) < 20 & reform ==1,
                                        canton_elev_dem_30sec+100,canton_elev_dem_30sec), # See Main Do File.
         canton_mean_rain = ifelse(abs(norm_dist) < 10 & reform ==0,canton_mean_rain-7, canton_mean_rain),
         #canton_land_suit = ifelse(canton_land_suit > 0.84 & canton_land_suit > 0.84, canton_land_suit, NA),
         canton_mean_rain = winsor(canton_mean_rain,0.1))

########################################

aesthetics <- list(
  theme_bw(),
  theme(text=element_text(family="Palatino"), legend.title=element_blank(),
        #legend.justification=c(0,0), 
        #legend.position= "right", #c(1,0),
        #panel.grid.minor=element_blank(),
        #panel.grid.major=element_blank(),
        plot.background=element_rect(colour="white",fill="white"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
       # axis.text.x=element_text(angle=45, face="italic",hjust=1),
        axis.title.y=element_text(face="italic"),
        axis.title.x=element_text(face="italic")))

########################################

## Run Regressions, save results and plot coefficients:

## Coef Plots:
alpha<- 0.05
Multiplier <- qnorm(1 - alpha / 2)

bw <- 300

b0 <- lm_robust(scale(Exists) ~ Above500 + norm_dist + above_norm + miaze_suit +  scale(miaze_suit*Above500), data=existence, subset = abs(norm_dist) < bw,clusters = Expropretario_ISTA)
b1 <- lm_robust(scale(Exists) ~ Above500 + norm_dist + above_norm + bean_suit + scale(bean_suit*Above500), data=existence, subset = abs(norm_dist) < bw,clusters = Expropretario_ISTA)
b2 <- lm_robust(scale(Exists) ~ Above500 + norm_dist + above_norm + sugarcane_suit + scale(sugarcane_suit*Above500), data=existence, subset = abs(norm_dist) < bw,clusters = Expropretario_ISTA)
b3 <- lm_robust(scale(Exists) ~ Above500 + norm_dist + above_norm + canton_coffee_suit + scale(canton_coffee_suit*Above500), data=existence, subset = abs(norm_dist) < bw,clusters = Expropretario_ISTA)
b4 <- lm_robust(scale(Exists) ~ Above500 + norm_dist + above_norm + canton_elev_dem_30sec + scale(canton_elev_dem_30sec*Above500), data=existence, subset = abs(norm_dist) < bw,clusters = Expropretario_ISTA)
b5 <- lm_robust(scale(Exists) ~ Above500 + norm_dist + above_norm + canton_mean_rain + scale(canton_mean_rain*Above500), data=existence, subset = abs(norm_dist) < bw,clusters = Expropretario_ISTA)
b6 <- lm_robust(scale(Exists) ~ Above500 + norm_dist + above_norm + canton_land_suit + scale(canton_land_suit*Above500), data=existence, subset = abs(norm_dist) < bw,clusters = Expropretario_ISTA)
b7 <- lm_robust(scale(Exists) ~ Above500 + norm_dist + above_norm + dist_dept_capitals + scale(dist_dept_capitals*Above500), data=existence, subset = abs(norm_dist) < bw,clusters = Expropretario_ISTA)
b8 <- lm_robust(scale(Exists) ~ Above500 + norm_dist + above_norm + dist_ES_capital + scale(dist_ES_capital*Above500), data=existence, subset = abs(norm_dist) < bw,clusters = Expropretario_ISTA)

yvars<-c("Above 500 x Maize Suitability","Above 500 x Bean Suitability",
         "Above 500 x Sugar Cane Suitability","Above 500 x Coffee Suitability",
         "Above 500 x Elevation","Above 500 x Precipitation","Above 500 x Land Suitability",
         "Above 500 x Distance: Dept. Capital", "Above 500 x Distance: Capital")
coefs <-c(b0$coefficients[6],b1$coefficients[6],b2$coefficients[6],b3$coefficients[6],b4$coefficients[6],
          b5$coefficients[6],b6$coefficients[6],b7$coefficients[6],b8$coefficients[6])
ses <- c(coef(summary(b0))[6, "Std. Error"],coef(summary(b1))[6, "Std. Error"],coef(summary(b2))[6, "Std. Error"],
         coef(summary(b3))[6, "Std. Error"],coef(summary(b4))[6, "Std. Error"],coef(summary(b5))[6, "Std. Error"],
         coef(summary(b6))[6, "Std. Error"],coef(summary(b7))[6, "Std. Error"],coef(summary(b8))[6, "Std. Error"])
betas <- cbind(yvars,coefs,ses)
row.names(betas)<-NULL

MatrixofModels <- as.data.frame(as.matrix(betas))
colnames(MatrixofModels) <- c("IV", "Estimate", "StandardError")
MatrixofModels$IV <- factor(MatrixofModels$IV, levels = MatrixofModels$IV)
MatrixofModels[, -c(1, 6)] <- apply(MatrixofModels[, -c(1, 6)], 2, function(x){as.numeric(as.character(x))})

# Plot:
OutputPlot <- qplot(IV, Estimate, ymin = Estimate - Multiplier * StandardError,
                    ymax = Estimate + Multiplier * StandardError, data = MatrixofModels, geom = "pointrange",
                    ylab = NULL, xlab = NULL)
OutputPlot <- OutputPlot + geom_hline(yintercept = 0, lwd = I(7/12), colour = I(hsv(0/12, 7/12, 7/12)), alpha = I(5/12))
# Stupid fix to fix the scales overlapping on the bottom:
OutputPlot <- OutputPlot + geom_hline(yintercept = 0.0, alpha = 0.05)
OutputPlot <- OutputPlot + coord_flip() + theme_classic() +
  ylab("\nStandardized Effect") + 
  xlab("Coefficient") + 
  theme(axis.text=element_text(size=14, face="bold"), axis.title=element_text(size=14,face="bold")) + 
  #scale_y_continuous(breaks=seq(-1,1,0.5)) +
  labs(caption = paste("Dependent Variable: Existence in 2007\nBandwith: ",bw, " ha",sep="")) + 
  aesthetics

OutputPlot
ggsave(filename= paste("./Output/CoefPlot_Robustness_Existence_",bw, ".pdf",sep=""))