---
title: "Assignment 2: Unfulfilled Dreams"
author: ""
date: ""
output:
  tufte::tufte_handout: 
    highlight: tango 
    df_print: kable
link-citations: yes
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
library(tufte)
# invalidate cache when the tufte version changes
knitr::opts_chunk$set(tidy = FALSE, cache.extra = packageVersion('tufte'))
options(htmltools.dir.version = FALSE, digits = 2)
```

\noindent Created: `r Sys.Date()`

\noindent Due Date: 

# Instructions

Assess the progress of sub-Saharan African (SSA) countries between independence and 1992. Compare the experience of SSA countries to those countries participating in the solidarity conference in Bandung, Indonesia &mdash; a set of post-colonial states that also achieved independence after WWII. Prepare a four-slide presentation to characterize their progress in terms of economic development, political liberalism, and social order.

- Slide 1: Characterize the evolution of human development, using infant mortality as your indicator.

- Slide 2: Characterize the evolution of political liberalism, using the Polity score as your indicator.

- Slide 3: Characterize the evolution of social order, using coups as your indicator.

- Slide 4: Interpret your results as the degree to which the dreams of Africa’s early independent leaders were fulfilled in the first generation of independence, how Africa compares to the countries from the Bandung set, and how your country compares to the average country in sub-Saharan Africa.



***


# 1. Preparation

To install the `tidyverse` package on your computer, run this code chunk. 

```{r eval = FALSE}
install.packages("tidyverse")
```

Now we can load the `tidyverse` package.

```{r message=FALSE, warning=FALSE}
library(tidyverse)
```

Finally, load the data set from the URL provided. 

```{r message = FALSE}
africa_df <- read_csv('https://african-states-book.info/img/portfolio/problem-sets/data/02-data.csv')
```

To start, you'll need to assign the name of your country to the variable `my_country`. You'll also need to assign the three-character ISO country code to the variable `my_iso_code`. By assigning the country name and country code to variables, you'll only have to type them once. 

```{r}
my_country <- "Kenya"
my_iso_code <- "KEN"

ssa_countries <- africa_df %>% 
  filter(SSA == 1) %>% 
  select(country, iso3c) %>% 
  unique()
```

The variable `my_country` will be used to label graphs, so it can be formatted however you prefer. For instance, if you're researching the Democratic Republic of the Congo, you may assign "Democratic Republic of the Congo", "Congo, Democratic Republic", "DRC", or any other appropriate name to the `my_country` variable. On the other hand, the code you assign to the `my_iso_code` variable must precisely match your country's three-character ISO code. You can look up your country's ISO code online, or you can run `view(ssa_countries)` in the RStudio console. 

We'll do some data manipulation with `dplyr` to get the data set ready to plot. In a few lines of code, we can `filter` the observations for your country and the Sub-Saharan and Bandung comparison groups. We can also remove observations before 1960 and after 1992. 

```{r}
df <- africa_df %>%
  filter(
    iso3c %in% c("SSA", "BND", my_iso_code), 
    year %in% 1960:1992
  )
```

Here are the first five observations for your country:

```{r}
df %>% filter(iso3c == my_iso_code) %>% head()
```

***


# 2. Evolution in Human Development

Now we use `ggplot2` to plot infant mortality from 1960 to 1992 in your country and the comparison groups. 

```{r fig.fullwidth = TRUE}
ggplot(df, aes(x = year, y = inftmort, color = country, linetype = country)) +
  geom_line() +
  labs(
    x = "",
    y = "Infant Mortality \n per 100,000 Live Births",
    title = str_c("Human Development in ", my_country),
    color = "", linetype = "") +
  theme_classic(7)
```

You can save this plot as an image or PDF using the `ggsave` function.

```{r, eval=FALSE}
ggsave('figure-02-1.png')
```

In R Studio, you can also save the plot by clicking the "Export" button at the top of the plots window. 


*** 


# 3. Evolution in Political Liberalism

Polity scores represent a coding of regime type, ranging from most autocratic (-10) to most democratic (10). Countries cannot be assigned a Polity score prior to independence or during state failure, so data is not available in every country for every year. Here, we plot Polity IV scores from 1960 to 1992 for your country and the comparison groups.

```{r fig.fullwidth = TRUE}
ggplot(df, aes(x = year, y = polity, color = country, linetype = country)) +
  annotate("rect", xmin=1958, xmax=1994, 
    ymin=5, ymax=10, fill = "grey80") +
  annotate("rect", xmin=1958, xmax=1994, 
    ymin=-10, ymax=-5, fill = "grey80") +
  geom_line() +
  labs(
    x = "",
    y = "Polity IV Score",
    title = str_c("Democracy in ", my_country),
    color = "", linetype = "") +
  theme_classic(7)
```

You can save this plot as an image or PDF using the `ggsave` function.

```{r, eval=FALSE}
ggsave('figure-02-2.png')
```

In R Studio, you can also save the plot by clicking the "Export" button at the top of the plots window. 



*** 


# 4. Evolution of Social Order

We can use `dplyr` to quickly summarize data on coups, adapted from Powell and Thyne (2011). In particular, we'll calculate the average probability that one or more coups occurs in a given year between 1960 and 1992. To prepare the data for analysis, we first use `mutate()` to create a new variable called `country_set`, then we select only coups that took place from 1960 to 1992. 

```{r, warning=FALSE}
africa_df <- africa_df %>% 
  mutate(
    country_set = case_when(
      iso3c == my_iso_code ~ my_country,
      SSA == 1 & iso3c != my_country ~ "Rest of SSA",
      BNDNG == 1 ~ "Bandung")
  ) %>%
  filter(
    year %in% 1960:1992,
    !is.na(country_set)
  )
```

Next, we can compute the probability of a coup occuring in a given year for each country in our data set using `summarise()`.

```{r, warning=FALSE}
country_data <- africa_df %>%
  group_by(country_set, country) %>%
  summarise(p_coup = mean(coups > 0) * 100)
```

Finally, we aggregate the data to compare your country to the regional averages.

```{r, warning=FALSE}
country_data %>%
  group_by(country_set) %>%
  summarize(
    "No. Countries" = n(),
    "No. with Coups" = sum(p_coup > 0),
    "% with Coups" = sum(p_coup > 0) / n() * 100,
    "Average Prob. Coup" = mean(p_coup)
  )
```

How do the descriptive statistics for your country compare to the average for Sub-Saharan Africa? For Bandung?

In the code above, we used the `coups` variable, which counts both successful and failed coups. As an extra challenge, try implementing the same code for `successful_coups` and `failed_coups`. Do these alternate specifications give the same impression of social order in your country and the comparison groups?


# Works Cited

Powell, Jonathan and Clayton Thyne. 2011. "Global Instances of Coups from 1950-Present." *Journal of Peace Research* 48 (2): 249-259.
