R Tutorial: Exploring Your Data Set

Learn network analysis on a 2003 Amazon co-purchase dataset. The tutorial covers filtering data with dplyr, building an igraph object, and analyzing dyad and triad censuses to understand reciprocity and transitivity in the network.
Chapters
Hi and welcome to this case study in network analysis. In this course, we'll be putting into practice all the concepts you learned in your Intro to Network Analysis using Real World Data sets In this first lesson, we'll be using a data set that has several daily snapshots of items purchased together from Amazon over the course of 2003. These are known as CO purchases. The first step when you're working with a new data set is to explore the raw data. While we'll go on to create an igraph object out of this raw data, it's useful to understand the data associated with each vertex. The columns that make up the graph itself are the to and from columns, each with a vertex id. Then there's associated metadata such as the name of the product, type of the product, sales rank, et cetera. We'll use this later in the lesson. We'll use dplyr to help create a graph directly from our data frame. The first step in our pipeline is to filter down to a single date. It's important that we look at just a single day so we don't conflate CO purchases on different days. Our next step is to select just the from and to columns. And lastly we'll say it's a directed graph. Checking the size, we can see there are around 10,000 vertices. The graph is obviously quite large, so we'll just look at a small subgraph. First, we'll use the function inducedsubgraph to make a new graph that is just the first 500 vertices. Next, we'll delete any vertices with a degree of zero. Finally, we'll make a plot. As you can see, this might look a bit different than your Platonic ideal of a graph, because just a few things tend to be purchased in a single Amazon order. That's why we see all these little clusters of connected vertices. Igraph provides a way to count all these small subpatterns, which are called dyads, for two connected vertices and triads three connected vertices. When we run a dyad census on our graph using DYADcensus, we'll see three outputs from a count of null, asymmetric and mutual dyads. These correspond to the following subgraph types. Null is when there is no connection, asymmetric is when there is a single directed edge, and mutual is where there are two directed edges. Back and forth this underlying pattern has implications for graph level metrics like reciprocity. Triads get even more complicated because in a directed graph there are 16 possible triad types. To understand all of these, a common three digit code is used. The first number is a count of the pairs of vertices connected by a bidirectional symmetric edge. The second number is the count of the pairs of vertices connected by an asymmetric edge and the third number is a count of pairs of unconnected vertices. Letter codes C, D, U and T are used to denote whether a triad is cyclic like 10, single edges go down from the top like 7 or 12, or up from the bottom like 8 or transitive like 9. A concept that says if any two vertices in a triad are connected to each other then there must exist a connection between the third. It should be clear that patterns 1, 2 and 3 are essentially the dyad patterns. When we run an IGRAPH triad census using the function triadcensus we'll get a count of each of these 16 possibilities. Now it's time to count the dyads and triads in the co purchase graph and then see how those numbers relate to the graph level metrics of transitivity and reciprocity.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







