## -----------------------------------------------------------------------------
#| echo: true
library(rpact)
futStage1 <- 0.15
futStage2 <- 0.41

designFut <- getDesignGroupSequential(
    kMax = 3,
    alpha = 0.025,
    beta = 0.2,
    sided = 1,
    typeOfDesign = "asOF",
    informationRates = c(1 / 3, 2 / 3, 1),
    futilityBounds = c(futStage1, futStage2)
)
designFut


## -----------------------------------------------------------------------------
#| echo: true
designFut |> getSampleSizeSurvival(
    hazardRatio = 0.7,
    median2 = 12,
    dropoutRate1 = 0.05,
    dropoutRate2 = 0.05,
    dropoutTime = 12,
    accrualTime = 18,
    followUpTime = 12
) |> fetch(futilityBoundsEffectScale)


## -----------------------------------------------------------------------------
#| echo: true
targetHr1 <- 0.95
targetHr2 <- 0.90

fStage1 <- uniroot(
    f = function(z) {
        ssTmp <- getDesignGroupSequential(
            kMax = 3,
            alpha = 0.025,
            beta = 0.2,
            sided = 1,
            typeOfDesign = "asOF",
            informationRates = c(1 / 3, 2 / 3, 1),
            futilityBounds = c(z, -6)
        ) |>
            getSampleSizeSurvival(
                hazardRatio = 0.7,
                median2 = 12,
                dropoutRate1 = 0.05,
                dropoutRate2 = 0.05,
                dropoutTime = 12,
                accrualTime = 18,
                followUpTime = 12
            )

        futVals <- as.numeric(ssTmp$futilityBoundsEffectScale)

        futVals[1] - targetHr1
    },
    interval = c(-1, 1)
)$root

fStage2 <- uniroot(
    f = function(z) {
        ssTmp <- getDesignGroupSequential(
            kMax = 3,
            alpha = 0.025,
            beta = 0.2,
            sided = 1,
            typeOfDesign = "asOF",
            informationRates = c(1 / 3, 2 / 3, 1),
            futilityBounds = c(-6, z)
        ) |>
            getSampleSizeSurvival(
                hazardRatio = 0.7,
                median2 = 12,
                dropoutRate1 = 0.05,
                dropoutRate2 = 0.05,
                dropoutTime = 12,
                accrualTime = 18,
                followUpTime = 12
            )

        futVals <- as.numeric(ssTmp$futilityBoundsEffectScale)

        futVals[2] - targetHr2
    },
    interval = c(-1, 1)
)$root

fStage1
fStage2


## -----------------------------------------------------------------------------
#| echo: true
designGS <- getDesignGroupSequential(
    kMax = 3,
    alpha = 0.025,
    beta = 0.2,
    sided = 1,
    typeOfDesign = "asOF",
    informationRates = c(1 / 3, 2 / 3, 1),
    futilityBounds = c(fStage1, fStage2)
)
designGS |> summary()


## -----------------------------------------------------------------------------
#| echo: true
sampleSize <- designGS |> getSampleSizeSurvival(
    hazardRatio = 0.7,
    median2 = 12,
    dropoutRate1 = 0.05,
    dropoutRate2 = 0.05,
    dropoutTime = 12,
    accrualTime = 18,
    followUpTime = 12
)
sampleSize |> summary()


## -----------------------------------------------------------------------------
#| echo: true
nPerArmPerStage <- ceiling(sampleSize$numberOfSubjects[1, 1] / 2)
nApproxTotal <- 3 * 3 * nPerArmPerStage

nPerArmPerStage
nApproxTotal


## -----------------------------------------------------------------------------
#| echo: true
designIN <- getDesignInverseNormal(
    kMax = 3,
    alpha = 0.025,
    beta = 0.2,
    sided = 1,
    typeOfDesign = "asOF",
    informationRates = c(1 / 3, 2 / 3, 1),
    futilityBounds = c(fStage1, fStage2)
)
designIN


## -----------------------------------------------------------------------------
#| echo: true

# Just to get quick results here we choose this so low!
maxNumberOfIterations <- 100

hazardRatioMatrix <- matrix(
    c(
        1.00, 1.00,
        0.70, 0.70,
        0.75, 0.65
    ),
    nrow = 3,
    byrow = TRUE
)
hazardRatioMatrix

plannedEvents <- ceiling(c(sampleSize$cumulativeEventsPerStage[, 1]))
plannedEvents


## -----------------------------------------------------------------------------
#| echo: true
sim1 <- designIN |> getSimulationMultiArmSurvival(
    activeArms = 2,
    effectMatrix = hazardRatioMatrix,
    typeOfShape = "userDefined",
    plannedEvents = plannedEvents,
    maxNumberOfIterations = maxNumberOfIterations,
    directionUpper = FALSE,
    intersectionTest = "Simes",
    typeOfSelection = "best",
    effectMeasure = "effectEstimate",
    successCriterion = "all",
    seed = 12345
)

sim1 |> summary()


## -----------------------------------------------------------------------------
#| echo: true

sim2 <- designIN |> getSimulationMultiArmSurvival(
    activeArms = 2,
    effectMatrix = hazardRatioMatrix,
    typeOfShape = "userDefined",
    plannedEvents = plannedEvents,
    maxNumberOfIterations = maxNumberOfIterations,
    directionUpper = FALSE,
    intersectionTest = "Simes",
    typeOfSelection = "epsilon",
    # Select arms with hazard ratio not more than 0.2 worse than the best arm:
    epsilonValue = 0.2,
    effectMeasure = "effectEstimate",
    successCriterion = "all",
    seed = 12345
)

sim2 |> summary()


## -----------------------------------------------------------------------------
#| echo: true
simCompare <- data.frame(
    scenario = c("[1] null", "[2] both active", "[3] mixed"),
    rejectAtLeastOne_sim1 = sim1 |> fetch(rejectAtLeastOne),
    rejectAtLeastOne_sim2 = sim2 |> fetch(rejectAtLeastOne)
)
simCompare

