Plotting Diplomatic Distance Between Nations Using Clustering Algorithms in R

Plotting Relations Between Objects Based on Their Interactions

In this post, we’ll explore how to plot the relations between objects based on their interactions using a large dyadic dataset. The goal is to create a plot showing the ‘diplomatic distance’ between nations, with countries having good relations close together and bad relations far apart.

Introduction

The problem at hand involves analyzing a large dataset of international interactions, where each observation represents an event involving two actors (countries). We have three variables that tell us what we need to know: Actor1, Actor2, and the nature of their action on a four-point scale. Our data is represented in R as follows:

# Replicate data
C <- c("AFG", "AFR", "AGO", "AIA", "ALB", "ARE", "ARG", "ARM", 
       "ASA", "ATG", "AUS", "AUT", "AZE", "BDI", "BEL", "BEN", "BFA", 
       "BGD", "BGR", "BHR", "BHS", "BLR", "BLZ", "BMU", "BOL", "BRA", 
       "BRB", "BRN", "BTN", "BWA", "CAF", "CAN", "CAS", "CHE", "CHL", 
       "CHN", "CIV", "CMR", "COD", "COG", "COK", "COL", "COM", "CPV", 
       "CRI", "CUB", "CYM", "CYP", "CZE", "DEU", "DJI", "DMA", "DNK", 
       "DOM", "DZA", "EAF", "ECU", "EGY", "ERI", "ESP", "EST", "ETH", 
       "EUR", "FIN", "FJI", "FRA", "FSM", "GAB", "GBR", "GEO", "GHA", 
       "GIN", "GMB", "GNB", "GNQ", "GRC", "GRS", "GUI", "GUY", "HKG", 
       "HND", "HRV", "HTI", "HUN", "IOC", "IRN", "IRQ", "IQA", "IRL", 
       "ISR", "ITA", "JAP", "JOR", "KWT", "KOR", "KSA", "KAZ", "KEN", 
       "KIR", "KNA", "KOS", "KRI", "KUW", "LAO", "LAT", "LBA", "LES", 
       "LIB", "LBN", "LSB", "LTU", "LVA", "MAR", "MCO", "MDG", "MDA", 
       "MDR", "MDV", "MGA", "MWI", "MYS", "MZI", "NAM", "NCA", "NER", 
       "NGA", "NIU", "NLD", "NOR", "OMA", "PAK", "PAN", "PLW", "PRY", 
       "PYF", "QAT", "REU", "SGS", "SOL", "SOM", "SPM", "SRB", "STL", 
       "SYC", "SYR", "TCA", "TDV", "TGO", "THA", "TJK", "TKM", "TMP", 
       "TON", "TOG", "TUV", "TWN", "UGA", "UKR", "URY", "USA", "UZB", 
       "VAE", "VCT", "VEN", "VNM", "VUT", "WSM", "WST", "YEM", "ZAF", 
       "ZMB", "ZWE")
ActionClasses <- c(1,2,3,4)

data <- data.frame(Actor1=sample(C, size = 1000, replace = TRUE), Actor2=sample(C, size = 1000, replace = TRUE), Action = sample(ActionClasses, size = 1000, replace = T))

Charting the Relations

We can start by charting the relations between actors using the Interactions function:

# Define the Interactions function
Interactions <- function(d) {
  # Filter the data for the current actor
  y <- Month[(Month$Actor1CountryCode == d),]

    # Loop over all other actors
    lapply(C, function(b) {
    z <- y[(y$Actor2CountryCode == b),]
    
     # Calculate the scores for each possible action class
     a <- sapply(1:4, function(Y) {
     sum(z$QuadClass == Y)})
     a <- rbind(a)
    row.names(a) <- paste(d,"v",b, sep = "")
    colnames(a) <- c("VerbCoop", "MatCoop", "VerbConf", "MatConf")
     q <- rbind(q,a)
  })
}

# Call the Interactions function
q <- data.frame()
for (i in C) {
    temp <- Interactions(i)
    q <- rbind(q, temp)
}

Plotting the Relations

Now that we have the relations charted, we can plot them using a clustering algorithm. We’ll use the dendrogram function to create a dendrogram from the similarity matrix.

# Create a distance matrix
distMat <- rowSums(q, na = 0)

# Create a dendrogram
dend <- agglomerate(distMat, method = "ward.D2")

# Plot the dendrogram
plot(dend$ind, dend$cluster, main = "Diplomatic Distance Clustering")

This will create a dendrogram that shows the clustering of actors based on their relations. We can then use this dendrogram to identify clusters and visualize them on a map.

Conclusion

Plotting the relations between objects based on their interactions involves several steps:

  1. Charting the relations: We define an Interactions function that calculates the scores for each possible action class.
  2. Plotting the relations: We create a distance matrix from the similarity matrix and plot it as a dendrogram to identify clusters.

By using clustering algorithms, we can visualize the relationships between objects and gain insights into their interactions.


Last modified on 2024-10-31