Introduction to Latent Attribute Network Analysis

blog
Network
DCM
Author

Jihong Zhang

Published

October 20, 2019

Network analysis is a very useful tool. This post show how to visualize the latent attribute network in Diagnostic Classification Modeling(DCM). There are a ton of R package could be used to visualize network structure.

⌘+C
library(CDM)
library(tidyverse)
library(network)

1 Data Preparation

I will use a simulated hierachial data from CDM package. The node.list depicts the traget nodes and starting nodes. Those information could be extracted from the Q^{T}Q square matrix, in which Q is the Q matrix of the model.

As shown below, there are 6 latent attributes including A1, A2, A3, B1, C1, C2. The A attributes share common items and the C attributes share common items but B attribute does not share common items with other attributes. The numbers in each cell represents the number of items shared by the pair of attributes. The number of common items will be used for the weights of network edges.

⌘+C
data("data.cdm10")
q.matrix <- data.cdm10$q.matrix
t(q.matrix) %*% q.matrix
   A1 A2 A3 B1 C1 C2
A1  6  4  2  0  0  0
A2  4  4  2  0  0  0
A3  2  2  2  0  0  0
B1  0  0  0  3  0  0
C1  0  0  0  0  6  3
C2  0  0  0  0  3  3
⌘+C
## prepare the edge and node table based on t(Q)%*%Q
edge.list = tibble(from = c(1,1,2,2,3,3,5,6), 
                   to = c(2,3,1,3,1,2,6,5), 
                   weight = c(4,2,4,2,2,2,3,3))
node.list = tibble(label = c("A1", "A2", "A3", "B1", "C1", "C2")) %>% rowid_to_column("id")

2 Network package

⌘+C
## Network package
library(network)
routes_work <- network(x = edge.list, vertex.attr = node.list, 
                       matrix.type = "edgelist", ignore.eval = FALSE)
plot(routes_work, vertex.cex = 3, mode = "circle")

3 igraph package

⌘+C
## igraph package 
detach(package:network)
rm(routes_work)
library(igraph)
routes_igraph <- graph_from_data_frame(d = edge.list, vertices = node.list, directed = TRUE)
plot(routes_igraph, edge.arrow.size = 0.5, layout = layout_with_graphopt)

4 tidygraph and ggraph

⌘+C
library(tidygraph)
library(ggraph)
routes_tidy <- tbl_graph(nodes = node.list, edges = edge.list, directed = FALSE)
⌘+C
ggraph(routes_tidy, layout = "graphopt") + 
  geom_node_point() +
  geom_edge_link(aes(width = weight), alpha = 0.8) + 
  scale_edge_width(range = c(0.2, 2)) +
  geom_node_text(aes(label = label), repel = TRUE) +
  labs(edge_width = "Number of Common Items") +
  theme_graph()

⌘+C
### Linear Layout
ggraph(routes_tidy, layout = "linear") + 
  geom_edge_arc(aes(width = weight), alpha = 0.8) + 
  scale_edge_width(range = c(0.2, 2)) +
  geom_node_text(aes(label = label)) +
  labs(edge_width = "Number of Common Items") +
  theme_graph()

Back to top