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

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

```{r, message=FALSE}
x1 <- gs_power_ahr()
x2 <- gs_power_ahr_()
```

```{r, echo=FALSE}
tibble(
  version = c(rep("new", 3), rep("old", 3)),
  analysis = rep(1:3, 2),
  samplesize = c(x1$analysis$n, rep(NA, 3)),
  events = c(x1$analysis$event, x2$Events[1:3]),
  time = c(x1$analysis$time, x2$Time[1:3]),
  theta = c(x1$analysis$theta, x2$theta[1:3]),
  Z_upper = c(
    (x1$bound %>% filter(bound == "upper"))$z,
    (x2 %>% filter(Bound == "Upper"))$Z
  ),
  Z_lower = c(
    (x1$bound %>% filter(bound == "lower"))$z,
    NA,
    NA,
    (x2 %>% filter(Bound == "Lower"))$Z
  ),
  prob_upper = c(
    (x1$bound %>% filter(bound == "upper"))$probability,
    (x2 %>% filter(Bound == "Upper"))$Probability
  ),
  prob_lower = c(
    (x1$bound %>% filter(bound == "lower"))$probability,
    NA,
    NA,
    (x2 %>% filter(Bound == "Lower"))$Probability
  ),
  AHR = c(x1$analysis$ahr, x2$AHR[1:3]),
  info = c(x1$analysis$info, x2$info[1:3]),
  info0 = c(x1$analysis$info0, x2$info0[1:3])
) %>%
  arrange(analysis) %>%
  group_by(analysis) %>%
  gt() %>%
  tab_style(
    style = list(cell_fill(color = "#d3edeb")),
    locations = cells_body(rows = `version` == "old")
  )
```

# Test 2

```{r, message=FALSE}
x1 <- gs_power_ahr(
  analysis_time = c(12, 24, 36),
  event = NULL,
  binding = TRUE,
  upper = gs_spending_bound,
  upar = list(sf = gsDesign::sfLDOF, total_spend = 0.025, param = NULL, timing = NULL),
  lower = gs_spending_bound,
  lpar = list(sf = gsDesign::sfLDOF, total_spend = 0.025, param = NULL, timing = NULL)
)

x2 <- gs_power_ahr_(
  analysisTimes = c(12, 24, 36),
  events = NULL,
  binding = TRUE,
  upper = gs_spending_bound,
  upar = list(sf = gsDesign::sfLDOF, total_spend = 0.025, param = NULL, timing = NULL),
  lower = gs_spending_bound,
  lpar = list(sf = gsDesign::sfLDOF, total_spend = 0.025, param = NULL, timing = NULL)
)
```

```{r, echo=FALSE}
tibble(
  version = c(rep("new", 3), rep("old", 3)),
  analysis = rep(1:3, 2),
  samplesize = c(x1$analysis$n, rep(NA, 3)),
  events = c(x1$analysis$event, x2$Events[1:3]),
  time = c(x1$analysis$time, x2$Time[1:3]),
  theta = c(x1$analysis$theta, x2$theta[1:3]),
  Z_upper = c(
    (x1$bound %>% filter(bound == "upper"))$z,
    (x2 %>% filter(Bound == "Upper"))$Z
  ),
  Z_lower = c(
    (x1$bound %>% filter(bound == "lower"))$z,
    (x2 %>% filter(Bound == "Lower"))$Z
  ),
  prob_upper = c(
    (x1$bound %>% filter(bound == "upper"))$probability,
    (x2 %>% filter(Bound == "Upper"))$Probability
  ),
  prob_lower = c(
    (x1$bound %>% filter(bound == "lower"))$probability,
    (x2 %>% filter(Bound == "Lower"))$Probability
  ),
  AHR = c(x1$analysis$ahr, x2$AHR[1:3]),
  info = c(x1$analysis$info, x2$info[1:3]),
  info0 = c(x1$analysis$info0, x2$info0[1:3])
) %>%
  arrange(analysis) %>%
  group_by(analysis) %>%
  gt() %>%
  tab_style(
    style = list(cell_fill(color = "#d3edeb")),
    locations = cells_body(rows = `version` == "old")
  )
```

# Test 3

