Skip to contents

[Stable]

A function that computes the recommended next best dose based on the corresponding rule nextBest, the posterior samples from the model and the underlying data.

Usage

nextBest(nextBest, doselimit, samples, model, data, ...)

# S4 method for class 'NextBestMTD,numeric,Samples,GeneralModel,Data'
nextBest(nextBest, doselimit = Inf, samples, model, data, ...)

# S4 method for class 'NextBestNCRM,numeric,Samples,GeneralModel,Data'
nextBest(nextBest, doselimit = Inf, samples, model, data, ...)

# S4 method for class 'NextBestNCRM,numeric,Samples,GeneralModel,DataParts'
nextBest(nextBest, doselimit = Inf, samples, model, data, ...)

# S4 method for class 'NextBestNCRMLoss,numeric,Samples,GeneralModel,Data'
nextBest(nextBest, doselimit = Inf, samples, model, data, ...)

# S4 method for class 'NextBestThreePlusThree,missing,missing,missing,Data'
nextBest(nextBest, doselimit, samples, model, data, ...)

# S4 method for class 'NextBestDualEndpoint,numeric,Samples,DualEndpoint,Data'
nextBest(nextBest, doselimit = Inf, samples, model, data, ...)

# S4 method for class 'NextBestMinDist,numeric,Samples,GeneralModel,Data'
nextBest(nextBest, doselimit = Inf, samples, model, data, ...)

# S4 method for class 'NextBestInfTheory,numeric,Samples,GeneralModel,Data'
nextBest(nextBest, doselimit = Inf, samples, model, data, ...)

# S4 method for class 'NextBestTD,numeric,missing,LogisticIndepBeta,Data'
nextBest(nextBest, doselimit = Inf, model, data, in_sim = FALSE, ...)

# S4 method for class 'NextBestTDsamples,numeric,Samples,LogisticIndepBeta,Data'
nextBest(nextBest, doselimit = Inf, samples, model, data, in_sim, ...)

# S4 method for class 'NextBestMaxGain,numeric,missing,ModelTox,DataDual'
nextBest(
  nextBest,
  doselimit = Inf,
  model,
  data,
  model_eff,
  in_sim = FALSE,
  ...
)

# S4 method for class 'NextBestMaxGainSamples,numeric,Samples,ModelTox,DataDual'
nextBest(
  nextBest,
  doselimit = Inf,
  samples,
  model,
  data,
  model_eff,
  samples_eff,
  in_sim = FALSE,
  ...
)

# S4 method for class 'NextBestProbMTDLTE,numeric,Samples,GeneralModel,Data'
nextBest(nextBest, doselimit, samples, model, data, ...)

# S4 method for class 'NextBestProbMTDMinDist,numeric,Samples,GeneralModel,Data'
nextBest(nextBest, doselimit, samples, model, data, ...)

# S4 method for class 'NextBestOrdinal,numeric,Samples,GeneralModel,Data'
nextBest(nextBest, doselimit = Inf, samples, model, data, ...)

# S4 method for class 'NextBestOrdinal,numeric,Samples,LogisticLogNormalOrdinal,DataOrdinal'
nextBest(nextBest, doselimit = Inf, samples, model, data, ...)

Arguments

nextBest

(NextBest)
the rule for the next best dose.

doselimit

(number)
the maximum allowed next dose. If it is an infinity (default), then essentially no dose limit will be applied in the course of dose recommendation calculation.

samples

(Samples)
posterior samples from model parameters given data.

model

(ModelTox)
the DLT model.

data

(Data)
data that was used to generate the samples.

...

additional arguments without method dispatch.

in_sim

(flag)
is this method used in simulations? Default as FALSE. If this flag is TRUE and target dose estimates (during trial and end-of-trial) are outside of the dose grid range, the information message is printed by this method.

model_eff

(Effloglog or EffFlexi)
the efficacy model.

samples_eff

(Samples)
posterior samples from model_eff parameters given data.

Value

A list with the next best dose recommendation (element named value) from the grid defined in data, and a plot depicting this recommendation (element named plot). In case of multiple plots also an element named singlePlots is included. The singlePlots is itself a list with single plots. An additional list with elements describing the outcome of the rule can be contained too.

