R/ce_estimate_rams_att_boot.R

Defines functions ce_estimate_rams_att_boot

Documented in ce_estimate_rams_att_boot

#' Causal inference with multiple treatments using RAMS for ATT effects
#' (bootstrapping for CI)
#'
#' The function \code{ce_estimate_rams_att_boot} implements
#' RAMS with bootstrapping to estimate ATT effect with
#' multiple treatments using observational data.
#'
#' @param y A numeric vector (0, 1) representing a binary outcome.
#' @param w A numeric vector representing the treatment groups.
#' @param x A dataframe, including all the covariates but not treatments.
#' @param reference_trt A numeric value indicating reference treatment group
#' for ATT effect.
#' @param method A character string. Users can selected from the
#' following methods including \code{"RAMS-Multinomial"},
#' \code{"RAMS-GBM"}, \code{"RAMS-SL"}.
#' @param nboots A numeric value representing the number of bootstrap samples.
#' @param verbose_boot A logical value indicating whether to
#' print the progress of nonparametric bootstrap.
#' @param ... Other parameters that can be passed through to functions.
#'
#' @return A summary of the effect estimates can be obtained
#' with \code{summary} function.
#' @importFrom stringr str_sub
#' @references
#' Hadley Wickham (2019).
#' \emph{stringr: Simple, Consistent Wrappers for Common String Operations}.
#' R package version 1.4.0.
#' URL:\url{https://CRAN.R-project.org/package=stringr}
ce_estimate_rams_att_boot <-
  function(y,
           w,
           x,
           reference_trt,
           method,
           nboots,
           verbose_boot,
           ...) {
    trim_alpha <- parent.frame()$trim_alpha
    sl_library <- parent.frame()$sl_library

    n_trt <- length(unique(w))
    for (i in 1:n_trt) {
      assign(paste0("rams_att_result_", i, "_all"), NULL)
    }
    # Start bootstrapping
    names_result <- NULL
    for (j in 1:nboots) {
      bootstrap_id <- sample(length(y), replace = T)
      y_boot <- y[bootstrap_id]
      trt_boot <- w[bootstrap_id]
      x_boot <- x[bootstrap_id, ]
      rams_att_result <- ce_estimate_rams_att(
        y = y_boot,
        x = x_boot,
        w = trt_boot,
        reference_trt = reference_trt,
        method = method,
        ...
      )
      names_result <- names(rams_att_result)
      for (i in 1:(n_trt - 1)) {
        assign(paste0("rams_att_result_", i, "_all"),
               cbind(eval(parse(
                 text = paste0("rams_att_result_", i, "_all")
               )), rams_att_result[[i]]))
      }
      if (verbose_boot == TRUE) {
        print(paste0("Finish bootstrapping ", j))
      }
    }
    # Save the results of bootstrapping
    result <- NULL
    for (i in 1:(n_trt - 1)) {
      assign(paste0("RD_", i), list(as.double(eval(
        parse(text = paste0("rams_att_result_", i, "_all"))
      )[1, ])))
      assign(paste0("RR_", i), list(as.double(eval(
        parse(text = paste0("rams_att_result_", i, "_all"))
      )[2, ])))
      assign(paste0("OR_", i), list(as.double(eval(
        parse(text = paste0("rams_att_result_", i, "_all"))
      )[3, ])))

      assign(paste0("RD_", i), stats::setNames(eval(parse(text = (
        paste0("RD_", i)
      ))), paste0(
        "ATT_RD", stringr::str_sub(names_result[i], 4, 5)
      )))
      assign(paste0("RR_", i), stats::setNames(eval(parse(text = (
        paste0("RR_", i)
      ))), paste0(
        "ATT_RR", stringr::str_sub(names_result[i], 4, 5)
      )))
      assign(paste0("OR_", i), stats::setNames(eval(parse(text = (
        paste0("OR_", i)
      ))), paste0(
        "ATT_OR", stringr::str_sub(names_result[i], 4, 5)
      )))
      result <-
        c(result, (eval(parse(text = (
          paste0("RD_", i)
        )))), (eval(parse(text = (
          paste0("RR_", i)
        )))), (eval(parse(text = (
          paste0("OR_", i)
        )))))
    }
    result <- c(result, list(method = parent.frame()$method))
    class(result) <- "CIMTx_ATT_posterior"
    return(result)
  }

Try the CIMTx package in your browser

Any scripts or data that you put into this service are public.

CIMTx documentation built on June 24, 2022, 9:07 a.m.