From cb2ea2e00402ab877a4f6d5b39bbd72b4b55155e Mon Sep 17 00:00:00 2001 From: mahaalbashir Date: Mon, 12 Oct 2026 09:52:12 +0100 Subject: [PATCH 01/11] adding functionality for cox regression --- DESCRIPTION | 5 +- NAMESPACE | 7 +- R/acro_regression.R | 123 +++++++++++++++ R/acro_tables.R | 2 + man/acro_coxph.Rd | 49 ++++++ tests/testthat/test-acro_coxph.R | 258 +++++++++++++++++++++++++++++++ 6 files changed, 441 insertions(+), 3 deletions(-) create mode 100644 man/acro_coxph.Rd create mode 100644 tests/testthat/test-acro_coxph.R diff --git a/DESCRIPTION b/DESCRIPTION index ac7845b..d7d01b0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,10 +33,11 @@ Suggests: knitr, rmarkdown, spelling, - testthat (>= 3.0.0) + testthat (>= 3.0.0), + survival VignetteBuilder: knitr -Config/roxygen2/version: 8.1.0 +Config/roxygen2/version: 8.1.1 Config/testthat/edition: 3 Encoding: UTF-8 Language: en-US diff --git a/NAMESPACE b/NAMESPACE index 7146dcf..a748a7e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,8 @@ # Generated by roxygen2: do not edit by hand -importFrom("stats", "complete.cases") + export(acro_add_comments) export(acro_add_exception) +export(acro_coxph) export(acro_crosstab) export(acro_custom_output) export(acro_disable_rounding) @@ -25,3 +26,7 @@ export(create_factors) export(is_excluded) export(is_invalid) export(to_pandas_categorical) +importFrom(stats, + complete.cases, + na.omit +) diff --git a/R/acro_regression.R b/R/acro_regression.R index d0315da..efd6728 100644 --- a/R/acro_regression.R +++ b/R/acro_regression.R @@ -36,3 +36,126 @@ acro_glm <- function(formula, data, family) { } model$summary() } + +#' Fits a Cox proportional hazards regression model +#' +#' @param formula a formula object, with the response on the left of a ~ operator, and the terms on the right. The response must be a survival object as returned by the Surv function. +#' @param data a data.frame in which to interpret the variables named in the formula, or in the subset and the weights argument. +#' @param weights vector of case weights +#' @param subset expression indicating which subset of the rows of data should be used in the fit. All observations are included by default. +#' @param na.action a missing-data filter function. +#' @param robust should a robust variance be computed. +#' @param init vector of initial values of the iteration. +#' @param cluster optional variable which clusters the observations, for the purposes of a robust variance. If present, it implies robust. This variable will normally be found in data. +#' @param istate optional variable giving the current state at the start each interval. +#' @param statedata optional data set used to describe multistate models. +#' @param ... Any other parameters. +#' +#' @returns Fitted CoxPHFitter results object +#' @export + +#' @importFrom stats na.omit + +acro_coxph <- function(formula, data, weights = NULL, subset = NULL, na.action = NULL, robust = TRUE, init = NULL, cluster = NULL, istate = NULL, statedata = NULL, ...) { + if (is.null(acroEnv$ac)) { + stop("ACRO has not been initialised. Please first call acro_init()") + } + + # Check for multi-state model arguments (not supported by lifelines) + if (!is.null(istate) || !is.null(statedata)) { + stop("Multi-state models utilizing 'istate' or 'statedata' are not supported at the moment.") + } + + # Handle subsetting if provided + if (!missing(subset)) { + subset_expr <- substitute(subset) + subsetted_columns <- eval(subset_expr, data) + data <- data[subsetted_columns & !is.na(subsetted_columns), ] + } + + # Handle na.action + if (is.null(na.action)) { + na.action <- getOption("na.action") + } + + na_function <- match.fun(na.action) + data <- na_function(data) + + # Parse the formula to extract duration and event columns from the left-hand side + surv_func <- formula[[2]] + if (!inherits(surv_func, "call") || as.character(surv_func[[1]]) != "Surv") { + stop("The left-hand side of the formula must use Surv(), e.g., Surv(time, event) ~ x1 + x2") + } + duration_col <- as.character(surv_func[[2]]) + event_col <- as.character(surv_func[[3]]) + + # Validate and transform event column for python lifelines + event_values <- unique(na.omit(data[[event_col]])) + + if (all(event_values %in% c(1, 2))) { + data[[event_col]][data[[event_col]] == 1] <- 0 + data[[event_col]][data[[event_col]] == 2] <- 1 + } else if (!all(event_values %in% c(0, 1))) { + # If it's neither {1, 2} nor {0, 1}, throw an error + stop(paste0("In Surv(", duration_col, ", ", event_col, ") : Invalid status values")) + } + + # Extract the right-hand side (independent variables) as a formula string + independent_vars <- deparse(formula[[3]]) + + # Handle weights if provided as a column or a vector + weights_col <- NULL + if (!missing(weights)) { + weights_expr <- substitute(weights) + if (!is.null(weights_expr)) { + # If weights passed as a quoted character string matching a column name + if (is.character(weights_expr) && weights_expr %in% names(data)) { + weights_col <- weights_expr + } + # If weights passed as an unquoted column name + else if (deparse(weights_expr) %in% names(data)) { + weights_col <- deparse(weights_expr) + } + # If weights passed as an external vector, expression, or function + else { + data$acro_weight_col__ <- eval(weights_expr, data, parent.frame()) + weights_col <- "acro_weight_col__" + } + } + } + + # Handle cluster + cluster_col <- NULL + if (!missing(cluster)) { + cluster_expr <- substitute(cluster) + if (!is.null(cluster_expr)) { + if (is.character(cluster_expr) && cluster_expr %in% names(data)) { + cluster_col <- cluster_expr + } else if (deparse(cluster_expr) %in% names(data)) { + cluster_col <- deparse(cluster_expr) + } + # If weights passed as an external vector, expression, or function + else { + data$acro_cluster_col__ <- eval(cluster_expr, data, parent.frame()) + cluster_col <- "acro_cluster_col__" + } + robust <- TRUE # Automatically trigger robust variance when clustering is provided + } + } + + # Check for any unused arguments + if (length(list(...)) > 0) { + warning("Unused arguments were provided: ", paste0(names(list(...)), collapse = ", "), "\n", "Please use the help command to learn more about the function.") + } + + model <- acroEnv$ac$coxph( + df = data, + duration_col = duration_col, + event_col = event_col, + weights_col = weights_col, + robust = robust, + formula = independent_vars, + initial_point = init, + cluster_col = cluster_col + ) +} diff --git a/R/acro_tables.R b/R/acro_tables.R index 57b58cb..bf0d5e8 100644 --- a/R/acro_tables.R +++ b/R/acro_tables.R @@ -177,6 +177,8 @@ acro_table <- function(index, columns, dnn = NULL, deparse.level = 0, useNA = "n #' @returns Summary of the data #' @export +#' @importFrom stats complete.cases + acro_summarise <- function(.data, ..., .groups = NULL, .by = NULL) { if (is.null(acroEnv$ac)) { stop("ACRO has not been initialised. Please first call acro_init()") diff --git a/man/acro_coxph.Rd b/man/acro_coxph.Rd new file mode 100644 index 0000000..87e5a4c --- /dev/null +++ b/man/acro_coxph.Rd @@ -0,0 +1,49 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/acro_regression.R +\name{acro_coxph} +\alias{acro_coxph} +\title{Fits a Cox proportional hazards regression model} +\usage{ +acro_coxph( + formula, + data, + weights = NULL, + subset = NULL, + na.action = NULL, + robust = TRUE, + init = NULL, + cluster = NULL, + istate = NULL, + statedata = NULL, + ... +) +} +\arguments{ +\item{formula}{a formula object, with the response on the left of a ~ operator, and the terms on the right. The response must be a survival object as returned by the Surv function.} + +\item{data}{a data.frame in which to interpret the variables named in the formula, or in the subset and the weights argument.} + +\item{weights}{vector of case weights} + +\item{subset}{expression indicating which subset of the rows of data should be used in the fit. All observations are included by default.} + +\item{na.action}{a missing-data filter function.} + +\item{robust}{should a robust variance be computed.} + +\item{init}{vector of initial values of the iteration.} + +\item{cluster}{optional variable which clusters the observations, for the purposes of a robust variance. If present, it implies robust. This variable will normally be found in data.} + +\item{istate}{optional variable giving the current state at the start each interval.} + +\item{statedata}{optional data set used to describe multistate models.} + +\item{...}{Any other parameters.} +} +\value{ +Fitted CoxPHFitter results object +} +\description{ +Fits a Cox proportional hazards regression model +} diff --git a/tests/testthat/test-acro_coxph.R b/tests/testthat/test-acro_coxph.R new file mode 100644 index 0000000..5be1006 --- /dev/null +++ b/tests/testthat/test-acro_coxph.R @@ -0,0 +1,258 @@ +test_data <- lung + +test_that("acro_coxph without initialising ACRO object first", { + acroEnv$ac <- NULL + expect_error(acro_coxph(Surv(time, status) ~ age + sex, data = test_data), "ACRO has not been initialised. Please first call acro_init()") +}) + +test_that("acro_coxph throws an error with multi-state arguments", { + acro_init() + expect_error( + acro_coxph(Surv(time, status) ~ age + sex, data = test_data, istate = 1), + "Multi-state models utilizing 'istate' or 'statedata' are not supported at the moment." + ) + expect_error( + acro_coxph(Surv(time, status) ~ age + sex, data = test_data, statedata = test_data), + "Multi-state models utilizing 'istate' or 'statedata' are not supported at the moment." + ) +}) + +test_that("acro_coxph validates the left side of the formula uses Surv() function", { + acro_init() + expect_error( + acro_coxph(time ~ age + sex, data = test_data), + "The left-hand side of the formula must use Surv\\(\\)" + ) +}) + +test_that("acro_coxph works", { + acro_init() + + # R survival model + r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data) + r_coefs <- stats::coef(r_model) + + # ACRO model + acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data) + + # Compare the coefficients + expect_equal( + py_to_r(unname(acro_model$params_["age"])), + unname(r_coefs["age"]), + tolerance = 0.05 + ) + expect_equal( + py_to_r(unname(acro_model$params_["sex"])), + unname(r_coefs["sex"]), + tolerance = 0.05 + ) +}) + +test_that("acro_coxph with subsetting", { + acro_init() + + # R survival model + r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, subset = age > 50) + r_coefs <- stats::coef(r_model) + + # ACRO model + acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, subset = age > 50) + + # Compare the coefficients + expect_equal( + py_to_r(unname(acro_model$params_["age"])), + unname(r_coefs["age"]), + tolerance = 0.05 + ) + expect_equal( + py_to_r(unname(acro_model$params_["sex"])), + unname(r_coefs["sex"]), + tolerance = 0.05 + ) +}) + +test_that("acro_coxph with na.action", { + acro_init() + + # R survival model + r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, na.action = na.pass) + r_coefs <- stats::coef(r_model) + + # ACRO model + acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, na.action = na.pass) + + # Compare the coefficients + expect_equal( + py_to_r(unname(acro_model$params_["age"])), + unname(r_coefs["age"]), + tolerance = 0.05 + ) + expect_equal( + py_to_r(unname(acro_model$params_["sex"])), + unname(r_coefs["sex"]), + tolerance = 0.05 + ) +}) + +test_that("acro_coxph with unvalid status options(eg. having a status as 4)", { + acro_init() + test_data$status[test_data$status == 1] <- 4 + + expect_error( + acro_coxph(Surv(time, status) ~ age + sex, data = test_data), + "In Surv\\(time, status\\) : Invalid status values" + ) +}) + +test_that("acro_coxph with weights as a column from the dataset", { + acro_init() + + #add a weight column in the dataset + test_data$my_weights <- runif(nrow(test_data), 0.8, 1.2) + + # R survival model + r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, weights = my_weights) + r_coefs <- stats::coef(r_model) + + acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, weights = my_weights) + + # Compare the coefficients + expect_equal( + py_to_r(unname(acro_model$params_["age"])), + unname(r_coefs["age"]), + tolerance = 0.05 + ) + expect_equal( + py_to_r(unname(acro_model$params_["sex"])), + unname(r_coefs["sex"]), + tolerance = 0.05 + ) +}) + +test_that("acro_coxph with weights as a string column name from the dataset", { + acro_init() + + #add a weight column in the dataset + test_data$my_weights <- runif(nrow(test_data), 0.8, 1.2) + + # R survival model + r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, weights = my_weights) + r_coefs <- stats::coef(r_model) + + acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, weights = "my_weights") + + # Compare the coefficients + expect_equal( + py_to_r(unname(acro_model$params_["age"])), + unname(r_coefs["age"]), + tolerance = 0.05 + ) + expect_equal( + py_to_r(unname(acro_model$params_["sex"])), + unname(r_coefs["sex"]), + tolerance = 0.05 + ) +}) + +test_that("acro_coxph with weights provided by the user", { + acro_init() + + #define a vector weight + my_weights <- runif(nrow(test_data), 0.8, 1.2) + + # R survival model + r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, weights = my_weights) + r_coefs <- stats::coef(r_model) + + acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, weights = my_weights) + + # Compare the coefficients + expect_equal( + py_to_r(unname(acro_model$params_["age"])), + unname(r_coefs["age"]), + tolerance = 0.05 + ) + expect_equal( + py_to_r(unname(acro_model$params_["sex"])), + unname(r_coefs["sex"]), + tolerance = 0.05 + ) +}) + +test_that("acro_coxph with cluster as a column in the dataset", { + acro_init() + + # R survival model + r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, cluster = inst) + r_coefs <- stats::coef(r_model) + + acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, cluster = inst) + + # Compare the coefficients + expect_equal( + py_to_r(unname(acro_model$params_["age"])), + unname(r_coefs["age"]), + tolerance = 0.05 + ) + expect_equal( + py_to_r(unname(acro_model$params_["sex"])), + unname(r_coefs["sex"]), + tolerance = 0.05 + ) +}) + +test_that("acro_coxph with cluster as a quoted column name in the dataset", { + acro_init() + + # R survival model + r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, cluster = inst) + r_coefs <- stats::coef(r_model) + + acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, cluster = "inst") + + # Compare the coefficients + expect_equal( + py_to_r(unname(acro_model$params_["age"])), + unname(r_coefs["age"]), + tolerance = 0.05 + ) + expect_equal( + py_to_r(unname(acro_model$params_["sex"])), + unname(r_coefs["sex"]), + tolerance = 0.05 + ) +}) + +test_that("acro_coxph with cluster provided by the user", { + acro_init() + + #define a cluster vector + my_cluster <- sample(1:10, size = nrow(test_data), replace = TRUE) + + # R survival model + r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, cluster = my_cluster) + r_coefs <- stats::coef(r_model) + + acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, cluster = my_cluster) + + # Compare the coefficients + expect_equal( + py_to_r(unname(acro_model$params_["age"])), + unname(r_coefs["age"]), + tolerance = 0.05 + ) + expect_equal( + py_to_r(unname(acro_model$params_["sex"])), + unname(r_coefs["sex"]), + tolerance = 0.05 + ) +}) + +test_that("acro_coxph warns on unused arguments in ...", { + acro_init() + + expect_warning( + acro_coxph(Surv(time, status) ~ age, data = test_data, dump_arg = 999), + "Unused arguments were provided: dump_arg" + ) +}) From 9868bd2fff8146eed8f29a0400080b1d6abfe55a Mon Sep 17 00:00:00 2001 From: mahaalbashir Date: Thu, 5 Nov 2026 19:03:12 +0000 Subject: [PATCH 02/11] changing teh acro version to 1.1.0 --- R/acro_init.R | 2 +- inst/WORDLIST | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/R/acro_init.R b/R/acro_init.R index f6bfd35..79f57fd 100644 --- a/R/acro_init.R +++ b/R/acro_init.R @@ -1,6 +1,6 @@ # Globals ----------------------------------------------------------------- acro_venv <- "r-acro" -acro_pkg <- "acro==1.0.2" +acro_pkg <- "acro==1.1.0" ch <- "conda-forge" diff --git a/inst/WORDLIST b/inst/WORDLIST index 34ce0cf..ec417f3 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -39,3 +39,6 @@ tidyverse www xlsx yaml +CoxPHFitter +multistate +Surv From bbb87c684f6edb8e62b5c0dd0666e2fb9db09069 Mon Sep 17 00:00:00 2001 From: mahaalbashir Date: Thu, 5 Nov 2026 19:42:56 +0000 Subject: [PATCH 03/11] changing teh pre commit version --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 31339a5..aa07231 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -45,7 +45,7 @@ repos: # Format R files - repo: https://github.com/lorenzwalthert/precommit - rev: v0.4.3.9029 + rev: v0.4.3.9034 hooks: - id: style-files args: [ From 90cde38538f39e114c291c2b441df1113b14329f Mon Sep 17 00:00:00 2001 From: mahaalbashir Date: Fri, 6 Nov 2026 11:55:27 +0000 Subject: [PATCH 04/11] resolving conflicts --- NAMESPACE | 4 - R/acro_regression.R | 74 ++++++++-------- R/utils.R | 21 +---- tests/testthat/test-acro_coxph.R | 139 +++++++++++++++---------------- 4 files changed, 108 insertions(+), 130 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index a748a7e..4f02894 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,10 +22,6 @@ export(acro_rename_output) export(acro_summarise) export(acro_surv_func) export(acro_table) -export(create_factors) -export(is_excluded) -export(is_invalid) -export(to_pandas_categorical) importFrom(stats, complete.cases, na.omit diff --git a/R/acro_regression.R b/R/acro_regression.R index efd6728..b993345 100644 --- a/R/acro_regression.R +++ b/R/acro_regression.R @@ -66,43 +66,6 @@ acro_coxph <- function(formula, data, weights = NULL, subset = NULL, na.action = stop("Multi-state models utilizing 'istate' or 'statedata' are not supported at the moment.") } - # Handle subsetting if provided - if (!missing(subset)) { - subset_expr <- substitute(subset) - subsetted_columns <- eval(subset_expr, data) - data <- data[subsetted_columns & !is.na(subsetted_columns), ] - } - - # Handle na.action - if (is.null(na.action)) { - na.action <- getOption("na.action") - } - - na_function <- match.fun(na.action) - data <- na_function(data) - - # Parse the formula to extract duration and event columns from the left-hand side - surv_func <- formula[[2]] - if (!inherits(surv_func, "call") || as.character(surv_func[[1]]) != "Surv") { - stop("The left-hand side of the formula must use Surv(), e.g., Surv(time, event) ~ x1 + x2") - } - duration_col <- as.character(surv_func[[2]]) - event_col <- as.character(surv_func[[3]]) - - # Validate and transform event column for python lifelines - event_values <- unique(na.omit(data[[event_col]])) - - if (all(event_values %in% c(1, 2))) { - data[[event_col]][data[[event_col]] == 1] <- 0 - data[[event_col]][data[[event_col]] == 2] <- 1 - } else if (!all(event_values %in% c(0, 1))) { - # If it's neither {1, 2} nor {0, 1}, throw an error - stop(paste0("In Surv(", duration_col, ", ", event_col, ") : Invalid status values")) - } - - # Extract the right-hand side (independent variables) as a formula string - independent_vars <- deparse(formula[[3]]) - # Handle weights if provided as a column or a vector weights_col <- NULL if (!missing(weights)) { @@ -143,6 +106,43 @@ acro_coxph <- function(formula, data, weights = NULL, subset = NULL, na.action = } } + # Handle subsetting if provided + if (!missing(subset)) { + subset_expr <- substitute(subset) + subsetted_columns <- eval(subset_expr, data) + data <- data[subsetted_columns & !is.na(subsetted_columns), ] + } + + # Handle na.action + if (is.null(na.action)) { + na.action <- getOption("na.action") + } + + na_function <- match.fun(na.action) + data <- na_function(data) + + # Parse the formula to extract duration and event columns from the left-hand side + surv_func <- formula[[2]] + if (!inherits(surv_func, "call") || as.character(surv_func[[1]]) != "Surv") { + stop("The left-hand side of the formula must use Surv(), e.g., Surv(time, event) ~ x1 + x2") + } + duration_col <- as.character(surv_func[[2]]) + event_col <- as.character(surv_func[[3]]) + + # Validate and transform event column for python lifelines + event_values <- unique(na.omit(data[[event_col]])) + + if (all(event_values %in% c(1, 2))) { + data[[event_col]][data[[event_col]] == 1] <- 0 + data[[event_col]][data[[event_col]] == 2] <- 1 + } else if (!all(event_values %in% c(0, 1))) { + # If it's neither {1, 2} nor {0, 1}, throw an error + stop(paste0("In Surv(", duration_col, ", ", event_col, ") : Invalid status values")) + } + + # Extract the right-hand side (independent variables) as a formula string + independent_vars <- deparse(formula[[3]]) + # Check for any unused arguments if (length(list(...)) > 0) { warning("Unused arguments were provided: ", paste0(names(list(...)), collapse = ", "), "\n", "Please use the help command to learn more about the function.") diff --git a/R/utils.R b/R/utils.R index ef7c2a2..b941bd1 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,10 +1,6 @@ #' Identify empty values; NA or NaN in an object #' -#' @param Values to check for empty values in it -#' -#' @returns logical vector that shows the places of the empty values #' @noRd - is_invalid <- function(Values) { # Check for NA or NaN in the object is.na(Values) | tolower(Values) %in% c("na", "nan") @@ -12,12 +8,7 @@ is_invalid <- function(Values) { #' Identify values to be excluded from an object #' -#' @param Values to exclude from -#' @param exclude_list Items to exclude from the values -#' -#' @returns logical vector that shows the places of the items that needs to be excluded #' @noRd - is_excluded <- function(Values, exclude_list) { # Catch everything in the exclude list mask <- Values %in% exclude_list @@ -39,12 +30,7 @@ is_excluded <- function(Values, exclude_list) { #' Convert data to factors and manage NA levels #' -#' @param Values to create factors on -#' @param useNA whether to include NA values in the table -#' -#' @returns R vector factor #' @noRd - create_factors <- function(Values, useNA) { # Check for NAs char_values <- as.character(Values) @@ -64,12 +50,7 @@ create_factors <- function(Values, useNA) { #' Convert an R factor to a Pandas Categorical #' -#' @param Values R factor vector to be converted -#' @param pd Reference to the Python `pandas` module -#' -#' @returns A Python `pandas.Categorical` object #' @noRd - to_pandas_categorical <- function(Values, pd) { # Build the Pandas Categorical pd$Categorical( @@ -79,6 +60,7 @@ to_pandas_categorical <- function(Values, pd) { ) } +#' @noRd parse_summary_expression <- function(quo) { expr <- rlang::quo_get_expr(quo) @@ -117,6 +99,7 @@ parse_summary_expression <- function(quo) { ) } +#' @noRd get_offset_hist_breaks <- function(data, column, breaks = "sturges") { # Extract the column from the data r_column <- data[[column]] diff --git a/tests/testthat/test-acro_coxph.R b/tests/testthat/test-acro_coxph.R index 5be1006..662134e 100644 --- a/tests/testthat/test-acro_coxph.R +++ b/tests/testthat/test-acro_coxph.R @@ -2,17 +2,17 @@ test_data <- lung test_that("acro_coxph without initialising ACRO object first", { acroEnv$ac <- NULL - expect_error(acro_coxph(Surv(time, status) ~ age + sex, data = test_data), "ACRO has not been initialised. Please first call acro_init()") + expect_error(acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data), "ACRO has not been initialised. Please first call acro_init()") }) test_that("acro_coxph throws an error with multi-state arguments", { acro_init() expect_error( - acro_coxph(Surv(time, status) ~ age + sex, data = test_data, istate = 1), + acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, istate = 1), "Multi-state models utilizing 'istate' or 'statedata' are not supported at the moment." ) expect_error( - acro_coxph(Surv(time, status) ~ age + sex, data = test_data, statedata = test_data), + acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, statedata = test_data), "Multi-state models utilizing 'istate' or 'statedata' are not supported at the moment." ) }) @@ -20,7 +20,7 @@ test_that("acro_coxph throws an error with multi-state arguments", { test_that("acro_coxph validates the left side of the formula uses Surv() function", { acro_init() expect_error( - acro_coxph(time ~ age + sex, data = test_data), + acro_coxph(time ~ age + ph.karno, data = test_data), "The left-hand side of the formula must use Surv\\(\\)" ) }) @@ -29,22 +29,22 @@ test_that("acro_coxph works", { acro_init() # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data) + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data) r_coefs <- stats::coef(r_model) # ACRO model - acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data) # Compare the coefficients expect_equal( - py_to_r(unname(acro_model$params_["age"])), + reticulate::py_to_r(unname(acro_model$params_["age"])), unname(r_coefs["age"]), - tolerance = 0.05 + tolerance = 0.02 ) expect_equal( - py_to_r(unname(acro_model$params_["sex"])), - unname(r_coefs["sex"]), - tolerance = 0.05 + reticulate::py_to_r(unname(acro_model$params_["ph.karno"])), + unname(r_coefs["ph.karno"]), + tolerance = 0.02 ) }) @@ -52,22 +52,22 @@ test_that("acro_coxph with subsetting", { acro_init() # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, subset = age > 50) + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, subset = age > 50) r_coefs <- stats::coef(r_model) # ACRO model - acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, subset = age > 50) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, subset = age > 50) # Compare the coefficients expect_equal( - py_to_r(unname(acro_model$params_["age"])), + reticulate::py_to_r(unname(acro_model$params_["age"])), unname(r_coefs["age"]), - tolerance = 0.05 + tolerance = 0.02 ) expect_equal( - py_to_r(unname(acro_model$params_["sex"])), - unname(r_coefs["sex"]), - tolerance = 0.05 + reticulate::py_to_r(unname(acro_model$params_["ph.karno"])), + unname(r_coefs["ph.karno"]), + tolerance = 0.02 ) }) @@ -75,22 +75,22 @@ test_that("acro_coxph with na.action", { acro_init() # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, na.action = na.pass) + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, na.action = na.exclude) r_coefs <- stats::coef(r_model) # ACRO model - acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, na.action = na.pass) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, na.action = na.exclude) # Compare the coefficients expect_equal( - py_to_r(unname(acro_model$params_["age"])), + reticulate::py_to_r(unname(acro_model$params_["age"])), unname(r_coefs["age"]), - tolerance = 0.05 + tolerance = 0.02 ) expect_equal( - py_to_r(unname(acro_model$params_["sex"])), - unname(r_coefs["sex"]), - tolerance = 0.05 + reticulate::py_to_r(unname(acro_model$params_["ph.karno"])), + unname(r_coefs["ph.karno"]), + tolerance = 0.02 ) }) @@ -99,7 +99,7 @@ test_that("acro_coxph with unvalid status options(eg. having a status as 4)", { test_data$status[test_data$status == 1] <- 4 expect_error( - acro_coxph(Surv(time, status) ~ age + sex, data = test_data), + acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data), "In Surv\\(time, status\\) : Invalid status values" ) }) @@ -111,21 +111,21 @@ test_that("acro_coxph with weights as a column from the dataset", { test_data$my_weights <- runif(nrow(test_data), 0.8, 1.2) # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, weights = my_weights) + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, weights = my_weights) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) # Compare the coefficients expect_equal( - py_to_r(unname(acro_model$params_["age"])), + reticulate::py_to_r(unname(acro_model$params_["age"])), unname(r_coefs["age"]), - tolerance = 0.05 + tolerance = 0.02 ) expect_equal( - py_to_r(unname(acro_model$params_["sex"])), - unname(r_coefs["sex"]), - tolerance = 0.05 + reticulate::py_to_r(unname(acro_model$params_["ph.karno"])), + unname(r_coefs["ph.karno"]), + tolerance = 0.02 ) }) @@ -136,21 +136,21 @@ test_that("acro_coxph with weights as a string column name from the dataset", { test_data$my_weights <- runif(nrow(test_data), 0.8, 1.2) # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, weights = my_weights) + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, weights = "my_weights") + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = "my_weights") # Compare the coefficients expect_equal( - py_to_r(unname(acro_model$params_["age"])), + reticulate::py_to_r(unname(acro_model$params_["age"])), unname(r_coefs["age"]), - tolerance = 0.05 + tolerance = 0.02 ) expect_equal( - py_to_r(unname(acro_model$params_["sex"])), - unname(r_coefs["sex"]), - tolerance = 0.05 + reticulate::py_to_r(unname(acro_model$params_["ph.karno"])), + unname(r_coefs["ph.karno"]), + tolerance = 0.02 ) }) @@ -159,23 +159,22 @@ test_that("acro_coxph with weights provided by the user", { #define a vector weight my_weights <- runif(nrow(test_data), 0.8, 1.2) - # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, weights = my_weights) + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, weights = my_weights) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) # Compare the coefficients expect_equal( - py_to_r(unname(acro_model$params_["age"])), + reticulate::py_to_r(unname(acro_model$params_["age"])), unname(r_coefs["age"]), - tolerance = 0.05 + tolerance = 0.02 ) expect_equal( - py_to_r(unname(acro_model$params_["sex"])), - unname(r_coefs["sex"]), - tolerance = 0.05 + reticulate::py_to_r(unname(acro_model$params_["ph.karno"])), + unname(r_coefs["ph.karno"]), + tolerance = 0.02 ) }) @@ -183,21 +182,21 @@ test_that("acro_coxph with cluster as a column in the dataset", { acro_init() # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, cluster = inst) + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, cluster = inst) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) # Compare the coefficients expect_equal( - py_to_r(unname(acro_model$params_["age"])), + reticulate::py_to_r(unname(acro_model$params_["age"])), unname(r_coefs["age"]), - tolerance = 0.05 + tolerance = 0.02 ) expect_equal( - py_to_r(unname(acro_model$params_["sex"])), - unname(r_coefs["sex"]), - tolerance = 0.05 + reticulate::py_to_r(unname(acro_model$params_["ph.karno"])), + unname(r_coefs["ph.karno"]), + tolerance = 0.02 ) }) @@ -205,21 +204,21 @@ test_that("acro_coxph with cluster as a quoted column name in the dataset", { acro_init() # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, cluster = inst) + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, cluster = "inst") + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = "inst") # Compare the coefficients expect_equal( - py_to_r(unname(acro_model$params_["age"])), + reticulate::py_to_r(unname(acro_model$params_["age"])), unname(r_coefs["age"]), - tolerance = 0.05 + tolerance = 0.02 ) expect_equal( - py_to_r(unname(acro_model$params_["sex"])), - unname(r_coefs["sex"]), - tolerance = 0.05 + reticulate::py_to_r(unname(acro_model$params_["ph.karno"])), + unname(r_coefs["ph.karno"]), + tolerance = 0.02 ) }) @@ -227,24 +226,24 @@ test_that("acro_coxph with cluster provided by the user", { acro_init() #define a cluster vector - my_cluster <- sample(1:10, size = nrow(test_data), replace = TRUE) + my_cluster <- sample(1:10, size = nrow(lung), replace = TRUE) # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + sex, data = test_data, cluster = my_cluster) + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = my_cluster) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(Surv(time, status) ~ age + sex, data = test_data, cluster = my_cluster) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = my_cluster) # Compare the coefficients expect_equal( - py_to_r(unname(acro_model$params_["age"])), + reticulate::py_to_r(unname(acro_model$params_["age"])), unname(r_coefs["age"]), - tolerance = 0.05 + tolerance = 0.02 ) expect_equal( - py_to_r(unname(acro_model$params_["sex"])), - unname(r_coefs["sex"]), - tolerance = 0.05 + reticulate::py_to_r(unname(acro_model$params_["ph.karno"])), + unname(r_coefs["ph.karno"]), + tolerance = 0.02 ) }) From 09abe422c8d4c374bdce5d51af55760a64ce1a3d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:57:42 +0000 Subject: [PATCH 05/11] style: pre-commit fixes --- tests/testthat/test-acro_coxph.R | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/testthat/test-acro_coxph.R b/tests/testthat/test-acro_coxph.R index 662134e..f79b6cd 100644 --- a/tests/testthat/test-acro_coxph.R +++ b/tests/testthat/test-acro_coxph.R @@ -1,4 +1,4 @@ -test_data <- lung +test_data <- lung test_that("acro_coxph without initialising ACRO object first", { acroEnv$ac <- NULL @@ -107,7 +107,7 @@ test_that("acro_coxph with unvalid status options(eg. having a status as 4)", { test_that("acro_coxph with weights as a column from the dataset", { acro_init() - #add a weight column in the dataset + # add a weight column in the dataset test_data$my_weights <- runif(nrow(test_data), 0.8, 1.2) # R survival model @@ -132,7 +132,7 @@ test_that("acro_coxph with weights as a column from the dataset", { test_that("acro_coxph with weights as a string column name from the dataset", { acro_init() - #add a weight column in the dataset + # add a weight column in the dataset test_data$my_weights <- runif(nrow(test_data), 0.8, 1.2) # R survival model @@ -157,8 +157,8 @@ test_that("acro_coxph with weights as a string column name from the dataset", { test_that("acro_coxph with weights provided by the user", { acro_init() - #define a vector weight - my_weights <- runif(nrow(test_data), 0.8, 1.2) + # define a vector weight + my_weights <- runif(nrow(test_data), 0.8, 1.2) # R survival model r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) r_coefs <- stats::coef(r_model) @@ -174,7 +174,7 @@ test_that("acro_coxph with weights provided by the user", { expect_equal( reticulate::py_to_r(unname(acro_model$params_["ph.karno"])), unname(r_coefs["ph.karno"]), - tolerance = 0.02 + tolerance = 0.02 ) }) @@ -225,7 +225,7 @@ test_that("acro_coxph with cluster as a quoted column name in the dataset", { test_that("acro_coxph with cluster provided by the user", { acro_init() - #define a cluster vector + # define a cluster vector my_cluster <- sample(1:10, size = nrow(lung), replace = TRUE) # R survival model From 8e4864c7f603bc3283a46cb7210ba50f9af3a9aa Mon Sep 17 00:00:00 2001 From: mahaalbashir Date: Fri, 6 Nov 2026 13:27:29 +0000 Subject: [PATCH 06/11] refactoring --- NAMESPACE | 4 +- R/acro_regression.R | 70 ++++++++++++-------------------- R/utils.R | 26 ++++++++++++ tests/testthat/test-acro_coxph.R | 4 +- 4 files changed, 58 insertions(+), 46 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 4f02894..feca183 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -24,5 +24,7 @@ export(acro_surv_func) export(acro_table) importFrom(stats, complete.cases, - na.omit + model.frame, + na.omit, + terms ) diff --git a/R/acro_regression.R b/R/acro_regression.R index b993345..1f4d96e 100644 --- a/R/acro_regression.R +++ b/R/acro_regression.R @@ -54,7 +54,7 @@ acro_glm <- function(formula, data, family) { #' @returns Fitted CoxPHFitter results object #' @export -#' @importFrom stats na.omit +#' @importFrom stats na.omit terms model.frame acro_coxph <- function(formula, data, weights = NULL, subset = NULL, na.action = NULL, robust = TRUE, init = NULL, cluster = NULL, istate = NULL, statedata = NULL, ...) { if (is.null(acroEnv$ac)) { @@ -66,51 +66,29 @@ acro_coxph <- function(formula, data, weights = NULL, subset = NULL, na.action = stop("Multi-state models utilizing 'istate' or 'statedata' are not supported at the moment.") } - # Handle weights if provided as a column or a vector + model_data <- as.data.frame(data) + weights_col <- NULL if (!missing(weights)) { - weights_expr <- substitute(weights) - if (!is.null(weights_expr)) { - # If weights passed as a quoted character string matching a column name - if (is.character(weights_expr) && weights_expr %in% names(data)) { - weights_col <- weights_expr - } - # If weights passed as an unquoted column name - else if (deparse(weights_expr) %in% names(data)) { - weights_col <- deparse(weights_expr) - } - # If weights passed as an external vector, expression, or function - else { - data$acro_weight_col__ <- eval(weights_expr, data, parent.frame()) - weights_col <- "acro_weight_col__" - } - } + res <- resolve_optional_column(substitute(weights), model_data, "weights", "acro_weight") + weights_col <- res$col_name + model_data <- res$data } - # Handle cluster + # 4. Handle cluster using the helper function cluster_col <- NULL if (!missing(cluster)) { - cluster_expr <- substitute(cluster) - if (!is.null(cluster_expr)) { - if (is.character(cluster_expr) && cluster_expr %in% names(data)) { - cluster_col <- cluster_expr - } else if (deparse(cluster_expr) %in% names(data)) { - cluster_col <- deparse(cluster_expr) - } - # If weights passed as an external vector, expression, or function - else { - data$acro_cluster_col__ <- eval(cluster_expr, data, parent.frame()) - cluster_col <- "acro_cluster_col__" - } - robust <- TRUE # Automatically trigger robust variance when clustering is provided - } + res <- resolve_optional_column(substitute(cluster), model_data, "cluster", "acro_cluster") + cluster_col <- res$col_name + model_data <- res$data + robust <- TRUE # Clustering implies robust variance } # Handle subsetting if provided if (!missing(subset)) { subset_expr <- substitute(subset) - subsetted_columns <- eval(subset_expr, data) - data <- data[subsetted_columns & !is.na(subsetted_columns), ] + subsetted_columns <- eval(subset_expr, model_data) + model_data <- model_data[subsetted_columns & !is.na(subsetted_columns), ] } # Handle na.action @@ -119,7 +97,7 @@ acro_coxph <- function(formula, data, weights = NULL, subset = NULL, na.action = } na_function <- match.fun(na.action) - data <- na_function(data) + model_data <- na_function(model_data) # Parse the formula to extract duration and event columns from the left-hand side surv_func <- formula[[2]] @@ -130,26 +108,30 @@ acro_coxph <- function(formula, data, weights = NULL, subset = NULL, na.action = event_col <- as.character(surv_func[[3]]) # Validate and transform event column for python lifelines - event_values <- unique(na.omit(data[[event_col]])) + event_values <- unique(na.omit(model_data[[event_col]])) if (all(event_values %in% c(1, 2))) { - data[[event_col]][data[[event_col]] == 1] <- 0 - data[[event_col]][data[[event_col]] == 2] <- 1 + model_data[[event_col]][model_data[[event_col]] == 1] <- 0 + model_data[[event_col]][model_data[[event_col]] == 2] <- 1 } else if (!all(event_values %in% c(0, 1))) { # If it's neither {1, 2} nor {0, 1}, throw an error stop(paste0("In Surv(", duration_col, ", ", event_col, ") : Invalid status values")) } - # Extract the right-hand side (independent variables) as a formula string - independent_vars <- deparse(formula[[3]]) + # Extract the right-hand side (independent variables) using terms() + trm <- terms(formula, data = model_data) + term_labels <- attr(trm, "term.labels") + + # Collapse back into a robust formula string + independent_vars <- paste(term_labels, collapse = " + ") # Check for any unused arguments if (length(list(...)) > 0) { - warning("Unused arguments were provided: ", paste0(names(list(...)), collapse = ", "), "\n", "Please use the help command to learn more about the function.") + warning("Unsupported arguments were provided: ", paste0(names(list(...)), collapse = ", "), "\n", "Please use the help command to learn more about the function.") } model <- acroEnv$ac$coxph( - df = data, + df = model_data, duration_col = duration_col, event_col = event_col, weights_col = weights_col, @@ -158,4 +140,6 @@ acro_coxph <- function(formula, data, weights = NULL, subset = NULL, na.action = initial_point = init, cluster_col = cluster_col ) + + return(model) } diff --git a/R/utils.R b/R/utils.R index b941bd1..e58cd11 100644 --- a/R/utils.R +++ b/R/utils.R @@ -114,3 +114,29 @@ get_offset_hist_breaks <- function(data, column, breaks = "sturges") { } return(breaks) } + + +#' Helper to resolve external inputs (weights, cluster) +#' @noRd +resolve_optional_column <- function(expr, data, arg_name, prefix) { + optional_col <- NULL + if (!missing(expr)) { + col_expr <- substitute(optional_col) + if (!is.null(col_expr)) { + # If exp passed as a quoted character string matching a column name + if (is.character(col_expr) && col_expr %in% names(data)) { + optional_col <- col_expr + } + # If exp passed as an unquoted column name + else if (deparse(col_expr) %in% names(data)) { + optional_col <- deparse(col_expr) + } + # If exp passed as an external vector, expression, or function + else { + optional_col <- paste0("acro_additional_", sample(10000:99999, 1)) + data[[optional_col]] <- eval(col_expr, data, parent.frame()) + } + } + } + return(list(col_name = optional_col, data = data)) +} diff --git a/tests/testthat/test-acro_coxph.R b/tests/testthat/test-acro_coxph.R index f79b6cd..4340bf1 100644 --- a/tests/testthat/test-acro_coxph.R +++ b/tests/testthat/test-acro_coxph.R @@ -94,7 +94,7 @@ test_that("acro_coxph with na.action", { ) }) -test_that("acro_coxph with unvalid status options(eg. having a status as 4)", { +test_that("acro_coxph with invalid status options(eg. having a status as 4)", { acro_init() test_data$status[test_data$status == 1] <- 4 @@ -252,6 +252,6 @@ test_that("acro_coxph warns on unused arguments in ...", { expect_warning( acro_coxph(Surv(time, status) ~ age, data = test_data, dump_arg = 999), - "Unused arguments were provided: dump_arg" + "Unsupported arguments were provided: dump_arg" ) }) From 4e01f859a9b6628b911becfd6f450204509921dd Mon Sep 17 00:00:00 2001 From: mahaalbashir Date: Fri, 6 Nov 2026 13:45:21 +0000 Subject: [PATCH 07/11] resolve code cov --- R/utils.R | 2 ++ tests/testthat/test-acro_coxph.R | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index e58cd11..1a8999b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -118,6 +118,7 @@ get_offset_hist_breaks <- function(data, column, breaks = "sturges") { #' Helper to resolve external inputs (weights, cluster) #' @noRd +## nocov start resolve_optional_column <- function(expr, data, arg_name, prefix) { optional_col <- NULL if (!missing(expr)) { @@ -140,3 +141,4 @@ resolve_optional_column <- function(expr, data, arg_name, prefix) { } return(list(col_name = optional_col, data = data)) } +## nocov end diff --git a/tests/testthat/test-acro_coxph.R b/tests/testthat/test-acro_coxph.R index 4340bf1..163fdce 100644 --- a/tests/testthat/test-acro_coxph.R +++ b/tests/testthat/test-acro_coxph.R @@ -8,7 +8,7 @@ test_that("acro_coxph without initialising ACRO object first", { test_that("acro_coxph throws an error with multi-state arguments", { acro_init() expect_error( - acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, istate = 1), + acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, istate = 1), "Multi-state models utilizing 'istate' or 'statedata' are not supported at the moment." ) expect_error( From b369154950c42a57b0ba1b0239fd1fb4ce8b5db9 Mon Sep 17 00:00:00 2001 From: mahaalbashir Date: Fri, 6 Nov 2026 14:36:20 +0000 Subject: [PATCH 08/11] suggested solution for the failing tests in Ubuntu --- tests/testthat/test-acro_coxph.R | 68 ++++++++++++++++---------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/tests/testthat/test-acro_coxph.R b/tests/testthat/test-acro_coxph.R index 163fdce..fbe1872 100644 --- a/tests/testthat/test-acro_coxph.R +++ b/tests/testthat/test-acro_coxph.R @@ -2,7 +2,7 @@ test_data <- lung test_that("acro_coxph without initialising ACRO object first", { acroEnv$ac <- NULL - expect_error(acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data), "ACRO has not been initialised. Please first call acro_init()") + expect_error(acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data), "ACRO has not been initialised. Please first call acro_init()") }) test_that("acro_coxph throws an error with multi-state arguments", { @@ -12,28 +12,28 @@ test_that("acro_coxph throws an error with multi-state arguments", { "Multi-state models utilizing 'istate' or 'statedata' are not supported at the moment." ) expect_error( - acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, statedata = test_data), + acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, statedata = test_data), "Multi-state models utilizing 'istate' or 'statedata' are not supported at the moment." ) }) -test_that("acro_coxph validates the left side of the formula uses Surv() function", { +test_that("acro_coxph validates the left side of the formula uses survival::Surv() function", { acro_init() expect_error( acro_coxph(time ~ age + ph.karno, data = test_data), - "The left-hand side of the formula must use Surv\\(\\)" + "The left-hand side of the formula must use survival::Surv\\(\\)" ) }) test_that("acro_coxph works", { acro_init() - # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data) + # R survival::Survival model + r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data) r_coefs <- stats::coef(r_model) # ACRO model - acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data) + acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data) # Compare the coefficients expect_equal( @@ -51,12 +51,12 @@ test_that("acro_coxph works", { test_that("acro_coxph with subsetting", { acro_init() - # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, subset = age > 50) + # R survival::Survival model + r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, subset = age > 50) r_coefs <- stats::coef(r_model) # ACRO model - acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, subset = age > 50) + acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, subset = age > 50) # Compare the coefficients expect_equal( @@ -74,12 +74,12 @@ test_that("acro_coxph with subsetting", { test_that("acro_coxph with na.action", { acro_init() - # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, na.action = na.exclude) + # R survival::Survival model + r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, na.action = na.exclude) r_coefs <- stats::coef(r_model) # ACRO model - acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, na.action = na.exclude) + acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, na.action = na.exclude) # Compare the coefficients expect_equal( @@ -99,8 +99,8 @@ test_that("acro_coxph with invalid status options(eg. having a status as 4)", { test_data$status[test_data$status == 1] <- 4 expect_error( - acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data), - "In Surv\\(time, status\\) : Invalid status values" + acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data), + "In survival::Surv\\(time, status\\) : Invalid status values" ) }) @@ -110,11 +110,11 @@ test_that("acro_coxph with weights as a column from the dataset", { # add a weight column in the dataset test_data$my_weights <- runif(nrow(test_data), 0.8, 1.2) - # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) + # R survival::Survival model + r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) + acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) # Compare the coefficients expect_equal( @@ -135,11 +135,11 @@ test_that("acro_coxph with weights as a string column name from the dataset", { # add a weight column in the dataset test_data$my_weights <- runif(nrow(test_data), 0.8, 1.2) - # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) + # R survival::Survival model + r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = "my_weights") + acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = "my_weights") # Compare the coefficients expect_equal( @@ -159,11 +159,11 @@ test_that("acro_coxph with weights provided by the user", { # define a vector weight my_weights <- runif(nrow(test_data), 0.8, 1.2) - # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) + # R survival::Survival model + r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) + acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) # Compare the coefficients expect_equal( @@ -181,11 +181,11 @@ test_that("acro_coxph with weights provided by the user", { test_that("acro_coxph with cluster as a column in the dataset", { acro_init() - # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) + # R survival::Survival model + r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) + acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) # Compare the coefficients expect_equal( @@ -203,11 +203,11 @@ test_that("acro_coxph with cluster as a column in the dataset", { test_that("acro_coxph with cluster as a quoted column name in the dataset", { acro_init() - # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) + # R survival::Survival model + r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = "inst") + acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = "inst") # Compare the coefficients expect_equal( @@ -228,11 +228,11 @@ test_that("acro_coxph with cluster provided by the user", { # define a cluster vector my_cluster <- sample(1:10, size = nrow(lung), replace = TRUE) - # R survival model - r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = my_cluster) + # R survival::Survival model + r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = my_cluster) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = my_cluster) + acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = my_cluster) # Compare the coefficients expect_equal( @@ -251,7 +251,7 @@ test_that("acro_coxph warns on unused arguments in ...", { acro_init() expect_warning( - acro_coxph(Surv(time, status) ~ age, data = test_data, dump_arg = 999), + acro_coxph(survival::Surv(time, status) ~ age, data = test_data, dump_arg = 999), "Unsupported arguments were provided: dump_arg" ) }) From dc97f4eadb16b2798ceb9cf1de0a21e39ccc1341 Mon Sep 17 00:00:00 2001 From: mahaalbashir Date: Fri, 6 Nov 2026 14:51:56 +0000 Subject: [PATCH 09/11] fixing typo --- tests/testthat/test-acro_coxph.R | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/testthat/test-acro_coxph.R b/tests/testthat/test-acro_coxph.R index fbe1872..4038b07 100644 --- a/tests/testthat/test-acro_coxph.R +++ b/tests/testthat/test-acro_coxph.R @@ -29,7 +29,7 @@ test_that("acro_coxph works", { acro_init() # R survival::Survival model - r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data) + r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data) r_coefs <- stats::coef(r_model) # ACRO model @@ -52,7 +52,7 @@ test_that("acro_coxph with subsetting", { acro_init() # R survival::Survival model - r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, subset = age > 50) + r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, subset = age > 50) r_coefs <- stats::coef(r_model) # ACRO model @@ -75,7 +75,7 @@ test_that("acro_coxph with na.action", { acro_init() # R survival::Survival model - r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, na.action = na.exclude) + r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, na.action = na.exclude) r_coefs <- stats::coef(r_model) # ACRO model @@ -111,7 +111,7 @@ test_that("acro_coxph with weights as a column from the dataset", { test_data$my_weights <- runif(nrow(test_data), 0.8, 1.2) # R survival::Survival model - r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) + r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) r_coefs <- stats::coef(r_model) acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) @@ -136,7 +136,7 @@ test_that("acro_coxph with weights as a string column name from the dataset", { test_data$my_weights <- runif(nrow(test_data), 0.8, 1.2) # R survival::Survival model - r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) + r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) r_coefs <- stats::coef(r_model) acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = "my_weights") @@ -160,7 +160,7 @@ test_that("acro_coxph with weights provided by the user", { # define a vector weight my_weights <- runif(nrow(test_data), 0.8, 1.2) # R survival::Survival model - r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) + r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) r_coefs <- stats::coef(r_model) acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) @@ -182,7 +182,7 @@ test_that("acro_coxph with cluster as a column in the dataset", { acro_init() # R survival::Survival model - r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) + r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) r_coefs <- stats::coef(r_model) acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) @@ -204,7 +204,7 @@ test_that("acro_coxph with cluster as a quoted column name in the dataset", { acro_init() # R survival::Survival model - r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) + r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) r_coefs <- stats::coef(r_model) acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = "inst") @@ -229,7 +229,7 @@ test_that("acro_coxph with cluster provided by the user", { my_cluster <- sample(1:10, size = nrow(lung), replace = TRUE) # R survival::Survival model - r_model <- survival::Survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = my_cluster) + r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = my_cluster) r_coefs <- stats::coef(r_model) acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = my_cluster) From 54877847cc3413b0c11c74550c65dd0de3827091 Mon Sep 17 00:00:00 2001 From: mahaalbashir Date: Fri, 6 Nov 2026 15:07:29 +0000 Subject: [PATCH 10/11] adding survival pacakge to import --- DESCRIPTION | 6 +-- tests/testthat/test-acro_coxph.R | 71 ++++++++++++++++---------------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d7d01b0..49fcc3c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,13 +28,13 @@ Imports: rlang, tibble, tidyselect, - utils + utils, + survival Suggests: knitr, rmarkdown, spelling, - testthat (>= 3.0.0), - survival + testthat (>= 3.0.0) VignetteBuilder: knitr Config/roxygen2/version: 8.1.1 diff --git a/tests/testthat/test-acro_coxph.R b/tests/testthat/test-acro_coxph.R index 4038b07..3d95aae 100644 --- a/tests/testthat/test-acro_coxph.R +++ b/tests/testthat/test-acro_coxph.R @@ -1,39 +1,40 @@ +library(survival) test_data <- lung test_that("acro_coxph without initialising ACRO object first", { acroEnv$ac <- NULL - expect_error(acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data), "ACRO has not been initialised. Please first call acro_init()") + expect_error(acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data), "ACRO has not been initialised. Please first call acro_init()") }) test_that("acro_coxph throws an error with multi-state arguments", { acro_init() expect_error( - acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, istate = 1), + acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, istate = 1), "Multi-state models utilizing 'istate' or 'statedata' are not supported at the moment." ) expect_error( - acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, statedata = test_data), + acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, statedata = test_data), "Multi-state models utilizing 'istate' or 'statedata' are not supported at the moment." ) }) -test_that("acro_coxph validates the left side of the formula uses survival::Surv() function", { +test_that("acro_coxph validates the left side of the formula uses Surv() function", { acro_init() expect_error( acro_coxph(time ~ age + ph.karno, data = test_data), - "The left-hand side of the formula must use survival::Surv\\(\\)" + "The left-hand side of the formula must use Surv\\(\\)" ) }) test_that("acro_coxph works", { acro_init() - # R survival::Survival model - r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data) + # R Survival model + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data) r_coefs <- stats::coef(r_model) # ACRO model - acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data) # Compare the coefficients expect_equal( @@ -51,12 +52,12 @@ test_that("acro_coxph works", { test_that("acro_coxph with subsetting", { acro_init() - # R survival::Survival model - r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, subset = age > 50) + # R Survival model + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, subset = age > 50) r_coefs <- stats::coef(r_model) # ACRO model - acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, subset = age > 50) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, subset = age > 50) # Compare the coefficients expect_equal( @@ -74,12 +75,12 @@ test_that("acro_coxph with subsetting", { test_that("acro_coxph with na.action", { acro_init() - # R survival::Survival model - r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, na.action = na.exclude) + # R Survival model + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, na.action = na.exclude) r_coefs <- stats::coef(r_model) # ACRO model - acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, na.action = na.exclude) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, na.action = na.exclude) # Compare the coefficients expect_equal( @@ -99,8 +100,8 @@ test_that("acro_coxph with invalid status options(eg. having a status as 4)", { test_data$status[test_data$status == 1] <- 4 expect_error( - acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data), - "In survival::Surv\\(time, status\\) : Invalid status values" + acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data), + "In Surv\\(time, status\\) : Invalid status values" ) }) @@ -110,11 +111,11 @@ test_that("acro_coxph with weights as a column from the dataset", { # add a weight column in the dataset test_data$my_weights <- runif(nrow(test_data), 0.8, 1.2) - # R survival::Survival model - r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) + # R Survival model + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) # Compare the coefficients expect_equal( @@ -135,11 +136,11 @@ test_that("acro_coxph with weights as a string column name from the dataset", { # add a weight column in the dataset test_data$my_weights <- runif(nrow(test_data), 0.8, 1.2) - # R survival::Survival model - r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) + # R Survival model + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = "my_weights") + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = "my_weights") # Compare the coefficients expect_equal( @@ -159,11 +160,11 @@ test_that("acro_coxph with weights provided by the user", { # define a vector weight my_weights <- runif(nrow(test_data), 0.8, 1.2) - # R survival::Survival model - r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) + # R Survival model + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, weights = my_weights) # Compare the coefficients expect_equal( @@ -181,11 +182,11 @@ test_that("acro_coxph with weights provided by the user", { test_that("acro_coxph with cluster as a column in the dataset", { acro_init() - # R survival::Survival model - r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) + # R Survival model + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) # Compare the coefficients expect_equal( @@ -203,11 +204,11 @@ test_that("acro_coxph with cluster as a column in the dataset", { test_that("acro_coxph with cluster as a quoted column name in the dataset", { acro_init() - # R survival::Survival model - r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) + # R Survival model + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = inst) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = "inst") + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = "inst") # Compare the coefficients expect_equal( @@ -228,11 +229,11 @@ test_that("acro_coxph with cluster provided by the user", { # define a cluster vector my_cluster <- sample(1:10, size = nrow(lung), replace = TRUE) - # R survival::Survival model - r_model <- survival::coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = my_cluster) + # R Survival model + r_model <- survival::coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = my_cluster) r_coefs <- stats::coef(r_model) - acro_model <- acro_coxph(survival::Surv(time, status) ~ age + ph.karno, data = test_data, cluster = my_cluster) + acro_model <- acro_coxph(Surv(time, status) ~ age + ph.karno, data = test_data, cluster = my_cluster) # Compare the coefficients expect_equal( @@ -251,7 +252,7 @@ test_that("acro_coxph warns on unused arguments in ...", { acro_init() expect_warning( - acro_coxph(survival::Surv(time, status) ~ age, data = test_data, dump_arg = 999), + acro_coxph(Surv(time, status) ~ age, data = test_data, dump_arg = 999), "Unsupported arguments were provided: dump_arg" ) }) From fe0a3265f149b8304756d1eea552224301fd5e96 Mon Sep 17 00:00:00 2001 From: mahaalbashir Date: Fri, 6 Nov 2026 16:50:53 +0000 Subject: [PATCH 11/11] updating the description of the coxph function --- R/acro_regression.R | 13 +++++++++++-- man/acro_coxph.Rd | 9 +++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/R/acro_regression.R b/R/acro_regression.R index 1f4d96e..7f5bf6b 100644 --- a/R/acro_regression.R +++ b/R/acro_regression.R @@ -37,7 +37,15 @@ acro_glm <- function(formula, data, family) { model$summary() } -#' Fits a Cox proportional hazards regression model +#' Fits a Cox proportional hazards regression model. +#' +#' @description +#' Fits a Cox proportional hazards model via ACRO. This function is designed +#' as a drop-in replacement for \code{survival::coxph()}. +#' +#' @details +#' Please note that multi-state models (using \code{istate} or \code{statedata}) +#' and time-varying covariate models are currently not supported. #' #' @param formula a formula object, with the response on the left of a ~ operator, and the terms on the right. The response must be a survival object as returned by the Surv function. #' @param data a data.frame in which to interpret the variables named in the formula, or in the subset and the weights argument. @@ -68,6 +76,7 @@ acro_coxph <- function(formula, data, weights = NULL, subset = NULL, na.action = model_data <- as.data.frame(data) + # Handle weights weights_col <- NULL if (!missing(weights)) { res <- resolve_optional_column(substitute(weights), model_data, "weights", "acro_weight") @@ -75,7 +84,7 @@ acro_coxph <- function(formula, data, weights = NULL, subset = NULL, na.action = model_data <- res$data } - # 4. Handle cluster using the helper function + # Handle cluster cluster_col <- NULL if (!missing(cluster)) { res <- resolve_optional_column(substitute(cluster), model_data, "cluster", "acro_cluster") diff --git a/man/acro_coxph.Rd b/man/acro_coxph.Rd index 87e5a4c..afcaf35 100644 --- a/man/acro_coxph.Rd +++ b/man/acro_coxph.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/acro_regression.R \name{acro_coxph} \alias{acro_coxph} -\title{Fits a Cox proportional hazards regression model} +\title{Fits a Cox proportional hazards regression model.} \usage{ acro_coxph( formula, @@ -45,5 +45,10 @@ acro_coxph( Fitted CoxPHFitter results object } \description{ -Fits a Cox proportional hazards regression model +Fits a Cox proportional hazards model via ACRO. This function is designed +as a drop-in replacement for \code{survival::coxph()}. +} +\details{ +Please note that multi-state models (using \code{istate} or \code{statedata}) +and time-varying covariate models are currently not supported. }