Functions

  • nextBest( nextBest = NextBestMTD, doselimit = numeric, samples = Samples, model = GeneralModel, data = Data ): find the next best dose based on the MTD rule.

  • nextBest( nextBest = NextBestNCRM, doselimit = numeric, samples = Samples, model = GeneralModel, data = Data ): find the next best dose based on the NCRM method. The additional element probs in the output's list contains the target and overdosing probabilities (across all doses in the dose grid) used in the derivation of the next best dose.

  • nextBest( nextBest = NextBestNCRM, doselimit = numeric, samples = Samples, model = GeneralModel, data = DataParts ): find the next best dose based on the NCRM method when two parts trial is used.

  • nextBest( nextBest = NextBestNCRMLoss, doselimit = numeric, samples = Samples, model = GeneralModel, data = Data ): find the next best dose based on the NCRM method and loss function.

  • nextBest( nextBest = NextBestThreePlusThree, doselimit = missing, samples = missing, model = missing, data = Data ): find the next best dose based on the 3+3 method.

  • nextBest( nextBest = NextBestDualEndpoint, doselimit = numeric, samples = Samples, model = DualEndpoint, data = Data ): find the next best dose based on the dual endpoint model. The additional list element probs contains the target and overdosing probabilities (across all doses in the dose grid) used in the derivation of the next best dose.

  • nextBest( nextBest = NextBestMinDist, doselimit = numeric, samples = Samples, model = GeneralModel, data = Data ): gives the dose which is below the dose limit and has an estimated DLT probability which is closest to the target dose.

  • nextBest( nextBest = NextBestInfTheory, doselimit = numeric, samples = Samples, model = GeneralModel, data = Data ): gives the appropriate dose within an information theoretic framework.

  • nextBest( nextBest = NextBestTD, doselimit = numeric, samples = missing, model = LogisticIndepBeta, data = Data ): find the next best dose based only on the DLT responses and for LogisticIndepBeta model class object without DLT samples.

  • nextBest( nextBest = NextBestTDsamples, doselimit = numeric, samples = Samples, model = LogisticIndepBeta, data = Data ): find the next best dose based only on the DLT responses and for LogisticIndepBeta model class object involving DLT samples.

  • nextBest( nextBest = NextBestMaxGain, doselimit = numeric, samples = missing, model = ModelTox, data = DataDual ): find the next best dose based only on pseudo DLT model ModelTox and Effloglog efficacy model without samples.

  • nextBest( nextBest = NextBestMaxGainSamples, doselimit = numeric, samples = Samples, model = ModelTox, data = DataDual ): find the next best dose based on DLT and efficacy responses with DLT and efficacy samples.

  • nextBest( nextBest = NextBestProbMTDLTE, doselimit = numeric, samples = Samples, model = GeneralModel, data = Data ): find the next best dose based with the highest probability of having a toxicity rate less or equal to the target toxicity level.

  • nextBest( nextBest = NextBestProbMTDMinDist, doselimit = numeric, samples = Samples, model = GeneralModel, data = Data ): find the next best dose based with the highest probability of having a toxicity rate with minimum distance to the target toxicity level.

  • nextBest( nextBest = NextBestOrdinal, doselimit = numeric, samples = Samples, model = GeneralModel, data = Data ): find the next best dose for ordinal CRM models.

  • nextBest( nextBest = NextBestOrdinal, doselimit = numeric, samples = Samples, model = LogisticLogNormalOrdinal, data = DataOrdinal ): find the next best dose for ordinal CRM models.

Examples

# Example of usage for `NextBestMTD` NextBest class.

# Create the data.
my_data <- Data(
  x = c(0.1, 0.5, 1.5, 3, 6, 10, 10, 10),
  y = c(0, 0, 0, 0, 0, 0, 1, 0),
  ID = 1:8,
  cohort = c(0, 1, 2, 3, 4, 5, 5, 5),
  doseGrid = c(0.1, 0.5, 1.5, 3, 6, seq(from = 10, to = 80, by = 2))
)

# Initialize the CRM model used to model the data.
my_model <- LogisticLogNormal(
  mean = c(-0.85, 1),
  cov = matrix(c(1, -0.5, -0.5, 1), nrow = 2),
  ref_dose = 56
)

# Set-up some MCMC parameters and generate samples from the posterior.
my_options <- McmcOptions(burnin = 100, step = 2, samples = 500)
my_samples <- mcmc(my_data, my_model, my_options)

# Define the rule for dose increments and calculate the maximum dose allowed.
my_increments <- IncrementsRelative(
  intervals = c(0, 20),
  increments = c(1, 0.33)
)
next_max_dose <- maxDose(my_increments, data = my_data)

# Define the rule which will be used to select the next best dose
# based on the 'NextBestMTD' class.
mtd_next_best <- NextBestMTD(
  target = 0.33,
  derive = function(mtd_samples) {
    quantile(mtd_samples, probs = 0.25)
  }
)

# Calculate the next best dose.
dose_recommendation <- nextBest(
  nextBest = mtd_next_best,
  doselimit = next_max_dose,
  samples = my_samples,
  model = my_model,
  data = my_data
)

# Example of usage for `NextBestNCRM` NextBest class.

# Create the data.
my_data <- Data(
  x = c(0.1, 0.5, 1.5, 3, 6, 10, 10, 10),
  y = c(0, 0, 0, 0, 0, 0, 1, 0),
  ID = 1:8,
  cohort = c(0, 1, 2, 3, 4, 5, 5, 5),
  doseGrid = c(0.1, 0.5, 1.5, 3, 6, seq(from = 10, to = 80, by = 2))
)

# Initialize the CRM model used to model the data.
my_model <- LogisticLogNormal(
  mean = c(-0.85, 1),
  cov = matrix(c(1, -0.5, -0.5, 1), nrow = 2),
  ref_dose = 56
)

# Set-up some MCMC parameters and generate samples from the posterior.
my_options <- McmcOptions(burnin = 100, step = 2, samples = 500)
my_samples <- mcmc(my_data, my_model, my_options)

# Define the rule for dose increments and calculate the maximum dose allowed.
my_increments <- IncrementsRelative(
  intervals = c(0, 20),
  increments = c(1, 0.33)
)
next_max_dose <- maxDose(my_increments, data = my_data)

# Define the rule which will be used to select the next best dose
# based on the 'NextBestNCRM' class.
nrcm_next_best <- NextBestNCRM(
  target = c(0.2, 0.35),
  overdose = c(0.35, 1),
  max_overdose_prob = 0.25
)

# Calculate the next best dose.
dose_recommendation <- nextBest(
  nextBest = nrcm_next_best,
  doselimit = next_max_dose,
  samples = my_samples,
  model = my_model,
  data = my_data
)

