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

some minor changes + R package dependency #33

Merged
merged 18 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ Imports:
stats,
survival,
DescTools,
MASS
MASS,
boot
Suggests:
knitr,
testthat (>= 2.0)
testthat (>= 2.0),
ggplot2
VignetteBuilder:
knitr
biocViews:
Expand Down
15 changes: 4 additions & 11 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,8 @@ export(process_agd)
export(resid_plot)
export(survfit_makeup)
import(DescTools)
import(MASS)
import(boot)
import(graphics)
import(stats)
importFrom(graphics,abline)
importFrom(graphics,axis)
importFrom(graphics,hist)
importFrom(graphics,legend)
importFrom(graphics,lines)
importFrom(graphics,par)
importFrom(graphics,points)
importFrom(survival,Surv)
importFrom(survival,cox.zph)
importFrom(survival,coxph)
importFrom(survival,survfit)
import(survival)
4 changes: 1 addition & 3 deletions R/bucher.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
#' \item{pval}{p-value of Z-test, with null hypothesis that \code{est} is zero}
#' }
#' @export
#' @import stats
#'
#' @examples
MikeJSeo marked this conversation as resolved.
Show resolved Hide resolved

bucher <- function(trt, com, conf_lv = 0.95) {
est <- trt$est - com$est
se <- sqrt(trt$se^2 + com$se^2)
Expand Down
7 changes: 2 additions & 5 deletions R/maic_unanchored_tte.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
#' \item time - numeric column, observation time of the \code{status}; unit in days
#' }
#'
#' @return
#' @importFrom survival Surv survfit coxph cox.zph
#' @importFrom graphics par axis lines points legend abline
#' @return A list of KM plot, analysis table, and diagnostic plot
#' @export
#'
#' @examples

MikeJSeo marked this conversation as resolved.
Show resolved Hide resolved
maic_tte_unanchor <- function(useWt, dat, dat_ext, trt, trt_ext,
time_scale = "month", endpoint_name = "OS",
transform = "log") {
Expand Down
30 changes: 30 additions & 0 deletions R/maicplus.R
MikeJSeo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' maicplus: open source R package for MAIC
#'
#' A package for running matching adjusted indirect comparison
#'
#' This package facilitates performing matching adjusted indirect comparison
#' (MAIC) analysis where the endpoint of interest is either time-to-event
#' (e.g. overall survival) or binary (e.g. objective tumor response).
#'
#' @docType package
#' @name maicplus-package
MikeJSeo marked this conversation as resolved.
Show resolved Hide resolved
#'
#'

#' @import graphics
NULL

#' @import stats
NULL

#' @import survival
NULL

#' @import DescTools
NULL

#' @import MASS
NULL

#' @import boot
NULL
MikeJSeo marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 7 additions & 11 deletions R/matching.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Functions for matching step: estimation of individual weights

# functions to be exported ---------------------------------------

#' Derive individual weights in the matching step of MAIC
#'
#' Assuming data is properly processed, this function takes individual patient data (IPD) with centered covariates
#' (effect modifiers and/or prognostic variables) as input, and generates weights for each individual in IPD trial that
#' matches the chosen statistics of those covariates in Aggregated Data (AgD) trial.
#' This function takes individual patient data (IPD) with centered covariates
#' (effect modifiers and/or prognostic variables) as input and generates
#' weights for each individual in IPD trial to match the covariates in aggregate data.
#'
#' @param data a numeric matrix, centered covariates of IPD, no missing value in any cell is allowed
#' @param centered_colnames a character or numeric vector (column indicators) of centered covariates
Expand All @@ -27,8 +25,7 @@
#' \item{opt}{R object returned by \code{base::optim()}, for assess convergence and other details}
#' }
#' @export
#'
#' @examples

MikeJSeo marked this conversation as resolved.
Show resolved Hide resolved
estimate_weights <- function(data, centered_colnames = NULL, start_val = 0, method = "BFGS", ...) {
# pre check
ch1 <- is.data.frame(data)
Expand Down Expand Up @@ -116,7 +113,6 @@ estimate_weights <- function(data, centered_colnames = NULL, start_val = 0, meth
#' @param main_title a character string, main title of the plot
#'
#' @return a plot of unscaled or scaled weights
#' @importFrom graphics hist
#' @export

plot_weights <- function(wt, bin_col = "#6ECEB2", vline_col = "#688CE8", main_title = "Unscaled Individual Weights") {
Expand Down Expand Up @@ -155,13 +151,13 @@ plot_weights <- function(wt, bin_col = "#6ECEB2", vline_col = "#688CE8", main_ti
#' This function checks to see if the optimization is done properly by checking the covariate averages
#' before and after adjustment.
#'
#' @param optimized object returned after calculating weights using \code{\link{estimate_weights}}
#' @param match_res object returned after calculating weights using \code{\link{estimate_weights}}
#' @param processed_agd a data frame, object returned after using \code{\link{process_agd}} or
#' aggregated data following the same naming convention
#'
#' @import DescTools
#'
#' @return data.frame of weighted and unweighted covariate averages of the IPD
#' @return data.frame of weighted and unweighted covariate averages of the IPD, and average of aggregate data
#' @export
#'
#' @examples
Expand Down Expand Up @@ -214,7 +210,7 @@ plot_weights <- function(wt, bin_col = "#6ECEB2", vline_col = "#688CE8", main_ti
#' )
#'
#' print(check)
check_weights <- function(optimized, processed_agd) {
check_weights <- function(match_res, processed_agd) {
MikeJSeo marked this conversation as resolved.
Show resolved Hide resolved
ipd_with_weights <- optimized$data
match_cov <- optimized$centered_colnames

MikeJSeo marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
20 changes: 3 additions & 17 deletions R/process_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#' by legal suffixes (i.e. MEAN, MEDIAN, SD, COUNT, or PROP).
#'
#' @examples
#' # example
#' target_pop <- read.csv(system.file("extdata", "aggregate_data_example_1.csv",
#' package = "maicplus", mustWork = TRUE
#' ))
Expand All @@ -33,20 +32,6 @@
#' target_pop2 <- process_agd(target_pop2)
#' target_pop3 <- process_agd(target_pop3)
#'
#' # another example
#' target_pop <- data.frame(
#' STUDY = "Study_XXXX",
#' ARM = "Total",
#' N = 300,
#' AGE_MEAN = 51,
#' AGE_MEDIAN = 49,
#' AGE_SD = 3.25,
#' SEX_MALE_COUNT = 147,
#' ECOG0_COUNT = 105,
#' SMOKE_PROP = 58 / 290
#' )
#' process_agd(target_pop)
#'
#' @return pre-processed aggregate level data
#' @export

Expand Down Expand Up @@ -155,7 +140,7 @@ dummize_ipd <- function(raw_ipd, dummize_cols, dummize_ref_level) {
#'
#' @param ipd IPD variable names should match the aggregate data names without the suffix.
#' This would involve either changing the aggregate data name or the ipd name.
#' For instance, if we binarize SEX variable with MALE as a reference, function names the new variable as SEX_MALE.
#' For instance, if we binarize SEX variable with MALE as a reference using [dummize_ipd], function names the new variable as SEX_MALE.
#' In this case, SEX_MALE should also be available in the aggregate data.
#' @param agd pre-processed aggregate data which contain STUDY, ARM, and N. Variable names should be followed
#' by legal suffixes (i.e. MEAN, MEDIAN, SD, or PROP). Note that COUNT suffix is no longer accepted.
Expand Down Expand Up @@ -247,13 +232,14 @@ complete_agd <- function(use_agd) {
}


#' helper function: transform TTE ADaM data to suitable input for survival R pkg
#' helper function: transform TTE ADaM data to suitable input for survival R package
#'
#' @param dd data frame, ADTTE read via haven::read_sas
#' @param time_scale a character string, 'year', 'month', 'week' or 'day', time unit of median survival time
#' @param trt values to include in treatment column
#'
#' @return a data frame that can be used as input to survival::Surv

ext_tte_transfer <- function(dd, time_scale = "month", trt = NULL) {
time_units <- list("year" = 365.24, "month" = 30.4367, "week" = 7, "day" = 1)

Expand Down
17 changes: 8 additions & 9 deletions R/survival-helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ medSurv_makeup <- function(km_fit, legend = "before matching", time_scale) {



#' helper function: makeup `survival::survfit` object for km plot
#' Helper function to select a set of variables used for Kaplan-Meier plot
#'
#' @param km_fit returned object from \code{survival::survfit}
#'
#' @return a list of data frames of variables from \code{survival::survfit}. Data frames are divided by treatment.
#' @export
#'
#' @return a list of data frames, one element per treatment

survfit_makeup <- function(km_fit) {
kmdat <- data.frame(
time = km_fit$time,
Expand Down Expand Up @@ -64,7 +63,7 @@ survfit_makeup <- function(km_fit) {
#' @param endpoint_name a character string, name of the endpoint
#'
#' @return a KM plot
km_makeup <- function(km_fit_before, km_fit_after = NULL, time_scale, trt, trt_ext, endpoint_name = "") {
km_plot <- function(km_fit_before, km_fit_after = NULL, time_scale, trt, trt_ext, endpoint_name = "") {
timeUnit <- list("year" = 365.24, "month" = 30.4367, "week" = 7, "day" = 1)

if (!time_scale %in% names(timeUnit)) stop("time_scale has to be 'year', 'month', 'week' or 'day'")
Expand All @@ -84,7 +83,7 @@ km_makeup <- function(km_fit_before, km_fit_after = NULL, time_scale, trt, trt_e
# base plot
par(mfrow = c(1, 1), bty = "n", tcl = -0.15, mgp = c(2.3, 0.5, 0))
plot(0, 0,
type = "n", xlab = paste0("Time in", time_scale), ylab = "Survival Probability",
type = "n", xlab = paste0("Time in ", time_scale), ylab = "Survival Probability",
ylim = c(0, 1), xlim = t_range, yaxt = "n",
main = paste0(
"Kaplan-Meier Curves of Comparator ", ifelse(!is.null(km_fit_after), "(AgD) ", ""),
Expand All @@ -94,7 +93,7 @@ km_makeup <- function(km_fit_before, km_fit_after = NULL, time_scale, trt, trt_e
)
axis(2, las = 1)

# add km lines from external trail
# add km lines from external trial
lines(
y = pd_be[[trt_ext]]$surv,
x = (pd_be[[trt_ext]]$time / timeUnit[[time_scale]]), col = "#5450E4",
Expand Down Expand Up @@ -142,7 +141,7 @@ km_makeup <- function(km_fit_before, km_fit_after = NULL, time_scale, trt, trt_e
legend = c(
paste0("Comparator: ", trt_ext),
paste0("Treatment: ", trt),
paste0("Treatment: ", trt, "(with weights)")
paste0("Treatment: ", trt, " (with weights)")
)[use_leg]
)
}
Expand Down Expand Up @@ -233,7 +232,7 @@ resid_plot <- function(coxobj, time_scale = "month", log_time = TRUE, endpoint_n
cex = 0.9, col = "navyblue", yaxt = "n",
ylab = "Unscaled Schoenfeld Residual", xlab = paste0(ifelse(log_time, "Log-", ""), "Time in ", time_scale),
main = paste0(
"Diagnosis Plot: Unscaled Schoenfeld Residual\nEndpoint: ", endpoint_name,
"Diagnostic Plot: Unscaled Schoenfeld Residual\nEndpoint: ", endpoint_name,
ifelse(subtitle == "", "", "\n"), subtitle
)
)
Expand Down
Loading