Type: Package
Title: Drug Demand Forecasting
Version: 0.1.3
Date: 2024-02-27
Description: Performs drug demand forecasting by modeling drug dispensing data while taking into account predicted enrollment and treatment discontinuation dates. The gap time between randomization and the first drug dispensing visit is modeled using interval-censored exponential, Weibull, log-logistic, or log-normal distributions (Anderson-Bergman (2017) <doi:10.18637/jss.v081.i12>). The number of skipped visits is modeled using Poisson, zero-inflated Poisson, or negative binomial distributions (Zeileis, Kleiber & Jackman (2008) <doi:10.18637/jss.v027.i08>). The gap time between two consecutive drug dispensing visits given the number of skipped visits is modeled using linear regression based on least squares or least absolute deviations (Birkes & Dodge (1993, ISBN:0-471-56881-3)). The number of dispensed doses is modeled using linear or linear mixed-effects models (McCulloch & Searle (2001, ISBN:0-471-19364-X)).
License: GPL-2 | GPL-3 [expanded from: GPL (≥ 2)]
Imports: Rcpp (≥ 1.0.10), dplyr (≥ 1.1.0), rlang (≥ 1.1.0), purrr (≥ 1.0.2), stringr (≥ 1.4.0), plotly (≥ 4.10.1), survival (≥ 2.41-3), mvtnorm (≥ 1.1-3), erify (≥ 0.4.0), stats (≥ 3.5.0), MASS (≥ 7.3-54), nlme (≥ 3.1-153), L1pack (≥ 0.41-24), eventPred (≥ 0.2.3), parallel (≥ 4.1.2), foreach (≥ 1.5.2), doParallel (≥ 1.0.17), doRNG (≥ 1.8.6)
LinkingTo: Rcpp
Depends: R (≥ 3.5.0)
LazyData: true
RoxygenNote: 7.3.0
Encoding: UTF-8
NeedsCompilation: yes
Packaged: 2024-02-27 22:56:55 UTC; kaife
Author: Kaifeng Lu ORCID iD [aut, cre]
Maintainer: Kaifeng Lu <kaifenglu@gmail.com>
Repository: CRAN
Date/Publication: 2024-02-28 01:20:02 UTC

Drug Demand Forecasting

Description

Performs drug demand forecasting by modeling drug dispensing data while taking into account predicted enrollment and treatment discontinuation dates. The gap time between randomization and the first drug dispensing visit is modeled using interval-censored exponential, Weibull, log-logistic, or log-normal distributions (Anderson-Bergman (2017) doi:10.18637/jss.v081.i12). The number of skipped visits is modeled using Poisson, zero-inflated Poisson, or negative binomial distributions (Zeileis, Kleiber & Jackman (2008) doi:10.18637/jss.v027.i08). The gap time between two consecutive drug dispensing visits given the number of skipped visits is modeled using linear regression based on least squares or least absolute deviations (Birkes & Dodge (1993, ISBN:0-471-56881-3)). The number of dispensed doses is modeled using linear or linear mixed-effects models (McCulloch & Searle (2001, ISBN:0-471-19364-X)).

Details

In clinical trials, patients do not always follow protocol-specified visit and drug dispensing schedules. Patients may encounter delays in their drug dispensing appointments, skip visits altogether, or receive doses different from the protocol-specified target. Relying solely on protocol-based predictions tends to result in an overestimation of drug demand. Consequently, we propose a method that models observed drug dispensing data, thereby accounting for these deviations.

For k0 and ki, we explore several modeling options, including constant, Poisson, zero-inflated Poisson (ZIP), and negative binomial distributions.

For t0, we consider various models such as constant, exponential, Weibull, log-logistic, and log-normal.

For t1 (given k0) and ti (given ki), we apply linear regression models using least squares or least absolute deviations.

For di, we evaluate constant, linear, and linear mixed-effects models with subject random effects.

Once the dosing models are fitted to the observed drug dispensing data, we draw model parameters from their approximate posterior distributions. Subsequently, we simulate drug dispensing data after cutoff for both ongoing and new patients.

Finally, we estimate the dose to dispense based on the simulated data.

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

References

Clifford Anderson-Bergman. icenReg: Regression Models for Interval Censored Data in R. J Stat Softw. 2017, Volume 81, Issue 12.

Achim Zeileis, Christian Kleiber, and Simon Jackman. Regression models for count data in R. J Stat Softw. 2008, Volume 27, Issue 8.

David Birkes and Yadolah Dodge. Alternative Methods of Regression. John Wiley & Sons: New York, 1993.

Charles E. McCulloch and Shayler R. Searle. Generalized, Linear, and Mixed Models. John Wiley & Sons: New York, 2001.


The subject-level enrollment and event data before enrollment completion.

Description

A data frame with the following columns:

trialsdt

The trial start date.

usubjid

The unique subject ID.

randdt

The randomization date for each subject.

treatment

The treatment group.

treatment_description

Description of the treatment group.

time

The number of days elapsed since randomization.

event

The event indicator, with a value of 1 indicating the occurrence of an event, and 0 indicating no event.

dropout

The dropout indicator, where 1 corresponds to a dropout and 0 implies no dropout.

cutoffdt

The cutoff date.

For drug demand forecasting, the event of interest is treatment discontinuation. The dropout variable is set to 0 for all patients in this context.

Usage

df1

Format

An object of class tbl_df (inherits from tbl, data.frame) with 175 rows and 9 columns.


The subject-level enrollment and event data after enrollment completion.

