inst/nami_simulations/simulation_code/RCT/binary/experiments.R

############# Experiments BINARY

#----
# 0) Setup
#----
source("../def.R")
NLIST <- 161
PROBNAM <- "binary"
source("../../helpers.R")

set.seed(SEED)
setups <- expand.grid(repl = seq(1, REPL), N = NLIST, P = PLIST, truetau = TAULIST, lambda = LAMBDALIST)
setups$seed <- round(runif(nrow(setups)) * 1e8)

#----
# 1) Load helper functions & libraries
#----
pkgs <- c("tram", "batchtools", "multcomp", "speff2trial", "tmle")
get_packages(pkgs)

generate_data <- function(...) generate_bin(..., RCT = TRUE)

#----
# 2) Define methods 
#----
fun_speff <- function(instance, fixed = FALSE, ...) {
  xnnames <- names(instance)[grep("x", names(instance))]
  fm <- as.formula(paste("y01 ~", paste(xnnames, collapse = "+")))
  instance$w01 <- (0:1)[instance$w]
  instance$y01 <- (0:1)[instance$y]
  if (fixed) {
      ### w/o variable selection
      m <- speff(fm, data = instance, trt.id = "w01", endpoint = "dichotomous",
                 force.in = 1:length(xnnames))
  } else {
      ### with variable selection
      m <- speff(fm, data = instance, trt.id = "w01", endpoint = "dichotomous")
  }
  tau <- m$coef["Speff","Treat Effect"]
  setau <- sqrt(m$varbeta)["Speff"]
  selvars <- unique(do.call("c", sapply(m$formula, function(x) list(all.vars(x[-2])))))
  list(tau = tau, se = setau, wald = tau + qnorm(c(.025, .975)) * setau, 
       pvalue = 2 * pnorm(abs(tau / setau), lower = FALSE), selvars = selvars)
}


fun_tmle <- function(instance, ...) {
   xnnames <- names(instance)[grep("x", names(instance))]
   Y <- (0:1)[instance$y]
   X <- data.frame(instance[, xnnames])
   names(X) <- xnnames
   formstringY <- paste0("Y ~ A + ", paste(xnnames, collapse = " + "))
   m <- tmle(Y = Y, A = (0:1)[instance$w], W = X, family = "binomial",
       Qform = formstringY, g1W = rep(0.5, nrow(instance)))
   mres <- m$estimate$OR
   list(tau = mres$log.psi, 
	se = sqrt(mres$var.log.psi), 
        lambda = NA, 
        wald = log(mres$CI), 
        pvalue = mres$pvalue
   )
}



methods <- list("polr_w" = fun_polr_w,
                "polr_wx" = fun_polr_wx,
                "mmlt_polr" = fun_mmlt_polr,
                "ystand" = fun_speff, 
		"tmle" = fun_tmle)

if (TESTONLY) {
   d <- fun(1, 100, 5, truetau = 0.5, lambda = 1, seed = 19724948)
   print(lapply(methods, function(f) f(d)))
} else {
   source("../../run_experiments.R")
}
  

Try the tram package in your browser

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

tram documentation built on Nov. 23, 2026, 5:07 p.m.