Skip to content

Commit

Permalink
Implement growth link function (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
gowerc authored May 22, 2024
1 parent 9d2ccdc commit 9e51ea6
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 23 deletions.
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ S3method(linkDSLD,LongitudinalRandomSlope)
S3method(linkDSLD,LongitudinalSteinFojo)
S3method(linkDSLD,PromiseLongitudinalModel)
S3method(linkDSLD,default)
S3method(linkGrowth,LongitudinalGSF)
S3method(linkGrowth,LongitudinalRandomSlope)
S3method(linkGrowth,LongitudinalSteinFojo)
S3method(linkGrowth,PromiseLongitudinalModel)
S3method(linkGrowth,default)
S3method(linkIdentity,LongitudinalGSF)
S3method(linkIdentity,LongitudinalRandomSlope)
S3method(linkIdentity,LongitudinalSteinFojo)
Expand Down Expand Up @@ -198,6 +203,7 @@ export(generateQuantities)
export(getPredictionNames)
export(initialValues)
export(linkDSLD)
export(linkGrowth)
export(linkIdentity)
export(linkNone)
export(linkTTG)
Expand Down
9 changes: 9 additions & 0 deletions R/LongitudinalGSF.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ linkIdentity.LongitudinalGSF <- function(prior = prior_normal(0, 2), model, ...)
)
}

#' @export
linkGrowth.LongitudinalGSF <- function(prior = prior_normal(0, 2), model, ...) {
LinkComponent(
key = "link_growth",
stan = StanModule("lm-gsf/link_growth.stan"),
prior = prior
)
}


#' @rdname getPredictionNames
#' @export
Expand Down
8 changes: 8 additions & 0 deletions R/LongitudinalRandomSlope.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ linkIdentity.LongitudinalRandomSlope <- function(prior = prior_normal(0, 2), mod
)
}

#' @export
linkGrowth.LongitudinalRandomSlope <- function(prior = prior_normal(0, 2), model, ...) {
LinkComponent(
key = "link_growth",
stan = StanModule("lm-random-slope/link_growth.stan"),
prior = prior
)
}

#' @rdname getPredictionNames
#' @export
Expand Down
9 changes: 9 additions & 0 deletions R/LongitudinalSteinFojo.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ linkIdentity.LongitudinalSteinFojo <- function(prior = prior_normal(0, 2), model
)
}

#' @export
linkGrowth.LongitudinalSteinFojo <- function(prior = prior_normal(0, 2), model, ...) {
LinkComponent(
key = "link_growth",
stan = StanModule("lm-stein-fojo/link_growth.stan"),
prior = prior
)
}

#' @rdname getPredictionNames
#' @export
getPredictionNames.LongitudinalSteinFojo <- function(object, ...) {
Expand Down
21 changes: 16 additions & 5 deletions R/SimLongitudinalGSF.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ NULL
#' @param link_dsld (`number`)\cr the link coefficient for the derivative contribution.
#' @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.
#' @param link_growth (`number`)\cr the link coefficient for the growth parameter contribution.
#'
#' @slot sigma (`numeric`)\cr See arguments.
#' @slot mu_s (`numeric`)\cr See arguments.
Expand All @@ -32,6 +33,7 @@ NULL
#' @slot link_dsld (`numeric`)\cr See arguments.
#' @slot link_ttg (`numeric`)\cr See arguments.
#' @slot link_identity (`numeric`)\cr See arguments.
#' @slot link_growth (`numeric`)\cr See arguments.
#' @family SimLongitudinal
#' @name SimLongitudinalGSF-class
#' @exportClass SimLongitudinalGSF
Expand All @@ -50,7 +52,8 @@ NULL
omega_g = "numeric",
link_dsld = "numeric",
link_ttg = "numeric",
link_identity = "numeric"
link_identity = "numeric",
link_growth = "numeric"
)
)

Expand All @@ -69,7 +72,8 @@ SimLongitudinalGSF <- function(
omega_g = 0.2,
link_dsld = 0,
link_ttg = 0,
link_identity = 0
link_identity = 0,
link_growth = 0
) {
.SimLongitudinalGSF(
times = times,
Expand All @@ -84,7 +88,8 @@ SimLongitudinalGSF <- function(
omega_g = omega_g,
link_dsld = link_dsld,
link_ttg = link_ttg,
link_identity = link_identity
link_identity = link_identity,
link_growth = link_growth
)
}

Expand All @@ -102,7 +107,12 @@ setValidity(
return("The parameters `mu_s`, `mu_g`, `a_phi`, and `b_phi` must have the same length.")
}

for (par in c("sigma", "omega_b", "omega_s", "omega_g", "link_dsld", "link_ttg", "link_identity")) {
len_1_pars <- c(
"sigma", "omega_b", "omega_s", "omega_g",
"link_dsld", "link_ttg", "link_identity",
"link_growth"
)
for (par in len_1_pars) {
if (length(slot(object, par)) != 1) {
return(sprintf("The `%s` parameter must be a length 1 numeric.", par))
}
Expand All @@ -129,7 +139,8 @@ sampleObservations.SimLongitudinalGSF <- function(object, times_df) {
log_haz_link =
(object@link_dsld * .data$dsld) +
(object@link_ttg * .data$ttg) +
(object@link_identity * .data$mu_sld)
(object@link_identity * .data$mu_sld) +
(object@link_growth * .data$psi_g)
)
}

Expand Down
31 changes: 19 additions & 12 deletions R/SimLongitudinalSteinFojo.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ NULL
#' @param link_dsld (`number`)\cr the link coefficient for the derivative contribution.
#' @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.
#' @param link_growth (`number`)\cr the link coefficient for the growth parameter contribution.
#'
#' @slot sigma (`numeric`)\cr See arguments.
#' @slot mu_s (`numeric`)\cr See arguments.
Expand All @@ -27,6 +28,7 @@ NULL
#' @slot link_dsld (`numeric`)\cr See arguments.
#' @slot link_ttg (`numeric`)\cr See arguments.
#' @slot link_identity (`numeric`)\cr See arguments.
#' @slot link_growth (`numeric`)\cr See arguments.
#'
#' @family SimLongitudinal
#' @name SimLongitudinalSteinFojo-class
Expand All @@ -44,7 +46,8 @@ NULL
omega_g = "numeric",
link_dsld = "numeric",
link_ttg = "numeric",
link_identity = "numeric"
link_identity = "numeric",
link_growth = "numeric"
)
)