Description

A data frame with the following columns:

trialsdt

The trial start date.

usubjid

The unique subject ID.

randdt

The randomization date for each subject.

treatment

The treatment group.

treatment_description

Description of the treatment group.

time

The number of days elapsed since randomization.

event

The event indicator, with a value of 1 indicating the occurrence of an event, and 0 indicating no event.

dropout

The dropout indicator, where 1 corresponds to a dropout and 0 implies no dropout.

cutoffdt

The cutoff date.

For drug demand forecasting, the event of interest is treatment discontinuation. The dropout variable is set to 0 for all patients in this context.

Usage

df2

Format

An object of class tbl_df (inherits from tbl, data.frame) with 250 rows and 9 columns.


The dosing schedule data frame.

Description

A data frame with the following columns:

kit

The numeric code of the kit type.

target_days

Number of days per treatment cycle.

target_dose

Dose per treatment cycle.

max_cycles

Maximum number of treatment cycles.

Usage

dosing_schedule_df

Format

An object of class tbl_df (inherits from tbl, data.frame) with 4 rows and 4 columns.


Cumulative Dose

Description

Obtains the cumulative dose given treatment duration and dosing schedule.

Usage

f_cum_dose(x, w, d, N)

Arguments

x

Treatment duration.

w

Number of days per treatment cycle.

d

Dose per treatment cycle.

N

Maximum number of treatment cycles.

Value

The cumulative dose to dispense for the drug over a specified treatment duration.

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

Examples


f_cum_dose(c(28, 70), 21, 2, 10000)


Drug Dispensing Model Fitting

Description

Fits drug dispensing models to the observed drug dispensing data.

Usage

f_dispensing_models(
  vf,
  dosing_schedule_df,
  model_k0,
  model_t0,
  model_t1,
  model_ki,
  model_ti,
  model_di,
  nreps,
  showplot = TRUE
)

Arguments

vf

A data frame for subject-level drug dispensing data, including the following variables: drug, drug_name, kit, kit_name, usubjid, treatment, treatment_description, arrivalTime, time, event, dropout, day, dose, cum_dose, and row_id.

dosing_schedule_df

A data frame providing dosing schedule information. It contains the following variables: kit, target_days, target_dose, and max_cycles.

model_k0

The model for the number of skipped visits between randomization and the first drug dispensing visit. Options include "constant", "poisson", "zero-inflated poisson", and "negative binomial".

model_t0

The model for the gap time between randomization and the first drug dispensing visit when there is no visit skipping. Options include "constant", "exponential", "weibull", "log-logistic", and "log-normal".

model_t1

The model for the gap time between randomization and the first drug dispensing visit when there is visit skipping. Options include "least squares", and "least absolute deviations".

model_ki

The model for the number of skipped visits between two consecutive drug dispensing visits. Options include "constant", "poisson", "zero-inflated poisson", and "negative binomial".

model_ti

The model for the gap time between two consecutive drug dispensing visits. Options include "least squares" and "least absolute deviations".

model_di

The model for the dispensed doses at drug dispensing visits. Options include "constant", "linear model", and "linear mixed-effects model".

nreps

The number of simulations for drawing posterior model parameters.

showplot

A Boolean variable that controls whether or not to show the model fit plot. It defaults to TRUE.

Value

A list with the following components:

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

See Also

f_fit_t0, f_fit_ki, f_fit_ti, f_fit_di

Examples

library(dplyr)

observed <- f_dose_observed(df2, visitview2, showplot = FALSE)

dispensing_models <- f_dispensing_models(
  observed$vf, dosing_schedule_df,
  model_k0 = "zero-inflated poisson",
  model_t0 = "log-logistic",
  model_t1 = "least squares",
  model_ki = "zero-inflated poisson",
  model_ti = "least squares",
  model_di = "linear mixed-effects model",
  nreps = 200, showplot = FALSE)

dispensing_models$ki_fit$fit_plot

Drug Dispensing Data Simulation

Description

Simulates drug dispensing data after cutoff for both ongoing and new patients.

Usage

f_dose_draw(
  vf_ongoing,
  vf_new,
  common_time_model,
  k0_fit,
  t0_fit,
  t1_fit,
  ki_fit,
  ti_fit,
  di_fit,
  t0,
  t,
  ncores_max
)

Arguments

vf_ongoing

A data frame for the observed drug dispensing data for ongoing patients with drug dispensing records. It includes the following variables: draw, kit, kit_name, usubjid, day, dose, arrivalTime, treatment, treatment_description, time, and totalTime.

vf_new

A data frame for the randomization date for new patients and ongoing patients with no drug dispensing records. It includes the following variables: draw, kit, kit_name, usubjid, arrivalTime, treatment, treatment_description, time, and totalTime.

common_time_model

A Boolean variable that indicates whether a common time model is used for drug dispensing visits.

k0_fit

The model fit for the number of skipped visits between randomization and the first drug dispensing visit.

t0_fit

The model fit for the gap time between randomization and the first drug dispensing visit when there is no visit skipping.

t1_fit

The model fit for the gap time between randomization and the first drug dispensing visit when there is visit skipping.

ki_fit

The model fit for the number of skipped visits between two consecutive drug dispensing visits.

ti_fit

The model fit for the gap time between two consecutive drug dispensing visits.

di_fit

The model fit for the dispensed doses at drug dispensing visits.

t0

The cutoff date relative to the trial start date.

t

A vector of new time points for drug dispensing prediction.

ncores_max

