# Summary 

Today we are going to look at research on the the 'nexus' of politics, economics, and conflict. This data comes from the study "Identifying National Types: A Cluster Analysis of Politics, Economics, and Conflict" by authors Murray Wolfson, Zagros Madjd-Sadjadi and Patrick James in the Journal of Peace Research (Vol. 41, No. 5 (Sep., 2004), pp. 607-623). 

This data was downloded from the replication archive: https://www.prio.org/JPR/Datasets/#2004

First, let's load in the data & necessary libraries. 

```{r}
#libraries
library(stats)
library(tidyverse)
library(factoextra)
library(ggplot2)

polData<-read.csv("/Users/dorffc/ProjectsGit/instructor-copy-eda/week11/file45801_dna_final.csv")


```

Just by a quick summary or general check of the data, we can guess that it is already standardized. This is also true if you download the replication materials and read the author's note about the data. 

Let's only look at one year of data for now and summarize the data.

```{r}


```

You can also see that missing data has been removed for us already. You can see this by checking summary() but also by looking at the dimensions of the polData74 before and after na.omit().

```{r}


#polData74 = na.omit(polData74)
```

# Explore 

To start, let's go ahead and reduce the size of our data to investigate only a few features. Below we subset the data to XRREG, XROPEN, and TYPE 3. 

XRREG indicates executive recruitment regulation and XROPEN is executive recruitment openness, both measures of how leaders are chosen in the country. For example, a country with a lower XROPEN score might have leadership change through a 'closed' system like succession by birthright, where as a country with a high score would have a more 'open' system such as a competitive election. TYPE 3 denotes internal conflict --- the higher the number, the more intense the civil war.

Next we can explore the data using k-means. Let's just start with k = 2 to get a good look at the data. 

```{r}
set.seed(1234) 
clusters2 <- 
```

For easy comparison of the cluster assignments to the original data, let's just append the cluster assignments to the dataset and call this a new variable: "cluster2."

```{r}
# Save the cluster number in the dataset as column
polData74$clusters2 <- 
```

Now we have a dataframe where each case is labeled into a cluster. Let's explore this a bit more. One guess is that the clusters are picking up on whether a country has an internal conflict or not.

```{r}



```

The plot above doesn't support our hunch that the clusters are driven by internal conflict ("TYPE 3" on the Y axis, but XRREG seems useful).  Let's explore the same trends, but for XRREG & XROPEN. 

```{r}


```

We can see some nice sorting along the dimensions in the previous plot. Some countries score quite low on both XRREG and XROPEN, while others fall into the positive values. 

Another question to ask as an analyst is not only whether these clusters map onto an underlying feature of the data that we can probe, and try to understand, but whether the classfication is capturing much of the variance in the data. 
Recall: we want the clusters to do a good job of minimizing the distance between data points and the k-mean, while maximizing the distance between clusters (groups of data). To assess this, we want to look at the cluster2 object.

```{r}
print(clusters2)
```

Already we can see that the between sum of squares / total sum of squares ratio tell us about 57.2% of the variation in data is captured by the clusters. So we might want to try a higher number of clusters.

```{r}
clusters3 <-
clusters4 <-

polData74$clusters3 <- 
```

Let's explore similar plots from above, and keep trying to learn a bit from the clusters themselves. 

```{r}



```

Here we can see that the blue and green clusters are nicely sorted, but the red cluster is largely mixed in with the blue for the dimensions we've chosen here. Since we can only choose 2 variables to map onto the x and y axis, let's explore the additional feature in our data: internal conflict. We will also play with different labeling approaches below.

Below, let's use the 1974 data to plot internal conflict on the x axis, and executive recruitment openness (XROPEN) on the Y-axis. 

```{r}
require("ggrepel")

```

# Practice

*PRACTICE 1 *

The plot above is super bonkers! Can you improve those labels? 

```{r}
# HINT: 

data = data.frame(
  cntries=paste0(letters,letters),
  x = rnorm(length(paste0(letters,letters))),
  y = rnorm(length(paste0(letters,letters))),
  stringsAsFactors=FALSE
)

keep = paste0(letters,letters)[sample(1:nrow(data), 10)]

data$cntries[which(!data$cntries %in% keep)] = ""

ggplot(data, 
       aes(x=x, y=y)) + 
  geom_text(aes(label=cntries))
```


```{r}
# practice



```


Now we can see that XROPEN and internal conflict ("TYPE3") might be doing a good bit of the work in driving the clusters. 

In class last time, we showed similar plots, but we used a built in function to conduct a PCA for when features are high and the analysts has little knowledge of which variables might best help to interpret trends. Let's explore our k=3 and k=4 cluster analysis below, using the `fviz_cluster` function.

```{r}

allClustersplot1 <- fviz_cluster()

allClustersplot2 <- fviz_cluster()

```


*Practice 2*

Wow, 1974 is a bit tough to interpret probably because most of us don't have working knowledge of what was going on across the globe at this time. Now, try out your skills: using `k = 3`, generate k-means clusters and jitter plots for years 1981, 1988, and 1995. Then collect all the plots together in a grid to show the movement of the clusters over time. Make sure to limit your labels to something more readible! 


```{r}

```

*Practice*

So far we have mainly explored the "nexus" of politics and conflict, can you also add economic conditions into the mix? 

