---
title: "5. Visualization"
author: "Yuki Atsusaka and Seo-young Silvia Kim"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{5. Visualization}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

The following code chunk shows how to plot the distribution of ranking
profiles. This can help eyeball whether the ranking data is uniformly
distributed, which can of course be formally tested.

```{r}
library(combinat)
library(rankingQ)

set.seed(100)
tab <- lapply(permn(seq(3)), paste0, collapse = "") |>
  sample(30, replace = TRUE) |>
  unlist() |>
  table() |>
  table_to_tibble()
plot_dist_ranking(tab, ylim = 0.5)
```

## Visualizing Average Ranks

The `plot_avg_ranking` function creates a visualization of average
ranks with confidence intervals:

```{r}
data(identity)

# First compute bias-corrected estimates
out_direct <- imprr_direct(
  data = identity,
  J = 4,
  main_q = c("party", "religion", "gender", "race"),
  anc_correct = "anc_correct_identity",
  n_bootstrap = 10
)

# Plot average ranks
library(dplyr)
out_direct$results |>
  filter(qoi == "average rank") |>
  mutate(
    item = factor(
      item,
      levels = c("party", "religion", "gender", "race"),
      labels = c("Party", "Religion", "Gender", "Race")
    )
  ) |>
  plot_avg_ranking()
```
