---
title: "Test of the function eEvents_df"
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 eEvents_df}
---

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

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

# Test 1

```{r}
enrollRates <- tibble(duration = c(2, 1, 2), rate = c(5, 10, 20))
failRates <- tibble(duration = c(1, 1, 1), failRate = c(.05, .02, .01), dropoutRate = .01)
totalDuration <- 20
x1 <- gsDesign::eEvents(
  lambda = failRates$failRate,
  S = failRates$duration[1:(nrow(failRates) - 1)],
  eta = failRates$dropoutRate,
  gamma = enrollRates$rate,
  R = enrollRates$duration,
  T = totalDuration
)$d
x2 <- eEvents_df_(enrollRates, failRates, totalDuration, simple = TRUE)


enrollRates <- tibble(duration = c(2, 1, 2), rate = c(5, 10, 20))
failRates <- tibble(duration = c(1, 1, 1), fail_rate = c(.05, .02, .01), dropout_rate = .01)
totalDuration <- 20
x3 <- expected_event(enrollRates, failRates, totalDuration, simple = TRUE)

tibble(
  method = c("gsDesign", "old version", "new version"),
  exp_events = c(x1, x2, x3)
) %>% gt()
```

# Test 2

```{r}
enrollRates <- tibble(duration = c(1, 1, 8), rate = c(3, 2, 0))
failRates <- tibble(duration = c(4, Inf), fail_rate = c(.03, .06), dropout_rate = c(.001, .002))
totalDuration <- 7
x <- expected_event(enrollRates, failRates, totalDuration, simple = FALSE) %>% data.frame()
# expected checked with alternate calculations in gsSurvNPH vignette
y <- data.frame(t = c(0, 4), failRate = c(0.03, 0.06), Events = c(0.5642911, 0.5194821))
x %>%
  gt() %>%
  tab_header(title = "From gsDesign2 new version")
y %>%
  gt() %>%
  tab_header(title = "From gsSurvNPH vignette")
```
