Skip to content

Commit 68d195c

Browse files
authored
Merge pull request #42 from generable/develop
v0.2.6
2 parents 3243b93 + ca097d0 commit 68d195c

18 files changed

Lines changed: 102 additions & 140 deletions

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.5
4+
Version: 0.2.6
55
Authors@R:
66
c(person(given = "Juho",
77
family = "Timonen",

NAMESPACE

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ export(ensure_exposed_stan_functions)
2323
export(example_sim_setup_illnessdeath)
2424
export(fit_stan)
2525
export(generate_paths)
26-
export(msfit_average_hazard)
27-
export(msfit_plot_cumhaz)
2826
export(msmfit_exposure)
2927
export(msmfit_inst_hazard_param_draws)
3028
export(msmfit_log_hazard_multipliers)

R/MultistateModel.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#' This only has an effect when simulating data. When fitting a model, all
1010
#' covariates are treated as continuous, so you should use a binary encoding
1111
#' for categories if there is more than two.
12-
#' @param pk_covs \emph{Experimental}. Covariates that affect the PK parameters. A list with
12+
#' @param pk_covs Covariates that affect the PK parameters. A list with
1313
#' elements \code{ka} \code{CL}, and \code{V2}. If \code{NULL}, a PK model
1414
#' will not be created.
1515
#' @param ... Arguments passed to \code{\link{MultistateModel}} init
@@ -32,9 +32,9 @@ create_msm <- function(tm, hazard_covs = NULL, pk_covs = NULL,
3232
#'
3333
#' @export
3434
#' @field system A \code{\link{MultistateSystem}}
35-
#' @field pk_model \emph{Experimental}. A \code{\link{PKModel}} or NULL.
35+
#' @field pk_model A \code{\link{PKModel}} or NULL.
3636
#' @field prior_only Should the model ignore likelihood?
37-
#' @field pk_only \emph{Experimental}. Should the model ignore the entire
37+
#' @field pk_only Should the model ignore the entire
3838
#' hazard model part?
3939
MultistateModel <- R6::R6Class("MultistateModel",
4040

@@ -208,7 +208,7 @@ MultistateModel <- R6::R6Class("MultistateModel",
208208
#' for categories if there is more than two.
209209
#' @param n_grid Number of time discretization points for integrating
210210
#' @param prior_only Should the model ignore likelihood?
211-
#' @param pk_only \emph{Experimental}. Should the model ignore the entire
211+
#' @param pk_only Should the model ignore the entire
212212
#' hazard model part?
213213
#' hazards.
214214
initialize = function(system, covariates = NULL, pk_model = NULL,

R/PathData.R

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
# util
2-
check_columns <- function(df, needed_columns) {
3-
if (!(all(needed_columns %in% colnames(df)))) {
4-
message("found = {", paste(colnames(df), collapse = ", "), "}")
5-
message("required = {", paste(needed_columns, collapse = ", "), "}")
6-
stop("some needed columns are missing from df")
7-
}
8-
}
9-
101
#' Path data class (R6 class)
112
#'
123
#' @export
@@ -411,13 +402,7 @@ PathData <- R6::R6Class(
411402
)
412403
)
413404

414-
#' Fit Cox PH model using Breslow method
415-
#'
416-
#' @param msdat \code{msdata} object
417-
#' @param formula_rhs Formula right hand side that is appended to
418-
#' \code{Surv(Tstart, Tstop, status) ~ }. If \code{NULL} (default), then
419-
#' \code{strata(trans)} is used
420-
#' @return value returned by \code{survival::coxph}
405+
# Fit Cox PH model using Breslow method
421406
fit_coxph <- function(msdat, formula_rhs = NULL) {
422407
if (is.null(formula_rhs)) {
423408
formula_rhs <- "strata(trans)"
@@ -428,11 +413,7 @@ fit_coxph <- function(msdat, formula_rhs = NULL) {
428413
)
429414
}
430415

431-
#' Plot cumulative hazard of 'msfit'
432-
#'
433-
#' @export
434-
#' @param msfit An \code{msfit} object
435-
#' @param legend transition name legend
416+
# Plot cumulative hazard of 'msfit'
436417
msfit_plot_cumhaz <- function(msfit, legend = NULL) {
437418
df <- msfit$Haz
438419
if (!is.null(legend)) {
@@ -449,10 +430,7 @@ msfit_plot_cumhaz <- function(msfit, legend = NULL) {
449430
ylab("Cumulative Hazard")
450431
}
451432

452-
#' Estimate average hazard of an 'msfit'
453-
#'
454-
#' @export
455-
#' @param msfit An \code{msfit} object
433+
# Estimate average hazard of an 'msfit'
456434
msfit_average_hazard <- function(msfit) {
457435
msfit$Haz |>
458436
dplyr::group_by(.data$trans) |>
@@ -527,33 +505,14 @@ potential_covariates <- function(pd, possible = NULL, ...) {
527505
df
528506
}
529507

530-
#' PathData to time-to-event data format for any state other than null state
531-
#'
532-
#' @export
533-
#' @inheritParams as_single_event
534-
#' @return A \code{\link{PathData}} object
535-
as_any_event <- function(pd, null_state = "Randomization") {
536-
pd_new <- pd$clone(deep = TRUE)
537-
df <- pd_new$path_df
538-
idx <- which(pd_new$transmat$states == null_state)
539-
if (length(idx) != 1) {
540-
stop("error")
541-
}
542-
df$state[which(df$state == idx)] <- 1
543-
df$state[which(df$state != 1)] <- 2
544-
tm <- transmat_survival(state_names = c(null_state, "Any event"))
545-
pd_new$path_df <- df
546-
pd_new$transmat <- tm
547-
as_single_event(pd_new, "Any event", null_state)
548-
}
549-
550508
#' PathData to time-to-event data format with a single event
551509
#'
552510
#' @export
553511
#' @param pd A \code{\link{PathData}} object
554512
#' @param event Name of the state corresponding to the event of interest (character)
555513
#' @param null_state Name of the base state
556514
#' @return A \code{\link{PathData}} object
515+
#' @family PathData mutation functions
557516
as_single_event <- function(pd, event, null_state = "Randomization") {
558517
checkmate::assert_class(pd, "PathData")
559518
checkmate::assert_character(event, len = 1)
@@ -594,12 +553,34 @@ as_single_event <- function(pd, event, null_state = "Randomization") {
594553
)
595554
}
596555

556+
#' PathData to time-to-event data format for any state other than null state
557+
#'
558+
#' @export
559+
#' @inheritParams as_single_event
560+
#' @return A \code{\link{PathData}} object
561+
#' @family PathData mutation functions
562+
as_any_event <- function(pd, null_state = "Randomization") {
563+
pd_new <- pd$clone(deep = TRUE)
564+
df <- pd_new$path_df
565+
idx <- which(pd_new$transmat$states == null_state)
566+
if (length(idx) != 1) {
567+
stop("given null_state not a state of given pd")
568+
}
569+
df$state[which(df$state == idx)] <- 1
570+
df$state[which(df$state != 1)] <- 2
571+
tm <- transmat_survival(state_names = c(null_state, "Any event"))
572+
pd_new$path_df <- df
573+
pd_new$transmat <- tm
574+
as_single_event(pd_new, "Any event", null_state)
575+
}
576+
577+
597578
#' PathData to event-free survival format
598579
#'
599580
#' @export
600-
#' @param pd A \code{\link{PathData}} object
601-
#' @param event Name of the event of interest (character)
581+
#' @inheritParams as_single_event
602582
#' @return A \code{\link{PathData}} object
583+
#' @family PathData mutation functions
603584
as_survival <- function(pd, event) {
604585
checkmate::assert_class(pd, "PathData")
605586
N_sub <- length(pd$unique_subjects())
@@ -635,6 +616,7 @@ count_paths_with_event <- function(c, t, S) {
635616
#' @inheritParams p_state_visit
636617
#' @param state_name Name of the state (character).
637618
#' @return A data frame
619+
#' @family PathData summary functions
638620
p_state_visit_per_subject <- function(pd, state_name, t = NULL) {
639621
checkmate::assert_character(state_name, len = 1)
640622
p_state_visit(pd, t, by = "subject_id") |>
@@ -650,6 +632,7 @@ p_state_visit_per_subject <- function(pd, state_name, t = NULL) {
650632
#' @param t The given time. If \code{NULL}, is set to \code{max(pd$get_path_df()$time)}.
651633
#' @param by Factor to summarize over.
652634
#' @return A data frame
635+
#' @family PathData summary functions
653636
p_state_visit <- function(pd, t = NULL, by = NULL) {
654637
checkmate::assert_class(pd, "PathData")
655638
S <- pd$transmat$num_states()
@@ -790,19 +773,15 @@ df_to_paths_df_part2 <- function(pdf, tm) {
790773
#' @param validate Do stricter data validation? Recommended to use \code{TRUE}.
791774
#' @return A \code{\link{PathData}} object
792775
df_to_pathdata <- function(df, tm, covs = NULL, validate = TRUE) {
793-
df <- df |> dplyr::arrange(.data$subject_id, .data$time)
794-
if (validate) {
795-
validate_transitions(df)
796-
}
797-
checkmate::assert_data_frame(df)
798-
checkmate::assert_true("state" %in% colnames(df))
776+
check_columns(df, c("state", "time", "subject_id", "is_transition"))
799777
checkmate::assert_integerish(df$state)
800-
checkmate::assert_true("time" %in% colnames(df))
801778
checkmate::assert_numeric(df$time)
802-
checkmate::assert_true("subject_id" %in% colnames(df))
803779
checkmate::assert_character(df$subject_id)
804-
checkmate::assert_true("is_transition" %in% colnames(df))
805780
checkmate::assert_logical(df$is_transition)
781+
df <- df |> dplyr::arrange(.data$subject_id, .data$time)
782+
if (validate) {
783+
validate_transitions(df)
784+
}
806785
checkmate::assert_class(tm, "TransitionMatrix")
807786
if (!is.null(covs)) {
808787
checkmate::assert_character(covs)

R/utils.R

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# Check data frame columns
2+
check_columns <- function(df, needed_columns) {
3+
checkmate::assert_data_frame(df)
4+
checkmate::assert_character(needed_columns)
5+
if (!(all(needed_columns %in% colnames(df)))) {
6+
msg <- paste0(
7+
"Some needed columns are missing from df:\n - found = {",
8+
paste(colnames(df), collapse = ", "), "}\n - required = {",
9+
paste(needed_columns, collapse = ", "), "}"
10+
)
11+
stop(msg)
12+
}
13+
invisible(TRUE)
14+
}
15+
116
# Check for unusual normalized covariate values (will cause issues for hazard)
217
check_normalized_covariate <- function(x_norm, name) {
318
mabs <- max(abs(x_norm))
@@ -9,7 +24,7 @@ check_normalized_covariate <- function(x_norm, name) {
924
warning(msg)
1025
return(FALSE)
1126
}
12-
TRUE
27+
invisible(TRUE)
1328
}
1429

1530

dev/list_exports.R

Lines changed: 0 additions & 20 deletions
This file was deleted.

man/MultiStateModel.Rd

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

man/as_any_event.Rd

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

man/as_single_event.Rd

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

man/as_survival.Rd

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)