Skip to content

Commit

Permalink
Finished Simulation Rework
Browse files Browse the repository at this point in the history
  • Loading branch information
gowerc committed Mar 13, 2024
1 parent edcac8f commit bc6cb23
Show file tree
Hide file tree
Showing 53 changed files with 1,320 additions and 636 deletions.
7 changes: 0 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ S3method(summary,Quantities)
S3method(summary,SurvivalQuantities)
S3method(write_stan,JointModel)
export(.JointModelSamples)
export(.SimLongitudinalSteinFojo)
export(DataJoint)
export(DataLongitudinal)
export(DataSubject)
Expand Down Expand Up @@ -141,9 +140,6 @@ export(brierScore)
export(compileStanModel)
export(enableLink)
export(generateQuantities)
export(gsf_dsld)
export(gsf_sld)
export(gsf_ttg)
export(initialValues)
export(linkDSLD)
export(linkIdentity)
Expand All @@ -167,9 +163,6 @@ export(prior_uniform)
export(sampleObservations)
export(sampleStanModel)
export(sampleSubjects)
export(sf_dsld)
export(sf_sld)
export(sf_ttg)
export(show)
export(write_stan)
exportClasses(DataJoint)
Expand Down
2 changes: 1 addition & 1 deletion R/DataJoint.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ setValidity(
#'
#' @description
#' Coerces a data object into a `list` of data components required
#' for fitting a [`JointModel`]. See the vignette (TODO) for more details.
#' for fitting a [`JointModel`]. See the "Extending jmpost" vignette for more details.
#'
#' @name as_stan_list.DataObject
#' @family as_stan_list
Expand Down
45 changes: 20 additions & 25 deletions R/SimJointData.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
# TODO - DOCS

#' @exportClass SimJointData
.SimJointData <- setClass(
"SimJointData",
slots = list(
longitudinal = "data.frame",
survival = "data.frame"
)
)




#' Simulating Joint Longitudinal and Time-to-Event Data
#'
#' @param design (`list`)\cr a list of [`SimGroup`] objects. See details.
#' @param times (`numeric`)\cr time grid, e.g. specifying the days after randomization.
#' @param lambda_cen (`number`)\cr rate of the exponential censoring distribution.
#' @param beta_cont (`number`)\cr coefficient for the continuous covariate.
#' @param beta_cat (`numeric`)\cr coefficients for the categorical covariate levels.
#' @param longitudinal (`function`)\cr function of `lm_base` generating the longitudinal model outcomes.
#' @param survival (`function`)\cr function of `lm_base` generating the survival model outcomes.
#' @param longitudinal ([`SimLongitudinal`])\cr object specifying how to simulate the longitudinal data
#' @param survival ([`SimSurvival`])\cr object specifying how to simulate the survival data
#' @param .silent (`flag`)\cr whether to suppress info messages
#' @param .debug (`flag`)\cr whether to enter debug mode such that the function
#' would only return a subset of columns.
#'
#' @slot longitudinal (`data.frame`)\cr the simulated longitudinal data.
#' @slot survival (`data.frame`)\cr the simulated survival data.
#'
#' @details
#'
Expand All @@ -38,7 +21,20 @@
#' )
#' ```
#'
#' @returns List with simulated `lm` (longitudinal) and `os` (survival) data sets.
#' @name SimJointData-class
#' @exportClass SimJointData
.SimJointData <- setClass(
"SimJointData",
slots = list(
longitudinal = "data.frame",
survival = "data.frame"
)
)




#' @rdname SimJointData-class
#' @export
SimJointData <- function(
design = list(
Expand All @@ -47,8 +43,7 @@ SimJointData <- function(
),
longitudinal,
survival,
.silent = FALSE,
.debug = FALSE
.silent = FALSE
) {

assert(
Expand Down
11 changes: 10 additions & 1 deletion R/SimLongitudinal.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# TODO - docs

#' Abstract Simulation Class for Longitudinal Data
#'
#' @param times (`numeric`) the times to generate observations at.
#'
#' @description
#' This class exists to be extended by other classes that simulate longitudinal data.
#' It is not intended to be used directly.
#' @name SimLongitudinal-class
#' @family SimLongitudinal
#' @exportClass SimLongitudinal
.SimLongitudinal <- setClass(
"SimLongitudinal",
Expand All @@ -8,6 +16,7 @@
)
)

#' @rdname SimLongitudinal-class
#' @export
SimLongitudinal <- function(times = seq(0, 100, 50)) {
.SimLongitudinal(times = times)
Expand Down
47 changes: 26 additions & 21 deletions R/SimLongitudinalGSF.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@


# TODO - Docs

#' Construct a Simulation Function for Longitudinal Data from GSF Model
#' Simulate Longitudinal Data from a GSF Model
#'
#' @param times (`numeric`)\cr the times to generate observations at.
#' @param sigma (`number`)\cr the variance of the longitudinal values.
#' @param mu_s (`numeric`)\cr the mean shrinkage rates for the two treatment arms.
#' @param mu_g (`numeric`)\cr the mean growth rates for the two treatment arms.
Expand All @@ -17,9 +15,20 @@
#' @param link_ttg (`number`)\cr the link coefficient for the time-to-growth contribution.
#' @param link_identity (`number`)\cr the link coefficient for the SLD Identity contribution.
#'
#' @returns A function with argument `lm_base` that can be used to simulate
#' longitudinal data from the corresponding GSF model.
#'
#' @slot sigma (`numeric`)\cr See arguments.
#' @slot mu_s (`numeric`)\cr See arguments.
#' @slot mu_g (`numeric`)\cr See arguments.
#' @slot mu_b (`numeric`)\cr See arguments.
#' @slot omega_b (`numeric`)\cr See arguments.
#' @slot omega_s (`numeric`)\cr See arguments.
#' @slot omega_g (`numeric`)\cr See arguments.
#' @slot a_phi (`numeric`)\cr See arguments.
#' @slot b_phi (`numeric`)\cr See arguments.
#' @slot link_dsld (`numeric`)\cr See arguments.
#' @slot link_ttg (`numeric`)\cr See arguments.
#' @slot link_identity (`numeric`)\cr See arguments.
#' @family SimLongitudinal
#' @name SimLongitudinalGSF-class
#' @exportClass SimLongitudinalGSF
.SimLongitudinalGSF <- setClass(
"SimLongitudinalGSF",
Expand All @@ -40,6 +49,7 @@
)
)

#' @rdname SimLongitudinalGSF-class
#' @export
SimLongitudinalGSF <- function(
times = c(-100, -50, 0, 50, 100, 150, 250, 350, 450, 550) / 365,
Expand Down Expand Up @@ -100,6 +110,7 @@ setValidity(
)


#' @rdname sampleObservations
#' @export
sampleObservations.SimLongitudinalGSF <- function(object, times_df) {
times_df |>
Expand All @@ -116,23 +127,26 @@ sampleObservations.SimLongitudinalGSF <- function(object, times_df) {
}


#' @rdname sampleSubjects
#' @export
sampleSubjects.SimLongitudinalGSF <- function(object, subjects_df) {
assert_that(
length(subjects_df$study) == length(object@mu_b),
length(subjects_df$arm) == length(object@mu_s),
is.factor(subjects_df$study),
is.factor(subjects_df$arm)
is.factor(subjects_df$arm),
length(levels(subjects_df$study)) == length(object@mu_b),
length(levels(subjects_df$arm)) == length(object@mu_s)
)

subjects_df |>
res <- subjects_df |>
dplyr::distinct(.data$pt, .data$arm, .data$study) |>
dplyr::mutate(study_idx = as.numeric(.data$study)) |>
dplyr::mutate(arm_idx = as.numeric(.data$arm)) |>
dplyr::mutate(psi_b = stats::rlnorm(dplyr::n(), log(object@mu_b[.data$study_idx]), object@omega_b)) |>
dplyr::mutate(psi_s = stats::rlnorm(dplyr::n(), log(object@mu_s[.data$arm_idx]), object@omega_s)) |>
dplyr::mutate(psi_g = stats::rlnorm(dplyr::n(), log(object@mu_g[.data$arm_idx]), object@omega_g)) |>
dplyr::mutate(psi_phi = stats::rbeta(dplyr::n(), object@a_phi[.data$arm_idx], object@b_phi[.data$arm_idx]))

res[, c("pt", "arm", "study", "psi_b", "psi_s", "psi_g", "psi_phi")]
}


Expand All @@ -149,21 +163,15 @@ sampleSubjects.SimLongitudinalGSF <- function(object, subjects_df) {
#' @param phi (`number`)\cr shrinkage proportion.
#'
#' @returns The function results.
#' @export
#' @keywords internal
#'
#' @examples
#' gsf_sld(1:10, 20, 0.3, 0.6, 0.2)
#' @keywords internal
gsf_sld <- function(time, b, s, g, phi) {
phi <- dplyr::if_else(time >= 0, phi, 0)
b * (phi * exp(-s * time) + (1 - phi) * exp(g * time))
}


#' @rdname gsf_sld
#' @export
#' @examples
#' gsf_ttg(1:10, 20, 0.3, 0.6, 0.2)
gsf_ttg <- function(time, b, s, g, phi) {
t1 <- (log(s * phi / (g * (1 - phi))) / (g + s))
t1[t1 <= 0] <- 0
Expand All @@ -172,9 +180,6 @@ gsf_ttg <- function(time, b, s, g, phi) {


#' @rdname gsf_sld
#' @export
#' @examples
#' gsf_dsld(1:10, 20, 0.3, 0.6, 0.2)
gsf_dsld <- function(time, b, s, g, phi) {
phi <- dplyr::if_else(time >= 0, phi, 0)
t1 <- (1 - phi) * g * exp(g * time)
Expand Down
21 changes: 14 additions & 7 deletions R/SimLongitudinalRandomSlope.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@


# TODO - Docs

#' Construct a Simulation Function for Longitudinal Data from Random Slope Model
#' Simulate Longitudinal Data from a Random Slope Model
#'
#' @param times (`numeric`)\cr the times to generate observations at.
#' @param intercept (`number`)\cr the mean baseline value for each study.
#' @param slope_mu (`numeric`)\cr the population slope for each treatment arm.
#' @param slope_sigma (`number`)\cr the random slope standard deviation.
#' @param sigma (`number`)\cr the variance of the longitudinal values.
#' @param link_dsld (`number`)\cr the link coefficient for the DSLD contribution.
#' @param link_identity (`number`)\cr the link coefficient for the identity contribution.
#'
#' @returns A function with argument `lm_base` that can be used to simulate
#' longitudinal data from the corresponding random slope model.
#' @slot intercept (`numeric`)\cr See arguments.
#' @slot slope_mu (`numeric`)\cr See arguments.
#' @slot slope_sigma (`numeric`)\cr See arguments.
#' @slot sigma (`numeric`)\cr See arguments.
#' @slot link_dsld (`numeric`)\cr See arguments.
#' @slot link_identity (`numeric`)\cr See arguments.
#'
#' @family SimLongitudinal
#' @name SimLongitudinalRandomSlope-class
#' @exportClass SimLongitudinalRandomSlope
.SimLongitudinalRandomSlope <- setClass(
"SimLongitudinalRandomSlope",
Expand All @@ -27,6 +32,7 @@
)
)

#' @rdname SimLongitudinalRandomSlope-class
#' @export
SimLongitudinalRandomSlope <- function(
times = c(-100, -50, 0, 50, 100, 150, 250, 350, 450, 550),
Expand All @@ -48,6 +54,7 @@ SimLongitudinalRandomSlope <- function(
)
}

#' @rdname sampleObservations
#' @export
sampleObservations.SimLongitudinalRandomSlope <- function(object, times_df) {
times_df |>
Expand All @@ -61,7 +68,7 @@ sampleObservations.SimLongitudinalRandomSlope <- function(object, times_df) {
)
}


#' @rdname sampleSubjects
#' @export
sampleSubjects.SimLongitudinalRandomSlope <- function(object, subjects_df) {
assert_that(
Expand Down
Loading

0 comments on commit bc6cb23

Please sign in to comment.