# See the probabilities.
dose_recommendation$probs
#>       dose target overdose
#>  [1,]  0.1  0.000    0.004
#>  [2,]  0.5  0.006    0.004
#>  [3,]  1.5  0.012    0.008
#>  [4,]  3.0  0.036    0.008
#>  [5,]  6.0  0.118    0.012
#>  [6,] 10.0  0.206    0.038
#>  [7,] 12.0  0.228    0.068
#>  [8,] 14.0  0.288    0.112
#>  [9,] 16.0  0.314    0.138
#> [10,] 18.0  0.356    0.174
#> [11,] 20.0  0.340    0.226
#> [12,] 22.0  0.412    0.252
#> [13,] 24.0  0.424    0.264
#> [14,] 26.0  0.418    0.304
#> [15,] 28.0  0.386    0.346
#> [16,] 30.0  0.402    0.394
#> [17,] 32.0  0.410    0.442
#> [18,] 34.0  0.388    0.470
#> [19,] 36.0  0.356    0.522
#> [20,] 38.0  0.326    0.570
#> [21,] 40.0  0.320    0.590
#> [22,] 42.0  0.324    0.606
#> [23,] 44.0  0.270    0.662
#> [24,] 46.0  0.256    0.684
#> [25,] 48.0  0.228    0.712
#> [26,] 50.0  0.216    0.736
#> [27,] 52.0  0.188    0.766
#> [28,] 54.0  0.160    0.798
#> [29,] 56.0  0.156    0.812
#> [30,] 58.0  0.148    0.820
#> [31,] 60.0  0.142    0.826
#> [32,] 62.0  0.106    0.870
#> [33,] 64.0  0.104    0.872
#> [34,] 66.0  0.106    0.880
#> [35,] 68.0  0.110    0.880
#> [36,] 70.0  0.100    0.890
#> [37,] 72.0  0.100    0.890
#> [38,] 74.0  0.100    0.894
#> [39,] 76.0  0.076    0.918
#> [40,] 78.0  0.068    0.926
#> [41,] 80.0  0.066    0.928

# Example of usage for `NextBestNCRM-DataParts` NextBest class.

# Create the data.
my_data <- DataParts(
  x = c(0.1, 0.5, 1.5),
  y = c(0, 0, 0),
  ID = 1:3,
  cohort = 1:3,
  doseGrid = c(0.1, 0.5, 1.5, 3, 6, seq(from = 10, to = 80, by = 2)),
  part = c(1L, 1L, 1L),
  nextPart = 1L,
  part1Ladder = c(0.1, 0.5, 1.5, 3, 6, 10)
)

# Initialize the CRM model used to model the data.
my_model <- LogisticLogNormal(
  mean = c(-0.85, 1),
  cov = matrix(c(1, -0.5, -0.5, 1), nrow = 2),
  ref_dose = 56
)

# Set-up some MCMC parameters and generate samples from the posterior.
my_options <- McmcOptions(burnin = 100, step = 2, samples = 500)
my_samples <- mcmc(my_data, my_model, my_options)

# Define the rule for dose increments and calculate the maximum dose allowed.
my_increments <- IncrementsRelativeParts(
  dlt_start = 0,
  clean_start = 1
)
next_max_dose <- maxDose(my_increments, data = my_data)

# Define the rule which will be used to select the next best dose
# based on the 'NextBestNCRM' class.
nrcm_next_best <- NextBestNCRM(
  target = c(0.2, 0.35),
  overdose = c(0.35, 1),
  max_overdose_prob = 0.25
)

# Calculate the next best dose.
dose_recommendation <- nextBest(
  nextBest = nrcm_next_best,
  doselimit = next_max_dose,
  samples = my_samples,
  model = my_model,
  data = my_data
)

dose_recommendation
#> $value
#> [1] 3
#> 
#> $plot
#> NULL
#> 

# Example of usage for `NextBestNCRMLoss` NextBest class.

# Create the data.
my_data <- Data(
  x = c(0.1, 0.5, 1.5, 3, 6, 10, 10, 10),
  y = c(0, 0, 0, 0, 0, 0, 1, 0),
  ID = 1:8,
  cohort = c(0, 1, 2, 3, 4, 5, 5, 5),
  doseGrid = c(0.1, 0.5, 1.5, 3, 6, seq(from = 10, to = 80, by = 2))
)

# Initialize the CRM model used to model the data.
my_model <- LogisticLogNormal(
  mean = c(-0.85, 1),
  cov = matrix(c(1, -0.5, -0.5, 1), nrow = 2),
  ref_dose = 56
)

# Set-up some MCMC parameters and generate samples from the posterior.
my_options <- McmcOptions(burnin = 100, step = 2, samples = 500)
my_samples <- mcmc(my_data, my_model, my_options)

# Define the rule for dose increments and calculate the maximum dose allowed.
my_increments <- IncrementsRelative(
  intervals = c(0, 20),
  increments = c(1, 0.33)
)
next_max_dose <- maxDose(my_increments, data = my_data)

# Define the rule which will be used to select the next best dose
# based on the class `NextBestNCRMLoss`.
nrcm_loss_next_best <- NextBestNCRMLoss(
  target = c(0.2, 0.35),
  overdose = c(0.35, 0.6),
  unacceptable = c(0.6, 1),
  max_overdose_prob = 0.999,
  losses = c(1, 0, 1, 2)
)

# Calculate the next best dose.
dose_recommendation <- nextBest(
  nextBest = nrcm_loss_next_best,
  doselimit = next_max_dose,
  samples = my_samples,
  model = my_model,
  data = my_data
)

# Next best dose.
dose_recommendation$value
#> [1] 20

# Look at the probabilities.
dose_recommendation$probs
#>     dose underdosing target excessive unacceptable       mean    std_dev
#> 0.1  0.1       0.986  0.014     0.000        0.000 0.01259909 0.03483179
#> 0.5  0.5       0.986  0.014     0.000        0.000 0.02460117 0.04982256
#> 1.5  1.5       0.964  0.022     0.014        0.000 0.04257593 0.06559018
#> 3    3.0       0.916  0.070     0.014        0.000 0.06304285 0.07945253
#> 6    6.0       0.840  0.138     0.022        0.000 0.09710005 0.09686174
#> 10  10.0       0.750  0.212     0.038        0.000 0.13686153 0.11130500
#> 12  12.0       0.722  0.196     0.082        0.000 0.15538111 0.11653392
#> 14  14.0       0.652  0.236     0.110        0.002 0.17321956 0.12089823
#> 16  16.0       0.604  0.274     0.120        0.002 0.19045976 0.12460510
#> 18  18.0       0.548  0.300     0.150        0.002 0.20715402 0.12780390
#> 20  20.0       0.474  0.330     0.190        0.006 0.22333742 0.13060530
#> 22  22.0       0.446  0.348     0.200        0.006 0.23903501 0.13309203
#> 24  24.0       0.378  0.396     0.212        0.014 0.25426602 0.13532596
#> 26  26.0       0.338  0.378     0.270        0.014 0.26904628 0.13735315
#> 28  28.0       0.302  0.380     0.304        0.014 0.28338964 0.13920799
#> 30  30.0       0.272  0.378     0.336        0.014 0.29730882 0.14091660
#> 32  32.0       0.236  0.368     0.380        0.016 0.31081579 0.14249960
#> 34  34.0       0.218  0.354     0.412        0.016 0.32392198 0.14397422
#> 36  36.0       0.198  0.324     0.460        0.018 0.33663833 0.14535574
#> 38  38.0       0.158  0.340     0.484        0.018 0.34897530 0.14665832
#> 40  40.0       0.146  0.330     0.496        0.028 0.36094286 0.14789517
#> 42  42.0       0.136  0.322     0.502        0.040 0.37255054 0.14907833
#> 44  44.0       0.130  0.282     0.522        0.066 0.38380750 0.15021828
#> 46  46.0       0.116  0.264     0.542        0.078 0.39472264 0.15132352
#> 48  48.0       0.114  0.224     0.580        0.082 0.40530467 0.15240035
#> 50  50.0       0.106  0.228     0.572        0.094 0.41556229 0.15345289
#> 52  52.0       0.098  0.216     0.576        0.110 0.42550421 0.15448334
#> 54  54.0       0.094  0.196     0.570        0.140 0.43513923 0.15549232
#> 56  56.0       0.066  0.196     0.570        0.168 0.44447624 0.15647937
#> 58  58.0       0.060  0.180     0.584        0.176 0.45352425 0.15744335
#> 60  60.0       0.058  0.164     0.594        0.184 0.46229231 0.15838282
#> 62  62.0       0.056  0.160     0.580        0.204 0.47078950 0.15929628
#> 64  64.0       0.054  0.150     0.554        0.242 0.47902488 0.16018237
#> 66  66.0       0.048  0.152     0.524        0.276 0.48700743 0.16103997
#> 68  68.0       0.048  0.146     0.506        0.300 0.49474602 0.16186828
#> 70  70.0       0.048  0.146     0.474        0.332 0.50224934 0.16266678
#> 72  72.0       0.046  0.144     0.472        0.338 0.50952592 0.16343526
#> 74  74.0       0.038  0.144     0.472        0.346 0.51658406 0.16417376
#> 76  76.0       0.038  0.132     0.466        0.364 0.52343181 0.16488257
#> 78  78.0       0.036  0.128     0.468        0.368 0.53007698 0.16556213
#> 80  80.0       0.034  0.128     0.446        0.392 0.53652712 0.16621308
#>     posterior_loss
#> 0.1          0.986
#> 0.5          0.986
#> 1.5          0.978
#> 3            0.930
#> 6            0.862
#> 10           0.788
#> 12           0.804
#> 14           0.766
#> 16           0.728
#> 18           0.702
#> 20           0.676
#> 22           0.658
#> 24           0.618
#> 26           0.636
#> 28           0.634
#> 30           0.636
#> 32           0.648
#> 34           0.662
#> 36           0.694
#> 38           0.678
#> 40           0.698
#> 42           0.718
#> 44           0.784
#> 46           0.814
#> 48           0.858
#> 50           0.866
#> 52           0.894
#> 54           0.944
#> 56           0.972
#> 58           0.996
#> 60           1.020
#> 62           1.044
#> 64           1.092
#> 66           1.124
#> 68           1.154
#> 70           1.186
#> 72           1.194
#> 74           1.202
#> 76           1.232
#> 78           1.240
#> 80           1.264

# Define another rule (loss function of 3 elements).
nrcm_loss_next_best_losses_3 <- NextBestNCRMLoss(
  target = c(0.2, 0.35),
  overdose = c(0.35, 1),
  max_overdose_prob = 0.30,
  losses = c(1, 0, 2)
)

# Calculate the next best dose.
dose_recommendation_losses_3 <- nextBest(
  nextBest = nrcm_loss_next_best_losses_3,
  doselimit = next_max_dose,
  samples = my_samples,
  model = my_model,
  data = my_data
)

# Next best dose.
dose_recommendation_losses_3$value
#> [1] 10

# Look at the probabilities.
dose_recommendation_losses_3$probs
#>     dose underdosing target overdose       mean    std_dev posterior_loss
#> 0.1  0.1       0.986  0.014    0.000 0.01259909 0.03483179          0.986
#> 0.5  0.5       0.986  0.014    0.000 0.02460117 0.04982256          0.986
#> 1.5  1.5       0.964  0.022    0.014 0.04257593 0.06559018          0.992
#> 3    3.0       0.916  0.070    0.014 0.06304285 0.07945253          0.944
#> 6    6.0       0.840  0.138    0.022 0.09710005 0.09686174          0.884
#> 10  10.0       0.750  0.212    0.038 0.13686153 0.11130500          0.826
#> 12  12.0       0.722  0.196    0.082 0.15538111 0.11653392          0.886
#> 14  14.0       0.652  0.236    0.112 0.17321956 0.12089823          0.876
#> 16  16.0       0.604  0.274    0.122 0.19045976 0.12460510          0.848
#> 18  18.0       0.548  0.300    0.152 0.20715402 0.12780390          0.852
#> 20  20.0       0.474  0.330    0.196 0.22333742 0.13060530          0.866
#> 22  22.0       0.446  0.348    0.206 0.23903501 0.13309203          0.858
#> 24  24.0       0.378  0.396    0.226 0.25426602 0.13532596          0.830
#> 26  26.0       0.338  0.378    0.284 0.26904628 0.13735315          0.906
#> 28  28.0       0.302  0.380    0.318 0.28338964 0.13920799          0.938
#> 30  30.0       0.272  0.378    0.350 0.29730882 0.14091660          0.972
#> 32  32.0       0.236  0.368    0.396 0.31081579 0.14249960          1.028
#> 34  34.0       0.218  0.354    0.428 0.32392198 0.14397422          1.074
#> 36  36.0       0.198  0.324    0.478 0.33663833 0.14535574          1.154
#> 38  38.0       0.158  0.340    0.502 0.34897530 0.14665832          1.162
#> 40  40.0       0.146  0.330    0.524 0.36094286 0.14789517          1.194
#> 42  42.0       0.136  0.322    0.542 0.37255054 0.14907833          1.220
#> 44  44.0       0.130  0.282    0.588 0.38380750 0.15021828          1.306
#> 46  46.0       0.116  0.264    0.620 0.39472264 0.15132352          1.356
#> 48  48.0       0.114  0.224    0.662 0.40530467 0.15240035          1.438
#> 50  50.0       0.106  0.228    0.666 0.41556229 0.15345289          1.438
#> 52  52.0       0.098  0.216    0.686 0.42550421 0.15448334          1.470
#> 54  54.0       0.094  0.196    0.710 0.43513923 0.15549232          1.514
#> 56  56.0       0.066  0.196    0.738 0.44447624 0.15647937          1.542
#> 58  58.0       0.060  0.180    0.760 0.45352425 0.15744335          1.580
#> 60  60.0       0.058  0.164    0.778 0.46229231 0.15838282          1.614
#> 62  62.0       0.056  0.160    0.784 0.47078950 0.15929628          1.624
#> 64  64.0       0.054  0.150    0.796 0.47902488 0.16018237          1.646
#> 66  66.0       0.048  0.152    0.800 0.48700743 0.16103997          1.648
#> 68  68.0       0.048  0.146    0.806 0.49474602 0.16186828          1.660
#> 70  70.0       0.048  0.146    0.806 0.50224934 0.16266678          1.660
#> 72  72.0       0.046  0.144    0.810 0.50952592 0.16343526          1.666
#> 74  74.0       0.038  0.144    0.818 0.51658406 0.16417376          1.674
#> 76  76.0       0.038  0.132    0.830 0.52343181 0.16488257          1.698
#> 78  78.0       0.036  0.128    0.836 0.53007698 0.16556213          1.708
#> 80  80.0       0.034  0.128    0.838 0.53652712 0.16621308          1.710

# Example of usage for `NextBestThreePlusThree` NextBest class.

# Create the data.
my_data <- Data(
  x = c(5, 5, 5, 10, 10, 10),
  y = c(0, 0, 0, 0, 1, 0),
  ID = 1:6,
  cohort = c(0, 0, 0, 1, 1, 1),
  doseGrid = c(0.1, 0.5, 1.5, 3, 5, seq(from = 10, to = 80, by = 2))
)

# The rule to select the next best dose will be based on the 3+3 method.
my_next_best <- NextBestThreePlusThree()

# Calculate the next best dose.
dose_recommendation <- nextBest(my_next_best, data = my_data)
# Example of usage for `NextBestDualEndpoint` NextBest class.

# Create the data.
my_data <- DataDual(
  x = c(0.1, 0.5, 1.5, 3, 6, 10, 10, 10, 20, 20, 20, 40, 40, 40, 50, 50, 50),
  y = c(0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1),
  ID = 1:17,
  cohort = c(1L, 2L, 3L, 4L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L),
  w = c(
    0.31, 0.42, 0.59, 0.45, 0.6, 0.7, 0.55, 0.6, 0.52, 0.54,
    0.56, 0.43, 0.41, 0.39, 0.34, 0.38, 0.21
  ),
  doseGrid = c(0.1, 0.5, 1.5, 3, 6, seq(from = 10, to = 80, by = 2))
)

# Initialize the Dual-Endpoint model (in this case RW1).
my_model <- DualEndpointRW(
  mean = c(0, 1),
  cov = matrix(c(1, 0, 0, 1), nrow = 2),
  sigma2betaW = 0.01,
  sigma2W = c(a = 0.1, b = 0.1),
  rho = c(a = 1, b = 1),
  rw1 = TRUE
)

# Set-up some MCMC parameters and generate samples from the posterior.
my_options <- McmcOptions(burnin = 100, step = 2, samples = 500)
my_samples <- mcmc(my_data, my_model, my_options)

# Define the rule for dose increments and calculate the maximum dose allowed.
my_increments <- IncrementsRelative(
  intervals = c(0, 20),
  increments = c(1, 0.33)
)
next_max_dose <- maxDose(my_increments, data = my_data)

# Define the rule which will be used to select the next best dose. In this case,
# target a dose achieving at least 0.9 of maximum biomarker level (efficacy)
# and with a probability below 0.25 that prob(DLT)>0.35 (safety).
de_next_best <- NextBestDualEndpoint(
  target = c(0.9, 1),
  overdose = c(0.35, 1),
  max_overdose_prob = 0.25
)

# Calculate the next best dose.
dose_recommendation <- nextBest(
  nextBest = de_next_best,
  doselimit = next_max_dose,
  samples = my_samples,
  model = my_model,
  data = my_data
)

# See the probabilities.
dose_recommendation$probs
#>       dose target overdose
#>  [1,]  0.1  0.004    0.000
#>  [2,]  0.5  0.008    0.000
#>  [3,]  1.5  0.028    0.004
#>  [4,]  3.0  0.024    0.004
#>  [5,]  6.0  0.080    0.022
#>  [6,] 10.0  0.100    0.038
#>  [7,] 12.0  0.108    0.068
#>  [8,] 14.0  0.066    0.190
#>  [9,] 16.0  0.062    0.314
#> [10,] 18.0  0.046    0.490
#> [11,] 20.0  0.002    0.584
#> [12,] 22.0  0.052    0.650
#> [13,] 24.0  0.058    0.764
#> [14,] 26.0  0.020    0.860
#> [15,] 28.0  0.004    0.942
#> [16,] 30.0  0.014    0.956
#> [17,] 32.0  0.014    0.962
#> [18,] 34.0  0.004    0.974
#> [19,] 36.0  0.006    0.974
#> [20,] 38.0  0.006    0.978
#> [21,] 40.0  0.000    0.984
#> [22,] 42.0  0.000    0.990
#> [23,] 44.0  0.000    0.992
#> [24,] 46.0  0.002    0.992
#> [25,] 48.0  0.000    0.992
#> [26,] 50.0  0.000    0.992
#> [27,] 52.0  0.000    0.992
#> [28,] 54.0  0.000    0.992
#> [29,] 56.0  0.008    0.992
#> [30,] 58.0  0.012    0.996
#> [31,] 60.0  0.006    0.996
#> [32,] 62.0  0.010    0.996
#> [33,] 64.0  0.014    0.996
#> [34,] 66.0  0.012    0.996
#> [35,] 68.0  0.012    0.996
#> [36,] 70.0  0.024    0.996
#> [37,] 72.0  0.046    0.996
#> [38,] 74.0  0.038    1.000
#> [39,] 76.0  0.044    1.000
#> [40,] 78.0  0.044    1.000
#> [41,] 80.0  0.022    1.000

# Joint plot.
print(dose_recommendation$plot)


# Show customization of single plot.
variant1 <- dose_recommendation$singlePlots$plot1 + xlim(0, 20)
print(variant1)
#> Warning: Removed 31 rows containing missing values or values outside the scale range
#> (`geom_bar()`).
#> Warning: Removed 1 row containing missing values or values outside the scale range
#> (`geom_vline()`).


# Example of usage for `NextBestTD` NextBest class.
my_data <- Data(
  x = c(25, 50, 50, 75, 150, 200, 225, 300),
  y = c(0, 0, 0, 0, 1, 1, 1, 1),
  ID = 1:8,
  cohort = c(1L, 2L, 2L, 3L, 4L, 5L, 6L, 7L),
  doseGrid = seq(from = 25, to = 300, by = 25)
)

my_model <- LogisticIndepBeta(
  binDLE = c(1.05, 1.8),
  DLEweights = c(3, 3),
  DLEdose = c(25, 300),
  data = my_data
)

# Target probabilities of the occurrence of a DLT during trial and
# at the end of the trial are defined as 0.35 and 0.3, respectively.
td_next_best <- NextBestTD(prob_target_drt = 0.35, prob_target_eot = 0.3)

# doselimit is the maximum allowable dose level to be given to subjects.
dose_recommendation <- nextBest(
  nextBest = td_next_best,
  doselimit = max(my_data@doseGrid),
  model = my_model,
  data = my_data
)

dose_recommendation$next_dose_drt
#> [1] 50
dose_recommendation$plot

# Example of usage for `NextBestTDsamples` NextBest class.
my_data <- Data(
  x = c(25, 50, 50, 75, 150, 200, 225, 300),
  y = c(0, 0, 0, 0, 1, 1, 1, 1),
  ID = 1:8,
  cohort = c(1L, 2L, 2L, 3L, 4L, 5L, 6L, 7L),
  doseGrid = seq(from = 25, to = 300, by = 25)
)

my_model <- LogisticIndepBeta(
  binDLE = c(1.05, 1.8),
  DLEweights = c(3, 3),
  DLEdose = c(25, 300),
  data = my_data
)

# Set-up some MCMC parameters and generate samples.
my_options <- McmcOptions(burnin = 100, step = 2, samples = 800)
my_samples <- mcmc(my_data, my_model, my_options)

# Target probabilities of the occurrence of a DLT during trial and
# at the end of the trial are defined as 0.35 and 0.3, respectively.
# 'derive' is specified such that the 30% posterior quantile of the TD35 and
# TD30 samples will be used as TD35 and TD30 estimates.
tds_next_best <- NextBestTDsamples(
  prob_target_drt = 0.35,
  prob_target_eot = 0.3,
  derive = function(samples) {
    as.numeric(quantile(samples, probs = 0.3))
  }
)

# doselimit is the maximum allowable dose level to be given to subjects.
dose_recommendation <- nextBest(
  nextBest = tds_next_best,
  doselimit = max(my_data@doseGrid),
  samples = my_samples,
  model = my_model,
  data = my_data
)

dose_recommendation$next_dose_drt
#> [1] 25
dose_recommendation$plot
#> Warning: Some data points are outside of `bounds`. Removing them.
#> Warning: Some data points are outside of `bounds`. Removing them.


# Example of usage for `NextBestMaxGain` NextBest class.

# Create the data.
my_data <- DataDual(
  x = c(25, 50, 25, 50, 75, 300, 250, 150),
  y = c(0, 0, 0, 0, 0, 1, 1, 0),
  ID = 1:8,
  cohort = 1:8,
  w = c(0.31, 0.42, 0.59, 0.45, 0.6, 0.7, 0.6, 0.52),
  doseGrid = seq(25, 300, 25),
  placebo = FALSE
)

# 'ModelTox' DLT model, e.g 'LogisticIndepBeta'.
my_model_dlt <- LogisticIndepBeta(
  binDLE = c(1.05, 1.8),
  DLEweights = c(3, 3),
  DLEdose = c(25, 300),
  data = my_data
)

# 'ModelEff' efficacy model, e.g. 'Effloglog'.
my_model_eff <- Effloglog(
  eff = c(1.223, 2.513),
  eff_dose = c(25, 300),
  nu = c(a = 1, b = 0.025),
  data = my_data
)

# Target probabilities of the occurrence of a DLT during trial and at the
# end of trial are defined as 0.35 and 0.3, respectively.
mg_next_best <- NextBestMaxGain(
  prob_target_drt = 0.35,
  prob_target_eot = 0.3
)

# doselimit is the maximum allowable dose level to be given to subjects.
dose_recommendation <- nextBest(
  nextBest = mg_next_best,
  doselimit = 300,
  model = my_model_dlt,
  model_eff = my_model_eff,
  data = my_data
)

dose_recommendation$next_dose
#> [1] 75
dose_recommendation$plot


# Example of usage for `NextBestMaxGainSamples` NextBest class.

# Create the data.
my_data <- DataDual(
  x = c(25, 50, 25, 50, 75, 300, 250, 150),
  y = c(0, 0, 0, 0, 0, 1, 1, 0),
  w = c(0.31, 0.42, 0.59, 0.45, 0.6, 0.7, 0.6, 0.52),
  ID = 1:8,
  cohort = 1:8,
  doseGrid = seq(25, 300, 25),
  placebo = FALSE
)

# 'ModelTox' DLT model, e.g 'LogisticIndepBeta'.
my_model_dlt <- LogisticIndepBeta(
  binDLE = c(1.05, 1.8),
  DLEweights = c(3, 3),
  DLEdose = c(25, 300),
  data = my_data
)

# 'ModelEff' efficacy model, e.g 'Effloglog'.
my_model_effll <- Effloglog(
  eff = c(1.223, 2.513),
  eff_dose = c(25, 300),
  nu = c(a = 1, b = 0.025),
  data = my_data
)

# Set-up some MCMC parameters and generate samples from the posterior.
my_options <- McmcOptions(burnin = 100, step = 2, samples = 500)
my_samples_dlt <- mcmc(my_data, my_model_dlt, my_options)
my_samples_effll <- mcmc(my_data, my_model_effll, my_options)

# Target probabilities of the occurrence of a DLT during trial and at the end of
# trial are defined as 0.35 and 0.3, respectively.
# Use 30% posterior quantile of the TD35 and TD30 samples as estimates of TD35
# and TD30.
# Use 50% posterior quantile of the Gstar (the dose which gives the maxim gain value)
# samples as Gstar estimate.
mgs_next_best <- NextBestMaxGainSamples(
  prob_target_drt = 0.35,
  prob_target_eot = 0.3,
  derive = function(samples) {
    as.numeric(quantile(samples, prob = 0.3))
  },
  mg_derive = function(mg_samples) {
    as.numeric(quantile(mg_samples, prob = 0.5))
  }
)

dose_recommendation <- nextBest(
  nextBest = mgs_next_best,
  doselimit = max(my_data@doseGrid),
  samples = my_samples_dlt,
  model = my_model_dlt,
  data = my_data,
  model_eff = my_model_effll,
  samples_eff = my_samples_effll
)

dose_recommendation$next_dose
#> [1] 50
dose_recommendation$plot


# Now using the 'EffFlexi' class efficacy model:

my_model_effflexi <- EffFlexi(
  eff = c(1.223, 2.513),
  eff_dose = c(25, 300),
  sigma2W = c(a = 0.1, b = 0.1),
  sigma2betaW = c(a = 20, b = 50),
  rw1 = FALSE,
  data = my_data
)

my_samples_effflexi <- mcmc(my_data, my_model_effflexi, my_options)

dose_recommendation <- nextBest(
  nextBest = mgs_next_best,
  doselimit = max(my_data@doseGrid),
  samples = my_samples_dlt,
  model = my_model_dlt,
  data = my_data,
  model_eff = my_model_effflexi,
  samples_eff = my_samples_effflexi
)

