Path: blob/main/Trabajo_final/grupo1/Grupo1_Pregunta1_OLS_R.R
2714 views
install.packages("rlang")1install.packages("Metrics")2install.packages("xtable")3install.packages("tidyverse")45library(ggplot2)6library(Metrics)7library(haven)8library(dplyr)9library(stargazer)10library(plm)11library(lmtest)12library(sandwich)13library(estimatr)14library(lfe)15library(caret)16library(texreg)17library(mfx)18library(fastDummies)19library(lubridate)20library(tidyverse)2122librarian::shelf(plm,lfe,caret,texreg,mfx,fastDummies,estimatr,dplyr,stargazer,tidyverse,sandwich,lmtest,xtable,haven)2324#Seteamos el directorio25user <- Sys.getenv("USERNAME") # username2627setwd( paste0("C:/Users/",user,"/Documents/GitHub/1ECO35_2022_2") ) # set directorio282930DATA <- read_dta("Trabajo_final/datos/mss_repdata.dta")31DATA <-as.data.frame(DATA)3233#Me quedo con las variables necesarias para realizar la tabla de estad�sticos34dat_2 <- DATA %>% dplyr::select(NDVI_g,tot_100,trade_pGDP,pop_den_rur,land_crop,va_agr,va_ind_manf) %>% as.data.frame()3536#Tabla de estad�sticas de variables seleccionadas37list_var <- c("Tasa de variaci�n del �ndice de vegetaci�n", "T�rminos de intercambio",38"Porcentaje de las exportaciones recpecto al PBI", "Densidad poblacional rural",39"Porcentaje de tierra cultivable en uso", "Valor agregado del sector agricultura respecto PBI",40"Valor agregado del sector manufacturero respecto PBI")4142stargazer(dat_2, title = "Descriptive Statistics", digits = 2, covariate.labels = list_var,43summary.stat = c("mean", "sd", "n"),min.max = F,44notes.append = FALSE, notes.align = 'l')4546#Se crean dummies para cada pa�s4748DATA <- dummy_cols(DATA, select_columns = 'ccode')4950index <- grep("ccode_", colnames(DATA))5152# Se crea la variable a�o a partir de 1981 en adelante53DATA$time_year <- DATA$year - 19805455list_vars_mod <- names(DATA)[index]56list_vars_mod[1]5758#Creamos la variable a�o x pa�s.5960i = 16162while(i < 42){6364var <- paste0(list_vars_mod[i],"_","time")65DATA[var] <- DATA[list_vars_mod[i]]*DATA["time_year"]6667i = i + 168}697071# Modelo (1)727374index_country_time <- grep("_time$", colnames(DATA))75country_time_trend <-names(DATA)[index_country_time]76index_country <- grep("^ccode_\\d+$", colnames(DATA))7778country_fe <-names(DATA)[index_country]798081model1_formula <- as.formula(82paste("any_prio",83"~",84paste("GPCP_g","GPCP_g_l", paste(country_time_trend, collapse = "+"),85paste(country_fe, collapse = "+")86, sep="+")87)88)899091ols_model1 <- lm_robust(model1_formula, data = DATA,92clusters = ccode, se_type = "stata")9394summary(ols_model1)95tidy(ols_model1)96glance(ols_model1)9798#Usamos LM y luego coestest para hallar los errores standar robusto"99100101m1 <- lm(model1_formula, data = DATA)102103lm_rmse1 <- round(rmse(m1$fitted.values, DATA$any_prio) ,2 )104105robust_model1 <- coeftest(m1 ,106vcov = vcovCL,107type = "HC1",108cluster = ~ ccode)109110111112sd_robust_model1 <- robust_model1[,2]113114115116# Modelo (2)117118model2_formula <- as.formula(119paste("war_prio",120"~",121paste("GPCP_g","GPCP_g_l", paste(country_time_trend, collapse = "+"),122paste(country_fe, collapse = "+")123, sep="+")124)125)126127128ols_model2 <- lm_robust(model2_formula, data = DATA,129clusters = ccode, se_type = "stata")130131summary(ols_model2)132133#Usando LM y luego coestest para los error standar robusto"134135136m2 <- lm(model2_formula, data = DATA)137138lm_rmse2 <- round(rmse(m2$fitted.values, DATA$war_prio ) ,2 )139140robust_model2 <- coeftest(m2 ,141vcov = vcovCL,142type = "HC1",143cluster = ~ ccode)144145146147sd_robust_model2 <- robust_model2[,2]148149#Exportando la tabla150151152stargazer( m1, m2,153se=list(sd_robust_model1, sd_robust_model2),154dep.var.labels = c(""),155title = "Rainfall and Civil Conflict (Reduced-Form)",156keep = c("GPCP_g","GPCP_g_l"),157covariate.labels=c("Growth in rainfall, t","Growth in rainfall, t-1"),158align = T, no.space = T,159add.lines=list(c("Country fixed effects","yes","yes"),160c("Country-specific time trends","yes","yes"),161c("Root mean square error",lm_rmse1,lm_rmse2)),162keep.stat = c("rsq","n"),163notes.append = FALSE, notes.align = "l",164notes ="Huber robust standard errors are in parentheses. Regression disturbance terms are clustered at the country level.A country-specific year time trend is included in all specifications (coefficient estimates not reported). * Significantly different from zero at 90 percent confidence. ** Significantly different from zero at 95 percent confidence. *** Significantly different from zero at 99 percent confidence.", style = "qje"165)166167168169##### Coef-plot170171# Primero calculamos los intervalos de confianza para ambos modelos172173#Modelos 1. c.i : confidence interval174175model1_lower = coefci(m1, df = Inf,176vcov. = vcovCL, cluster = ~ ccode, type = "HC1")[2,1]177178model1_upper = coefci(m1, df = Inf,179vcov. = vcovCL, cluster = ~ ccode, type = "HC1")[2,2]180181#Modelo 2. c.i : confidence interval182183model2_lower = coefci(m2, df = Inf,184vcov. = vcovCL, cluster = ~ ccode, type = "HC1")[2,1]185186model2_upper = coefci(m2, df = Inf,187vcov. = vcovCL, cluster = ~ ccode, type = "HC1")[2,2]188189190#Extraemos los coeficientes de la variable gpcp_g de ambos modelos191192model1_coef<- ols_model1$coefficients[2]193194model2_coef<- ols_model2$coefficients[2]195196# Creamos una tabla de resultados que recopile los coeficientes y los intervalos de confianza197198table<- matrix(0, 2, 3)199200table[1,1]<- model1_coef201table[1,2]<- model1_lower202table[1,3]<- model1_upper203204table[2,1]<- model2_coef205table[2,2]<- model2_lower206table[2,3]<- model2_upper207208#Le damos nombres a las filas y columnas209colnames(table)<- c("Estimate","lower_bound","upper_bound")210rownames(table)<- c("OLS 1", "OLS 2")211212213# Lo convertimos a dataframe para poder ploterarlo214tab <- as.data.frame(table)215216#Ploteamos el gr�fico217218theme_set(theme_bw(20)) # tema con fondo blanco y cuadro con bordes en negro219220options(repr.plot.width = 8, repr.plot.height =8) # tama�o del gr�fico221222223tab %>% ggplot(aes(x=rownames(tab), y=Estimate)) +224geom_point(size=2, color = 'red') +225geom_errorbar(aes(ymin=lower_bound, ymax=upper_bound) , width = 0.1,color="blue", size = 0.9) +226labs(x="", y="") + ggtitle("Stimated Coefficient of growth in rainfall (95% CI)") +227theme(text=element_text(size =8), plot.title = element_text(hjust = 0.5)) +228geom_hline(yintercept=0, linetype="dashed", color = "black", size=1) +229scale_x_discrete(limits = c("OLS 1","OLS 2")) + # order x - axis230theme(panel.grid.major = element_blank(), # borramos las cuadr�culas en el fondo231panel.grid.minor = element_blank())232233# Guardamos la imagen del coefplot en la ruta especificada234235ggsave("Trabajo_final/grupo1/plots/Coef_plot.png"236, height = 8 # alto237, width = 12 # ancho238, dpi = 320 # resoluci�n (calidad de la imagen)239)240241242243244