---
title: "Bioequivalence Tests for Parallel Trial Designs: 3 Arms, 1 Endpoint"
output: 
  rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Bioequivalence Tests for Parallel Trial Designs: 3 Arms, 1 Endpoint}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
bibliography: 'references.bib'
link-citations: yes
---

```{r setup, include=FALSE, message = FALSE, warning = FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(comment = "#>", collapse = TRUE)
options(rmarkdown.html_vignette.check_title = FALSE) #title of doc does not match vignette title
doc.cache <- T #for cran; change to F
```

In the `SimTOST` R package, which is specifically designed for sample size estimation for bioequivalence studies, hypothesis testing is based on the Two One-Sided Tests (TOST) procedure. [@sozu_sample_2015]  In TOST, the equivalence test is framed as a comparison between the the null hypothesis of ‘new product is worse by a clinically relevant quantity’ and the alternative hypothesis of ‘difference between products is too small to be clinically relevant’. This vignette focuses on a parallel design, with 3 arms/treatments and 1 primary endpoint.

# Introduction
This vignette demonstrates advanced sample size calculation techniques for parallel trial designs involving three arms and one endpoint. Specifically, we calculate the required sample size to test bioequivalence between a new treatment (SB2) and a reference product (Remicade) administered in two different locations ("EU_Remicade" and "USA_Remicade"). The endpoint of interest is the Area Under the Curve (AUCinf), a commonly used pharmacokinetic measure.

In this example, we assume the endpoint follows a log-normal distribution with equal variances across arms. The goal is to determine the sample size needed to achieve 90\% power while controlling the type I error rate at 5\%. The equivalence margin is defined as $E_L = 80\%$ and $E_U = 125\%$ of the reference mean on the original scale.

The methods presented in this vignette build on fundamental bioequivalence testing concepts and extend them to multi-arm scenarios. These examples provide practical insights for designing robust parallel trials with complex equivalence testing requirements.

We assume that the primary endpoint, AUCinf, is on the original scale, with the mean and standard deviation for each arm available. This information is organized into a structured data table for further analysis:


```{r}
library(SimTOST)

data <- data.table::data.table(arm = c("SB2","RemEU","RemUSA"),
                               mean = c(37162.0, 37705.0, 37702.8), 
                               sd = c(11113.62172, 12332.41615,12113.72))
```


# Sample Size Calculation for AUCinf: Equivalence to EU Remicade

This example demonstrates how to calculate the required sample size when testing the equivalence of SB2 to a reference drug, Remicade, as administered in the EU. The goal is to determine the minimum number of participants needed to ensure adequate power for the equivalence test.

## Hypotheses

Null Hypothesis (No Equivalence):
$$H_0: \frac{\mu_{SB2}}{\mu_{RemEU}} \le E_L ~~ or~~ \frac{\mu_{SB2}}{\mu_{RemEU}} \ge E_U$$

Alternative Hypothesis (Equivalence):

$$H_1: E_L<\frac{\mu_{SB2}}{\mu_{RemEU}} < E_U$$

Here, $E_L$ and $E_U$ represent the lower and upper equivalence margins, respectively.


## Preparing the function arguments

To proceed with the sample size calculation, we first need to organize the mean and standard deviation values of each arm (SB2, EU Remicade, USA Remicade) as list objects.

Since this example focuses on a single endpoint, the mean list (`mu_list`) contains three scalar elements corresponding to each arm, and the standard deviation list (`sigma_list`) contains three 1x1 matrix elements.

```{r}
mu_list <- as.list(data$mean)  # Organize mean values into a list
sigma_list <- as.list(data$sd)  # Organize standard deviation values into a list
```

Next, we define the comparison parameters, including the lower (`lequi.tol`) and upper (`uequi.tol`) equivalence boundaries, as well as the list of comparators. Since we are only comparing two arms (SB2 and EU Remicade), the list of comparators contains a single element specifying these two arms:

```{r}
list_comparator <- list("Comparison" = c("SB2","RemEU"))
list_lequi.tol <- list("Comparison" = 0.8)
list_uequi.tol <- list("Comparison" = 1/0.8)
```

## Computing Sample Size

Finally, we use the [sampleSize()](../reference/sampleSize.html) function to calculate the required sample size based on stochastic simulations of the trial. The function accepts several parameters, such as the desired power, confidence level, and design specifications. By default, it assumes:

* A parallel design,
* A test based on the ratio of means (ROM),
* Equal variances across arms,
* A lognormal distribution for the endpoint.


