Skip to content

Commit

Permalink
Document custom longitudinal models (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
gowerc authored May 22, 2024
1 parent 9e51ea6 commit 65f56a0
Show file tree
Hide file tree
Showing 14 changed files with 614 additions and 12 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ Suggests:
flexsurv,
purrr,
vdiffr,
prodlim
prodlim,
loo
Config/testthat/edition: 3
Collate:
'DataSubject.R'
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export(brierScore)
export(compileStanModel)
export(enableLink)
export(generateQuantities)
export(getParameters)
export(getPredictionNames)
export(initialValues)
export(linkDSLD)
Expand All @@ -211,6 +212,7 @@ export(merge)
export(prior_beta)
export(prior_cauchy)
export(prior_gamma)
export(prior_init_only)
export(prior_invgamma)
export(prior_logistic)
export(prior_loglogistic)
Expand Down Expand Up @@ -267,6 +269,7 @@ importFrom(ggplot2.utils,geom_km)
importFrom(glue,as_glue)
importFrom(stats,.checkMFClasses)
importFrom(stats,acf)
importFrom(stats,as.formula)
importFrom(stats,delete.response)
importFrom(stats,model.frame)
importFrom(stats,model.matrix)
Expand Down
3 changes: 2 additions & 1 deletion R/Prior.R
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ prior_beta <- function(a, b) {
)
}

#' Only Initial Values Specification
#' Initial Values Specification
#'
#' @param dist (`Prior`)\cr a prior Distribution
#' @family Prior
Expand All @@ -346,6 +346,7 @@ prior_beta <- function(a, b) {
#' This is primarily used for hierarchical parameters whose distributions
#' are fixed within the model and cannot be altered by the user.
#'
#' @export
prior_init_only <- function(dist) {
Prior(
parameters = list(),
Expand Down
2 changes: 1 addition & 1 deletion R/StanModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ as.list.StanModel <- function(x, ...) {

#' @rdname getParameters
#' @export
getParameters.StanModel <- function(object) object@parameters
getParameters.StanModel <- function(object, ...) object@parameters


#' @export
Expand Down
2 changes: 1 addition & 1 deletion R/defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ NULL

#' @rdname getParameters
#' @export
getParameters.default <- function(object) {
getParameters.default <- function(object, ...) {
if (missing(object) || is.null(object)) {
return(NULL)
}
Expand Down
9 changes: 7 additions & 2 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ as.StanModule <- function(object, ...) {
#' from a model.
#'
#' @param object where to obtain the parameters from.
#' @param ... additional options.
#'
#' @keywords internal
getParameters <- function(object) {
#' @export
getParameters <- function(object, ...) {
UseMethod("getParameters")
}

Expand Down Expand Up @@ -409,6 +410,9 @@ resolvePromise.default <- function(object, ...) {
enableLink <- function(object, ...) {
UseMethod("enableLink")
}
enableLink.default <- function(object, ...) {
object
}



Expand Down Expand Up @@ -439,6 +443,7 @@ as_formula <- function(x, ...) {
UseMethod("as_formula")
}

#' @importFrom stats as.formula
#' @export
as_formula.default <- function(x, ...) {
as.formula(x, ...)
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ reference:
- StanModel
- StanModule
- write_stan
- getParameters

- title: Convenience Functions
contents:
Expand Down
9 changes: 5 additions & 4 deletions man/getParameters.Rd

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

2 changes: 1 addition & 1 deletion man/prior_init_only.Rd

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

12 changes: 12 additions & 0 deletions vignettes/custom-model-dsld.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
functions {
// Provide definition for the dsld link function
matrix link_dsld_contrib(matrix time, matrix link_function_inputs) {
int nrow = rows(time);
int ncol = cols(time);
// broadcast input vectors to match the size of the time matrix
matrix[nrow, ncol] baseline = rep_matrix(link_function_inputs[,1], ncol);
matrix[nrow, ncol] shrinkage = rep_matrix(link_function_inputs[,2], ncol);
matrix[nrow, ncol] growth = rep_matrix(link_function_inputs[,3], ncol);
return growth - baseline .* shrinkage .* exp(- shrinkage .* time);
}
}
7 changes: 7 additions & 0 deletions vignettes/custom-model-enable-link.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
transformed parameters {
// Define matrix required for link functions
matrix[n_subjects, 3] link_function_inputs;
link_function_inputs[,1] = baseline_idv;
link_function_inputs[,2] = shrinkage_idv;
link_function_inputs[,3] = growth_idv;
}
Loading

0 comments on commit 65f56a0

Please sign in to comment.