Skip to content

Instantly share code, notes, and snippets.

View nt-williams's full-sized avatar
:octocat:
Procrastinating

Nicholas Williams nt-williams

:octocat:
Procrastinating
View GitHub Profile
@nt-williams
nt-williams / SL.lightgbm.R
Last active August 11, 2022 18:09
lightgbm method for use with SuperLearner
SL.lightgbm <- function(Y, X, newX, family, obsWeights, id, nrounds = 1000, verbose = -1,
learning_rate = 0.1, min_data_in_leaf = 10, max_depth = -1, ...) {
if(!requireNamespace("lightgbm", quietly = FALSE)) {
stop("loading required package (lightgbm) failed", call. = FALSE)
}
if (family$family == "gaussian") {
objective <- "regression"
evalu <- ""
}
@nt-williams
nt-williams / rubins_rules.R
Created May 10, 2023 17:13
Rubins rules for lmtp
lmtp_type <- \(x) structure(x, class = class(x[[1]]))
rubins_rules <- \(x, ...) UseMethod("rubins_rules", lmtp_type(x))
rubins_rules.lmtp <- function(lmtps, label, alpha = 0.05) {
thetas <- purrr::map_dbl(lmtps, "theta")
vw <- mean(purrr::map_dbl(lmtps, \(x) x$standard_error^2))
vb <- var(thetas)
theta <- mean(thetas)
se <- pooled_se(vw, vb, length(thetas))
glmnet3 <- function(X, y, family = c("gaussian", "binomial"), id = NULL) {
if (!is.null(id)) {
folds <- origami::make_folds(
nrow(X), fold_fun = origami::folds_vfold,
cluster_ids = id, V = 10
)
foldid <- vector("numeric", nrow(X))
for (i in 1:nrow(X)) {
for (v in 1:10) {
read_zip_rds <- function(tar) {
files <- unzip(tar, list = TRUE)$Name
p <- progressr::progressor(along = 1:length(files))
purrr::map(files, function(file) {
p()
con <- gzcon(unz(tar, file))
x <- readRDS(con)
close(con)
x
})
@nt-williams
nt-williams / tmle_mlr3.R
Last active July 9, 2024 18:16
tmle_mlr3
tmle_mlr3 <- function(data,
trt,
covar_trt,
covar_outcome,
covar_cens,
outcome,
id = NULL,
g_learners = "glm",
Q_learners = "glm",
c_learners = "glm",