library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
alliances <- read.csv("alliance_v4.1_by_member_yearly.csv")
midb <- read.csv("MIDB 5.0.csv")
alliances$Alliance_Type <- "Type III"
alliances$Alliance_Type[alliances$neutrality == 1 | alliances$nonaggression == 1] <- "Type II"
alliances$Alliance_Type[alliances$defense == 1] <- "Type I"
alliances_collapsed <- alliances %>% group_by(ccode, year) %>%
summarize(
Main_Alliance = min(Alliance_Type, na.rm = TRUE)
)
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by ccode and year.
## ℹ Output is grouped by ccode.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(ccode, year))` for per-operation grouping
## (`?dplyr::dplyr_by`) instead.
head(alliances_collapsed)
## # A tibble: 6 × 3
## # Groups: ccode [1]
## ccode year Main_Alliance
## <int> <int> <chr>
## 1 2 1908 Type III
## 2 2 1909 Type III
## 3 2 1910 Type III
## 4 2 1921 Type III
## 5 2 1922 Type III
## 6 2 1923 Type III
table(alliances_collapsed$Main_Alliance)
##
## Type I Type II Type III
## 7507 1298 542
midb$is_targeted <- 0
midb$is_targeted[midb$sidea == 0 & midb$orig == 1] <- 1
table(midb$is_targeted)
##
## 0 1
## 3290 2593
midb_clean <- midb %>%
group_by(ccode, styear) %>%
summarize(
is_targeted = max(is_targeted, na.rm=TRUE)
)
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by ccode and styear.
## ℹ Output is grouped by ccode.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(ccode, styear))` for per-operation grouping
## (`?dplyr::dplyr_by`) instead.
head(midb_clean)
## # A tibble: 6 × 3
## # Groups: ccode [1]
## ccode styear is_targeted
## <int> <int> <dbl>
## 1 2 1816 0
## 2 2 1818 0
## 3 2 1834 1
## 4 2 1835 1
## 5 2 1836 1
## 6 2 1837 1
names(alliances_collapsed)
## [1] "ccode" "year" "Main_Alliance"
names(midb_clean)
## [1] "ccode" "styear" "is_targeted"
midb_clean <- midb_clean %>%
rename(year = styear)
merged_data <- left_join(alliances_collapsed, midb_clean, by = c("ccode", "year"))
merged_data$is_targeted[is.na(merged_data$is_targeted)] <- 0
merged_data$Status <- ifelse(merged_data$is_targeted == 1, "Conflict", "Peace")
head(merged_data)
## # A tibble: 6 × 5
## # Groups: ccode [1]
## ccode year Main_Alliance is_targeted Status
## <int> <int> <chr> <dbl> <chr>
## 1 2 1908 Type III 0 Peace
## 2 2 1909 Type III 0 Peace
## 3 2 1910 Type III 0 Peace
## 4 2 1921 Type III 0 Peace
## 5 2 1922 Type III 0 Peace
## 6 2 1923 Type III 0 Peace
merged_data$Era <- "All"
merged_data$Era[merged_data$year >= 1816 & merged_data$year <= 1913] <- "Pre-WW1"
merged_data$Era[merged_data$year >= 1914 & merged_data$year <= 1945] <- "WW1/Interwar Years/WW2"
merged_data$Era[merged_data$year >= 1946 & merged_data$year <= 1991] <- "Cold War"
merged_data$Era[merged_data$year >= 1992 & merged_data$year <= 2012] <- "Post-Cold War"
summary_table <- merged_data %>%
group_by(Main_Alliance, Status) %>%
summarise(Count = n()) %>%
mutate(Percentage = Count / sum(Count) * 100)
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by Main_Alliance and Status.
## ℹ Output is grouped by Main_Alliance.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(Main_Alliance, Status))` for per-operation grouping
## (`?dplyr::dplyr_by`) instead.
print(summary_table)
## # A tibble: 6 × 4
## # Groups: Main_Alliance [3]
## Main_Alliance Status Count Percentage
## <chr> <chr> <int> <dbl>
## 1 Type I Conflict 1027 13.7
## 2 Type I Peace 6480 86.3
## 3 Type II Conflict 240 18.5
## 4 Type II Peace 1058 81.5
## 5 Type III Conflict 111 20.5
## 6 Type III Peace 431 79.5