---
title: "Pairwise comparisons with `{ggstatsplot}`"
output:
  rmarkdown::html_vignette:
    toc: true
vignette: >
  %\VignetteIndexEntry{Pairwise comparisons with `{ggstatsplot}`}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r}
#| label = "setup",
#| include = FALSE

source("../setup.R")
```

```{r}
#| label = "suggested_pkgs",
#| include = FALSE

pkgs <- "PMCMRplus"

successfully_loaded <- purrr::map_lgl(pkgs, requireNamespace, quietly = TRUE)
can_evaluate <- all(successfully_loaded)

if (can_evaluate) {
  purrr::walk(pkgs, library, character.only = TRUE)
} else {
  knitr::opts_chunk$set(eval = FALSE)
}
```

---

You can cite this package/vignette as:

```{r}
#| label = "citation",
#| echo = FALSE,
#| comment = ""
citation("ggstatsplot")
```

---

## Introduction

Pairwise comparisons with `{ggstatsplot}`.

## Summary of types of statistical analyses

Following table contains a brief summary of the currently supported pairwise
comparison tests-

### Between-subjects design

Type | Equal variance? | Test | *p*-value adjustment? | Function used
----------- | --- | ------------------------- | --- | -----
Parametric | No | Games-Howell test | ✅ | `PMCMRplus::gamesHowellTest`
Parametric | Yes | Student's *t*-test | ✅ | `stats::pairwise.t.test`
Non-parametric | No | Dunn test | ✅ | `PMCMRplus::kwAllPairsDunnTest`
Robust | No | Yuen's trimmed means test | ✅ | `WRS2::lincon`
Bayesian | `NA` | Student's *t*-test | `NA` | `BayesFactor::ttestBF`

### Within-subjects design

Type | Test | *p*-value adjustment? | Function used
----------- | ---------------------------- | --- | -----
Parametric | Student's *t*-test | ✅ | `stats::pairwise.t.test`
Non-parametric | Durbin-Conover test | ✅ | `PMCMRplus::durbinAllPairsTest` 
Robust | Yuen's trimmed means test | ✅ | `WRS2::rmmcp`
Bayesian | Student's *t*-test | `NA` | `BayesFactor::ttestBF`


## Data frame outputs

See data frame outputs [here](https://indrajeetpatil.github.io/statsExpressions/articles/web_only/dataframe_outputs.html#pairwise-comparisons).

## Using `pairwise_comparisons()` with `ggsignif`

### Example-1: between-subjects

```{r}
#| label = "ggsignif",
#| fig.height = 5

library(ggplot2)
library(ggsignif)

## converting to factor
mtcars$cyl <- as.factor(mtcars$cyl)

## creating a basic plot
p <- ggplot(mtcars, aes(cyl, wt)) +
  geom_boxplot()

## using `pairwise_comparisons()` package to create a data frame with results
df <-
  pairwise_comparisons(mtcars, cyl, wt) %>%
  dplyr::mutate(groups = purrr::pmap(.l = list(group1, group2), .f = c)) %>%
  dplyr::arrange(group1)

df

## using `geom_signif` to display results
## (note that you can choose not to display all comparisons)
p +
  ggsignif::geom_signif(
    comparisons = list(df$groups[[1]]),
    annotations = as.character(df$expression)[[1]],
    test        = NULL,
    na.rm       = TRUE,
    parse       = TRUE
  )
```

### Example-2: within-subjects

```{r}
#| label = "ggsignif2",
#| fig.height = 7

library(ggplot2)
library(ggsignif)

## creating a basic plot
p <- ggplot(WRS2::WineTasting, aes(Wine, Taste)) +
  geom_boxplot()

## using `pairwise_comparisons()` package to create a data frame with results
df <-
  pairwise_comparisons(
    WRS2::WineTasting,
    Wine,
    Taste,
    subject.id = Taster,
    type = "bayes",
    paired = TRUE
  ) %>%
  dplyr::mutate(groups = purrr::pmap(.l = list(group1, group2), .f = c)) %>%
  dplyr::arrange(group1)

df

## using `geom_signif` to display results
p +
  ggsignif::geom_signif(
    comparisons      = df$groups,
    map_signif_level = TRUE,
    tip_length       = 0.01,
    y_position       = c(6.5, 6.65, 6.8),
    annotations      = as.character(df$expression),
    test             = NULL,
    na.rm            = TRUE,
    parse            = TRUE
  )
```