```{r}
AUCinf_1comp <- sampleSize(
  power = 0.9,                  # Target power
  alpha = 0.05,                 # Confidence level
  arm_names = data$arm,         # Names of trial arms
  list_comparator = list_comparator,  # Comparator configuration
  mu_list = mu_list,            # Mean values
  sigma_list = sigma_list,      # Standard deviation values
  list_lequi.tol = list_lequi.tol,   # Lower equivalence boundary
  list_uequi.tol = list_uequi.tol,   # Upper equivalence boundary
  nsim = 1000                   # Number of stochastic simulations
)

AUCinf_1comp
```

The required sample size for this scenario is `r AUCinf_1comp$response$n_total`, or `r AUCinf_1comp$response$n_SB2` for each arm. 


# Sample Size Calculation for AUCinf: Equivalence to US Remicade and EU Remicade

In this section, we calculate the sample size required to demonstrate equivalence with both the European reference product (RemEU) and the US reference product (RemUS). This scenario involves a more complex comparison, as we must simultaneously establish equivalence with two distinct reference arms. 

## Hypotheses

Null Hypothesis (No Equivalence):

$$H_0: \frac{\mu_{SB2}}{\mu_{RemEU}} \le E_L ~~ or~~ \frac{\mu_{SB2}}{\mu_{RemEU}} \ge E_U~~ or~~ \frac{\mu_{SB2}}{\mu_{RemUSA}} \le E_L ~~ or~~ \frac{\mu_{SB2}}{\mu_{RemUSA}} \ge E_U$$

Alternative Hypothesis (Equivalence):

$$H_1: E_L<\frac{\mu_{SB2}}{\mu_{RemEU}} < E_U~~ and~~E_L<\frac{\mu_{SB2}}{\mu_{RemUSA}} < E_U$$
**Key Considerations**

* The function [sampleSize()](../reference/sampleSize.html) adjusts the alpha level for multiple comparisons within a single comparator (e.g., comparisons involving only RemEU or RemUSA).
* The function does not yet account for alpha adjustments between comparators when simultaneously comparing SB2 with both reference products (RemEU and RemUSA). Thus, the null hypothesis is rejected if any ROM falls outside the equivalence boundaries. Future package updates will address this limitation and include proper alpha adjustments for comparisons between comparators.

## Implementation Details

Implementation of this scenario is similar to the initial example, in which the `list_comparator` is modified to include multiple comparators. The list contains two elements specifying the simultaneous comparison between SB2 vs. RemEU and SB2 vs. RemUSA. This approach ensures that the equivalence assessment accounts for both reference products.

Additionally, we must specify equivalence boundaries for each comparison individually. These boundaries define the acceptable range for equivalence and are provided separately for each comparator.

```{r}
list_comparator <- list("EMA" = c("SB2", "RemEU"),
                        "FDA" = c("SB2", "RemUSA"))
list_lequi.tol <- list("EMA" = 0.8, "FDA" = 0.8)     # Lower equivalence boundary
list_uequi.tol <- list("EMA" = 1/0.8, "FDA" = 1/0.8) # Upper equivalence boundary
```

## Computing Sample Size

We then pass these values into the [sampleSize()](../reference/sampleSize.html) function to calculate the required sample size for multiple comparisons.

```{r}
(AUCinf_2comp <- sampleSize(
  power = 0.9,                  # Target power
  alpha = 0.05,                 # Confidence level
  arm_names = data$arm,         # Names of trial arms
  list_comparator = list_comparator,  # Comparator configuration
  mu_list = mu_list,            # Mean values
  sigma_list = sigma_list,      # Standard deviation values
  list_lequi.tol = list_lequi.tol,   # Lower equivalence boundary
  list_uequi.tol = list_uequi.tol,   # Upper equivalence boundary
  nsim = 1000                   # Number of stochastic simulations
))
```

## Results and Interpretation

The required total sample size for this scenario is `r AUCinf_2comp$response$n_total`. Notably, an additional `r AUCinf_2comp$response$n_SB2 - AUCinf_1comp$response$n_SB2` patients per arm are required to achieve equivalence with both reference products (RemEU and RemUSA), compared to the scenario where equivalence is required with only one reference arm (`r AUCinf_1comp$response$n_SB2`).

This example demonstrates the added complexity and sample size requirements when multiple comparators are involved in an equivalence trial.

The SimTOST package includes the [plot()](../reference/plot.simss.html) function, which is designed to permit visualization of the relationship between sample size (x-axis) and achieved power (y-axis) for all combinations of endpoints and comparators. In this example, we use the [plot()](../reference/plot.simss.html) function to generate a plot for the `AUCinf_2comp` object.

```{r, fig.height=4, fig.width = 10, out.width = "95%"}
plot(AUCinf_2comp)
```

# References

.
