inst/nami_simulations/simulation_code/observational/sim_dandl/binary/experiments.R

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

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

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

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

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

#----
# 2) Define extra methods 
#----

fun_polr_cw <- function(instance, ...) {
  fun_cw(instance, FUN = Polr, ...)
}

fun_polr_ipw <- function(instance, ...) {
  fun_ipw(instance, FUN = Polr, ...)
}

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

SL.library <- c("SL.gam", "SL.earth")


fun_tmle_super <- function(instance, ...) {
   xnnames <- names(instance)[grep("x", names(instance))]
   Y <- (0:1)[instance$y]
   X <- as.matrix(instance[, xnnames]) 
   m <- tmle(Y = Y, A = (0:1)[instance$w], W = X, family = "binomial", 
       Q.SL.library=SL.library, g.SL.library=SL.library)
       
   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_cw" = fun_polr_cw,
                "polr_ipw" = fun_polr_ipw, 
                "polr_wx" = fun_polr_wx,
                "mmlt_polr" = fun_mmlt_polr,
                "tmle_super" = fun_tmle_super)

if (TESTONLY) {
   d <- fun(1, 1000, 2, truetau = 0.5, lambda = -1, pisetup = "C", 724948)
   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.