File size: 18,812 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
#############################################
########### ESLR - EXTENTSIONS ##############
############### COEF PLOTS ##################
#############################################
rm(list = ls()) # Clear variables
require(foreign)
require(ggplot2)
require(RColorBrewer) # creates nice color schemes
require(scales) # customize scales
require(plyr) # join function
require(dplyr)
require(haven) # stata save
require(dotwhisker)
########################################
# write a simple function to add footnote
makeFootnote <- function(footnoteText =
format(Sys.time(), "%d %b %Y"),
size = .7, color = grey(.5))
{
require(grid)
pushViewport(viewport())
grid.text(label = footnoteText ,
x = unit(1,"npc") - unit(2, "mm"),
y = unit(2, "mm"),
just = c("right", "bottom"),
gp = gpar(cex = size, col = color))
popViewport()
}
# Source: http://statmodeling.com/best-way-to-add-a-footnote-to-a-plot-created-with-ggplot2.html
########################################
## Making Standarized Coefficient Plots:
# Set aesthetics:
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="bold",hjust=1),
axis.title.y=element_text(face="bold.italic")))
#axis.text=element_blank(),
#axis.ticks=element_blank(),
#panel.border = element_blank()))
########################################
#### Plots for Different Minor Crops:
## load data:
data <- read.csv("./Output/Temp/MinorCropProduction.csv")
data <- filter(data,estimate!=0)
# Clean data for plotting:
alpha<- 0.05
Multiplier <- qnorm(1 - alpha / 2)
# Find the outcome var for each regression:
data$idstr <- as.character(data$idstr)
data$y_var <- data$idstr
data <- filter(data,y_var!="S5BEJOTE",
y_var!="S5BMELON",
y_var!="S5BCAMOTE")
# Replace y_var with nice names:
data$y_var[which(data$y_var == "S5BCAMOTE")] <- "Sweet Potato"
data$y_var[which(data$y_var == "S5BCHILE")] <- "Bell Peppers"
data$y_var[which(data$y_var == "S5BCHILEPICANTE")] <- "Chile"
data$y_var[which(data$y_var == "S5BEJOTE")] <- "Bejote"
data$y_var[which(data$y_var == "S5BGUISQUIL")] <- "Squash"
data$y_var[which(data$y_var == "S5BLOROCO")] <- "Loroco"
data$y_var[which(data$y_var == "S5BMELON")] <- "Melon"
data$y_var[which(data$y_var == "S5BPEPINO")] <- "Cucumber"
data$y_var[which(data$y_var == "S5BPIPIAN")] <- "Pipian"
data$y_var[which(data$y_var == "S5BRABANO")] <- "Radish"
data$y_var[which(data$y_var == "S5BSANDIA")] <- "Watermelon"
data$y_var[which(data$y_var == "S5BTOMATE")] <- "Tomato"
data$y_var[which(data$y_var == "S5BYUCA")] <- "Yuca"
# Now, keep only the betas of interest:
betas <- data %>% filter(!grepl("S5B",y_var))
dim(betas)
betas <- arrange(betas,betas$y_var)
# Create Matrix for plotting:
MatrixofModels <- betas[c("y_var", "estimate","stderr","z","p","idnum")]
colnames(MatrixofModels) <- c("IV", "Estimate", "StandardError", "TValue", "PValue", "ModelName")
MatrixofModels$IV <- factor(MatrixofModels$IV, levels = MatrixofModels$IV)
#MatrixofModels$Legend <- c(" AES Coefficient", rep(" Standard Coefficients",4))
# Re-name for plotting:
MatrixofModels$ModelName <- "Minor Vegetable Production"
#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))
OutputPlot <- OutputPlot + theme_bw() + ylab("\nEstimated Effect") + aesthetics + xlab("")
# Save:
OutputPlot
ggsave(filename = "./Output/CoefPlot_MinorCrops.pdf", height=6, width=9)
########################################
#### Plots for Different Minor Fruits:
## load data:
data <- read.csv("./Output/Temp/MinorFruitProduction.csv")
data <- filter(data,estimate!=0)
#data$parm[which(data$parm == "RD_Estimate")] <- "Above 500"
# Clean data for plotting:
alpha<- 0.05
Multiplier <- qnorm(1 - alpha / 2)
# Find the outcome var for each regression:
data$idstr <- as.character(data$idstr)
data$y_var <- data$idstr
# Replace y_var with nice names:
data$y_var[which(data$y_var == "S8BCOCO")] <- "Coconut"
data$y_var[which(data$y_var == "S8BGUINEO")] <- "Guineo Banana"
data$y_var[which(data$y_var == "S8BJOCOTE")] <- "Jocote"
data$y_var[which(data$y_var == "S8BLIMON")] <- "Lemon"
data$y_var[which(data$y_var == "S8BMANDARINA")] <- "Mandarin"
data$y_var[which(data$y_var == "S8BMANGO")] <- "Mango"
data$y_var[which(data$y_var == "S8BNARANJA")] <- "Orange"
data$y_var[which(data$y_var == "S8BNISPERO")] <- "Medlar"
data$y_var[which(data$y_var == "S8BPAPAYA")] <- "Papaya"
data$y_var[which(data$y_var == "S8BPLATANO")] <- "Plantain"
data$y_var[which(data$y_var == "S8BZAPOTE")] <- "Sapodilla"
# Now, keep only the betas of interest:
betas <- data %>% filter(!grepl("S8B",y_var))
dim(betas)
betas <- arrange(betas,betas$y_var)
# Create Matrix for plotting:
MatrixofModels <- betas[c("y_var", "estimate","stderr","z","p","idnum")]
colnames(MatrixofModels) <- c("IV", "Estimate", "StandardError", "TValue", "PValue", "ModelName")
MatrixofModels$IV <- factor(MatrixofModels$IV, levels = MatrixofModels$IV)
#MatrixofModels$Legend <- c(" AES Coefficient", rep(" Standard Coefficients",4))
# Re-name for plotting:
MatrixofModels$ModelName <- "Minor Fruit Production"
#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))
OutputPlot <- OutputPlot + theme_bw() + ylab("\nEstimated Effect") + aesthetics + xlab("")
# Save:
OutputPlot
ggsave(filename = "./Output/CoefPlot_MinorFruits.pdf", height=6, width=9)
########################################
#### Plots for Different Inputs:
## load data:
data <- read.csv("./Output/Temp/InputUse.csv")
data <- filter(data,estimate!=0)
#data$parm[which(data$parm == "RD_Estimate")] <- "Above 500"
# Clean data for plotting:
alpha<- 0.05
Multiplier <- qnorm(1 - alpha / 2)
# Find the outcome var for each regression:
data$idstr <- as.character(data$idstr)
data$y_var <- data$idstr
data <- filter(data,y_var!="S15BCASTRACION",
y_var!="S15BCONTROLBIOLOGICOPECESABEJAS",
y_var!="S15BCONTROLQUIMICODEPLAGASYENFE",
y_var!="S15BDESPARASITACION",
y_var!="S15BDESPARASITANTES",
y_var!="S15BINSEMINACIONARTIFICIAL",
y_var!="S15BMANEJOINTEGRADODEPLAGASMIP",
y_var!="S15BMEJORAMIENTOGENETICO",
y_var!="S15BPIEDECRIA",
y_var!="S15BPRACTICASPREVENTIVASDEDANOS",
y_var!="S15BPRODUCTOSVETERINARIOSALCOHO",
y_var!="S15BREGISTROSADMINISTRATIVOSDEL",
y_var!="S15BREGULADORESDECRECIMIENTO",
y_var!="S15BREGULADORESDECRECIMIENTOENZ",
y_var!="S15BROTACIONDEPOTREROS",
y_var!="S15BSUPLEMENTOSNUTRICIONALES",
y_var!="S15BVACUNACION",
y_var!="S15BVACUNAS",
y_var!="S15BANTIBIOTICOS")
# Replace y_var with nice names:
# cat(paste("data$y_var[which(data$y_var == ",data$y_var,")] <- "),sep="\n")
data$y_var[which(data$y_var == "S15BABONOOFERTILIZANTEFOLIARLIQ" )] <- "Fertilizer - Liquid"
data$y_var[which(data$y_var == "S15BABONOOFERTILIZANTEGRANULARS" )] <- "Fertilizer - Solid"
data$y_var[which(data$y_var == "S15BAGENTESDEMADURACIONPOSTCOSE" )] <- "Compost"
data$y_var[which(data$y_var == "S15BAGENTESPARAPROTECCIONDEPROD" )] <- "Pesticides"
data$y_var[which(data$y_var == "S15BANALISISDESUELOYOFOLIAR" )] <- "Soil Tests"
data$y_var[which(data$y_var == "S15BAPLICACIONDEABONOYFERTILIZA" )] <- "Fertilizer Applied"
data$y_var[which(data$y_var == "S15BAPLICACIONDERIEGOPORASPERSI" )] <- "Sprinkler Irrigation"
data$y_var[which(data$y_var == "S15BAPLICACIONDERIEGOPORGOTEO" )] <- "Drip Irrigation"
data$y_var[which(data$y_var == "S15BAPLICACIONDERIEGOPORGRAVEDA" )] <- "Gravity Irrigation"
data$y_var[which(data$y_var == "S15BBACTERICIDAS" )] <- "Bactericides"
data$y_var[which(data$y_var == "S15BFUNGICIDAS" )] <- "Fungicides"
data$y_var[which(data$y_var == "S15BLABORESCULTURALES" )] <- "Labor Trimming"
data$y_var[which(data$y_var == "S15BMATERIALVEGETATIVO" )] <- "Organic Fertilizer"
data$y_var[which(data$y_var == "S15BNEMATICIDAS" )] <- "Nematicides"
data$y_var[which(data$y_var == "S15BOBRASDECONSERVACIONDESUELOS" )] <- "Erosion Work"
data$y_var[which(data$y_var == "S15BPREPARACIONDELSUELO" )] <- "Soil Preparation"
data$y_var[which(data$y_var == "S15BPROTECCIONDECULTIVOS" )] <- "Crop Protection"
data$y_var[which(data$y_var == "S15BRESIEMBRAYOREPLANTACION" )] <- "Reseeding + Replanting"
data$y_var[which(data$y_var == "S15BSEMILLACERTIFICADA" )] <- "Certified Seeds"
data$y_var[which(data$y_var == "S15BSEMILLACRIOLLA" )] <- "Creole Seeds"
data$y_var[which(data$y_var == "S15BSEMILLAMEJORADA" )] <- "Improved Seeds"
# Now, keep only the betas of interest:
betas <- data %>% filter(!grepl("S15B",y_var))
dim(betas)
betas <- arrange(betas,betas$estimate)
# Create Matrix for plotting:
MatrixofModels <- betas[c("y_var", "estimate","stderr","z","p","idnum")]
colnames(MatrixofModels) <- c("IV", "Estimate", "StandardError", "TValue", "PValue", "ModelName")
MatrixofModels$IV <- factor(MatrixofModels$IV, levels = MatrixofModels$IV)
#MatrixofModels$Legend <- c(" AES Coefficient", rep(" Standard Coefficients",4))
# Re-name for plotting:
MatrixofModels$ModelName <- "Input Use"
#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))
OutputPlot <- OutputPlot + theme_bw() + ylab("\nEstimated Effect") + aesthetics + xlab("")
# Save:
OutputPlot
ggsave(filename = "./Output/CoefPlot_Inputs.pdf", height=6, width=9)
########################################
## load data:
data <- read.csv("./Output/Temp/CapitalStocks.csv")
data <- filter(data,estimate!=0 & !is.na(p))
# Clean data for plotting:
alpha<- 0.05
Multiplier <- qnorm(1-alpha/2) #qt(1 - alpha / 2, df=75) # Small estimates
Multiplier <- qt(1 - alpha / 2, df=75) # Small estimates
# Find the outcome var for each regression:
data$idstr <- as.character(data$idstr)
data$y_var <- gsub("S16A","",data$idstr)
data <- filter(data,y_var!="ALIMENTADORES",
y_var!="AUTOCLAVE",
y_var!="BANDADEINCUBACION",
y_var!="DESPLUMADORAS",
y_var!="EQUIPODEIDENTIFICACION",
y_var!="EQUIPOPARAINSEMINACIONARTIF",
y_var!="EQUIPOPARAORDENO",
y_var!="EQUIPOPREVENTIVODEDANOSENAN",
y_var!="ESTABLOS",
y_var!="GALERAS",
y_var!="INFRAESTRUCTURAPARAALIMENTA",
y_var!="LABORATORIOINVITRO",
y_var!="LABORATORIOSDEANALISISDESUE",
y_var!="MANGASOCEPOS",
y_var!="MAQUINARIAPARAPRODUCCIONDEA",
y_var!="OTROSTALLERESPISTADEATERRIZ",
y_var!="MOLEDORADEGRANOS",
y_var!="REDES",
y_var!="SALASDEINCUBACION",
y_var!="SALASDEORDENO",
y_var!="BANDARECOLECTORADEHUEVOS",
y_var!="CLASIFICADORADEFRUTALESHORT",
y_var!="UTENSILIOSPARARECOLECCIONDE",
y_var!="HERRAMIENTASAGROPECUARIAS",
y_var!="TANQUESDEFERTIRRIEGO",
y_var!="SALASDECURADO")
# Replace y_var with nice names:
# cat(paste('data$y_var[which(data$y_var == \"',data$y_var,"\")] <- \"",data$y_var,"\"",sep=""),sep="\n")
data$y_var[which(data$y_var == "ARADOSDEHIERRO")] <- "Plows"
data$y_var[which(data$y_var == "BALANZAPARACARGASPESADAS")] <- "Balances"
data$y_var[which(data$y_var == "BASCULA")] <- "Coffee Weighing Machines"
data$y_var[which(data$y_var == "BODEGAS")] <- "Wharehouses"
data$y_var[which(data$y_var == "BOMBAACHICADORAMECANICA")] <- "Fumigation Backpacks"
data$y_var[which(data$y_var == "CAMIONOVEHICULOS")] <- "Trucks"
data$y_var[which(data$y_var == "DESPULPADORADECAFEMANUAL")] <- "Manual Coffee Pulping Machines"
data$y_var[which(data$y_var == "DESPULPADORADECAFEMECANICA")] <- "Mecanical Coffee Pulping Machines"
data$y_var[which(data$y_var == "EQUIPOBENEFICIADORCAFE")] <- "Coffee Equipement"
data$y_var[which(data$y_var == "EQUIPODEFUMIGACION")] <- "Fumigation Equipement"
data$y_var[which(data$y_var == "EQUIPODERIEGO")] <- "Irrigration Equipement"
data$y_var[which(data$y_var == "EQUIPODETRANSPORTEDEAGUA")] <- "Water Transportation Equipement"
data$y_var[which(data$y_var == "EQUIPOPARALACOSECHA")] <- "Harvest Equipment"
data$y_var[which(data$y_var == "HERRAMIENTASAGROPECUARIAS")] <- "Agrigultural Tools"
data$y_var[which(data$y_var == "MANGUERAS")] <- "Hoses"
data$y_var[which(data$y_var == "MOTOSIERRAS")] <- "Saws"
data$y_var[which(data$y_var == "OFICINAS")] <- "Offices"
data$y_var[which(data$y_var == "PATIOSDESECADO")] <- "Drying Patios"
data$y_var[which(data$y_var == "PICADORADEPASTO")] <- "Lawnmowers"
data$y_var[which(data$y_var == "RASTRASYMONTACARGAS")] <- "Harrows"
data$y_var[which(data$y_var == "SEMBRADORAMECANICA")] <- "Mecanical Seeders"
data$y_var[which(data$y_var == "SILOSPARAFORRAJEFRESCO")] <- "Storage Silos"
data$y_var[which(data$y_var == "TANQUESDEFERTIRRIEGO")] <- "Irrigation Tanks"
data$y_var[which(data$y_var == "TANQUESPARAALMACENAMIENTODE")] <- "Water Storage Tanks"
data$y_var[which(data$y_var == "TOLDODERECIBIDERODECAFE")] <- "Coffee Drying Tarps"
data$y_var[which(data$y_var == "TRACTORES")] <- "Tractors"
data$y_var[which(data$y_var == "UTENSILIOSPARARECOLECCIONDE")] <- "UTENSILIOSPARARECOLECCIONDE"
data$y_var[which(data$y_var == "VIVIENDAS")] <- "Houses"
data$y_var[which(data$y_var == "BALANZADEPRECISION")] <- "Precision Scales"
data$y_var[which(data$y_var == "DESOPERCULADORYOTRASHERRAMI")] <- "Uncapper"
data$y_var[which(data$y_var == "EQUIPOPARAALIMENTACION")] <- "Feeding Equipement"
data$y_var[which(data$y_var == "EQUIPODECALEFACCION")] <- "Heating Equipement"
data$y_var[which(data$y_var == "PULVERIZADORES")] <- "Spraying Equipement"
data$y_var[which(data$y_var == "ESPATULAS")] <- "Spatulas"
data$y_var[which(data$y_var == "EXTRATORDEMIEL")] <- "Honey Extractor"
data$y_var[which(data$y_var == "VESTIMENTAESPECIAL")] <- "Special Clothing"
data$y_var[which(data$y_var == "AHUMADORES")] <- "Smoking Equipement"
data$y_var[which(data$y_var == "PORQUERIZAS")] <- "Pig Equipement"
data <- filter(data,y_var!="Offices", y_var!="Wharehouses",
y_var!="Lawnmowers",
y_var!="Water Storage Tanks",
y_var!="Storage Silos") # Remove largest estimates/unclear topic/unrelated to AG
# Now, keep only the betas of interest:
betas <- data %>% filter(!grepl("S16B",y_var))
dim(betas)
betas <- arrange(betas,betas$estimate)
# Create Matrix for plotting:
MatrixofModels <- betas[c("y_var", "estimate","stderr","z","p","idnum")]
colnames(MatrixofModels) <- c("IV", "Estimate", "StandardError", "TValue", "PValue", "ModelName")
MatrixofModels$IV <- factor(MatrixofModels$IV, levels = MatrixofModels$IV)
#MatrixofModels$Legend <- c(" AES Coefficient", rep(" Standard Coefficients",4))
# Re-name for plotting:
#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))
OutputPlot <- OutputPlot + theme_bw() + ylab("\nRD Estimate") + aesthetics + xlab("")
# Save:
OutputPlot
# Add list of brackets (Coffee Related, Non-Coffee Related)
coffee_goods <- c("Coffee-Specific Capital",
"Manual Coffee Pulping Machines",
"Mecanical Coffee Pulping Machines",
"Coffee Equipement",
"Drying Patios",
"Coffee Weighing Machines",
"Balances",
"Water Storage Tanks",
"Coffee Drying Tarps")
MatrixofModels <- suppressWarnings(MatrixofModels %>% mutate(Group = ifelse(IV %in% coffee_goods,1,0),
term=IV,
estimate= Estimate,
std.error = StandardError) %>%
arrange(-Group, -IV))
# Create list of brackets (label, topmost included predictor, bottommost included predictor)
bracket1 <- c("Coffee-Specific Capital",
"Coffee Weighing Machines",
"Mecanical Coffee Pulping Machines")
bracket2 <- c("General Ag. Capital",
"Hoses",
"Trucks")
brackets <- list(bracket1, bracket2)
{dwplot(MatrixofModels, vline = geom_vline(xintercept = 0, colour = "red", linetype = 2),
dot_args = list(color="black"),
whisker_args = list(color="black")) +
theme_bw() + xlab("RD Estimate") + ylab("") +
theme(plot.title = element_text(face="bold"),
legend.title = element_blank(), text=element_text(family="Palatino"))} %>%
add_brackets(brackets, face="bold")
# Save:
ggsave(filename = "./Output/CoefPlot_Capital_wBrackets.pdf", scale=2)
|