Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Gamma Survival Model #413

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Description: Implements joint models combining a non-linear mixed effects model
License: Apache License (>= 2)
Encoding: UTF-8
Language: en-GB
URL: https://genentech.github.io/jmpost/
BugReports: https://github.com/Genentech/jmpost/issues
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Depends:
Expand Down Expand Up @@ -58,6 +60,7 @@ Suggests:
purrr,
vdiffr,
prodlim,
tidyr,
loo
Additional_repositories: https://stan-dev.r-universe.dev
Config/testthat/edition: 3
Expand Down Expand Up @@ -110,6 +113,7 @@ Collate:
'SimLongitudinalSteinFojo.R'
'SimSurvival.R'
'SurvivalExponential.R'
'SurvivalGamma.R'
'SurvivalLoglogistic.R'
'SurvivalWeibullPH.R'
'brier_score.R'
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,14 @@ export(SimLongitudinalRandomSlope)
export(SimLongitudinalSteinFojo)
export(SimSurvival)
export(SimSurvivalExponential)
export(SimSurvivalGamma)
export(SimSurvivalLogLogistic)
export(SimSurvivalWeibullPH)
export(StanModel)
export(StanModule)
export(Surv)
export(SurvivalExponential)
export(SurvivalGamma)
export(SurvivalLogLogistic)
export(SurvivalModel)
export(SurvivalQuantities)
Expand Down Expand Up @@ -287,6 +289,7 @@ exportClasses(SimSurvival)
exportClasses(StanModel)
exportClasses(StanModule)
exportClasses(SurvivalExponential)
exportClasses(SurvivalGamma)
exportClasses(SurvivalLogLogistic)
exportClasses(SurvivalModel)
exportClasses(SurvivalWeibullPH)
Expand All @@ -304,9 +307,11 @@ importFrom(stats,.checkMFClasses)
importFrom(stats,acf)
importFrom(stats,as.formula)
importFrom(stats,delete.response)
importFrom(stats,dgamma)
importFrom(stats,median)
importFrom(stats,model.frame)
importFrom(stats,model.matrix)
importFrom(stats,pgamma)
importFrom(stats,qlogis)
importFrom(stats,rbeta)
importFrom(stats,rcauchy)
Expand Down
39 changes: 38 additions & 1 deletion R/SimSurvival.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ sampleObservations.SimSurvival <- function(object, times_df) {
dplyr::mutate(event = 0)

if (!(nrow(os_had_censor) == 0)) {
message(sprintf("INFO: %i subjects did not die before max(times)", nrow(os_had_censor)))
message(sprintf("INFO: %i subject(s) did not die before max(times)", nrow(os_had_censor)))
}

os_dat_complete <- os_had_event |>
Expand Down Expand Up @@ -289,3 +289,40 @@ SimSurvivalExponential <- function(
name = "SimSurvivalExponential"
)
}


#' Simulate Survival Data from a Gamma Proportional Hazard Model
#'
#' @param k (`number`)\cr the shape parameter.
#' @param theta (`number`)\cr the scale parameter.
#'
#' @inheritParams SimSurvival-Shared
#' @inheritSection SimSurvival-Shared Hazard Evaluation
#'
#' @family SimSurvival
#'
#' @importFrom stats dgamma pgamma
#'
#' @export
SimSurvivalGamma <- function(
k,
theta,
time_max = 2000,
time_step = 1,
lambda_censor = 1 / 3000,
beta_cont = 0.2,
beta_cat = c("A" = 0, "B" = -0.4, "C" = 0.2)
) {
SimSurvival(
time_max = time_max,
time_step = time_step,
lambda_censor = lambda_censor,
beta_cont = beta_cont,
beta_cat = beta_cat,
loghazard = function(time) {
dgamma(time, k, scale = theta, log = TRUE) -
pgamma(time, k, scale = theta, lower.tail = FALSE, log.p = TRUE)
},
name = "SimSurvivalGamma"
)
}
45 changes: 45 additions & 0 deletions R/SurvivalGamma.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' @include SurvivalModel.R
NULL


