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

add generic print for check_weights #17

Merged
merged 3 commits into from
Aug 8, 2023
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(print,maicplus_check_weights)
export(bucher)
export(center_ipd)
export(check_weights)
Expand Down
102 changes: 85 additions & 17 deletions R/matching.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,63 @@ plot_weights <- function(wt, bin_col = "#6ECEB2", vline_col = "#688CE8", main_ti
#' @param optimized 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
#' @param mean_digits number of digits for rounding mean columns in the output
#' @param prop_digits number of digits for rounding proportion columns in the output
#' @param sd_digits number of digits for rounding mean columns in the output

#'
#' @import DescTools
#'
#' @return data.frame of weighted and unweighted covariate averages of the IPD
#' @export

check_weights <- function(optimized, processed_agd, mean_digits = 2, prop_digits = 2, sd_digits = 3) {
#'
#' @examples
#' adsl <- read.csv(system.file("extdata", "adsl.csv",
#' package = "maicplus",
#' mustWork = TRUE
#' ))
#' adrs <- read.csv(system.file("extdata", "adrs.csv",
#' package = "maicplus",
#' mustWork = TRUE
#' ))
#' adtte <- read.csv(system.file("extdata", "adtte.csv",
#' package = "maicplus",
#' mustWork = TRUE
#' ))
#'
#' ### AgD
#' # Baseline aggregate data for the comparator population
#' 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))
#'
#' # for time-to-event endpoints, pseudo IPD from digitalized KM
#' pseudo_ipd <- read.csv(system.file("extdata", "psuedo_IPD.csv",
#' package = "maicplus",
#' mustWork = TRUE
#' ))
#'
#' #### prepare data ----------------------------------------------------------
#' target_pop <- process_agd(target_pop)
#' # target_pop2 <- process_agd(target_pop2) # demo of process_agd in different scenarios
#' # target_pop3 <- process_agd(target_pop3) # demo of process_agd in different scenarios
#' adsl <- dummize_ipd(adsl, dummize_cols = c("SEX"), dummize_ref_level = c("Female"))
#' use_adsl <- center_ipd(ipd = adsl, agd = target_pop)
#'
#' match_res <- estimate_weights(
#' data = use_adsl,
#' centered_colnames = grep("_CENTERED$", names(use_adsl)),
#' start_val = 0,
#' method = "BFGS"
#' )
#'
#' check <- check_weights(
#' optimized = match_res,
#' processed_agd = target_pop
#' )
#'
#' print(check)
check_weights <- function(optimized, processed_agd) {
ipd_with_weights <- optimized$data
match_cov <- optimized$centered_colnames

Expand Down Expand Up @@ -196,9 +242,7 @@ check_weights <- function(optimized, processed_agd, mean_digits = 2, prop_digits
outdata$covariate <- gsub("_MEDIAN|_SQUARED", "", outdata$covariate)
# fill in corresponding agd data
outdata$external_trial <- unlist(processed_agd[paste(outdata$covariate, toupper(outdata$match_stat), sep = "_")])
outdata$external_trial[outdata$match_stat == "Mean"] <- round(outdata$external_trial[outdata$match_stat == "Mean"], mean_digits)
outdata$external_trial[outdata$match_stat == "Prop"] <- round(outdata$external_trial[outdata$match_stat == "Prop"], prop_digits)
outdata$external_trial[outdata$match_stat == "SD"] <- round(outdata$external_trial[outdata$match_stat == "SD"], sd_digits)

# fill in stat from unweighted and weighted IPD
for (ii in 1:nrow(outdata)) {
covname <- outdata$covariate[ii]
Expand Down Expand Up @@ -235,14 +279,38 @@ check_weights <- function(optimized, processed_agd, mean_digits = 2, prop_digits
outdata$internal_trial_after_weighted[ii] <- sqrt(wm_squared - ms_agd)
}
}
# formating

outdata$internal_trial[outdata$match_stat == "Mean"] <- round(outdata$internal_trial[outdata$match_stat == "Mean"], mean_digits)
outdata$internal_trial[outdata$match_stat == "Prop"] <- round(outdata$internal_trial[outdata$match_stat == "Prop"], prop_digits)
outdata$internal_trial[outdata$match_stat == "SD"] <- round(outdata$internal_trial[outdata$match_stat == "SD"], sd_digits)
outdata$internal_trial_after_weighted[outdata$match_stat == "Mean"] <- round(outdata$internal_trial_after_weighted[outdata$match_stat == "Mean"], mean_digits)
outdata$internal_trial_after_weighted[outdata$match_stat == "Prop"] <- round(outdata$internal_trial_after_weighted[outdata$match_stat == "Prop"], prop_digits)
outdata$internal_trial_after_weighted[outdata$match_stat == "SD"] <- round(outdata$internal_trial_after_weighted[outdata$match_stat == "SD"], sd_digits)

# output
class(outdata) <- c("maicplus_check_weights", "data.frame")
outdata
}


#' Print method for Check Weights objects
#'
#' @param x object from [check_weights]
#' @param mean_digits number of digits for rounding mean columns in the output
#' @param prop_digits number of digits for rounding proportion columns in the output
#' @param sd_digits number of digits for rounding mean columns in the output
#' @param digits minimal number of significant digits, see [print.default].
#' @param ... further arguments to [print.data.frame]
#'
#' @describeIn check_weights Print method for check_weights objects
#' @export
print.maicplus_check_weights <- function(x, mean_digits = 2, prop_digits = 2, sd_digits = 3, digits = getOption("digits"), ...) {
round_digits <- c("Mean" = mean_digits, "Prop" = prop_digits, "SD" = sd_digits)[x$match_stat]
round_digits[is.na(round_digits)] <- digits

x$external_trial <- round(x$external_trial, round_digits)
x$internal_trial <- round(x$internal_trial, round_digits)
x$internal_trial_after_weighted <- round(x$internal_trial_after_weighted, round_digits)

print.data.frame(x, ...)
footer <- unlist(attr(x, "footer"))
if (length(footer)) {
cat("\n")
for (f in seq_along(footer)) {
cat(paste0("[", f, "] ", footer[f]))
}
}
}
8 changes: 4 additions & 4 deletions inst/extdata/aggregate_data_example_1.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
STUDY,ARM,N,AGE_MEAN,AGE_MEDIAN,AGE_SD,SEX_MALE_COUNT,ECOG0_COUNT,SMOKE_COUNT
Study_XXXX,Total,300,51,49,3.25,147,105,58
Study_XXXX,Intervention,,,,,,,
Study_XXXX,Comparator,,,,,,,
STUDY,ARM,N,AGE_MEAN,AGE_MEDIAN,AGE_SD,SEX_MALE_COUNT,ECOG0_COUNT,SMOKE_COUNT
Study_XXXX,Total,300,51,49,3.25,147,105,58
Study_XXXX,Intervention,,,,,,,
Study_XXXX,Comparator,,,,,,,
74 changes: 70 additions & 4 deletions man/check_weights.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.