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
421406fit_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'
436417msfit_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'
456434msfit_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
557516as_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
603584as_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
638620p_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
653636p_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
792775df_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 )
0 commit comments