Last active
November 9, 2022 14:08
-
-
Save resulumit/16d0997f85695a2f6705d483f292a359 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(fixest) | |
library(marginaleffects) | |
library(modelsummary) | |
ui <- fluidPage( | |
titlePanel("Explore the relationship between development and democracy"), | |
sidebarLayout( | |
sidebarPanel( | |
sliderInput( | |
inputId = "year", | |
label = "Period", | |
value = c(1950, 1990), | |
min = 1800, | |
max = 2016, | |
sep = "", | |
step = 1, | |
ticks = FALSE, | |
), | |
selectInput( | |
inputId = "region", | |
label = "Region", | |
choices = c( | |
"All regions" = 0, | |
"Asia and Pacific" = 6, | |
"Eastern Europe and Central Asia" = 1, | |
"Middle East and North Africa" = 3, | |
"Latin America and the Caribbean" = 2, | |
"Sub-Saharan Africa" = 4, | |
"Western Europe and North America" = 5 | |
), | |
selected = "All regions", | |
multiple = FALSE | |
), | |
selectInput( | |
inputId = "dv", | |
label = "Democracy Index", | |
choices = c( | |
"Polity combined score" = "e_polity2", | |
"Egalitarian democracy index" = "v2x_egaldem", | |
"Electoral democracy index" = "v2x_polyarchy", | |
"Deliberative democracy index" = "v2x_delibdem", | |
"Liberal democracy index" = "v2x_libdem", | |
"Participatory democracy index" = "v2x_partipdem" | |
), | |
selected = "Polity combined score", | |
multiple = FALSE | |
), | |
selectInput( | |
inputId = "iv", | |
label = "Control Variable(s)", | |
choices = c( | |
"Education" = "e_peaveduc", | |
"Educational inequality" = "e_peedgini", | |
"Exports" = "e_cow_exports", | |
"Equal distribution of resources" = "v2xeg_eqdr", | |
"Democratic diffusion" = "e_polity2_scale_geo", | |
"Fertility rate" = "e_miferrat", | |
"Fossil fuel income per capita" = "e_total_fuel_income_pc", | |
"Land area" = "e_area", | |
"Land inequality" = "land_inequality", | |
"Life expectancy" = "e_pelifeex", | |
"Natural resource dependence" = "e_resdep2", | |
"Imports" = "e_cow_imports", | |
"Infant mortality rate" = "e_peinfmor", | |
"Inflation" = "e_miinflat", | |
"Political corruption" = "v2x_corr", | |
"Population" = "e_pop", | |
"Urbanisation rate" = "e_miurbani" | |
), | |
multiple = TRUE | |
) | |
), | |
mainPanel(tabsetPanel( | |
type = "tabs", | |
tabPanel("Adjusted Predictions", plotOutput("plot")), | |
tabPanel("Model Summary", htmlOutput("table")), | |
tabPanel("Data & Variables", htmlOutput('explanations')) | |
)) | |
) | |
) | |
# SERVER | |
server <- function(input, output) { | |
model <- reactive({ | |
df <- if (input$region == 0) { | |
read_rds("dev_dem_data.rds") %>% | |
filter(year >= input$year[1] & | |
year <= input$year[2]) %>% | |
mutate(e_polity2 = if_else(e_polity2 < -10, NA_real_, e_polity2)) | |
} else { | |
read_rds("dev_dem_data.rds") %>% | |
filter( | |
year >= input$year[1] & year <= input$year[2] & | |
e_regionpol_6C == input$region | |
) %>% | |
mutate(e_polity2 = if_else(e_polity2 < -10, NA_real_, e_polity2)) | |
} | |
if (length(input$iv) == 0) { | |
current_formula <- paste0(input$dv, | |
" ~ e_migdppcln_ipo*e_mibmr ") | |
} else if (length(input$iv) == 1) { | |
current_formula <- | |
paste0(input$dv, | |
" ~ e_migdppcln_ipo*e_mibmr + ", | |
input$iv) | |
} else { | |
current_formula <- | |
paste0( | |
input$dv, | |
" ~ e_migdppcln_ipo*e_mibmr + ", | |
paste(input$iv, collapse = " + ") | |
) | |
} | |
current_formula <- as.formula(current_formula) | |
tryCatch({ | |
feols( | |
current_formula, | |
cluster = df$country_id, | |
data = df | |
) | |
} | |
, error = function(e) { | |
"" | |
}) | |
}) | |
output$plot <- renderPlot({ | |
validate( | |
need( | |
model() != "", | |
"This model cannot be calculated. One or more variables might be unavailable for the time period and/or region. Please try a different specification." | |
) | |
) | |
plot_cap(model(), | |
condition = c("e_migdppcln_ipo", "e_mibmr")) + | |
labs(x = "\nGDP per capita (log)", | |
y = "Democracy index\n", | |
title = "\nAdjusted predictions, conditional on regime type") + | |
theme( | |
plot.title = element_text( | |
size = 16, | |
hjust = 0, | |
face = "bold", | |
margin = margin(0, 0, 30, 0) | |
), | |
axis.title = element_text(size = 16), | |
legend.position = "bottom", | |
legend.text = element_text(size = 16) | |
) + | |
scale_color_discrete( | |
name = "\n", | |
breaks = c("1", "0"), | |
labels = c("Democracies", "Autocracies") | |
) + | |
scale_fill_discrete( | |
name = "\n", | |
breaks = c("1", "0"), | |
labels = c("Democracies", "Autocracies") | |
) | |
}) | |
# model summary | |
output$table <- renderPrint({ | |
validate( | |
need( | |
model() != "", | |
"This model cannot be calculated. One or more variables might be unavailable for the time period and/or region. Please try a different specification." | |
) | |
) | |
modelsummary( | |
model(), | |
output = "html", | |
estimate = "{estimate}{stars}", | |
coef_rename = c( | |
"e_migdppcln_ipo" = "GDP per capita (log)", | |
"e_mibmr" = "Autocracy/Democracy", | |
"e_peaveduc" = "Education", | |
"e_peedgini" = "Educational inequality", | |
"e_cow_exports" = "Exports", | |
"v2xeg_eqdr" = "Equal distribution of resources", | |
"e_polity2_scale_geo" = "Democratic diffusion", | |
"e_miferrat" = "Fertility rate", | |
"e_total_fuel_income_pc" = "Fossil fuel income per capita", | |
"e_area" = "Land area", | |
"land_inequality" = "Land inequality", | |
"e_pelifeex" = "Life expectancy", | |
"e_resdep2" = "Natural resource dependence", | |
"e_cow_imports" = "Imports", | |
"e_peinfmor" = "Infant mortality rate", | |
"e_miinflat" = "Inflation", | |
"v2x_corr" = "Political corruption", | |
"e_pop" = "Population", | |
"e_miurbani" = "Urbanisation rate" | |
), | |
gof_map = c("nobs", "r.squared"), | |
stars = TRUE, | |
shape = term ~ statistic, | |
notes = list( | |
"Standard errors are clustered at the country level.", | |
"Notes: The results are estimated with ordinary least square regressions.", | |
"+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001" | |
) | |
) | |
}) | |
# data & variables | |
output$explanations <- renderPrint({ | |
HTML( | |
'<br> | |
<p>The data in this app comes from two sources: Varieties of Democracy Project (<a href="https://www.v-dem.net/documents/1/codebookv12.pdf" target="_blank" rel="noopener noreferrer">V-Dem, version 12</a>) and | |
an article published by Knutsen et al. (<a href="https://ejpr.onlinelibrary.wiley.com/action/downloadSupplement?doi=10.1111%2F1475-6765.12282&file=ejpr12282-sup-0001-Appendix.pdf" target="_blank" rel="noopener noreferrer">2019</a>)</p>. | |
<br> | |
<div> | |
<style type="text/css"> | |
.tg {border-collapse:collapse;border-spacing:0;} | |
.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; | |
overflow:hidden;padding:10px 5px;word-break:normal;} | |
.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; | |
font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;} | |
.tg .tg-fymr{border-color:inherit;font-weight:bold;text-align:left;vertical-align:top} | |
.tg .tg-za14{border-color:inherit;text-align:left;vertical-align:bottom} | |
.tg .tg-7zrl{text-align:left;vertical-align:bottom} | |
</style> | |
<table class="tg"> | |
<thead> | |
<tr> | |
<th class="tg-fymr">Variable label</th> | |
<th class="tg-fymr">Data source</th> | |
<th class="tg-fymr">Variable name in source</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Autocracy/Democracy</span></td> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Knutsen et al. (2019)</span></td> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_mibmr</span></td> | |
</tr> | |
<tr> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Deliberative democracy index</span></td> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">v2x_delibdem</span></td> | |
</tr> | |
<tr> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Democratic diffusion</span></td> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Knutsen et al. (2019)</span></td> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_polity2_scale_geo</span></td> | |
</tr> | |
<tr> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Education</span></td> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_peaveduc</span></td> | |
</tr> | |
<tr> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Educational inequality</span></td> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-za14"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_peedgini</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Egalitarian democracy index</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">v2x_egaldem</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Electoral democracy index</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">v2x_polyarchy</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Equal distribution of resources</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">v2xeg_eqdr</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Exports</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_cow_exports</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Fertility rate</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_miferrat</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Fossil fuel income per capita</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_total_fuel_income_pc</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">GDP per capita (log)</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Knutsen et al. (2019)</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_migdppcln_ipo</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Imports</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_cow_imports</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Infant mortality rate</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_peinfmor</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Inflation</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_miinflat</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Land area</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_area</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Land inequality</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Knutsen et al. (2019)</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">land_inequality</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Liberal democracy index</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">v2x_libdem</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Life expectancy</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_pelifeex</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Natural resource dependence</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Knutsen et al. (2019)</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_resdep2</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Participatory democracy index</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">v2x_partipdem</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Political corruption</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">v2x_corr</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Polity combined score</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_polity2</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Population</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_pop</span></td> | |
</tr> | |
<tr> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">Urbanisation rate</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">V-Dem</span></td> | |
<td class="tg-7zrl"><span style="font-weight:400;font-style:normal;text-decoration:none;color:black">e_miurbani</span></td> | |
</tr> | |
</tbody> | |
</table> | |
</div>' | |
) | |
}) | |
} | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment