---
title: "DTR_Tutorial"
author: "Jun He, Jason Sinnwell"
output:
  rmarkdown::html_vignette:
    toc: yes
    toc_depth: 2
vignette: |
  %\VignetteIndexEntry{DTR_Tutorial}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
---



```{r, setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, tidy.opts=list(width.cutoff=80), comment=NA)
require(smartDesign)
```

SMART: Dynamic Treatment  (DTR)
========================================
The purpose of this developing this R package is to quantify and visualize the misclassification effect on mean/variance of dynamic treatment regimens (DTRs) and power comparasion of two DTRs in the setting of sequential multiple assigned randomized trial (SMART). 

This vignette describes the use cases of DTRs for
the *smart* package. Most of the trial settings and calculations are handled by the *smartDTR()* function, where the arguments and settings follow the terminology described in *smartSST()* function, and the formulae listed in the manuscript He et al (in preparation).

Based on typical two-stage SMART design, the first stage includes two initial treatments denoted as treatment A1 and A2. After making the responder/non-responder designation based on an intermediate outcome, participants are then re-randomized into one of treatments B1 to B8 in treatment stage II. There are then eight possible DTRs: A1B1B3, A1B1B4, A1B2B3, A1B2B4, A2B5B7, A2B5B8, A2B6B7, and A2B6B8. Therefore each DTR includes outcomes from both responder and non-reponder treatments.

Let mu_BsBt=E(Y│B=s or B=t) represents the expected mean response of the DTRs with corresponding variance sigsq_BsBt.  


DTR Calculations
===================

We run *smartDTR* for mean/variance/relative bias of DTRs (eg. B1B3, B2B4, B5B7). The calculations needed for one run of smartDTR are for two B-levels and the G1,G0 pairing for each B-level. For example, if we want to calculate the mean of B1B3, we will define Barm=c(1,3), then the first number in the setting of G1 represents B1G1, the second number in the setting of G1 represents B3G1, the first number in the setting of G0 represents the B1G0, and the second number in the setting of G0 represents B3G0.   

This example supposed that true responders who were correctly assigned into treatment B1 (B1G1) had a better response than true non-responders who were misclassified to B1 (B1G0), and the true non-responders who were correctly classified to treatment B3 (B3G0) had a better response than true responders who were misclassifed to B3 (B3G1).  

'pran_Barm' represents the probability that participants are assigned into corresponding Blevel. For example, if pran_Blevel=c(0.5, 0.6) under Barm=c(1,3), then the probability of being assigned to B1 group is 0.5, and the probability of being assigned to B3 is 0.6.

```{r, smartDTR13}
mumat13 <- cbind(G1=c(30,25), G0=c(20,22))
mumat13
varmat13 <- cbind(G1=c(16,16),G0=c(16,16))
varmat13
dtr13 <- smartDTR(mu_Barm=mumat13, sigsq_Barm=varmat13,
                 Barm=c(1,3), nsubject=252, pG_A1=0.8, pran_Barm=c(.5,.5))
dtr13$dtrdat  # last row shoul dhave sigsq just larger than to input
dtr13$true_mumix
dtr13$true_sigmix
```

This is an example of plotting the mean of B1B3 from the above example.

```{r, plotmeanDTR13}
plot.smartDTR(dtr13, metric="mean", xtype="spec", mar=c(4,4,4,6), legend.inset=c(-.2, 0))
plot(dtr13, metric="mean", relativeBias=TRUE, xtype="sens", mar=c(4,4,4,6), legend.inset=c(-.2, 0), ylim=c(0,10), xlab="Sens")

```

This is an example of plotting the variance of B1B3 from the above example.

```{r, plotDTRB}
plot.smartDTR(dtr13, metric="variance", xtype="spec", mar=c(4,4,4,6), legend.inset=c(-.2, 0))
plot.smartDTR(dtr13, metric="variance", xtype="sens", relativeBias=TRUE)
```


## Relative Bias plots
This is an example of plotting the relative mean and relative variance.
```{r, plotDTRbias}
plot.smartDTR(dtr13, metric="mean", relativeBias=TRUE, xtype="sens", mar=c(4,4,4,6), legend.inset=c(-.2, 0))
plot.smartDTR(dtr13, metric="variance", relativeBias=TRUE, xtype="sens", mar=c(4,4,4,6), legend.inset=c(-.2, 0))
```


## DTR Power comparison-with same initial treatment regimen

To evaluate the power comparison between two DTRs (eg.B1B3 versus B2B4, B1B3 verus B5B7), the calculated results of mean and variance from two DTRs are required. We will use two examples to show the misclassification effects on power based on targeted power of 0.8 (hline=0.8). 

The first example shows the power comparison with the same initial treatment regimen A1 (B1B3 versus B2B4). The printed results will include the z-statistics and power under different settings of sensitivity and specificity. 

```{r, dtrpower}
mumat13 <- cbind(G1=c(30,35), G0=c(20,28))
varmat13 <- cbind(G1=c(100,100),G0=c(100,100))
#mumat13 <- cbind(G1=c(30,25), G0=c(20,22))
#varmat13 <- cbind(G1=c(16,16),G0=c(16,16))

dtr13 <- smartDTR(mu_Barm=mumat13, sigsq_Barm=varmat13,
                 Barm=c(1,3), nsubject=252)
plot(dtr13, metric="mean", xtype="spec", relativeBias=TRUE)
plot(dtr13, metric="variance", xtype="sens", relativeBias=FALSE)
mumat24 <- cbind(G1=c(25,32), G0=c(18,23))
varmat24 <- cbind(G1=c(100,100),G0=c(100,100))
dtr24 <- smartDTR(mu_Barm=mumat24, sigsq_Barm=varmat24,
                 Barm=c(2,4), nsubject=252)
plot(dtr24)
pdtr13vs24 <- powerDTR(dtr13, dtr24)
names(pdtr13vs24)
#source("../R/powerDTR.R")
plot(pdtr13vs24,  mar=c(4,4,4,6), legend.inset=c(-.2, 0), hline=0.8)
plot(pdtr13vs24, alpha=0.05, xtype="sens", cex.lab=.5, cex.axis=.5)
```

## DTR Power comparison-with different initial treatment regimens

The second example shows the power comparison with the different initial treatment regimens (A1B1B3 versus A2B5B7).  

```{r, dtrpower2}
mumat13 <- cbind(G1=c(30,35), G0=c(20,10))
varmat13 <- cbind(G1=c(100,100),G0=c(100,100))
dtr13 <- smartDTR(mu_Barm=mumat13, sigsq_Barm=varmat13,
                 Barm=c(1,3), nsubject=252)

mumat57 <- cbind(G1=c(29,32), G0=c(18,9))
varmat57 <- cbind(G1=c(100,100),G0=c(100,100))
dtr57 <- smartDTR(mu_Barm=mumat57, sigsq_Barm=varmat57,
                 Barm=c(5,7), nsubject=252)

pdtr13vs57 <- powerDTR(dtr13, dtr57)
names(pdtr13vs57)

## Plot DTR Power

plot(pdtr13vs57, mar=c(4,4,4,6), cex.axis=.5, cex.lab=.5, legend.inset=c(-.2, 0), hline=0.8)
```
