---
title: "Finding cliques and communities with tna"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Finding cliques and communities with tna}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  fig.width = 6,
  fig.height = 4,
  dev = "svg",
  fig.ext="svg",
  out.width = "100%",
  dpi = 60,
  comment = "#>"
)
```

The `tna` package includes functionalities for finding cliques of the transition network as well as discovering communities.
We begin by loading the package and the example data set `group_regulation`.
```{r}
library("tna")
data("group_regulation", package = "tna")
```

We fit the TNA model to the data.
```{r}
tna_model <- tna(group_regulation)
print(tna_model)
plot(tna_model)
```

Next, we apply several community finding algorithms to the model (see `?communities` for more details), and plot the results for the `leading_eigen` algorithm.
```{r, warning = FALSE}
cd <- communities(tna_model)
plot(cd, method = "leading_eigen")
```

Cliques can be obtained with the `cliques` function. Here we look for dyads and triads by setting `size = 2` and `size = 3`, respectively.
Finally, we plot the results.
```{r, figures-side, fig.show="hold", fig.height=6, fig.width=8}
layout(matrix(1:4, ncol = 2, byrow = T))
dyads <- cliques(tna_model, size = 2, threshold = 0.2)
triads <- cliques(tna_model, size = 3, threshold = 0.05)
plot(dyads, ask = F)
plot(triads, ask = F)
```