The maximum number of cores to use for parallel computing. The actual number of cores used is the minimum of ncores_max and half of the detected number of cores.

Value

A list with two components:

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

See Also

f_fit_t0, f_fit_ki, f_fit_ti, f_fit_di

Examples



set.seed(431)
library(dplyr)

pred <- eventPred::getPrediction(
  df = df2,
  to_predict = "event only",
  target_d = 250,
  event_model = "log-logistic",
  dropout_model = "none",
  pilevel = 0.95,
  nyears = 3,
  nreps = 200,
  showsummary = FALSE,
  showplot = FALSE,
  by_treatment = TRUE)

observed <- f_dose_observed(df2, visitview2, showplot = FALSE)

fit <- f_dispensing_models(
  observed$vf, dosing_schedule_df,
  model_k0 = "zero-inflated poisson",
  model_t0 = "log-logistic",
  model_t1 = "least squares",
  model_ki = "zero-inflated poisson",
  model_ti = "least squares",
  model_di = "linear mixed-effects model",
  nreps = 200, showplot = FALSE)

trialsdt = df2$trialsdt[1]
cutoffdt = df2$cutoffdt[1]
t0 = as.numeric(cutoffdt - trialsdt + 1)
nyears = 3
t1 = t0 + nyears*365
t = c(seq(t0, t1, 30), t1)

vf_ongoing_new <- f_ongoing_new(
  pred$event_pred$newEvents,
  observed$kit_description_df,
  observed$treatment_by_drug_df,
  observed$vf)

dose_draw <- f_dose_draw(
  vf_ongoing_new$vf_ongoing,
  vf_ongoing_new$vf_new,
  fit$common_time_model,
  fit$k0_fit, fit$t0_fit, fit$t1_fit,
  fit$ki_fit, fit$ti_fit, fit$di_fit,
  t0, t, ncores_max = 2)

head(dose_draw$dosing_subject_new)
head(dose_draw$dosing_summary_new)



Drug Dispensing Data Simulation for One Iteration

Description

Simulates drug dispensing data for one iteration.

Usage

f_dose_draw_1(
  i,
  common_time_model,
  k0_fit,
  t0_fit,
  t1_fit,
  ki_fit,
  ti_fit,
  di_fit,
  vf_ongoing,
  vf_ongoing1,
  vf_new,
  vf_new1,
  vf_kit,
  l,
  t
)

Arguments

i

The iteration number.

common_time_model

A Boolean variable that indicates whether a common time model is used for drug dispensing visits.

k0_fit

The model fit for the number of skipped visits between randomization and the first drug dispensing visit.

t0_fit

The model fit for the gap time between randomization and the first drug dispensing visit when there is no visit skipping.

t1_fit

The model fit for the gap time between randomization and the first drug dispensing visit when there is visit skipping.

ki_fit

The model fit for the number of skipped visits between two consecutive drug dispensing visits.

ti_fit

The model fit for the gap time between two consecutive drug dispensing visits.

di_fit

The model fit for the dispensed doses at drug dispensing visits.

vf_ongoing

A data frame for the observed drug dispensing data for ongoing patients with drug dispensing records. It includes the following variables: draw, kit, kit_name, usubjid, day, dose, arrivalTime, treatment, treatment_description, time, and totalTime.

vf_ongoing1

A data frame for the last observed drug dispensing date for ongoing patients with drug dispensing records. For the common time model, it includes the following variables: draw, usubjid, arrivalTime, treatment, treatment_description, time, totalTime, V, C, and D. For separate time models, it includes the following variables: draw, kit, kit_name, usubjid, arrivalTime, treatment, treatment_description, time, totalTime, V, C, and D.

vf_new

A data frame for the randomization date for new patients and ongoing patients with no drug dispensing records. It includes the following variables: draw, kit, kit_name, usubjid, arrivalTime, treatment, treatment_description, time, and totalTime.

vf_new1

A data frame for the randomization date for new patients and ongoing patients with no drug dispensing records. For the common time model, it includes the following variables: draw, usubjid, arrivalTime, treatment, treatment_description, time, totalTime, V, C, and D. For separate time models, it includes the following variables: draw, kit, kit_name, usubjid, arrivalTime, treatment, treatment_description, time, totalTime, V, C, and D.

vf_kit

A data frame indicating the kit names for each subject by draw. It includes the following variables: draw, usubjid, kit, and kit_name.

l

Number of kit types.

t

A vector of new time points for drug dispensing prediction.

Value

A list of two components:

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

See Also

f_fit_t0, f_fit_ki, f_fit_ti, f_fit_di

Examples



set.seed(431)
library(dplyr)

pred <- eventPred::getPrediction(
  df = df2,
  to_predict = "event only",
  target_d = 250,
  event_model = "log-logistic",
  dropout_model = "none",
  pilevel = 0.95,
  nyears = 3,
  nreps = 200,
  showsummary = FALSE,
  showplot = FALSE,
  by_treatment = TRUE)

observed <- f_dose_observed(df2, visitview2, showplot = FALSE)

fit <- f_dispensing_models(
  observed$vf, dosing_schedule_df,
  model_k0 = "zero-inflated poisson",
  model_t0 = "log-logistic",
  model_t1 = "least squares",
  model_ki = "zero-inflated poisson",
  model_ti = "least squares",
  model_di = "linear mixed-effects model",
  nreps = 200, showplot = FALSE)

