Skip to content

Instantly share code, notes, and snippets.

@alexpghayes
Created April 23, 2023 17:17
Show Gist options
  • Save alexpghayes/ae28d0a1ebb29588871e02d340fd430e to your computer and use it in GitHub Desktop.
Save alexpghayes/ae28d0a1ebb29588871e02d340fd430e to your computer and use it in GitHub Desktop.
library(tidyverse)
library(tidygraph)
library(netmediate)
data <- read_csv("final_regime.IGO.sanction.csv") |>
# remove some columns of missing data to avoid downstream issues
select(-1, -AMCOW, -MOPAN)
node_data <- data |>
select(country, ccode, polity2, democracy, sanction)
incidence <- data |>
select(-country, -ccode, -polity2, -democracy, -sanction) |>
as.matrix()
rownames(incidence) <- data$country
# setting up the data
graph <- incidence |>
igraph::graph_from_incidence_matrix() |>
as_tbl_graph() |>
activate(nodes) |>
left_join(node_data, by = c("name" = "country")) |>
activate(edges) |>
mutate(
weight = 1
)
# getting and plotting estimates
# unclear to me if trt should be as.factor(polity2) or polity2
# also unclear to me how sanction should be coded
curve <- graph |>
activate(nodes) |>
sensitivity_curve(
sanction ~ as.factor(polity2),
max_rank = 10,
ranks_to_consider = 5,
coembedding = "U"
)
plot(curve) +
theme_classic()
# checking the quality of the embeddings
library(vsp)
fa <- incidence |>
as("CsparseMatrix") |>
vsp(rank = 10)
plot_ipr_pairs(fa)
fa |>
get_varimax_y() |>
dplyr::select(-id) |>
GGally::ggpairs(ggplot2::aes(alpha = 0.001)) +
ggplot2::theme_minimal()
fa |>
get_varimax_z() |>
dplyr::select(-id) |>
GGally::ggpairs(ggplot2::aes(alpha = 0.001)) +
ggplot2::theme_minimal()
# embeddings seem possibly okay but certainly not great
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment