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

```{r, echo=FALSE, message=FALSE}
library(gt)
library(dplyr)
library(tibble)
# library(gsDesign2)
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

In this test, we verify `tEvents()` by `AHR()`.
```{r, message=FALSE}
enrollRates <- tibble(Stratum = "All", duration = c(2, 2, 10), rate = c(3, 6, 9) * 5)
failRates <- tibble(
  Stratum = "All", duration = c(3, 100), failRate = log(2) / c(9, 18),
  hr = c(.9, .6), dropoutRate = rep(.001, 2)
)
ratio <- 1

x <- AHR_(
  enrollRates = enrollRates,
  failRates = failRates,
  ratio = ratio, totalDuration = 20
)
cat("The number of events by 20 months is ", x$Events, ".\n")

y <- tEvents(
  enroll_rate = enrollRates,
  fail_rate = failRates %>% rename(fail_rate = failRate, dropout_rate = dropoutRate),
  ratio = ratio, target_event = x$Events
)

cat("The time to get ", x$Events, " is ", y$Time, "months.\n")
```

# Test 2

```{r, message=FALSE}
x1 <- tEvents()
x2 <- tEvents_()

x1 %>%
  union_all(x2) %>%
  mutate(`function comes from` = c("new version", "old version")) %>%
  select(`function comes from`, Time, AHR, Events, info, info0) %>%
  gt::gt()
```

# Test 3

```{r, message=FALSE}
enrollRates <- tibble(Stratum = "All", duration = c(2, 2, 10), rate = c(3, 6, 9) * 5)
failRates <- tibble(
  Stratum = "All", duration = c(3, 100),
  failRate = log(2) / c(9, 18), hr = c(.9, .6),
  dropoutRate = rep(.001, 2)
)
ratio <- 1

x1 <- tEvents(
  enroll_rate = enrollRates,
  fail_rate = failRates %>% rename(fail_rate = failRate, dropout_rate = dropoutRate),
  ratio = ratio,
  target_event = 200
)
x2 <- tEvents_(
  enrollRates = enrollRates,
  failRates = failRates,
  ratio = ratio,
  targetEvents = 200
)

x1 %>%
  union_all(x2) %>%
  mutate(`function comes from` = c("new version", "old version")) %>%
  select(`function comes from`, Time, AHR, Events, info, info0) %>%
  gt::gt()
```