trialsdt = df2$trialsdt[1]
cutoffdt = df2$cutoffdt[1]
t0 = as.numeric(cutoffdt - trialsdt + 1)
nyears = 3
t1 = t0 + nyears*365
t = c(seq(t0, t1, 30), t1)

l = nrow(observed$kit_description_df)

vf_ongoing_new <- f_ongoing_new(
  pred$event_pred$newEvents,
  observed$kit_description_df,
  observed$treatment_by_drug_df,
  observed$vf)

vf_ongoing <- vf_ongoing_new$vf_ongoing
vf_new <- vf_ongoing_new$vf_new

vf_kit <- vf_ongoing %>%
  select(-c("day", "dose")) %>%
  bind_rows(vf_new) %>%
  group_by(draw, usubjid, kit, kit_name) %>%
  slice(1) %>%
  select(c("draw", "usubjid", "kit", "kit_name"))

vf_ongoing1 <- vf_ongoing %>%
  group_by(draw, usubjid) %>%
  slice(n()) %>%
  mutate(V = day - 1,
         C = as.numeric(t0 - arrivalTime),
         D = pmin(time - 1, t1 - arrivalTime)) %>%
  select(-c("kit", "kit_name", "day", "dose"))

### new patients and ongoing patients with no dosing records ###
vf_new1 <- vf_new %>%
  group_by(draw, usubjid) %>%
  slice(n()) %>%
  mutate(V = 0,
         C = as.numeric(t0 - arrivalTime),
         D = pmin(time - 1, t1 - arrivalTime)) %>%
  select(-c("kit", "kit_name"))

# first iteration to extract subject and summary data
list1 <- f_dose_draw_1(
  1, fit$common_time_model,
  fit$k0_fit, fit$t0_fit, fit$t1_fit,
  fit$ki_fit, fit$ti_fit, fit$di_fit,
  vf_ongoing, vf_ongoing1, vf_new, vf_new1,
  vf_kit, l, t)

head(list1$dosing_subject_newi)
head(list1$dosing_summary_newi)



Drug Dispensing Visit Dates Simulation for One Iteration

Description

Simulates drug dispensing visit dates for one iteration.

Usage

f_dose_draw_t_1(
  i,
  k0_fit,
  t0_fit,
  t1_fit,
  ki_fit,
  ti_fit,
  vf_ongoing1,
  vf_new1
)

Arguments

i

The iteration number.

k0_fit

The model fit for the number of skipped visits between randomization and the first drug dispensing visit.

t0_fit

The model fit for the gap time between randomization and the first drug dispensing visit when there is no visit skipping.

t1_fit

The model fit for the gap time between randomization and the first drug dispensing visit when there is visit skipping.

ki_fit

The model fit for the number of skipped visits between two consecutive drug dispensing visits.

ti_fit

The model fit for the gap time between two consecutive drug dispensing visits.

vf_ongoing1

A data frame for the last observed drug dispensing date for ongoing patients with drug dispensing records. For the common time model, it includes the following variables: draw, usubjid, arrivalTime, treatment, treatment_description, time, totalTime, V, C, and D. For separate time models, it includes the following variables: draw, kit, kit_name, usubjid, arrivalTime, treatment, treatment_description, time, totalTime, V, C, and D.

vf_new1

A data frame for the randomization date for new patients and ongoing patients with no drug dispensing records. For the common time model, it includes the following variables: draw, usubjid, arrivalTime, treatment, treatment_description, time, totalTime, V, C, and D. For separate time models, it includes the following variables: draw, kit, kit_name, usubjid, arrivalTime, treatment, treatment_description, time, totalTime, V, C, and D.

Value

A data frame containing the simulated drug dispensing visit dates at the subject level for ongoing and new subjects. It includes the following variables: usubjid, day, draw, arrivalTime, treatment, treatment_description, time, totalTime, and status.

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

See Also

f_fit_t0, f_fit_ki, f_fit_ti

Examples



set.seed(431)
library(dplyr)

pred <- eventPred::getPrediction(
  df = df2,
  to_predict = "event only",
  target_d = 250,
  event_model = "log-logistic",
  dropout_model = "none",
  pilevel = 0.95,
  nyears = 3,
  nreps = 200,
  showsummary = FALSE,
  showplot = FALSE,
  by_treatment = TRUE)

observed <- f_dose_observed(df2, visitview2, showplot = FALSE)

fit <- f_dispensing_models(
  observed$vf, dosing_schedule_df,
  model_k0 = "zero-inflated poisson",
  model_t0 = "log-logistic",
  model_t1 = "least squares",
  model_ki = "zero-inflated poisson",
  model_ti = "least squares",
  model_di = "linear mixed-effects model",
  nreps = 200, showplot = FALSE)

trialsdt = df2$trialsdt[1]
cutoffdt = df2$cutoffdt[1]
t0 = as.numeric(cutoffdt - trialsdt + 1)
nyears = 3
t1 = t0 + nyears*365

vf_ongoing_new <- f_ongoing_new(
  pred$event_pred$newEvents,
  observed$kit_description_df,
  observed$treatment_by_drug_df,
  observed$vf)

vf_ongoing <- vf_ongoing_new$vf_ongoing
vf_new <- vf_ongoing_new$vf_new

vf_ongoing1 <- vf_ongoing %>%
  group_by(draw, usubjid) %>%
  slice(n()) %>%
  mutate(V = day - 1,
         C = as.numeric(t0 - arrivalTime),
         D = pmin(time - 1, t1 - arrivalTime)) %>%
  select(-c("kit", "kit_name", "day", "dose"))

