Skip to content

Commit 1c7afa7

Browse files
authored
Merge pull request #43 from generable/develop
v0.2.7
2 parents 68d195c + cde35f8 commit 1c7afa7

5 files changed

Lines changed: 59 additions & 18 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: bmstate
22
Type: Package
33
Title: Bayesian multistate modeling
4-
Version: 0.2.6
4+
Version: 0.2.7
55
Authors@R:
66
c(person(given = "Juho",
77
family = "Timonen",

R/MultistateModel.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ MultistateModel <- R6::R6Class("MultistateModel",
192192
invisible(NULL)
193193
},
194194

195+
#' @description Set assumed prior mean baseline hazard rates (side
196+
#' effect) based on average hazards in data.
197+
#' @param data A \code{\link{JointData}} or \code{\link{PathData}} object.
198+
set_prior_mean_h0_data = function(data) {
199+
if (inherits(data, "PathData")) {
200+
data <- JointData$new(data, NULL)
201+
}
202+
checkmate::assert_class(data, "JointData")
203+
df_ttype <- average_haz_per_ttype(data$paths) |> dplyr::arrange(.data$trans_idx)
204+
self$set_prior_mean_h0(exp(df_ttype$log_h0_avg))
205+
},
195206

196207
#' @description Create model
197208
#'

R/stan.R

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ ensure_exposed_stan_functions <- function(...) {
5757
#' @param model A \code{\link{MultistateModel}} object.
5858
#' @param data A \code{\link{JointData}} or \code{\link{PathData}} object.
5959
#' @param return_stanfit Return also the raw 'Stan' fit object?
60-
#' @param set_auc_normalizers Set AUC normalization based on SS doses.
6160
#' @inheritParams create_stan_model
61+
#' @param set_normalizers Set covariate normalization automatically?
62+
#' @param set_prior_h0 Set prior mean average baseline hazard levels based
63+
#' on data?
6264
#' @param max_conc_factor Factor to multiply observed max concentration by
6365
#' to get concentration upper bound.
6466
#' @param method Must be one of \code{"sample"} (default),
@@ -69,7 +71,8 @@ ensure_exposed_stan_functions <- function(...) {
6971
#' @return A \code{\link{MultistateModelFit}} object.
7072
#' @family Stan-related functions
7173
fit_stan <- function(model, data,
72-
set_auc_normalizers = TRUE,
74+
set_normalizers = TRUE,
75+
set_prior_h0 = TRUE,
7376
filepath = NULL,
7477
return_stanfit = FALSE,
7578
max_conc_factor = 100,
@@ -82,20 +85,28 @@ fit_stan <- function(model, data,
8285
checkmate::assert_choice(method, c("sample", "pathfinder", "optimize"))
8386

8487
checkmate::assert_logical(return_stanfit, len = 1)
85-
checkmate::assert_logical(set_auc_normalizers, len = 1)
88+
checkmate::assert_logical(set_normalizers, len = 1)
89+
checkmate::assert_logical(set_prior_h0, len = 1)
8690
prefit_checks(model, data)
8791

8892
# Get Stan model object
8993
stan_model <- create_stan_model(filepath = filepath)
9094

91-
# Set normalizing locations and scales (side effect)
92-
model$set_normalizers(data)
93-
if (!is.null(data$dosing) && set_auc_normalizers) {
94-
mu_CL <- exp(-2)
95-
aaa <- data$dosing$dose_ss / mu_CL
96-
loc <- mean(aaa)
97-
sca <- stats::sd(aaa)
98-
model$set_auc_normalizers(loc, sca)
95+
# Set prior mean baseline hazard rates (side effect)
96+
if (set_prior_h0) {
97+
model$set_prior_mean_h0_data(data)
98+
}
99+
100+
# Set covariate normalizing locations and scales (side effect)
101+
if (set_normalizers) {
102+
model$set_normalizers(data)
103+
if (!is.null(data$dosing)) {
104+
mu_CL <- exp(-2)
105+
aaa <- data$dosing$dose_ss / mu_CL
106+
loc <- mean(aaa)
107+
sca <- stats::sd(aaa)
108+
model$set_auc_normalizers(loc, sca)
109+
}
99110
}
100111

101112
# Set max PK concentration
@@ -106,10 +117,6 @@ fit_stan <- function(model, data,
106117
model$pk_model$set_max_conc(MC)
107118
}
108119

109-
# Set prior mean baseline hazard rates (side effect)
110-
df_ttype <- average_haz_per_ttype(data$paths) |> dplyr::arrange(.data$trans_idx)
111-
model$set_prior_mean_h0(exp(df_ttype$log_h0_avg))
112-
113120
# Create Stan input list
114121
sd <- create_stan_data(model, data)
115122

man/MultiStateModel.Rd

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fit_stan.Rd

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)