---
title: "Quality Measures"
output:
    rmarkdown::html_vignette:
        toc: true
description: >
  Calculate context-agnostic measures of clustering compactness and separation.
vignette: >
  %\VignetteIndexEntry{Quality Measures}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

<style>
div.aside { background-color:#fff2e6; }
</style>

```{r, include = FALSE}
knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>"
)
```

```{r echo = FALSE}
options(crayon.enabled = FALSE, cli.num_colors = 0)
```

Download a copy of the vignette to follow along here: [quality_measures.Rmd](https://raw.githubusercontent.com/BRANCHlab/metasnf/main/vignettes/quality_measures.Rmd)

This vignette walks through calculation of silhouette scores, Dunn indices, and Davies-Boulding indices a we will highlight the main stability measure options in the `metasnf` package.

To use these functions, you will need to have the `clv` package installed.

```{r eval = FALSE}
# load package
library(metasnf)

# generate data_list
dl <- data_list(
    list(cort_t, "cort_t", "neuroimaging", "continuous"),
    list(cort_sa, "cort_sa", "neuroimaging", "continuous"),
    list(subc_v, "subc_v", "neuroimaging", "continuous"),
    list(income, "income", "demographics", "continuous"),
    list(pubertal, "pubertal", "demographics", "continuous"),
    uid = "unique_id"
)

# build SNF config
set.seed(42)
sc <- snf_config(
    dl = dl,
    n_solutions = 15
)

# collect similarity matrices and solutions data frame from batch_snf
sol_df <- batch_snf(
    dl = dl,
    sc,
    return_sim_mats = TRUE
)

# calculate Davies-Bouldin indices
davies_bouldin_indices <- calculate_db_indices(sol_df)

# calculate Dunn indices
dunn_indices <- calculate_dunn_indices(sol_df)

# calculate silhouette scores
silhouette_scores <- calculate_silhouettes(sol_df)

# plot the silhouette scores of the first solutions
plot(silhouette_scores[[1]])
```
