Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

109 adapt all examples and tests to datasets #128

Merged
merged 22 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ S3method(print,maicplus_bucher)
S3method(print,maicplus_check_weights)
export(basic_kmplot)
export(basic_kmplot2)
export(bootstrap_HR)
export(bucher)
export(calculate_weights_legend)
export(center_ipd)
export(check_weights)
export(dummize_ipd)
export(estimate_weights)
export(find_SE_from_CI)
export(generate_survival_data)
export(get_pseudo_ipd_binary)
export(get_time_as)
export(get_time_conversion)
export(kmplot)
export(kmplot2)
Expand Down
2 changes: 1 addition & 1 deletion R/bucher.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ print.maicplus_bucher <- function(x, ci_digits = 2, pval_digits = 3,
#' @param exponentiate whether the treatment effect and confidence
#' interval should be exponentiated. This applies to relative
#' treatment effects. Default is set to false.

#' @keywords internal

reformat <- function(x, ci_digits = 2, pval_digits = 3,
show_pval = TRUE, exponentiate = FALSE) {
Expand Down
10 changes: 6 additions & 4 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
#' Pseudo individual patient survival data from published study
#' @format A data frame with 300 rows and 3 columns:
#' \describe{
#' \item{Time}{Survival time in days.}
#' \item{Event}{Event indicator `0`/`1`.}
#' \item{TIME}{Survival time in days.}
#' \item{EVENT}{Event indicator `0`/`1`.}
#' \item{ARM}{Assigned treatment arm, `"B"`.}
#' }
#' @family unanchored datasets
Expand Down Expand Up @@ -169,13 +169,15 @@
#' \item{PARAM}{Parameter type of `AVAL`.}
#' \item{RESPONSE}{Indicator of response.}
#' }
#' @family anchored datasets
#' @keywords dataset
"adrs_twt"

#' Pseudo individual patient survival data from published two arm study
#' @format A data frame with 800 rows and 3 columns:
#' \describe{
#' \item{Time}{Survival time in days.}
#' \item{Event}{Event indicator `0`/`1`.}
#' \item{TIME}{Survival time in days.}
#' \item{EVENT}{Event indicator `0`/`1`.}
#' \item{ARM}{Assigned treatment arm, `"B"`, `"C"`.}
#' }
#' @family anchored datasets
Expand Down
1 change: 1 addition & 0 deletions R/maic_anchored.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#' @importFrom boot boot boot.ci
#' @return A list, contains 'descriptive' and 'inferential'
#' @example inst/examples/maic_anchored_ex.R
#' @example inst/examples/maic_anchored_binary_ex.R
#' @export

maic_anchored <- function(weights_object,
Expand Down
2 changes: 2 additions & 0 deletions R/maic_unanchored.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#' @importFrom sandwich vcovHC
#' @importFrom boot boot boot.ci
#' @return A list, contains 'descriptive' and 'inferential'
#' @example inst/examples/maic_unanchored_ex.R
#' @example inst/examples/maic_unanchored_binary_ex.R
#' @export

maic_unanchored <- function(weights_object,
Expand Down
13 changes: 6 additions & 7 deletions R/matching.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@
#' }
#'
#' @examples
#' data(agd)
#' data(adsl_sat)
#' ipd_centered <- center_ipd(ipd = adsl_sat, agd = process_agd(agd))
#' centered_colnames <- grep("_CENTERED", colnames(ipd_centered), value = TRUE)
#' centered_colnames
#' weighted_data <- estimate_weights(data = ipd_centered, centered_colnames = centered_colnames)
#' data(centered_ipd_sat)
#' centered_colnames <- grep("_CENTERED", colnames(centered_ipd_sat), value = TRUE)
#' weighted_data <- estimate_weights(data = centered_ipd_sat, centered_colnames = centered_colnames)
#' \donttest{
#' # To later estimate bootstrap confidence intervals, we calculate the weights
#' # for the bootstrap samples:
#' weighted_data_boot <- estimate_weights(
#' data = ipd_centered, centered_colnames = centered_colnames, n_boot_iteration = 500
#' data = centered_ipd_sat, centered_colnames = centered_colnames, n_boot_iteration = 100
#' )
#' }
#' @export
Expand Down Expand Up @@ -400,6 +397,8 @@ plot.maicplus_estimate_weights <- function(x, ggplot = FALSE,
#' aggregated data following the same naming convention
#'
#' @examples
#' data(weighted_sat)
#' data(agd)
#' check_weights(weighted_sat, process_agd(agd))
#'
#' @import DescTools
Expand Down
26 changes: 17 additions & 9 deletions R/plot_km.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
#' appended.
#' \item if "all", 2 by 2 plot, all plots in "by_trial" and "by_arm" without risk set table appended.
#' }
#' @example inst/examples/kmplot_anchored_ex.R
#' @example inst/examples/kmplot_unanchored_ex.R
#' @example inst/examples/kmplot_anchored_ex.R
#' @export

kmplot <- function(weights_object,
Expand Down Expand Up @@ -444,12 +444,13 @@ basic_kmplot <- function(kmdat,
#' "TIME" columns and a column indicating treatment assignment
#' @param tte_pseudo_ipd a data frame of pseudo IPD by digitized KM curves of external trial (for time-to-event
#' endpoint), contain at least "EVENT", "TIME"
#' @param trt_ipd a string, name of the interested investigation arm in internal trial \code{dat_igd} (real IPD)
#' @param trt_agd a string, name of the interested investigation arm in external trial \code{dat_pseudo} (pseudo IPD)
#' @param trt_ipd a string, name of the interested investigation arm in internal trial \code{tte_ipd} (real IPD)
#' @param trt_agd a string, name of the interested investigation arm in external trial
#' \code{tte_pseudo_ipd} (pseudo IPD)
#' @param trt_common a string, name of the common comparator in internal and external trial, by default is NULL,
#' indicating unanchored case
#' @param trt_var_ipd a string, column name in \code{dat_ipd} that contains the treatment assignment
#' @param trt_var_agd a string, column name in \code{dat_ipd} that contains the treatment assignment
#' @param trt_var_ipd a string, column name in \code{tte_ipd} that contains the treatment assignment
#' @param trt_var_agd a string, column name in \code{tte_pseudo_ipd} that contains the treatment assignment
#' @param endpoint_name a string, name of time to event endpoint, to be show in the last line of title
#' @param time_scale a string, time unit of median survival time, taking a value of 'years', 'months', 'weeks' or 'days'
#' @param zph_transform a string, pass to \code{survival::cox.zph}, default is "log"
Expand All @@ -458,6 +459,9 @@ basic_kmplot <- function(kmdat,
#'
#' @return a 3 by 2 plot, include log-cumulative hazard plot, time dependent hazard function and unscaled Schoenfeld
#' residual plot, before and after matching
#'
#' @example inst/examples/ph_diagplot_unanchored_ex.R
#' @example inst/examples/ph_diagplot_anchored_ex.R
#' @export
ph_diagplot <- function(weights_object,
tte_ipd,
Expand Down Expand Up @@ -611,8 +615,10 @@ ph_diagplot <- function(weights_object,
#' @param exclude_censor logical, should censored data point be plotted
#' @examples
#' library(survival)
#' load(system.file("extdata", "combined_data_tte.rda", package = "maicplus", mustWork = TRUE))
#' kmobj <- survfit(Surv(TIME, EVENT) ~ ARM, combined_data_tte, conf.type = "log-log")
#' data(adtte_sat)
#' data(pseudo_ipd_sat)
#' combined_data <- rbind(adtte_sat[, c("TIME", "EVENT", "ARM")], pseudo_ipd_sat)
#' kmobj <- survfit(Surv(TIME, EVENT) ~ ARM, combined_data, conf.type = "log-log")
#' ph_diagplot_lch(kmobj,
#' time_scale = "month", log_time = TRUE,
#' endpoint_name = "OS", subtitle = "(Before Matching)"
Expand Down Expand Up @@ -687,8 +693,10 @@ ph_diagplot_lch <- function(km_fit,
#' @param subtitle a character string, subtitle of the plot
#' @examples
#' library(survival)
#' load(system.file("extdata", "combined_data_tte.rda", package = "maicplus", mustWork = TRUE))
#' unweighted_cox <- coxph(Surv(TIME, EVENT == 1) ~ ARM, data = combined_data_tte)
#' data(adtte_sat)
#' data(pseudo_ipd_sat)
#' combined_data <- rbind(adtte_sat[, c("TIME", "EVENT", "ARM")], pseudo_ipd_sat)
#' unweighted_cox <- coxph(Surv(TIME, EVENT == 1) ~ ARM, data = combined_data)
#' ph_diagplot_schoenfeld(unweighted_cox,
#' time_scale = "month", log_time = TRUE,
#' endpoint_name = "OS", subtitle = "(Before Matching)"
Expand Down
3 changes: 2 additions & 1 deletion R/plot_km2.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#' appended.
#' \item if "all", 2 by 2 plot, all plots in "by_trial" and "by_arm" without risk set table appended.
#' }
#' @example inst/examples/kmplot2_anchored_ex.R
#' @example inst/examples/kmplot2_unanchored_ex.R
#' @example inst/examples/kmplot2_anchored_ex.R
#' @export

kmplot2 <- function(weights_object,
Expand Down Expand Up @@ -160,6 +160,7 @@ kmplot2 <- function(weights_object,
#' it will be passed to 'col' of \code{lines()}
#' @param use_line_types a numeric vector of length up to 4, line type to the KM curves,
#' it will be passed to \code{lty} of \code{lines()}
#' @example inst/examples/basic_kmplot2_ex.R
#' @export

basic_kmplot2 <- function(kmlist,
Expand Down
30 changes: 5 additions & 25 deletions R/process_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,8 @@
#' by legal suffixes (i.e. MEAN, MEDIAN, SD, COUNT, or PROP).
#'
#' @examples
#' target_pop <- read.csv(system.file("extdata", "aggregate_data_example_1.csv",
#' package = "maicplus", mustWork = TRUE
#' ))
#' target_pop2 <- read.csv(system.file("extdata", "aggregate_data_example_2.csv",
#' package = "maicplus", mustWork = TRUE
#' ))
#' target_pop3 <- read.csv(system.file("extdata", "aggregate_data_example_3.csv",
#' package = "maicplus", mustWork = TRUE
#' ))
#'
#' target_pop <- process_agd(target_pop)
#' target_pop2 <- process_agd(target_pop2)
#' target_pop3 <- process_agd(target_pop3)
#' data(agd)
#' agd <- process_agd(agd)
#'
#' @return pre-processed aggregate level data
#' @export
Expand Down Expand Up @@ -151,20 +140,10 @@ dummize_ipd <- function(raw_ipd, dummize_cols, dummize_ref_level) {
#' should be followed by legal suffixes (i.e. MEAN, MEDIAN, SD, or PROP). Note that COUNT
#' suffix is no longer accepted.
#' @examples
#' # load in IPD
#' data(adsl_sat)
#' adsl <- dummize_ipd(adsl_sat, dummize_cols = c("SEX"), dummize_ref_level = c("Female"))
#'
#' # Reading aggregate data by Excel
#' target_pop <- read.csv(
#' system.file("extdata", "aggregate_data_example_1.csv", package = "maicplus", mustWork = TRUE)
#' )
#' agd <- process_agd(target_pop)
#'
#' # Alternatively, you can specify aggregate data manually in data frame
#' data(agd)
#' ipd_centered <- center_ipd(ipd = adsl, agd = agd)
#'
#' agd <- process_agd(agd)
#' ipd_centered <- center_ipd(ipd = adsl_sat, agd = agd)
#' @return centered ipd using aggregate level data averages
#' @export

Expand Down Expand Up @@ -259,6 +238,7 @@ complete_agd <- function(use_agd) {
#' @param trt values to include in treatment column
#'
#' @return a data frame that can be used as input to `survival::Surv`
#' @keywords internal

ext_tte_transfer <- function(dd, time_scale = "months", trt = NULL) {
time_scale <- match.arg(time_scale, choices = c("years", "months", "weeks", "days"))
Expand Down
6 changes: 3 additions & 3 deletions R/reporting.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#'
#' @return a data frame with sample size, incidence rate, median survival time with 95% CI, hazard ratio estimate with
#' 95% CI and Wald test of hazard ratio
#'
#' @example inst/examples/report_table_tte_ex.R
#' @export

report_table_tte <- function(coxobj, medSurvobj, tag = NULL) {
Expand Down Expand Up @@ -51,13 +51,13 @@ report_table_tte <- function(coxobj, medSurvobj, tag = NULL) {
#' helper function: sort out a nice report table to summarize binary analysis results
#'
#' @param binobj object from glm()
#' @param weighted_result object res_AB
#' @param weighted_result weighted result object
#' @param eff_measure a string, binary effect measure, could be "OR", "RR", "RD"
#' @param tag a string, by default NULL, if specified, an extra 1st column is created in the output
#'
#' @return a data frame with sample size, incidence rate, estimate of binary effect measure with
#' 95% CI and Wald test of hazard ratio
#'
#' @example inst/examples/report_table_binary_ex.R
#' @export

report_table_binary <- function(binobj, weighted_result = NULL, eff_measure = c("OR", "RD", "RR"), tag = NULL) {
Expand Down
22 changes: 10 additions & 12 deletions R/survival-helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
#' @param time_scale a character string, 'years', 'months', 'weeks' or 'days', time unit of median survival time
#'
#' @examples
#' data(adtte_sat)
#' data(pseudo_ipd_sat)
#' library(survival)
#' load(system.file("extdata", "combined_data_tte.rda", package = "maicplus", mustWork = TRUE))
#' kmobj <- survfit(Surv(TIME, EVENT) ~ ARM, combined_data_tte, conf.type = "log-log")
#' kmobj_adj <- survfit(Surv(TIME, EVENT) ~ ARM, combined_data_tte,
#' weights = combined_data_tte$weights, conf.type = "log-log"
#' )
#' combined_data <- rbind(adtte_sat[, c("TIME", "EVENT", "ARM")], pseudo_ipd_sat)
#' kmobj <- survfit(Surv(TIME, EVENT) ~ ARM, combined_data, conf.type = "log-log")
#'
#' # Derive median survival time
#' medSurv <- medSurv_makeup(kmobj, legend = "before matching", time_scale = "day")
#' medSurv_adj <- medSurv_makeup(kmobj_adj, legend = "after matching", time_scale = "day")
#' medSurv_out <- rbind(medSurv, medSurv_adj)
#' medSurv_out
#' medSurv
#' @return a data frame with a index column 'type', median survival time and confidence interval
#' @export

Expand Down Expand Up @@ -47,11 +44,12 @@ medSurv_makeup <- function(km_fit, legend = "before matching", time_scale) {
#' @param single_trt_name name of treatment if no strata are specified in `km_fit`
#'
#' @examples
#' \dontrun{
#' load(system.file("extdata", "combined_data_tte.rda", package = "maicplus", mustWork = TRUE))
#' kmobj <- survfit(Surv(TIME, EVENT) ~ ARM, combined_data_tte, conf.type = "log-log")
#' library(survival)
#' data(adtte_sat)
#' data(pseudo_ipd_sat)
#' combined_data <- rbind(adtte_sat[, c("TIME", "EVENT", "ARM")], pseudo_ipd_sat)
#' kmobj <- survfit(Surv(TIME, EVENT) ~ ARM, combined_data, conf.type = "log-log")
#' survfit_makeup(kmobj)
#' }
#' @return a list of data frames of variables from [survival::survfit()]. Data frame is divided by treatment.
#' @export

Expand Down
5 changes: 3 additions & 2 deletions R/time-helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ settings_env <- new.env()
#' # The default time scale is days:
#' set_time_conversion(default = "days", days = 1, weeks = 7, months = 365.25 / 12, years = 365.25)
#'
#'
#' # Set the default time scale to years
#' set_time_conversion(
#' default = "years",
Expand Down Expand Up @@ -68,7 +67,9 @@ get_time_conversion <- function(factor = c("days", "weeks", "months", "years"))
#' @param as A time scale to convert to. One of "days", "weeks", "months", "years"
#'
#' @return Returns a numeric vector calculated from `times / get_time_conversion(factor = as)`

#' @export
#' @examples
MikeJSeo marked this conversation as resolved.
Show resolved Hide resolved
#' get_time_as(50, as = "months")
get_time_as <- function(times, as = NULL) {
if (is.null(as)) as <- settings_env$default_time_scale
if (!is.numeric(times)) stop("times arguments must be numeric")
Expand Down
5 changes: 3 additions & 2 deletions data-raw/dummy_anchored.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#### create anchored example datasets ####

devtools::load_all()
# devtools::load_all()
library(flexsurv)
set.seed(2024)
set.seed(1234)

# create adsl_twt
adsl <- read.csv(system.file("extdata", "adsl.csv",
Expand Down Expand Up @@ -67,6 +67,7 @@ pseudo_ipd2$Time <- tmp$time_1
pseudo_ipd2$Event <- tmp$event_1

pseudo_ipd_twt <- rbind(pseudo_ipd, pseudo_ipd2)
colnames(pseudo_ipd_twt) <- c("TIME", "EVENT", "ARM")

# create centered adsl_twt
agd <- process_agd(agd)
Expand Down
3 changes: 2 additions & 1 deletion data-raw/dummy_unanchored.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#### create datasets for unanchored case ####
## adsl_sat, adtte_sat, adrs_sat, agd (AgD of effect modifiers), pseudo_ipd_sat (AgD, tte data)

devtools::load_all()
# devtools::load_all()
# Read in relevant ADaM data and rename variables of interest
adsl_sat <- read.csv(system.file("extdata", "adsl.csv",
package = "maicplus",
Expand Down Expand Up @@ -31,6 +31,7 @@ pseudo_ipd_sat <- read.csv(system.file("extdata", "psuedo_IPD.csv",
mustWork = TRUE
))
pseudo_ipd_sat$ARM <- "B"
colnames(pseudo_ipd_sat) <- c("TIME", "EVENT", "ARM")

### Centered IPD
agd_sat <- process_agd(agd)
Expand Down
Binary file modified data/adrs_sat.rda
Binary file not shown.
Binary file modified data/adrs_twt.rda
Binary file not shown.
Binary file modified data/adsl_sat.rda
Binary file not shown.
Binary file modified data/adsl_twt.rda
Binary file not shown.
Binary file modified data/adtte_sat.rda
Binary file not shown.
Binary file modified data/adtte_twt.rda
Binary file not shown.
Binary file modified data/agd.rda
Binary file not shown.
Binary file modified data/centered_ipd_sat.rda
Binary file not shown.
Binary file modified data/centered_ipd_twt.rda
Binary file not shown.
Binary file modified data/pseudo_ipd_sat.rda
Binary file not shown.
Binary file modified data/pseudo_ipd_twt.rda
Binary file not shown.
Binary file modified data/weighted_sat.rda
Binary file not shown.
Binary file modified data/weighted_twt.rda
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions inst/examples/basic_kmplot2_ex.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
library(survival)
data(adtte_sat)
data(pseudo_ipd_sat)

gravesti marked this conversation as resolved.
Show resolved Hide resolved
kmobj_A <- survfit(Surv(TIME, EVENT) ~ ARM,
data = adtte_sat,
conf.type = "log-log"
)

kmobj_B <- survfit(Surv(TIME, EVENT) ~ ARM,
data = pseudo_ipd_sat,
conf.type = "log-log"
)

kmlist <- list(kmobj_A = kmobj_A, kmobj_B = kmobj_B)
kmlist_name <- c("A", "B")

basic_kmplot2(kmlist, kmlist_name)
7 changes: 5 additions & 2 deletions inst/examples/basic_kmplot_ex.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
library(survival)
load(system.file("extdata", "combined_data_tte.rda", package = "maicplus", mustWork = TRUE))
kmobj <- survfit(Surv(TIME, EVENT) ~ ARM, combined_data_tte, conf.type = "log-log")
data(adtte_sat)
MikeJSeo marked this conversation as resolved.
Show resolved Hide resolved
data(pseudo_ipd_sat)

combined_data <- rbind(adtte_sat[, c("TIME", "EVENT", "ARM")], pseudo_ipd_sat)
kmobj <- survfit(Surv(TIME, EVENT) ~ ARM, combined_data, conf.type = "log-log")
kmdat <- do.call(rbind, survfit_makeup(kmobj))
kmdat$treatment <- factor(kmdat$treatment)

Expand Down
Loading
Loading