dose_recommendation$next_dose
#> [1] 50
dose_recommendation$plot
# Example of usage for `NextBestProbMTDLTE` NextBest class.

# Create the data.
my_data <- Data(
  x = c(0.1, 0.5, 1.5, 3, 6, 10, 10, 10),
  y = c(0, 0, 0, 0, 0, 0, 1, 0),
  ID = 1:8,
  cohort = c(0, 1, 2, 3, 4, 5, 5, 5),
  doseGrid = c(0.1, 0.5, 1.5, 3, 6, seq(from = 10, to = 80, by = 2))
)

# Initialize the CRM model used to model the data.
my_model <- LogisticLogNormal(
  mean = c(-0.85, 1),
  cov = matrix(c(1, -0.5, -0.5, 1), nrow = 2),
  ref_dose = 56
)

# Set-up some MCMC parameters and generate samples from the posterior.
my_options <- McmcOptions(burnin = 100, step = 2, samples = 500)
my_samples <- mcmc(my_data, my_model, my_options)

# Define the rule for dose increments and calculate the maximum dose allowed.
my_increments <- IncrementsRelative(
  intervals = c(0, 20),
  increments = c(1, 0.33)
)
next_max_dose <- maxDose(my_increments, data = my_data)

# Define the rule which will be used to select the next best dose
# based on the 'NextBestProbMTDLTE' class.
nb_mtd_lte <- NextBestProbMTDLTE(target = 0.33)

# Calculate the next best dose.
dose_recommendation <- nextBest(
  nextBest = nb_mtd_lte,
  doselimit = next_max_dose,
  samples = my_samples,
  model = my_model,
  data = my_data
)
# Example of usage for `NextBestProbMTDMinDist` NextBest class.

# Create the data.
my_data <- Data(
  x = c(1.5, 1.5, 1.5, 2.5, 2.5, 2.5, 3.5, 3.5, 3.5),
  y = c(0, 0, 0, 0, 0, 0, 1, 1, 0),
  ID = 1:9,
  cohort = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
  doseGrid = c(1.5, 2.5, 3.5, 4.5, 6, 7)
)

# Initialize the CRM model used to model the data.
my_model <- my_model <- LogisticKadaneBetaGamma(
  theta = 0.3,
  xmin = 1.5,
  xmax = 7,
  alpha = 1,
  beta = 19,
  shape = 0.5625,
  rate = 0.125
)

# Set-up some MCMC parameters and generate samples from the posterior.
my_options <- McmcOptions(burnin = 100, step = 2, samples = 500)
my_samples <- mcmc(my_data, my_model, my_options)

# Define the rule for dose increments and calculate the maximum dose allowed.
my_increments <- IncrementsDoseLevels(levels = 1)

next_max_dose <- maxDose(my_increments, data = my_data)

# Define the rule which will be used to select the next best dose
# based on the 'NextBestProbMTDMinDist' class.
nb_mtd_min_dist <- NextBestProbMTDMinDist(target = 0.3)

# Calculate the next best dose.
dose_recommendation <- nextBest(
  nextBest = nb_mtd_min_dist,
  doselimit = next_max_dose,
  samples = my_samples,
  model = my_model,
  data = my_data
)
ordinal_data <- .DefaultDataOrdinal()
ordinal_model <- .DefaultLogisticLogNormalOrdinal()
options <- .DefaultMcmcOptions()
ordinal_samples <- mcmc(ordinal_data, ordinal_model, options)
#> Warning: Unused variable "y" in data

nextBest(
  nextBest = NextBestOrdinal(2L, .DefaultNextBestNCRM()),
  samples = ordinal_samples,
  doselimit = Inf,
  model = ordinal_model,
  data = ordinal_data
)

#> $value
#> [1] 60
#> 
#> $plot
#> 
#> $singlePlots
#> $singlePlots$plot1

#> 
#> $singlePlots$plot2
#> 
#> 
#> $probs
#>       dose target overdose
#>  [1,]   10  0.001    0.000
#>  [2,]   20  0.001    0.000
#>  [3,]   30  0.001    0.000
#>  [4,]   40  0.003    0.001
#>  [5,]   50  0.028    0.002
#>  [6,]   60  0.214    0.139
#>  [7,]   70  0.181    0.500
#>  [8,]   80  0.131    0.670
#>  [9,]   90  0.091    0.765
#> [10,]  100  0.074    0.813
#> 
ordinal_data <- .DefaultDataOrdinal()
ordinal_model <- .DefaultLogisticLogNormalOrdinal()
options <- .DefaultMcmcOptions()
ordinal_samples <- mcmc(ordinal_data, ordinal_model, options)
#> Warning: Unused variable "y" in data

nextBest(
  nextBest = NextBestOrdinal(2L, .DefaultNextBestNCRM()),
  samples = ordinal_samples,
  doselimit = Inf,
  model = ordinal_model,
  data = ordinal_data
)

#> $value
#> [1] 60
#> 
#> $plot
#> 
#> $singlePlots
#> $singlePlots$plot1

#> 
#> $singlePlots$plot2

#> 
#> 
#> $probs
#>       dose target overdose
#>  [1,]   10  0.000    0.000
#>  [2,]   20  0.000    0.000
#>  [3,]   30  0.001    0.000
#>  [4,]   40  0.003    0.000
#>  [5,]   50  0.014    0.004
#>  [6,]   60  0.224    0.134
#>  [7,]   70  0.177    0.467
#>  [8,]   80  0.135    0.625
#>  [9,]   90  0.111    0.714
#> [10,]  100  0.080    0.777
#>