```{r, message=FALSE}
x1 <- gs_power_ahr(
  analysis_time = NULL,
  event = c(20, 50, 70),
  binding = TRUE,
  upper = gs_spending_bound,
  upar = list(sf = gsDesign::sfLDOF, total_spend = 0.025, param = NULL, timing = NULL),
  lower = gs_spending_bound,
  lpar = list(sf = gsDesign::sfLDOF, total_spend = 0.025, param = NULL, timing = NULL)
)

x2 <- gs_power_ahr_(
  analysisTimes = NULL,
  events = c(20, 50, 70),
  binding = TRUE,
  upper = gs_spending_bound,
  upar = list(sf = gsDesign::sfLDOF, total_spend = 0.025, param = NULL, timing = NULL),
  lower = gs_spending_bound,
  lpar = list(sf = gsDesign::sfLDOF, total_spend = 0.025, param = NULL, timing = NULL)
)
```

```{r, echo=FALSE}
tibble(
  version = c(rep("new", 3), rep("old", 3)),
  analysis = rep(1:3, 2),
  samplesize = c(x1$analysis$n, rep(NA, 3)),
  events = c(x1$analysis$event, x2$Events[1:3]),
  time = c(x1$analysis$time, x2$Time[1:3]),
  theta = c(x1$analysis$theta, x2$theta[1:3]),
  Z_upper = c(
    (x1$bound %>% filter(bound == "upper"))$z,
    (x2 %>% filter(Bound == "Upper"))$Z
  ),
  Z_lower = c(
    (x1$bound %>% filter(bound == "lower"))$z,
    (x2 %>% filter(Bound == "Lower"))$Z
  ),
  prob_upper = c(
    (x1$bound %>% filter(bound == "upper"))$probability,
    (x2 %>% filter(Bound == "Upper"))$Probability
  ),
  prob_lower = c(
    (x1$bound %>% filter(bound == "lower"))$probability,
    (x2 %>% filter(Bound == "Lower"))$Probability
  ),
  AHR = c(x1$analysis$ahr, x2$AHR[1:3]),
  info = c(x1$analysis$info, x2$info[1:3]),
  info0 = c(x1$analysis$info0, x2$info0[1:3])
) %>%
  arrange(analysis) %>%
  group_by(analysis) %>%
  gt() %>%
  tab_style(
    style = list(cell_fill(color = "#d3edeb")),
    locations = cells_body(rows = `version` == "old")
  )
```

# Test 4

```{r, message=FALSE}
x1 <- gs_power_ahr(
  analysis_time = c(12, 24, 36),
  event = c(30, 40, 50),
  binding = TRUE,
  upper = gs_spending_bound,
  upar = list(sf = gsDesign::sfLDOF, total_spend = 0.025, param = NULL, timing = NULL),
  lower = gs_spending_bound,
  lpar = list(sf = gsDesign::sfLDOF, total_spend = 0.025, param = NULL, timing = NULL)
)

x2 <- gs_power_ahr_(
  analysisTimes = c(12, 24, 36),
  events = c(30, 40, 50),
  binding = TRUE,
  upper = gs_spending_bound,
  upar = list(sf = gsDesign::sfLDOF, total_spend = 0.025, param = NULL, timing = NULL),
  lower = gs_spending_bound,
  lpar = list(sf = gsDesign::sfLDOF, total_spend = 0.025, param = NULL, timing = NULL)
)
```

```{r, echo=FALSE}
tibble(
  version = c(rep("new", 3), rep("old", 3)),
  analysis = rep(1:3, 2),
  samplesize = c(x1$analysis$n, rep(NA, 3)),
  events = c(x1$analysis$event, x2$Events[1:3]),
  time = c(x1$analysis$time, x2$Time[1:3]),
  theta = c(x1$analysis$theta, x2$theta[1:3]),
  Z_upper = c(
    (x1$bound %>% filter(bound == "upper"))$z,
    (x2 %>% filter(Bound == "Upper"))$Z
  ),
  Z_lower = c(
    (x1$bound %>% filter(bound == "lower"))$z,
    (x2 %>% filter(Bound == "Lower"))$Z
  ),
  prob_upper = c(
    (x1$bound %>% filter(bound == "upper"))$probability,
    (x2 %>% filter(Bound == "Upper"))$Probability
  ),
  prob_lower = c(
    (x1$bound %>% filter(bound == "lower"))$probability,
    (x2 %>% filter(Bound == "Lower"))$Probability
  ),
  AHR = c(x1$analysis$ahr, x2$AHR[1:3]),
  info = c(x1$analysis$info, x2$info[1:3]),
  info0 = c(x1$analysis$info0, x2$info0[1:3])
) %>%
  arrange(analysis) %>%
  group_by(analysis) %>%
  gt() %>%
  tab_style(
    style = list(cell_fill(color = "#d3edeb")),
    locations = cells_body(rows = `version` == "old")
  )
```