### new patients and ongoing patients with no dosing records ###
vf_new1 <- vf_new %>%
  group_by(draw, usubjid) %>%
  slice(n()) %>%
  mutate(V = 0,
         C = as.numeric(t0 - arrivalTime),
         D = pmin(time - 1, t1 - arrivalTime)) %>%
  select(-c("kit", "kit_name"))

dosing_subject_new1 <- f_dose_draw_t_1(
  1, fit$k0_fit, fit$t0_fit, fit$t1_fit,
  fit$ki_fit, fit$ti_fit, vf_ongoing1, vf_new1)

head(dosing_subject_new1)



Dosing Date Imputation for New Patients

Description

Imputes the dosing dates for new patients and ongoing patients with no dosing records.

Usage

f_dose_new_cpp(
  usubjid,
  V,
  C,
  D,
  model_k0,
  theta_k0,
  model_t0,
  theta_t0,
  model_t1,
  theta_t1,
  model_ki,
  theta_ki,
  model_ti,
  theta_ti
)

Arguments

usubjid

The unique subject ID.

V

Initialized to 0 and corresponds to the randomization visit.

C

The cutoff date relative to randomization.

D

The discontinuation date relative to randomization.

model_k0

The model for the number of skipped visits between randomization and the first drug dispensing visit. Options include "constant", "poisson", "zero-inflated poisson", and "negative binomial".

theta_k0

The model parameters for the number of skipped visits between randomization and the first drug dispensing visit.

model_t0

The model for the gap time between randomization and the first drug dispensing visit when there is no visit skipping. Options include "constant", "exponential", "weibull", "log-logistic", and "log-normal".

theta_t0

The model parameters for the gap time between randomization and the first drug dispensing visit when there is no visit skipping.

model_t1

The model for the gap time between randomization and the first drug dispensing visit when there is visit skipping. Options include "least squares", and "least absolute deviations".

theta_t1

The model parameters for the gap time between randomization and the first drug dispensing visit when there is visit skipping.

model_ki

The model for the number of skipped visits between two consecutive drug dispensing visits. Options include "constant", "poisson", "zero-inflated poisson", and "negative binomial".

theta_ki

The model parameters for the number of skipped visits between two consecutive drug dispensing visits.

model_ti

The model for the gap time between two consecutive drug dispensing visits. Options include "least squares" and "least absolute deviations".

theta_ti

The model parameters for the gap time between two consecutive drug dispensing visits.

Value

A data frame with two variables:

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

Examples

set.seed(529)

f_dose_new_cpp(
  usubjid = "Z001", V = 0, C = 87, D = 985,
  model_k0 = "zero-inflated poisson", theta_k0 = c(0.6, 1.1),
  model_t0 = "log-logistic", theta_t0 = c(-1.0, 0.7),
  model_t1 = "least squares", theta_t1 = c(21.5, 1.9),
  model_ki = "zero-inflated poisson", theta_ki = c(0.1, 0.4),
  model_ti = "least squares", theta_ti = c(21, 2.3))


Observed Drug Dispensing Data Summary

Description

Provides an overview of the observed drug dispensing data, including the summary of cumulative dispensed doses, bar chart of the gap time between randomization and the first drug dispensing visit, the gap time between two consecutive drug dispensing visits, and the dispensed doses at drug dispensing visits by drug.

Usage

f_dose_observed(df = NULL, visitview = NULL, showplot = TRUE)

Arguments

df

A data frame for subject-level enrollment and event data, including the following variables: trialsdt, usubjid, randdt, treatment, treatment_description, time, event, dropout, and cutoffdt.

visitview

A data frame containing the observed drug dispensing data, including the following variables: usubjid, visit, date, drug, drug_name, kit, kit_name, kit_number, and dispensed_quantity.

showplot

A Boolean variable that controls whether or not to show the drug dispensing model fit and drug demand prediction plots. It defaults to TRUE.

Value

A list with the following components:

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

Examples


observed <- f_dose_observed(df = df2, visitview = visitview2)


Dosing Date Imputation for Ongoing Patients

Description

Imputes the dosing dates after cutoff for ongoing patients with dosing records.

Usage

f_dose_ongoing_cpp(usubjid, V, C, D, model_ki, theta_ki, model_ti, theta_ti)

Arguments

usubjid

The unique subject ID.

V

The last dosing visit date relative to randomization.

C

The cutoff date relative to randomization.

D

The discontinuation date relative to randomization.

model_ki

The model for the number of skipped visits between two consecutive drug dispensing visits. Options include "constant", "poisson", "zero-inflated poisson", and "negative binomial".

theta_ki

The model parameters for the number of skipped visits between two consecutive drug dispensing visits.

model_ti

The model for the gap time between two consecutive drug dispensing visits. Options include "least squares" and "least absolute deviations".

theta_ti

The model parameters for the gap time between two consecutive drug dispensing visits.

Value

A data frame with two variables:

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

Examples

set.seed(314)

f_dose_ongoing_cpp(
  usubjid = "A001", V = 297, C = 329, D = 569,
  model_ki = "zero-inflated poisson", theta_ki = c(0.4, 2.5),
  model_ti = "least squares", theta_ti = c(21, 2.3))


Drug Demand Per Protocol

Description

Obtains drug demand prediction based on protocol-assumed visit and dosing schedules.

Usage

f_dose_pp(
  dosing_summary_t0,
  vf_ongoing,
  vf_new,
  dosing_schedule_df,
  t0,
  t,
  pilevel
)

