---
title: "Design of the DEFUSE3 Trial"
author: "Tze Leung Lai, Philip W. Lavori, Olivia Liao, Ka Wai Tsang and Balasubramanian Narasimhan"
date: '`r Sys.Date()`'
bibliography: assistant.bib
output:
  html_document:
  theme: cerulean
  toc: yes
  toc_depth: 2
vignette: >
  %\VignetteIndexEntry{Design of the DEFUSE3 Trial}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r echo=F}
### get knitr just the way we like it

knitr::opts_chunk$set(
  message = FALSE,
  warning = FALSE,
  error = FALSE,
  tidy = FALSE,
  cache = FALSE
)
```

## Continuous Rankin Simulations

Here, we present the calculations for the initial design of the
DEFUSE3 trial based on [@Lai2014191] and [@Lai201593]. The trial
parameters are fixed as follows.


```{r}
library(ASSISTant)
##Fix randomization vector N, errors, eps
trialParameters <- list(N = c(200, 340, 476), type1Error = 0.025,
                        eps = 1/2, type2Error = 0.1)
```

The design parameters are the following for various scenarios.

```{r}
designParameters <- list(
    nul0 = list(prevalence = rep(1/6, 6), mean = matrix(0, 2, 6),
                sd = matrix(1, 2, 6)),
    alt1 = list(prevalence = rep(1/6, 6), mean = rbind(rep(0, 6),
                                                       c(0.5, 0.4, 0.3, 0, 0, 0)),
                sd = matrix(1, 2, 6)),
    alt2 = list(prevalence = rep(1/6, 6), mean = rbind(rep(0, 6),
                                                     c(0.5, 0.5, 0, 0, 0, 0)),
                sd = matrix(1,2, 6)),
    alt3 = list(prevalence = rep(1/6, 6), mean = rbind(rep(0, 6), rep(0.36, 6)),
                sd = matrix(1,2, 6)),
    alt4 = list(prevalence = rep(1/6, 6), mean = rbind(rep(0, 6), rep(0.30, 6)),
                sd = matrix(1,2, 6)),
    alt5 = list(prevalence = rep(1/6, 6), mean = rbind(rep(0, 6),
                                                       c(0.4, 0.3, 0.2, 0, 0, 0)),
                sd = matrix(1,2, 6)),
    alt6 = list(prevalence = rep(1/6, 6), mean = rbind(rep(0, 6),
                                                       c(0.5, 0.5, 0.3, 0.3, 0.1, 0.1)),
                sd = matrix(1,2, 6))
)
```

### The NULL Scenario

```{r}
defuse3 <- DEFUSE3Design$new(trialParameters = trialParameters,
                             numberOfSimulations = 500,
                             designParameters = designParameters$nul0,
                             showProgress = FALSE)
print(defuse3)
```

```{r}
result <- defuse3$explore(numberOfSimulations = 500,
                          showProgress = FALSE,
                          rngSeed = 28912)
analysis <- defuse3$analyze(result)
print(defuse3$summary(analysis))
```

### The ALT1 Scenario

```{r}
result1 <- defuse3$explore(numberOfSimulations = 500,
                           trueParameters = designParameters$alt1,
                           showProgress = FALSE,
                           rngSeed = 737218)
analysis1 <- defuse3$analyze(result1)
print(defuse3$summary(analysis1))
```

### The ALT2 Scenario

```{r}
result2 <- defuse3$explore(numberOfSimulations = 500,
                           trueParameters = designParameters$alt2,
                           showProgress = FALSE,
                          rngSeed = 928812)
analysis2 <- defuse3$analyze(result2)
print(defuse3$summary(analysis2))
```

## Discrete Rankin Simulations

### The Discretized Scenarios 

The discretized scenarios are designed to generally mimic the trends
above in the alternatives. However, we have a problem: we cannot
simulatenously match the mean and sd of the alternatives
above. (Actually, we can, but not with Rankin scores 0 through 6. The
software can easily be modified to generate discrete values where the
values are 0 to 6 divided by the standard deviation of the respective
distribution, for example.)  

_Also in future versions, I need to allow for more general support
values for the scores, not just 0 through 6. Easy to do, but not done
yet._


Some types of distributions:

```{r}
null.uniform <- rep(1, 7L) ## uniform on 7 support points
hourglass <- c(1, 2, 2, 1, 2, 2, 1)
inverted.hourglass <- c(2, 1, 1, 2, 1, 1, 2)
bottom.heavy <- c(2, 2, 2, 1, 1, 1, 1)
bottom.heavier <- c(3, 3, 2, 2, 1, 1, 1)
bottom.loaded <- c(4, 4, 3, 3, 2, 1, 1)
top.heavy <- c(1, 1, 1, 1, 2, 2, 2)
top.heavier <- c(1, 1, 1, 2, 2, 3, 3)
top.loaded <- c(1, 1, 2, 3, 3, 4, 4)
```

It is instructive to see what the means and standard deviations are. 

```{r}
```{r}
knitr::kable(
           sapply(list(null = null.uniform,
                       hourglass = hourglass,
                       inv.hourglass = inverted.hourglass,
                       bot.heavy = bottom.heavy,
                       bot.heavier = bottom.heavier,
                       bot.loaded = bottom.loaded,
                       top.heavy = top.heavy,
                       top.heavier = top.heavier,
                       top.loaded = top.loaded),
                  computeMeanAndSD)
       )
```

With this in mind, we can reel off some runs. Phil, you mentioned you
wanted $J = 2$, which I adhere to, below.

```{r}
designParameters <- list(
    nul0 = list(prevalence = rep(1, 2),
                ctlDist = null.uniform,
                trtDist = cbind(null.uniform,
                                null.uniform)),
    alt1 = list(prevalence = rep(1, 2), 
                ctlDist = null.uniform,
                trtDist = cbind(top.loaded,
                                null.uniform)),
    alt2 = list(prevalence = rep(1, 2), 
                ctlDist = null.uniform,
                trtDist = cbind(null.uniform,
                                top.loaded))
)
```

### The NULL Scenario

```{r}
discDefuse3 <- DEFUSE3Design$new(trialParameters = trialParameters,
                                 numberOfSimulations = 5000,
                                 discreteData = TRUE,
                                 designParameters = designParameters$nul0,
                                 showProgress = FALSE)
print(discDefuse3)
```

```{r}
result <- discDefuse3$explore(numberOfSimulations = 50,
                              showProgress = FALSE,
                              rngSeed = 3783)
analysis <- discDefuse3$analyze(result)
print(discDefuse3$summary(analysis))
```

### The ALT1 Scenario

```{r}
result1 <- discDefuse3$explore(numberOfSimulations = 50,
                               trueParameters = designParameters$alt1,
                               showProgress = FALSE,
                               rngSeed = 28912)
analysis1 <- discDefuse3$analyze(result1)
print(discDefuse3$summary(analysis1))
```

### The ALT2 Scenario

```{r}
result2 <- discDefuse3$explore(numberOfSimulations = 50,
                               trueParameters = designParameters$alt2,
                               showProgress = FALSE,
                               rngSeed = 931)
analysis2 <- discDefuse3$analyze(result2)
print(discDefuse3$summary(analysis2))
```

## References
