Skip to content

Commit

Permalink
Merge branch 'main' into 402-individual-loo
Browse files Browse the repository at this point in the history
  • Loading branch information
gowerc authored Aug 27, 2024
2 parents 7e141a4 + f50c5d0 commit bb458ad
Show file tree
Hide file tree
Showing 21 changed files with 456 additions and 5 deletions.
8 changes: 6 additions & 2 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,8 +60,9 @@ Suggests:
purrr,
vdiffr,
prodlim,
loo,
brms
brms,
tidyr,
loo
Additional_repositories: https://stan-dev.r-universe.dev
Config/testthat/edition: 3
Config/testthat/parallel: true
Expand Down Expand Up @@ -111,6 +114,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.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ sim_data <- SimJointData(
gamma = 0.97
)
)
#> INFO: 1 subjects 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.

26 changes: 26 additions & 0 deletions tests/testthat/_snaps/SurvivalGamma.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Print method for SurvivalGamma works as expected

Code
x <- SurvivalGamma()
print(x)
Output
Gamma Survival Model with parameters:
sm_gamma_k ~ gamma(alpha = 2, beta = 0.5)
sm_gamma_theta ~ gamma(alpha = 2, beta = 0.5)
beta_os_cov ~ normal(mu = 0, sigma = 2)

---

Code
x <- SurvivalGamma(k = prior_gamma(3, 4), theta = prior_cauchy(0, 1))
print(x)
Output
Gamma Survival Model with parameters:
sm_gamma_k ~ gamma(alpha = 3, beta = 4)
sm_gamma_theta ~ cauchy(mu = 0, sigma = 1)
beta_os_cov ~ normal(mu = 0, sigma = 2)

Loading

0 comments on commit bb458ad

Please sign in to comment.