Arguments

dosing_summary_t0

A data frame for the cumulative doses dispensed before the cutoff date. It contains the following variables: kit, kit_name, and cum_dose_t0.

vf_ongoing

A data frame for the observed drug dispensing data for ongoing patients with drug dispensing records. It includes the following variables: draw, kit, kit_name, usubjid, day, dose, arrivalTime, treatment, treatment_description, time, and totalTime.

vf_new

A data frame for the randomization date for new patients and ongoing patients with no drug dispensing records. It includes the following variables: draw, kit, kit_name, usubjid, arrivalTime, treatment, treatment_description, time, and totalTime.

dosing_schedule_df

A data frame providing dosing schedule information. It contains the following variables: kit, target_days, target_dose, and max_cycles.

t0

The cutoff date relative to the trial start date.

t

A vector of new time points for drug dispensing prediction.

pilevel

The prediction interval level.

Value

A data frame for dosing summary by drug and time point per protocol. It contains the following variables: kit, kit_name, t, n, pilevel, lower, upper, mean, and var.

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

Examples



# Design stage drug demand forecasting per protocol.

set.seed(312)
library(dplyr)

pred <- eventPred::getPrediction(
  df = NULL,
  to_predict = "enrollment and event",
  target_n = 250,
  target_d = 250,
  enroll_prior = list(
    model = "piecewise poisson",
    theta = c(-0.74, -1.18),
    vtheta = matrix(c(0.0087, 0, 0, 0.0082), 2, 2),
    accrualTime = c(0, 240)),
  event_prior = list(
    list(model = "log-logistic",
         theta = c(5.9, -0.2),
         vtheta = matrix(c(0.022, 0.004, 0.004, 0.012), 2, 2)),
    list(model = "log-logistic",
         theta = c(5.6, 0.02),
         vtheta = matrix(c(0.032, 0.003, 0.003, 0.012), 2, 2)),
    list(model = "log-logistic",
         theta = c(5.7, -0.3),
         vtheta = matrix(c(0.071, 0.013, 0.013, 0.054), 2, 2))),
  dropout_prior = NULL,
  pilevel = 0.95,
  nyears = 3,
  nreps = 200,
  showsummary = FALSE,
  showplot = FALSE,
  by_treatment = TRUE,
  ngroups = 3,
  alloc = c(2, 2, 1),
  treatment_label = c("Drug A + Drug B",
                      "Drug C + Placebo",
                      "Drug A + Placebo"))

dosing_summary_t0 = kit_description_df %>%
  mutate(cum_dose_t0 = 0) %>%
  select(-c("drug", "drug_name"))

vf_ongoing_new <- f_ongoing_new(
  pred$event_pred$newEvents, kit_description_df,
  treatment_by_drug_df, NULL)

t0 = 1
nyears = 3
t1 = t0 + nyears*365
t = c(seq(t0, t1, 30), t1)
pilevel = 0.95

dosing_pred_pp <- f_dose_pp(
  dosing_summary_t0, vf_ongoing_new$vf_ongoing,
  vf_ongoing_new$vf_new, dosing_schedule_df, t0, t, pilevel)

head(dosing_pred_pp)



Drug Demand Forecasting

Description

Obtains drug demand forecasting via modeling and simulation.

Usage

f_drug_demand(
  df = NULL,
  newEvents = NULL,
  visitview = NULL,
  kit_description_df = NULL,
  treatment_by_drug_df = NULL,
  dosing_schedule_df = NULL,
  model_k0 = "negative binomial",
  model_t0 = "log-logistic",
  model_t1 = "least squares",
  model_ki = "negative binomial",
  model_ti = "least absolute deviations",
  model_di = "linear mixed-effects model",
  pilevel = 0.95,
  nyears = 1,
  ncores_max = 10,
  pred_pp_only = FALSE,
  showplot = TRUE
)

Arguments

df

A data frame for subject-level enrollment and event data, including the following variables: trialsdt, usubjid, randdt, treatment, treatment_description, time, event, dropout, and cutoffdt.

newEvents

A data frame containing the imputed event data for both ongoing and new patients, typically obtained from the output of the getPrediction function of the eventPred package. It contains the following variables: draw, usubjid, arrivalTime, treatment, treatment_description, time, event, dropout, and totalTime. It must be provided.

visitview

A data frame containing the observed drug dispensing data, including the following variables: usubjid, visit, date, drug, drug_name, kit, kit_name, kit_number, and dispensed_quantity.

kit_description_df

A data frame indicating the drug and kit descriptions, including the following variables: drug, drug_name, kit, and kit_name. It must be specified at the design stage. It will be replaced with the observed information at the analysis stage.

treatment_by_drug_df

A data frame indicating the treatments associated with each drug, including the following variables: treatment and drug. It must be specified at the design stage. It will be replaced with the observed information at the analysis stage.

dosing_schedule_df

A data frame providing dosing schedule information. It contains the following variables: kit, target_days, target_dose, and max_cycles. It must be provided.

model_k0

The model for the number of skipped visits between randomization and the first drug dispensing visit. Options include "constant", "poisson", "zero-inflated poisson", and "negative binomial".

model_t0

The model for the gap time between randomization and the first drug dispensing visit when there is no visit skipping. Options include "constant", "exponential", "weibull", "log-logistic", and "log-normal".

model_t1

The model for the gap time between randomization and the first drug dispensing visit when there is visit skipping. Options include "least squares" and "least absolute deviations".

model_ki