Expand All @@ -61,7 +64,8 @@ SimLongitudinalSteinFojo <- function(
omega_g = 0.2,
link_dsld = 0,
link_ttg = 0,
link_identity = 0
link_identity = 0,
link_growth = 0
) {
.SimLongitudinalSteinFojo(
times = times,
Expand All @@ -74,7 +78,8 @@ SimLongitudinalSteinFojo <- function(
omega_g = omega_g,
link_dsld = link_dsld,
link_ttg = link_ttg,
link_identity = link_identity
link_identity = link_identity,
link_growth = link_growth
)
}

Expand All @@ -89,14 +94,15 @@ setValidity(
if (length(unique(par_lengths)) != 1) {
return("The parameters `mu_s` and `mu_g` must have the same length.")
}
if (length(object@sigma) != 1) {
return("The parameter `sigma` must have length 1.")
}
if (length(c(object@omega_b, object@omega_s, object@omega_g)) != 3) {
return("The parameters `omega_b`, `omega_s`, and `omega_g` must be length 1.")
}
if (length(c(object@link_dsld, object@link_ttg, object@link_identity)) != 3) {
return("The parameters `link_dsld`, `link_ttg`, and `link_identity` must be length 1.")
len_1_pars <- c(
"sigma", "omega_b", "omega_s", "omega_g",
"link_dsld", "link_ttg", "link_identity",
"link_growth"
)
for (par in len_1_pars) {
if (length(slot(object, par)) != 1) {
return(sprintf("The `%s` parameter must be a length 1 numeric.", par))
}
}
return(TRUE)
}
Expand All @@ -119,7 +125,8 @@ sampleObservations.SimLongitudinalSteinFojo <- function(object, times_df) {
log_haz_link =
(object@link_dsld * .data$dsld) +
(object@link_ttg * .data$ttg) +
(object@link_identity * .data$mu_sld)
(object@link_identity * .data$mu_sld) +
(object@link_growth * .data$psi_g)
)
}

Expand Down
15 changes: 15 additions & 0 deletions R/link_generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,18 @@ linkIdentity.PromiseLongitudinalModel <- function(prior = prior_normal(0, 2), mo
linkIdentity.default <- function(prior, model, ...) {
stop(sprintf("Method `linkIdentity` is not available for `%s`", class(model)[[1]]))
}


#' @describeIn standard-link-user Growth Parameter link
#' @export
linkGrowth <- function(prior, model = PromiseLongitudinalModel(), ...) {
UseMethod("linkGrowth", model)
}
#' @export
linkGrowth.PromiseLongitudinalModel <- function(prior = prior_normal(0, 2), model, ...) {
PromiseLinkComponent(fun = linkGrowth, prior = prior, key = "link_growth")
}
#' @export
linkGrowth.default <- function(prior, model, ...) {
stop(sprintf("Method `linkGrowth` is not available for `%s`", class(model)[[1]]))
}
15 changes: 15 additions & 0 deletions inst/stan/lm-gsf/link_growth.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


functions {
//
// Source - lm-gsf/link_growth.stan
//
matrix link_growth_contrib(
matrix time,
matrix link_function_inputs
) {
int nrows = rows(link_function_inputs);
int ncols = cols(time);
return rep_matrix(link_function_inputs[,3], ncols);
}
}
18 changes: 18 additions & 0 deletions inst/stan/lm-random-slope/link_growth.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

functions {
//
// Source - lm-random-slope/link_dsld.stan
//
matrix link_growth_contrib(
matrix time,
matrix link_function_inputs
) {
int nrows = rows(time);
int ncols = cols(time);
vector[nrows] lm_rs_ind_rnd_slope = link_function_inputs[,2];
matrix[nrows, ncols] rnd_slope_mat = rep_matrix(lm_rs_ind_rnd_slope, ncols);
return rnd_slope_mat;
}
}


15 changes: 15 additions & 0 deletions inst/stan/lm-stein-fojo/link_growth.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


functions {
//
// Source - lm-stein/link_growth.stan
//
matrix link_growth_contrib(
matrix time,
matrix link_function_inputs
) {
int nrows = rows(link_function_inputs);
int ncols = cols(time);
return rep_matrix(link_function_inputs[,3], ncols);
}
}
7 changes: 6 additions & 1 deletion man/SimLongitudinalGSF-class.Rd

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

7 changes: 6 additions & 1 deletion man/SimLongitudinalSteinFojo-class.Rd

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

5 changes: 5 additions & 0 deletions man/standard-link-user.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-LongitudinalGSF.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test_that("Centralised parameterisation compiles without issues", {
jm <- JointModel(
longitudinal = LongitudinalGSF(centred = TRUE),
survival = SurvivalWeibullPH(),
link = Link(linkTTG(), linkDSLD())
link = Link(linkTTG(), linkDSLD(), linkGrowth())
)
expect_false(any(
c("lm_gsf_eta_tilde_kg", "lm_gsf_eta_tilde_bsld") %in% names(jm@parameters)
Expand Down
Loading

0 comments on commit 9e51ea6

Please sign in to comment.