Skip to content

Commit

Permalink
submission
Browse files Browse the repository at this point in the history
  • Loading branch information
Carrie Cheng committed Dec 2, 2024
1 parent 36d00b9 commit e9deff9
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 229 deletions.
3 changes: 3 additions & 0 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Version: 0.1.0
Date: 2024-12-01 17:39:28 UTC
SHA: 36d00b91829dc9bb06f4b60996041137a30b8aa9
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Authors@R: c(person("Zhaoxi", "Cheng", role = c("aut", "cre"),
email = "mhernan@hsph.harvard.edu"),
person("2024 The President and Fellows of Harvard College",
role = c("cph")))
Description: Implements iterative conditional expectation (ICE) estimators of the plug-in g-formula.
Description: Implements iterative conditional expectation (ICE) estimators of the plug-in g-formula (Wen, Young, Robins, and Hernán (2020) <doi: 10.1111/biom.13321>).
Both singly robust and doubly robust ICE estimators based on parametric models are available.
The package can be used to estimate survival curves under sustained treatment strategies (interventions) using longitudinal data with time-varying treatments, time-varying confounders, censoring, and competing events.
The interventions can be static or dynamic, and deterministic or stochastic (including threshold interventions). Both prespecified and user-defined interventions are available.
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# gfoRmulaICE 0.1.0

* Initial CRAN submission.
147 changes: 42 additions & 105 deletions R/ICE.R

Large diffs are not rendered by default.

