knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

This vignette introduces you to some functions of the package with the [data integrated][Incorporated data] in the package. Here, we are interested to the exploration of dynamic networks.

Building your list of networks

library(networkflow)
library(magrittr)
library(dplyr)
library(tidygraph)
nodes <- Nodes_stagflation %>%  
  dplyr::rename(ID_Art = ItemID_Ref) %>% 
  dplyr::filter(Type == "Stagflation")

references <- Ref_stagflation %>%  
  dplyr::rename(ID_Art = Citing_ItemID_Ref)
single_network <- dynamic_network_cooccurrence(nodes = nodes,
                                               directed_edges = references,
                                               source_column = "ID_Art",
                                               target_column = "ItemID_Ref",
                                               time_variable = NULL,
                                               cooccurrence_method = "coupling_similarity",
                                               time_window = NULL,
                                               edges_threshold = 1,
                                               compute_size = FALSE,
                                               keep_singleton = FALSE,
                                               overlapping_window = TRUE)
network_list <- dynamic_network_cooccurrence(nodes = nodes,
                                             directed_edges = references,
                                             source_column = "ID_Art",
                                             target_column = "ItemID_Ref",
                                             time_variable = "Year",
                                             cooccurrence_method = "coupling_similarity",
                                             time_window = 15,
                                             edges_threshold = 1,
                                             compute_size = FALSE,
                                             keep_singleton = FALSE,
                                             overlapping_window = TRUE)

network_list[[1]]

Clustering and intertemporal naming

network_list <- lapply(network_list, 
                       function(tbl) tbl %N>% mutate(clusters = group_louvain()))
network_list <- intertemporal_cluster_naming(list_graph = network_list,
                                             cluster_column = "clusters",
                                             node_key = "ID_Art",
                                             threshold_similarity = 0.5001,
                                             similarity_type = "partial")

network_list[[1]]

Building Alluvial

library(ggplot2)
library(ggalluvial)

alluv_dt <- networks_to_alluv(list_graph = network_list,
                            intertemporal_cluster_column = "intertemporal_name",
                            node_key = "ID_Art",
                            summary_cl_stats = FALSE)

alluv_dt <- minimize_crossing_alluvial(alluv_dt = alluv_dt,
                                     node_key = "ID_Art")
alluv_dt[,y_alluv:=1/.N, Window]

ggplot(alluv_dt, aes(x = Window, y= y_alluv, stratum = intertemporal_name, alluvium = ID_Art, fill = intertemporal_name, label = intertemporal_name)) +
  geom_stratum(alpha =1, size=1/12) +
  geom_flow() +
  theme(legend.position = "none") +
  theme_minimal() +
  theme(plot.background = element_rect(fill = 'white', colour = NA)) +
  ggtitle("")

exploring tf-idf

corpus <- merge(alluv_dt,
                nodes,
                by = "ID_Art",
                all.x = TRUE)

tf_idf <- extract_tfidf(data = corpus,
                        text_columns = "Title",
                        grouping_columns = "intertemporal_name",
                        n_gram = 3L,
                        stopwords = NULL,
                        stopwords_type = "smart",
                        clean_word_method = "lemmatize",
                        ngrams_filter = 2)

tf_idf %>% 
  group_by(document) %>% 
  slice_max(order_by = tf_idf, n = 1, with_ties = FALSE) %>% 
  ungroup() %>% 
  arrange(intertemporal_name) %>% 
  select(-document)


agoutsmedt/networkflow documentation built on March 15, 2023, 11:51 p.m.