The model for the number of skipped visits between two consecutive drug dispensing visits. Options include "constant", "poisson", "zero-inflated poisson", and "negative binomial".

model_ti

The model for the gap time between two consecutive drug dispensing visits. Options include "least squares" and "least absolute deviations".

model_di

The model for the dispensed doses at drug dispensing visits. Options include "constant", "linear model", and "linear mixed-effects model".

pilevel

The prediction interval level.

nyears

The number of years after the data cut for prediction.

ncores_max

The maximum number of cores to use for parallel computing. The actual number of cores used is the minimum of ncores_max and half of the detected number of cores.

pred_pp_only

A Boolean variable that controls whether or not to make protocol-based predictions only.

showplot

A Boolean variable that controls whether or not to show the drug dispensing model fit and drug demand prediction plots. It defaults to TRUE.

Value

For design-stage drug demand forecasting, a list with the following components:

For analysis-stage drug demand forecasting, a list with the following components:

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

See Also

f_fit_t0, f_fit_ki, f_fit_ti, f_fit_di

Examples



set.seed(529)

pred <- eventPred::getPrediction(
  df = df2,
  to_predict = "event only",
  target_d = 250,
  event_model = "log-logistic",
  dropout_model = "none",
  pilevel = 0.95,
  nyears = 1,
  nreps = 200,
  showplot = FALSE,
  by_treatment = TRUE)

drug_demand <- f_drug_demand(
  df = df2,
  newEvents = pred$event_pred$newEvents,
  visitview = visitview2,
  dosing_schedule_df = dosing_schedule_df,
  model_k0 = "zero-inflated poisson",
  model_t0 = "log-logistic",
  model_t1 = "least squares",
  model_ki = "zero-inflated poisson",
  model_ti = "least squares",
  model_di = "linear mixed-effects model",
  pilevel = 0.95,
  nyears = 1,
  ncores_max = 2,
  showplot = FALSE)

drug_demand$dosing_pred_plot



Model Fitting for Dispensed Doses

Description

Fits a linear mixed-effects model to the dispensed doses at drug dispensing visits.

Usage

f_fit_di(df, model, nreps, showplot = TRUE)

Arguments

df

The subject-level dosing data, including usubjid, day, kit, and dose.

model

The model used to analyze the dispensed doses, with options including "constant", "linear model", and "linear mixed-effects model".

nreps

The number of simulations for drawing posterior model parameters.

showplot

A Boolean variable that controls whether or not to show the fitted dose bar chart. It defaults to TRUE.

Value

A list with three components:

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

Examples

library(dplyr)

observed <- f_dose_observed(df2, visitview2, showplot = FALSE)
vf <- observed$vf

vf1 <- vf %>% filter(kit == 3)
di_fit <- f_fit_di(vf1, model = "linear mixed-effects model", nreps = 200)


Model Fitting for Number of Skipped Visits

Description

Fits a count model to the number of skipped visits between two consecutive drug dispensing visits.

Usage

f_fit_ki(df, model, nreps, showplot = TRUE)

Arguments

df

The subject-level dosing data, including skipped to indicate the number of skipped visits.

model

The count model used to analyze the number of skipped visits, with options including "constant", "poisson", "zero-inflated poisson", and "negative binomial".

nreps

The number of simulations for drawing posterior model parameter values.

showplot

A Boolean variable that controls whether or not to show the fitted count bar chart. It defaults to TRUE.

Value

A list with three components:

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

Examples

library(dplyr)

observed <- f_dose_observed(df2, visitview2, showplot = FALSE)
vf <- observed$vf

vf <- vf %>% left_join(dosing_schedule_df, by = "kit")

df_ti <- vf %>%
  mutate(time = lead(day) - day,
         skipped = pmax(floor((time - target_days/2)/target_days), 0),
         k1 = skipped + 1) %>%
  filter(row_id < n())

ki_fit <- f_fit_ki(df_ti, model = "zero-inflated poisson", nreps = 200)


Model Fitting for Dispensing Delay After Randomization

Description

Fits a specified time-to-event model to the gap time between randomization and the first drug dispensing visit when there is no visit skipping.

Usage

f_fit_t0(df, model, nreps, showplot = TRUE)

Arguments

df

The subject-level dosing data, including the following variables:

  • time: The number of days between randomization and the first drug dispensing visit (first drug dispensing visit date - randomization date + 1).

  • left: Equals time - 1, used to indicate the left endpoint of an interval for interval censoring.

  • right: Equals time, used to indicate the right endpoint of an interval for interval censoring.

model

The event model used to analyze the gap time between randomization and the first drug dispensing visit when there is no visit skipping, with options including "constant", "exponential", "weibull", "log-logistic", and "log-normal".

nreps

The number of simulations for drawing posterior model parameter values.

showplot

A Boolean variable that controls whether or not to show the fitted time-to-event bar chart. It defaults to TRUE.

Value

A list with three components:

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

Examples

library(dplyr)

observed <- f_dose_observed(df2, visitview2, showplot = FALSE)
vf <- observed$vf

vf <- vf %>% left_join(dosing_schedule_df, by = "kit")

# time from randomization to the first drug dispensing visit
df_k0 <- vf %>%
  filter(row_id == 1) %>%
  mutate(time = day,
         skipped = floor((time - target_days/2)/target_days) + 1)

# no skipping
df_t0 <- df_k0 %>%
  filter(skipped == 0) %>%
  mutate(left = time - 1, right = time)