#' `SurvivalGamma`
#'
#' This class extends the general [`SurvivalModel`] class for using the
#' Gamma survival model.
#'
#' @exportClass SurvivalGamma
.SurvivalGamma <- setClass(
Class = "SurvivalGamma",
contains = "SurvivalModel"
)

# SurvivalGamma-constructors ----

#' @rdname SurvivalGamma-class
#'
#' @param k (`Prior`)\cr for the shape `k`.
#' @param theta (`Prior`)\cr for the scale `theta`.
#' @param beta (`Prior`)\cr for covariates coefficients `beta`.
#'
#' @export
SurvivalGamma <- function(
k = prior_gamma(2, 0.5),
theta = prior_gamma(2, 0.5),
beta = prior_normal(0, 2)
) {

k <- set_limits(k, lower = 0)
theta <- set_limits(theta, lower = 0)

.SurvivalGamma(
SurvivalModel(
name = "Gamma",
stan = StanModule(x = "sm-gamma/model.stan"),
parameters = ParameterList(
Parameter(name = "sm_gamma_k", prior = k, size = 1),
Parameter(name = "sm_gamma_theta", prior = theta, size = 1),
Parameter(name = "beta_os_cov", prior = beta, size = "p_os_cov_design")
)
)
)
}
4 changes: 4 additions & 0 deletions R/SurvivalWeibullPH.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ SurvivalWeibullPH <- function(
gamma = prior_gamma(2, 0.5),
beta = prior_normal(0, 2)
) {

lambda <- set_limits(lambda, lower = 0)
gamma <- set_limits(gamma, lower = 0)

.SurvivalWeibullPH(
SurvivalModel(
name = "Weibull-PH",
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ sim_data <- SimJointData(
joint_data <- DataJoint(
subject = DataSubject(
data = sim_data@survival,
subject = "pt",
subject = "subject",
arm = "arm",
study = "study"
),
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

<!-- markdownlint-disable-file -->

<!-- README.md needs to be generated from README.Rmd. Please edit that file -->

# jmpost <a href="https://genentech.github.io/jmpost/"><img src="man/figures/logo.png" align="right" height="139" /></a>
Expand Down Expand Up @@ -93,7 +94,7 @@ sim_data <- SimJointData(
gamma = 0.97
)
)
#> INFO: 1 subject did not die before max(times)
#> INFO: 1 subject(s) did not die before max(times)

joint_data <- DataJoint(
subject = DataSubject(
Expand Down
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ reference:
- SimSurvivalLogLogistic
- SimSurvivalExponential
- SimSurvivalWeibullPH
- SimSurvivalGamma
- SimGroup
- title: Prior Distributions
contents:
Expand Down Expand Up @@ -79,6 +80,7 @@ reference:
- SurvivalLogLogistic
- SurvivalModel
- SurvivalWeibullPH
- SurvivalGamma


- title: Link Specification
Expand Down
33 changes: 33 additions & 0 deletions inst/stan/sm-gamma/model.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@


functions {
//
// Source - sm-gamma/model.stan
//
matrix log_h0(matrix time, vector pars_os) {
real k = pars_os[1];
real theta = pars_os[2];
matrix[rows(time), cols(time)] result;
result = (k - 1) .* log(time) - (time ./ theta) -
(k * log(theta)) - log(tgamma(k) * (1 - gamma_p(k, time ./ theta)));
return result;
}
}


parameters {
//
// Source - sm-gamma/model.stan
//
real<lower={{ machine_double_eps }}> sm_gamma_k;
real<lower={{ machine_double_eps }}> sm_gamma_theta;
}


transformed parameters {
//
// Source - sm-gamma/model.stan
//
vector[2] pars_os = [sm_gamma_k, sm_gamma_theta]';
}

1 change: 1 addition & 0 deletions man/SimSurvival-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/SimSurvivalExponential.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions man/SimSurvivalGamma.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/SimSurvivalLogLogistic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/SimSurvivalWeibullPH.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions man/SurvivalGamma-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions man/jmpost-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading