Setup

First, load the metapaths package, as well as other requisite packages.

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
# load packages
library(metapaths)
library(data.table)
library(magrittr)

# for comparison and visualization
library(igraph)

Print the example dataset.

mtcars_node_list = get_node_list(mtcars_edge_list)

head(mtcars_node_list)
head(mtcars_edge_list)

Visualize the network as a graph.

mtcars_graph = graph.data.frame(mtcars_edge_list, vertices = mtcars_node_list, directed = T)
mtcars_col = factor(V(mtcars_graph)$NodeType)
plot(mtcars_graph, vertex.size = 15, vertex.label = NA, edge.arrow.size = .25, vertex.color = mtcars_col)

Get all node types.

mtcars_types = node_types(mtcars_edge_list, verbose = T)

Retrieve a list of unique nodes.

mtcars_nodes = all_nodes(mtcars_edge_list, verbose = T)

Edge List

For a given node, find all neighbors of a specified type.

get_neighbors("Cadillac Fleetwood", "Three", mtcars_edge_list) %>%
  cat(sep = "\n")

For a given node, find all neighbors of series of types.

get_neighbors_type("Cadillac Fleetwood", c("Three", "Four"), mtcars_edge_list)

Construct the neighbor list.

mtcars_neighbor_list = get_neighbor_list(mtcars_edge_list, verbose = T)
head(mtcars_neighbor_list)

Neighbor List

Identify the neighbors using the constructed neighbor list rather than the edge list.

search_neighbors("Cadillac Fleetwood", "Three", mtcars_neighbor_list)%>%
  cat(sep = "\n")

Compute degree stratified by node type. Here, degree is defined as the number of adjacent nodes (rather than edges).

search_degrees("Cadillac Fleetwood", "Three", mtcars_neighbor_list, "neighbor") %>%
  paste("Degree:", .) %>%
  cat(sep = "\n")

Meta-Path Based Similarity

Compute similarity between two nodes using a sample meta-path: Three-Four-Five.

mtcars_sim = get_similarity("Camaro Z28", "Ferrari Dino",
                            c("Three", "Four", "Five"),
                            c("pc", "npc", "dwpc"),
                            node_list = mtcars_node_list,
                            neighbor_list = mtcars_neighbor_list)

Let's inspect the output of the get_similarity() primitive. get_similarity() returns a named list. OriginPaths contains the paths from the origin node (i.e., Camaro Z28) to the destination node (i.e., Ferrari Dino) following the specified meta-path.

mtcars_sim$OriginPaths

Similarly, DestinationPaths contains the paths from the destination node to the origin node following the reverse of the specified meta-path.

mtcars_sim$DestinationPaths

These tables are used to compute various meta-path based similarity metrics.

head(mtcars_sim$Similarity)

Indeed, there are two paths which connect Camaro Z28 and Ferrari Dino via the meta-path Three-Four-Five.

mtcars_sim$OriginPaths[Five == "Ferrari Dino"]

Those paths are shown on the graph below.

keep = V(mtcars_graph)$name %in% c("Camaro Z28", "Merc 240D", "Merc 280", "Ferrari Dino")
mtcars_col_rem = mtcars_col
mtcars_col_rem[!keep] = NA
plot(mtcars_graph, vertex.size = 15, vertex.label = NA, edge.arrow.size = 0.25, vertex.color = mtcars_col_rem)


ayushnoori/metapaths documentation built on March 19, 2023, 7:35 p.m.