rm(list=ls(all=TRUE))
librarian::shelf(
tidyverse
, haven
, fastDummies
, stargazer
, sandwich
, lmtest
, estimatr
, lfe
, caret
, texreg
, mfx
)
user <- Sys.getenv("USERNAME")
setwd( paste0("C:/Users/",user,"/Documents/GitHub/1ECO35_2022_2/Lab10") )
repdata <- read_dta("../data/dataverse_files/mss_repdata.dta")
str(repdata)
lapply(repdata, class)
names(repdata)
attributes(repdata)
summary(repdata)
lapply(repdata, attr, 'label')
lapply(repdata, attr, 'labels')
repdata$ccode %>% attr('label')
repdata <- dummy_cols(repdata, select_columns = 'ccode')
index <- grep("ccode_", colnames(repdata))
repdata$time_year <- repdata$year - 1978
list_vars <- names(repdata)[index]
list_vars[1]
i = 1
while(i < 42){
var <- paste0(list_vars[i],"_","time")
repdata[var] <- repdata[list_vars[i]]*repdata["time_year"]
i = i + 1
}
table1 <- repdata %>% dplyr::select(any_prio, any_prio_on, any_prio_off,
war_prio, war_prio_on, war_prio_off, war_col, war_inc, war)
stargazer(table1)
table1 <- repdata %>% dplyr::select(any_prio, any_prio_on, any_prio_off,
war_prio, war_prio_on, war_prio_off, war_col, war_inc, war,
GPCP, GPCP_g, GPCP_g_l,gdp_g, gdp_g_l,
y_0, polity2l, polity2l_6, ethfrac, relfrac, Oil, lmtnest, lpopl1, tot_100_g) %>% as.data.frame()
stargazer(table1)
stargazer(table1, title = "Descriptive Statistics", digits = 2,
covariate.labels = c("Civil conflict with ≥25 deaths: (PRIO/
Uppsala)","Onset","Offset","Civil conflict with ≥1,000 deaths:
PRIO/Uppsala","Onset","Offset","Collier and Hoeffler (2002)",
"Doyle and Sambanis (2000)","Fearon and Laitin (2003)"))
stargazer(table1, title = "Descriptive Statistics", digits = 2,
covariate.labels = c("Civil conflict with ≥25 deaths: (PRIO/
Uppsala)","Onset","Offset","Civil conflict with ≥1,000 deaths:
PRIO/Uppsala","Onset","Offset","Collier and Hoeffler (2002)",
"Doyle and Sambanis (2000)","Fearon and Laitin (2003)"),
min.max = F)
list_vars <- c("Civil conflict with ≥25 deaths: (PRIO/
Uppsala)","Onset","Offset","Civil conflict with ≥1,000 deaths:
PRIO/Uppsala","Onset","Offset","Collier and Hoeffler (2002)",
"Doyle and Sambanis (2000)","Fearon and Laitin (2003)",
"Annual rainfall (mm), GPCP measure",
"Annual growth in rainfall, time t",
"Annual growth in rainfall, time t-1",
"Annual economic growth rate, time t",
"Annual economic growth rate, time t-1",
"Log(GDP per capita), 1979",
"Democracy level (Polity IV score, -10 to 10), time t-1",
"Democracy indicator (Polity IV score > 15),
time t-1",
"Ethnolinguistic fractionalization (source:
Atlas Marodov Mira)",
"Religious fractionalization (source: CIA
Factbook)",
"Oil-exporting country (source: WDI)",
"Log(mountainous) (source: Fearon and
Laitin 2003)",
"Log(national population), time t-1
(source: WDI)",
"Growth in terms of trade, time t (source:
WDI)"
)
stargazer(table1, title = "Descriptive Statistics", digits = 2,
covariate.labels = list_vars,
summary.stat = c("mean", "sd", "n"),
min.max = F,
notes = "Note.—The source of most characteristics is the World Bank’s World Development Indicators (WDI)."
, notes.append = FALSE,
notes.align = 'l')
ols_model <- lm(gdp_g ~ GPCP_g + GPCP_g_l, data = repdata)
attributes(ols_model)
ols_model$coefficients
ols_model$fitted.values
summary(ols_model)
summary(ols_model)$call
summary(ols_model)$coef
coeftest(ols_model, vcov = vcovHC(ols_model, "HC"))
coeftest(ols_model, vcov = vcovHC(ols_model, "HC0"))
coeftest(ols_model, vcov = vcovHC(ols_model, "HC1"))
coeftest(ols_model, vcov = vcovHC(ols_model, "HC2"))
coeftest(ols_model, vcov = vcovHC(ols_model, "HC3"))
coeftest(ols_model, vcov = vcovHC(ols_model, "HC4"))
robust_model <- coeftest(ols_model,
vcov = vcovCL,
type = "HC1",
cluster = ~ ccode)
sd_robust <- robust_lm[,2]
ols_model1 <- lm_robust(gdp_g ~ GPCP_g + GPCP_g_l, data = repdata,
clusters = ccode, se_type = "stata")
summary(ols_model1)
attributes(ols_model1)
ols_model1$r.squared
rmse1 <- RMSE(ols_model1$fitted.values, repdata$gdp_g )
RMSE(ols_model1$fitted.values, repdata$gdp_g )^2
glance(ols_model1)
tidy(ols_model1)
tidy(ols_model1)
htmlreg(ols_model1)
tidy(ols_model1)[2,2]
tidy(ols_model1)[2,6]
tidy(ols_model1)[2,7]
"Usando LM y luego coestest para los error standar robusto"
m1 <- lm(gdp_g ~ GPCP_g + GPCP_g_l, data = repdata)
lm_rmse1 <- round(RMSE(m1$fitted.values, repdata$gdp_g ) ,2 )
robust_model1 <- coeftest(m1,
vcov = vcovCL,
type = "HC1",
cluster = ~ ccode)
sd_robust_model1 <- robust_model1[,2]
model1_lower = coefci(m1, df = Inf,
vcov. = vcovCL, cluster = ~ ccode, type = "HC1")[2,1]
model1_upper = coefci(m1, df = Inf,
vcov. = vcovCL, cluster = ~ ccode, type = "HC1")[2,2]
tidy(m1, conf.int = TRUE)
control_vars <- c("y_0", "polity2l", "ethfrac", "relfrac", "Oil", "lmtnest","lpopl1")
names(repdata)
index_country_time <- grep("_time$", colnames(repdata))
country_time_trend <-names(repdata)[index_country_time]
model2_formula <- as.formula(
paste("gdp_g",
"~",
paste("GPCP_g","GPCP_g_l", paste(country_time_trend, collapse = "+"),
paste(control_vars, collapse = "+")
,sep="+")
)
)
ols_model2 <- lm_robust(model2_formula, data = repdata,
clusters = ccode, se_type = "stata")
summary(ols_model2)
glance(ols_model2)
tidy(ols_model2)
rmse2 <- RMSE(ols_model2$fitted.values, repdata$gdp_g )
htmlreg(list(ols_model1,ols_model2))
"Usando LM y luego coestest para error standar robusto"
m2 <- lm(model2_formula, data = repdata)
lm_rmse2 <- round(RMSE(m2$fitted.values, repdata$gdp_g ) ,2 )
robust_model2 <- coeftest(m2,
vcov = vcovCL,
type = "HC1",
cluster = ~ ccode)
sd_robust_model2 <- robust_model2[,2]
coefci(m2, df = Inf,
vcov. = vcovCL, cluster = ~ ccode, type = "HC1")
index_country_time <- grep("_time$", colnames(repdata))
country_time_trend <-names(repdata)[index_country_time]
index_country <- grep("^ccode_\\d+$", colnames(repdata))
country_fe <-names(repdata)[index_country]
model3_formula <- as.formula(
paste("gdp_g",
"~",
paste("GPCP_g","GPCP_g_l", paste(country_time_trend, collapse = "+"),
paste(country_fe, collapse = "+")
, sep="+")
)
)
ols_model3 <- lm_robust(model3_formula, data = repdata,
clusters = ccode, se_type = "stata")
summary(ols_model3)
glance(ols_model3)
tidy(ols_model3)
rmse3 <- RMSE(ols_model3$fitted.values, repdata$gdp_g )
rmse3 <- RMSE(ols_model3$fitted.values, repdata$gdp_g )
model3_formula <- as.formula(
paste("gdp_g",
"~",
paste("GPCP_g","GPCP_g_l",
paste(country_time_trend, collapse = "+")
, sep="+")
)
)
ols_model3 <- lm_robust(model3_formula, data = repdata,
clusters = ccode, se_type = "stata", fixed_effects = ~ ccode)
summary(ols_model3)
tidy(ols_model3)
glance(ols_model3)
rmse3 <- RMSE(ols_model3$fitted.values, repdata$gdp_g )
repdata$ccode_factor <- as.factor(repdata$ccode)
class(repdata$ccode_factor)
class(repdata$ccode)
model3_formula <- as.formula(
paste("gdp_g",
"~",
paste("GPCP_g","GPCP_g_l", "ccode_factor",
paste(country_time_trend, collapse = "+")
, sep="+")
)
)
ols_model3 <- lm_robust(model3_formula, data = repdata,
clusters = ccode, se_type = "stata")
summary(ols_model3)
tidy(ols_model3)
glance(ols_model3)
"Usando LM y luego coestest para los error standar robusto"
m3 <- lm(model3_formula, data = repdata)
lm_rmse3 <- round(RMSE(m3$fitted.values, repdata$gdp_g ) ,2 )
robust_model3 <- coeftest(m3 ,
vcov = vcovCL,
type = "HC1",
cluster = ~ ccode)
sd_robust_model3 <- robust_model3[,2]
coefci(m3, df = Inf,
vcov. = vcovCL, cluster = ~ ccode, type = "HC1")
model4_formula <- as.formula(
paste("gdp_g",
"~",
paste("GPCP_g","GPCP_g_l","GPCP_g_fl",
paste(country_time_trend, collapse = "+")
, sep="+")
)
)
ols_model4 <- lm_robust(model4_formula, data = repdata,
clusters = ccode, se_type = "stata", fixed_effects = ~ ccode)
summary(ols_model4)
tidy(ols_model4)
glance(ols_model4)
rmse4 <- RMSE(ols_model4$fitted.values, repdata$gdp_g )
"Usando LM y luego coestest para los error standar robusto"
model4_formula <- as.formula(
paste("gdp_g",
"~",
paste("GPCP_g","GPCP_g_l","GPCP_g_fl","ccode_factor",
paste(country_time_trend, collapse = "+")
, sep="+")
)
)
m4 <- lm(model4_formula, data = repdata)
lm_rmse4 <- round(RMSE(m4$fitted.values, repdata$gdp_g ) ,2 )
robust_model4 <- coeftest(m4,
vcov = vcovCL,
type = "HC1",
cluster = ~ ccode)
sd_robust_model4 <- robust_model4[,2]
coefci(m4, df = Inf,
vcov. = vcovCL, cluster = ~ ccode, type = "HC1")
model5_formula <- as.formula(
paste("gdp_g",
"~",
paste("GPCP_g","GPCP_g_l","tot_100_g",
paste(country_time_trend, collapse = "+")
, sep="+")
)
)
ols_model5 <- lm_robust(model5_formula, data = repdata,
clusters = ccode, se_type = "stata", fixed_effects = ~ ccode)
summary(ols_model5)
tidy(ols_model5)
glance(ols_model5)
rmse5 <- RMSE(ols_model5$fitted.values, repdata$gdp_g )
"Usando LM y luego coestest para los error standar robusto"
model5_formula <- as.formula(
paste("gdp_g",
"~",
paste("GPCP_g","GPCP_g_l","tot_100_g","ccode_factor",
paste(country_time_trend, collapse = "+")
, sep="+")
)
)
m5 <- lm(model5_formula, data = repdata)
robust_model5 <- coeftest(m5,
vcov = vcovCL,
type = "HC1",
cluster = ~ ccode)
sd_robust_model5 <- robust_model5[,2]
coefci(m5, df = Inf,
vcov. = vcovCL, cluster = ~ ccode, type = "HC1")
sq_resid <- m5$residuals**2
lm_rmse5 <- round( mean(sq_resid)^0.5, 2)
texreg(list(ols_model1, ols_model2, ols_model3, ols_model4, ols_model5),
custom.coef.map = list("GPCP_g"="Growth in rainfall, t",
"GPCP_g_l"="Growth in rainfall, t-1",
"GPCP_g_fl" = "Growth in rainfall,t+1",
"tot_100_g" = "Growth in terms of trade, t" ,
"y_0" = "Log(GDP per capita), 1979",
"polity2l" = "Democracy (Polity IV), t-1",
"ethfrac" = "Ethnolinguistic fractionalization",
"relfrac" = "Religious fractionalization",
"Oil" = "Oil-exporting country",
"lmtnest" = "Log(mountainous)",
"lpopl1" = "Log(national population), t-1"
), digits = 3,
stars = c(0.01, 0.05, 0.1),
custom.gof.rows = list("Country fixed effects" = c("no", "no", "yes", "yes", "yes"),
"Country-specific time trends" = c("no", "yes", "yes", "yes", "yes"),
"RMSE" = c(rmse1,rmse2,rmse3,rmse4,rmse5)),
caption = "Dependent Variable: Economic Growth Rate, t")
stargazer( m1, m2, m3, m4, m5,
se=list(sd_robust_model1, sd_robust_model2, sd_robust_model3,
sd_robust_model4, sd_robust_model5),
dep.var.labels = c(""),
title = "Rainfall and Economic Growth (First-Stage)",
keep = c("GPCP_g","GPCP_g_l","GPCP_g_fl","tot_100_g","y_0",
"polity2l","ethfrac","relfrac","Oil",
"lmtnest","lpopl1"),
covariate.labels=c("Growth in rainfall, t","Growth in rainfall, t-1",
"Growth in rainfall, t+1",
"Growth in terms of trade, t","Log(GDP per capita), 1979",
"Democracy (Polity IV), t-1","Ethnolinguistic fractionalization",
"Religious fractionalization","Oil-exporting country",
"Log(mountainous)","Log(national population), t-1"),
align = T, no.space = T,
add.lines=list(c("Country fixed effects", "no", "no","yes","yes","yes"),
c("Country-specific time trends","no", "yes","yes","yes","yes"),
c("Root mean square error",lm_rmse1,lm_rmse2,lm_rmse3,lm_rmse4,lm_rmse5)),
keep.stat = c("rsq","n"),
notes.append = FALSE, notes.align = "l",
notes ="Huber robust standard errors are in parentheses", style = "qje"
)
prob_formula <- as.formula(
paste("any_prio",
"~",
paste("gdp_g","gdp_g_l",
paste(control_vars, collapse = "+"),
"time_year"
, sep="+")
)
)
logit <- glm(prob_formula, data = repdata, family = binomial)
attributes(logit)
coeftest(logit, vcov. = vcovCL(logit, cluster = ~ccode, type = "HC"))
probit <- glm(prob_formula, data = repdata, family = binomial(link = "probit"))
attributes(probit)
coeftest(probit, vcov. = vcovCL(probit, cluster = ~ccode, type = "HC"))
logit <- logitmfx(prob_formula, data = repdata, atmean = TRUE, robust = T)
logit <- logitmfx(prob_formula, data = repdata, atmean = TRUE, robust = T,
clustervar1 = "ccode")
probit <- probitmfx(prob_formula, data = repdata, atmean = TRUE, robust = T)
probit <- probitmfx(prob_formula, data = repdata, atmean = TRUE, robust = T,
clustervar1 = "ccode")
summary(probit)
texreg(list(logit, probit))
htmlreg(list(logit, probit))
model2_formula <- as.formula(
paste("any_prio",
"~",
paste("gdp_g","gdp_g_l",
paste(control_vars, collapse = "+"),
"time_year"
, sep="+")
)
)
ols_model2 <- lm_robust(model2_formula, data = repdata,
clusters = ccode, se_type = "stata")
summary(ols_model2)
glance(ols_model2)
tidy(ols_model2)
rmse2 <- round( RMSE(ols_model2$fitted.values, repdata$any_prio ), 2)
model3_formula <- as.formula(
paste("any_prio",
"~",
paste("gdp_g","gdp_g_l",
paste(control_vars, collapse = "+"),
paste(country_time_trend, collapse = "+")
, sep="+")
)
)
ols_model3 <- lm_robust(model3_formula, data = repdata,
clusters = ccode, se_type = "stata")
summary(ols_model3)
glance(ols_model3)
tidy(ols_model3)
rmse3 <- round(RMSE(ols_model3$fitted.values, repdata$any_prio ), 2)
model4_formula <- as.formula(
paste("any_prio",
"~",
paste("gdp_g","gdp_g_l",
paste(country_time_trend, collapse = "+")
, sep="+")
)
)
ols_model4 <- lm_robust(model4_formula, data = repdata,
clusters = ccode, se_type = "stata", fixed_effects = ~ ccode)
summary(ols_model4)
glance(ols_model4)
tidy(ols_model4)
rmse4 <- round( RMSE(ols_model4$fitted.values, repdata$any_prio ), 2)
model5_formula <- as.formula(
paste(
paste("any_prio",
"~",
paste("gdp_g","gdp_g_l",
paste(country_time_trend, collapse = "+")
, paste(control_vars, collapse = "+")
, sep="+")
)
, paste("GPCP_g", "GPCP_g_l",
paste(country_time_trend, collapse = "+")
, paste(control_vars, collapse = "+")
, sep="+")
, sep=" | "
)
)
ols_model5 <- iv_robust(model5_formula, data = repdata,
clusters = ccode, se_type = "stata")
summary(ols_model5)
glance(ols_model5)
tidy(ols_model5)
rmse5 <- round(RMSE(ols_model5$fitted.values, repdata$any_prio ), 2)
model6_formula <- as.formula(
paste(
paste("any_prio",
"~",
paste("gdp_g","gdp_g_l",
paste(country_time_trend, collapse = "+")
, sep="+")
)
, paste("GPCP_g", "GPCP_g_l",
paste(country_time_trend, collapse = "+")
, sep="+")
, sep=" | "
)
)
ols_model6 <- iv_robust(model6_formula, data = repdata,
clusters = ccode, se_type = "stata",
fixed_effects = ~ ccode)
summary(ols_model6)
glance(ols_model6)
tidy(ols_model6)
rmse6 <- round(RMSE(ols_model6$fitted.values, repdata$any_prio ), 2)
model7_formula <- as.formula(
paste(
paste("war_prio",
"~",
paste("gdp_g","gdp_g_l",
paste(country_time_trend, collapse = "+")
, sep="+")
)
, paste("GPCP_g", "GPCP_g_l",
paste(country_time_trend, collapse = "+")
, sep="+")
, sep=" | "
)
)
ols_model7 <- iv_robust(model7_formula, data = repdata,
clusters = ccode, se_type = "stata",
fixed_effects = ~ ccode)
summary(ols_model7)
glance(ols_model7)
tidy(ols_model7)
rmse7 <- round(RMSE(ols_model7$fitted.values, repdata$war_prio ) ,2 )
texreg(list(logit, probit, ols_model2, ols_model3, ols_model4,
ols_model5, ols_model6, ols_model7),
custom.coef.map = list("gdp_g"="Economic growth rate, t",
"gdp_g_l"="Economic growth rate, t-1",
"y_0" = "Log(GDP per capita), 1979",
"polity2l" = "Democracy (Polity IV), t-1",
"ethfrac" = "Ethnolinguistic fractionalization",
"relfrac" = "Religious fractionalization",
"Oil" = "Oil-exporting country",
"lmtnest" = "Log(mountainous)",
"lpopl1" = "Log(national population), t-1"
), digits = 3,
stars = c(0.01, 0.05, 0.1),
custom.gof.rows = list("Country fixed effects" = c("no","no", "no","no", "yes", "no","yes", "yes"),
"Country-specific time trends" = c("no","no", "no","no", "yes", "yes","yes", "yes"),
"RMSE" = c("","",rmse2,rmse3,rmse4,rmse5,rmse6,rmse7)),
caption = "Economic Growth and Civil Conflict")
"https://glmnet.stanford.edu/articles/glmnet.html"
"chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/https://www.princeton.edu/~otorres/NiceOutputR.pdf"
"chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/https://cran.r-project.org/web/packages/stargazer/stargazer.pdf"
model3_formula <- as.formula(
paste("gdp_g",
"~",
paste("GPCP_g","GPCP_g_l",
paste(country_time_trend, collapse = "+")
, sep="+")," |ccode|0| ccode"
)
)
ols_model3 <- felm( model3_formula, data = repdata )
attributes(ols_model3)
rmse3 <- RMSE(ols_model3$fitted.values, repdata$gdp_g )
summary(ols_model3)
tidy(ols_model3)
glance(ols_model3)
ols_model3$rse
model5_formula <- as.formula(
paste(
paste("any_prio",
"~",
paste("gdp_g","gdp_g_l",
paste(country_time_trend, collapse = "+")
, paste(control_vars, collapse = "+")
, sep="+")
)
, "|0|(gdp_g|gdp_g_l ~ GPCP_g + GPCP_g_l)| ccode"
)
)
ols_model5_fle <- felm( model5_formula, data = repdata )
summary(ols_model5_fle)
glance(ols_model5_fle)
tidy(ols_model5_fle)
rmse5_fle <- RMSE(ols_model5_fle$fitted.values, repdata$any_prio )
model6_formula <- as.formula(
paste(
paste("any_prio",
"~",
paste("gdp_g","gdp_g_l",
paste(country_time_trend, collapse = "+")
, sep="+")
)
, "|ccode|(gdp_g|gdp_g_l ~ GPCP_g + GPCP_g_l)| ccode"
)
)
ols_model6_fle <- felm( model6_formula, data = repdata )
summary(ols_model6_fle)
glance(ols_model6_fle)
tidy(ols_model6_fle)
rmse6_fle <- round( RMSE(ols_model6_fle$fitted.values, repdata$any_prio ), 2)