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.

## 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.

```
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
```

```
## 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")
```

`Network`

package

```
## 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")
```

`igraph`

package

```
## 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)
```

`tidygraph`

and
`ggraph`