48 changes: 24 additions & 24 deletions R/ICE_pool.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@
#' ice_static <- ice_pool(data = data, K = 5,
#' id = "id", time_name = "t0", outcome_name = "Y",
#' censor_name = "C", competing_name = "D",
#' total_effect = F,
#' total_effect = FALSE,
#' outcome_model = Y ~ L1 + L2 + A1 + A2,
#' censor_model = C ~ L1 + L2 + A1 + A2,
#' competing_model = D ~ L1 + L2 + A1 + A2,
#' hazard_model = Y ~ L1 + L2 + A1 + A2,
#' interventions = list(static(1)),
#' intervention_names = list("A"),
#' hazard_based = T,
#' global_hazard = F,
#' hazard_based = TRUE,
#' global_hazard = FALSE,
#' intervention_description = "Always Treat")
#'
#' ice_static
Expand All @@ -107,12 +107,12 @@
#' ice_natural_course <- ice_pool(data = data, K = 5,
#' id = "id", time_name = "t0", outcome_name = "Y",
#' censor_name = "C", competing_name = "D",
#' total_effect = T,
#' total_effect = TRUE,
#' outcome_model = Y ~ L1 + L2 + A1 + A2,
#' censor_model = C ~ L1 + L2 + A1 + A2,
#' interventions = list(natural_course()),
#' intervention_names = list("A"),
#' hazard_based = F,
#' hazard_based = FALSE,
#' intervention_description = "Natural Course")
#'
#' ice_natural_course
Expand All @@ -126,12 +126,12 @@
#' ice_dynamic <- ice_pool(data = data, K = 5,
#' id = "id", time_name = "t0", outcome_name = "Y",
#' censor_name = "C", competing_name = "D",
#' total_effect = F,
#' total_effect = FALSE,
#' outcome_model = Y ~ L1 + L2 + A1 + A2,
#' censor_model = C ~ L1 + L2 + A1 + A2,
#' interventions = list(dynamic("L1 > 0", static(0), static(1), absorb = T)),
#' interventions = list(dynamic("L1 > 0", static(0), static(1), absorb = TRUE)),
#' intervention_names = list("A"),
#' hazard_based = F,
#' hazard_based = FALSE,
#' intervention_description = "Dynamic Treat")
#'
#' ice_dynamic
Expand All @@ -143,14 +143,14 @@
#' ice_grace_period <- ice_pool(data = data, K = 5,
#' id = "id", time_name = "t0", outcome_name = "Y",
#' censor_name = "C", competing_name = "D",
#' total_effect = T,
#' total_effect = TRUE,
#' outcome_model = Y ~ L1 + L2 + A1 + A2,
#' censor_model = C ~ L1 + L2 + A1 + A2,
#' competing_model = D ~ L1 + L2 + A1 + A2,
#' hazard_model = Y ~ L1 + L2 + A1 + A2,
#' interventions = list(grace_period("uniform", 2, "L1 = 0")),
#' intervention_names = list("A"),
#' hazard_based = T,
#' hazard_based = TRUE,
#' intervention_description = "Dynamic Treat Grace Period")
#'
#' ice_grace_period
Expand All @@ -163,14 +163,14 @@
#' ice_threshold <- ice_pool(data = data, K = 5,
#' id = "id", time_name = "t0", outcome_name = "Y",
#' censor_name = "C", competing_name = "D",
#' total_effect = T,
#' total_effect = TRUE,
#' outcome_model = Y ~ L1 + L2 + A1 + A2,
#' censor_model = C ~ L1 + L2 + A1 + A2,
#' competing_model = D ~ L1 + L2 + A1 + A2,
#' hazard_model = Y ~ L1 + L2 + A1 + A2,
#' interventions = list(threshold(-3, Inf)),
#' intervention_names = list("A"),
#' hazard_based = T,
#' hazard_based = TRUE,
#' intervention_description = "Threshold Intervention")
#'
#' ice_threshold
Expand All @@ -181,7 +181,7 @@ ice_pool <- function(data, K, id, time_name, outcome_name,
competing_model = NULL, hazard_model = NULL,
interventions,
intervention_names, intervention_times = NULL,
compute_nc_risk = T, hazard_based, global_hazard = NULL,
compute_nc_risk = TRUE, hazard_based, global_hazard = NULL,
intervention_description, verbose = TRUE)
# global_haz_model, )
{
Expand All @@ -194,8 +194,8 @@ ice_pool <- function(data, K, id, time_name, outcome_name,
intervention_varnames <- intervention_names
time0 <- time_name

if (hazard_based == F) {
global_hazard <- F
if (hazard_based == FALSE) {
global_hazard <- FALSE
}

if (is.null(intervention_times) | (length(intervention_times[[1]]) == 0)) {
Expand Down Expand Up @@ -427,7 +427,7 @@ ice_pool <- function(data, K, id, time_name, outcome_name,
competing_covar_nc <- competing_covar
}

if (total_effect == F) {
if (total_effect == FALSE) {

competing_formula <- as.formula(paste0(competing_varname, "~",
paste0(competing_covar_nc, collapse = "+")))
Expand Down Expand Up @@ -477,7 +477,7 @@ ice_pool <- function(data, K, id, time_name, outcome_name,
abar_all <- list()
data <- as.data.frame(data)

assign.global(F, "gp_indicator")
assign.global(FALSE, "gp_indicator")

assign.global(data, "interv_data")

Expand Down Expand Up @@ -572,7 +572,7 @@ ice_pool <- function(data, K, id, time_name, outcome_name,

comp_init <- c()

if (!is.null(competing_varname) & (total_effect == T) & hazard_based) {
if (!is.null(competing_varname) & (total_effect == TRUE) & hazard_based) {
formula_full_comp <- as.formula(paste0(competing_varname,"~", paste0(c(competing_covar), collapse = "+")))
yfitog_comp = speedglm(formula_full_comp, family = binomial(), data = data)
paramcomp = (yfitog_comp)$coef
Expand Down Expand Up @@ -723,14 +723,14 @@ ice_pool <- function(data, K, id, time_name, outcome_name,
## 5. regression at each time point

if (verbose) {
cat("Running ", intervention_description[[1]], "... \n")
message("Running ", intervention_description[[1]], "... \n")
}
for (i in 1:K) {

t <- K - i + 1

if (verbose) {
cat(paste0("Running Time ", t, "...", "\n"))
message(paste0("Running Time ", t, "...", "\n"))
}

covar_t <- paste0(outcome_covar, sep = paste0("_", t - 1))
Expand All @@ -739,7 +739,7 @@ ice_pool <- function(data, K, id, time_name, outcome_name,
if (t == 1) {
data_pred_tmp <- data_fit <- tmpdata
} else {
if (!is.null(competing_varname) & total_effect == F) {
if (!is.null(competing_varname) & total_effect == FALSE) {
data_pred_tmp <- data_fit <- tmpdata[!is.na(tmpdata[, paste0(censor_varname, "_", t - 1)]) & tmpdata[, paste0(censor_varname, "_", t - 1)] == 0 &
!is.na(tmpdata[, paste0(competing_varname, "_", t - 2)]) & tmpdata[, paste0(competing_varname, "_", t - 2)] == 0, ]
} else {
Expand Down Expand Up @@ -819,7 +819,7 @@ ice_pool <- function(data, K, id, time_name, outcome_name,
data_fit = tmpdata
} else {

if (!is.null(competing_varname) & total_effect == F) {
if (!is.null(competing_varname) & total_effect == FALSE) {
data_fit = tmpdata[!is.na(tmpdata[, paste0(censor_varname, "_", iter - 1)]) & tmpdata[, paste0(censor_varname, "_", iter - 1)] == 0 &
!is.na(tmpdata[, paste0(competing_varname, "_", iter - 2)]) & tmpdata[, paste0(competing_varname, "_", iter - 2)] == 0, ]
} else {
Expand Down Expand Up @@ -868,7 +868,7 @@ ice_pool <- function(data, K, id, time_name, outcome_name,
has_time <- str_detect(haz_global_covar, time_name)

if (any(has_time)) {
time_idx <- which(has_time == T)
time_idx <- which(has_time == TRUE)
haz_global_covar_tmp <- haz_global_covar[-time_idx]
pred_data <- data.frame(matrix(NA,
ncol = length(haz_global_covar_tmp) + length(time_idx),
Expand All @@ -894,7 +894,7 @@ ice_pool <- function(data, K, id, time_name, outcome_name,
}

## need to calculate d_hat and multiply it with the hazard
if (!is.null(competing_varname) & total_effect == T) {
if (!is.null(competing_varname) & total_effect == TRUE) {
predict_comp <- plogis(as.matrix(covar_mat) %*% matrix(paramcomp, nrow = length(paramcomp)))
data_fit[, paste0("y", t, "pred")] <- predict(fit_temp, newdata = data_fit, type = "response") * (1 - predict_tmp) * (1 - predict_comp) + predict_tmp
} else {
Expand Down
Loading

0 comments on commit e9deff9

Please sign in to comment.