---
title: "Assignment 10: A New Era?"
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

Prepare a four-slide presentation on the contemporary era, specifically after 1992. Prepare three slides on human development (infant mortality), political liberalization (Polity scores), and social order (fragility).

- Slides 1 and 2: Plot infant mortality and Polity scores over time for your country and the Sub-Saharan and Bandung comparison groups. Include data from the year of independence to the latest year information is available for your country. Draw a vertical line at 1992 indicating the 'new era.'

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

- Slide 4: Answer the following question: has your country moved “toward a new era”? To answer this question, assess if there has been a dramatic shift in values after 1992.


***


# 1. Preparation

If you completed the R tutorial, the `tidyverse` package should already be installed on your computer. If not, run this code chunk. 

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

Now 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/10-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. 

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

Here are the first five observations for your country:

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


***


# 2. Evolution in Human Development

Now we can use `ggplot2` to plot infant mortality from 1960 to 2014 in your country and the comparison groups. Do you notice a shift in infant mortality across Sub-Saharan Africa after 1992? What about the infant mortality rate in your country?

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

You can save this plot using the `ggsave` function.

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

In R Studio, you can also save this 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 2014 for your country and the comparison groups. Do you notice a shift in regime type across Sub-Saharan Africa after 1992? What about regime type in your country?

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

You can save this plot using the `ggsave` function.

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

In R Studio, you can also save this 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 for each period: 1960 to 1992 and 1992 to 2014. Do you notice a change in the probability of a coup after 1992 in Sub-Saharan Africa as a whole? What about the probability of a coup in your country?

As in problem set two, the code below uses the `coups` variable, which counts both successful and failed coups. 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?

```{r, warning=FALSE}
africa_df %>% 
  mutate(
    country_set = case_when(
      iso3c == my_iso_code ~ my_country,
      SSA == 1 & iso3c != my_country ~ "Rest of SSA",
      BNDNG == 1 ~ "Bandung"),
    Era = case_when(
      year %in% 1960:1992 ~ "1960 to 1992",
      year %in% 1992:2014 ~ "1992 to 2014"
    )
  ) %>%
  filter(!is.na(country_set)) %>%
  group_by(country_set, Era, country) %>%
  summarise(p_coup = mean(coups > 0) * 100) %>%
  group_by(country_set, Era) %>%
  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)
  )
```

# Works Cited

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