t0_fit <- f_fit_t0(df_t0, model = "log-logistic", nreps = 200)


Model Fitting for Gap Times

Description

Fits a linear regression model to the gap time between two consecutive drug dispensing visits.

Usage

f_fit_ti(df, model, nreps, showplot = TRUE)

Arguments

df

The subject-level dosing data, including the following variables:

  • time: The gap time to the next drug dispensing visit.

  • skipped: The number of skipped visits.

  • k1: The covariate for the linear regression. It equals skipped for the gap time between randomization and the first drug dispensing visit and skipped + 1 for the gap time between two consecutive drug dispensing visits.

model

The model used to analyze the gap time. Options include "least squares" and "least absolute deviations".

nreps

The number of simulations for drawing posterior model parameter values.

showplot

A Boolean variable that controls whether or not to show the fitted gap time bar chart. It defaults to TRUE.

Value

A list with three components:

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

Examples

library(dplyr)

observed <- f_dose_observed(df2, visitview2, showplot = FALSE)
vf <- observed$vf

vf <- vf %>% left_join(dosing_schedule_df, by = "kit")

df_ti <- vf %>%
  mutate(time = lead(day) - day,
         skipped = pmax(floor((time - target_days/2)/target_days), 0),
         k1 = skipped + 1) %>%
  filter(row_id < n())

ti_fit <- f_fit_ti(df_ti, model = "least squares", nreps = 200)


Observed Dosing for Ongoing and New Subjects

Description

Prepares the dosing data sets to impute for ongoing and new subjects.

Usage

f_ongoing_new(newEvents, kit_description_df, treatment_by_drug_df, vf)

Arguments

newEvents

A data frame containing the imputed event data for both ongoing and new patients, typically obtained from the output of the getPrediction function of the eventPred package. It contains the following variables: draw, usubjid, arrivalTime, treatment, treatment_description, time, event, dropout, and totalTime.

kit_description_df

A data frame indicating the drug and kit descriptions, including the following variables: drug, drug_name, kit, and kit_name. It must be specified at the design stage. It will be replaced with the observed information at the analysis stage.

treatment_by_drug_df

A data frame indicating the treatments associated with each drug, including the following variables: treatment and drug.

vf

A data frame for subject-level drug dispensing data, including the following variables: drug, drug_name, kit, kit_name, usubjid, treatment, treatment_description, arrivalTime, time, event, dropout, day, dose, cum_dose, and row_id.

Value

A list with the following components:

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

Examples



set.seed(2000)

pred <- eventPred::getPrediction(
  df = df2,
  to_predict = "event only",
  target_d = 250,
  event_model = "log-logistic",
  dropout_model = "none",
  pilevel = 0.95,
  nyears = 1,
  nreps = 200,
  showplot = FALSE,
  by_treatment = TRUE)

observed <- f_dose_observed(df = df2, visitview = visitview2)

vf_ongoing_new <- f_ongoing_new(
  pred$event_pred$newEvents,
  observed$kit_description_df,
  observed$treatment_by_drug_df,
  observed$vf)

head(vf_ongoing_new$vf_ongoing)
head(vf_ongoing_new$vf_new)



The kit description data frame.

Description

A data frame with the following columns:

drug

The numeric code of the drug.

drug_name

The name of the drug.

kit

The numeric code of the kit type.

kit_name

The name of the kit type.

p_kit

The prior probability of different kit types within a drug.

Usage

kit_description_df

Format

An object of class tbl_df (inherits from tbl, data.frame) with 4 rows and 5 columns.


Random Number Generator for the Dirichlet Distribution

Description

Generates cell probabilities from the Dirichlet distribution.

Usage

rdirichlet(n = 1, alpha)

Arguments

n

The number of observations.

alpha

The shape parameters of the Dirichlet distribution.

Value

A matrix of n rows and k columns, where n is the number of observations and k is the number of cells.

Author(s)

Kaifeng Lu, kaifenglu@gmail.com

Examples


rdirichlet(2, c(50, 20, 30))


The data frame indicating the treatments associated with each drug.

Description

A data frame with the following columns:

treatment

The numeric code of the treatment group.

drug

The numeric code of the drug.

Usage

treatment_by_drug_df

Format

An object of class tbl_df (inherits from tbl, data.frame) with 6 rows and 2 columns.


The observed subject drug dispensing data before enrollment completion.

Description

A data frame with the following columns:

usubjid

The unique subject ID.

visit

The drug dispensing visit, e.g., "Cycle 1 Day 1".

date

The date of the drug dispensing visit.

drug

The numeric code of the drug.

drug_name

The name of the drug.

kit

The numeric code of the kit type.

kit_name

The name of the kit type.

kit_number

The kit number for drug dispensing.

dispensed_quantity

The number of kits dispensed at the visit.

Usage

visitview1

Format

An object of class tbl_df (inherits from tbl, data.frame) with 2290 rows and 9 columns.


The observed subject drug dispensing data after enrollment completion.

Description

A data frame with the following columns:

usubjid

The unique subject ID.

visit

The drug dispensing visit, e.g., "Cycle 1 Day 1".

date

The date of the drug dispensing visit.

drug

The numeric code of the drug.

drug_name

The name of the drug.

kit

The numeric code of the kit type.

kit_name

The name of the kit type.

kit_number

The kit number for drug dispensing.

dispensed_quantity

The number of kits dispensed at the visit.

Usage

visitview2

Format

An object of class tbl_df (inherits from tbl, data.frame) with 5006 rows and 9 columns.