---
title: "Power Calculation With Stratification Variables"
author: "Kaifeng Lu"
date: "12/15/2021"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Power Calculation With Stratification Variables}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

This R Markdown document illustrates the power calculation in the presence of stratification variables. This example is taken from EAST 6.4 section 56.7 on lung cancer patients comparing two treatment groups in a target patient population with some prior therapy. There are three stratification variables: 

- type of cancer cell (small, adeno, large, squamous)

- age in years (<=50, >50)

- performance status score (<=50, >50-<=70, >70)

We consider a three stage Lan-DeMets O'Brien-Fleming group sequential design. The stratum fractions are
```{r}
p1 = c(0.28, 0.13, 0.25, 0.34)
p2 = c(0.28, 0.72)
p3 = c(0.43, 0.37, 0.2)
stratumFraction = p1 %x% p2 %x% p3
```

Using the small cancer cell, age <=50, and performance status score <=50 as the reference stratum, the hazard ratios are
```{r}
theta1 = c(1, 2.127, 0.528, 0.413)
theta2 = c(1, 0.438)
theta3 = c(1, 0.614, 0.159)
```

If the hazard rate of the reference stratum is 0.009211, then the hazard rate for the control group is
```{r}
lambda2 = 0.009211*exp(log(theta1) %x% log(theta2) %x% log(theta3))
```

The hazard ratio of the active treatment group versus the control group is 0.4466. 

In addition, we assume an enrollment period of 24 months with a constant enrollment rate of 12 patients per month to enroll 288 patients, and the target number of events of 66. 

First we obtain the calendar time at which 66 events will occur. 
```{r}
library(lrstat)
caltime(nevents = 66, accrualDuration = 24, accrualIntensity = 12,
        stratumFraction = stratumFraction, 
        lambda1 = 0.4466*lambda2, lambda2 = lambda2, 
        followupTime = 100)
```

Therefore, the follow-up time for the last enrolled patient is 30.92 months. Now we can evaluate the power using the lrpower function. 
```{r}
lrpower(kMax = 3, 
        informationRates = c(0.333, 0.667, 1), 
        alpha = 0.025, typeAlphaSpending = "sfOF", 
        accrualIntensity = 12,
        stratumFraction = stratumFraction,
        lambda1 = 0.4466*lambda2, 
        lambda2 = lambda2, 
        accrualDuration = 24, 
        followupTime = 30.92)
```

Therefore, the overall power is about 88% for the stratified analysis. This is confirmed by the simulation below. 
```{r}
lrsim(kMax = 3, 
      informationTime = c(0.333, 0.667, 1), 
      criticalValues = c(3.710, 2.511, 1.993), 
      accrualIntensity = 12,
      stratumFraction = stratumFraction,
      lambda1 = 0.4466*lambda2, 
      lambda2 = lambda2, 
      accrualDuration = 24, 
      followupTime = 30.92,
      plannedEvents = c(22, 44, 66),
      maxNumberOfIterations = 1000, 
      seed = 314159)
```


