## ----echo = FALSE-------------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", eval = TRUE)

## ----setup--------------------------------------------------------------------
library(testflow)

## ----eval = requireNamespace("PowerTOST", quietly = TRUE)---------------------
# Current (fixed) implementation: testflow treats n as one continuous total
# and rounds up to the nearest even number, since a 2x2 crossover has two
# equal-sized sequences
x <- sample_size_bioequivalence(
  design = "crossover", gmr = 0.95, cv_within = 0.15,
  alpha = 0.05, power = 0.90, method = "iterative_tost"
)
n_testflow <- 2 * ceiling(x$n / 2)
n_testflow

# Cross-check against PowerTOST's Owen's-Q-based exact calculation (the
# calculation used in real regulatory bioequivalence submissions)
PowerTOST::sampleN.TOST(
  alpha = 0.05, targetpower = 0.90, theta0 = 0.95,
  theta1 = 0.80, theta2 = 1.25, CV = 0.15, design = "2x2", print = FALSE
)$`Sample size`

## ----eval = requireNamespace("irr", quietly = TRUE)---------------------------
set.seed(2)
n <- 200
cats <- c("A", "B", "C")
rater1 <- sample(cats, n, replace = TRUE, prob = c(0.5, 0.3, 0.2))
rater2 <- ifelse(runif(n) < 0.75, rater1, sample(cats, n, replace = TRUE))
dat <- data.frame(r1 = factor(rater1, levels = cats), r2 = factor(rater2, levels = cats))

x <- test_agreement(dat, rater1 = r1, rater2 = r2)
x$primary_test[, c("statistic", "p.value")]

irr::kappa2(dat[, c("r1", "r2")])[c("statistic", "p.value")]

