---
title: "Test of the function AHR"
output:
  rmarkdown::html_document:
    toc: true
    toc_float: true
    toc_depth: 2
    highlight: "textmate"
    css: "custom.css"
# bibliography: "example.bib"
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{Test of the function AHR}
---

```{r, message=FALSE}
library(tibble)
library(dplyr)
library(testthat)
devtools::load_all()
```

```{r, warning=FALSE, message=FALSE}
setwd("./..")
my_path <- "./old_function/"
source_files <- list.files(my_path, "*.R$")
sapply(paste0(my_path, source_files), source)
```

# Test 1: unstratified population

```{r, message=FALSE}
enrollRates <- tibble(
  Stratum = "All",
  duration = c(2, 10, 4, 4, 8),
  rate = c(5, 10, 0, 3, 6)
)
failRates <- tibble(
  Stratum = "All",
  duration = 1,
  failRate = c(.1, .2, .3, .4),
  hr = c(.9, .75, .8, .6),
  dropoutRate = .001
)

x1 <- AHR(
  enroll_rate = enrollRates,
  fail_rate = failRates %>% rename(fail_rate = failRate, dropout_rate = dropoutRate),
  total_duration = c(15, 30)
)

x2 <- AHR_(
  enrollRates = enrollRates,
  failRates = failRates,
  totalDuration = c(15, 30)
)

x1 %>%
  gt::gt() %>%
  gt::tab_header("AHR from gsDesign2 new version")
x2 %>%
  gt::gt() %>%
  gt::tab_header("AHR from gsDesign2 old version")
```

# Test 2: stratified population

```{r, message=FALSE}
enrollRates <- tibble(
  Stratum = c(rep("Low", 2), rep("High", 3)),
  duration = c(2, 10, 4, 4, 8),
  rate = c(5, 10, 0, 3, 6)
)

failRates <- tibble(
  Stratum = c(rep("Low", 2), rep("High", 2)),
  duration = 1,
  failRate = c(.1, .2, .3, .4),
  hr = c(.9, .75, .8, .6),
  dropoutRate = .001
)

x1 <- AHR(
  enroll_rate = enrollRates,
  fail_rate = failRates %>% rename(fail_rate = failRate, dropout_rate = dropoutRate),
  total_duration = c(15, 30)
)

x2 <- AHR_(
  enrollRates = enrollRates,
  failRates = failRates,
  totalDuration = c(15, 30)
)

x1 %>%
  gt::gt() %>%
  gt::tab_header("AHR from gsDesign2 new version")
x2 %>%
  gt::gt() %>%
  gt::tab_header("AHR from gsDesign2 old version")
```
