Skip to content

Instantly share code, notes, and snippets.

@wleoncio
Created March 20, 2024 10:32
Show Gist options
  • Save wleoncio/03802bd9886cc8da7b78b06b1f5b5145 to your computer and use it in GitHub Desktop.
Save wleoncio/03802bd9886cc8da7b78b06b1f5b5145 to your computer and use it in GitHub Desktop.
Reducing calls to `solve()`
# Loading package ==============================================================
library("MADMMplasso")
message(paste("MADMMplasso version", packageVersion("MADMMplasso")))
# Loading data =================================================================
CRC_data <- readRDS("aux/CRC_data_300.rds") # change this manually, I won't bother with commandArgs()
X23 <- CRC_data$x
# log2 transfermation f or the expression levels
X1 <- (X23)
set.seed(1339)
X <- X1
y <- CRC_data$y
Z <- CRC_data$z
my_data <- cbind(y, Z, X)
split1 <- sample(c(rep(0, 0.7 * nrow(my_data)), rep(1, 0.3 * nrow(my_data))))
train <- my_data[split1 == 0, ]
test <- my_data[split1 == 1, ]
y_train <- as.matrix(train[, 1:ncol(y)], ncol = ncol(y))
Z_train <- as.matrix(train[, (ncol(y) + 1):(ncol(y) + 1)], ncol = 1)
X_train <- train[, -(1:(ncol(y) + 1))]
y <- y_train
X <- X_train
Z <- Z_train
p <- ncol(X)
N <- nrow(X)
K <- ncol(Z)
mx <- colMeans(X)
sx <- sqrt(apply(X, 2, var))
X <- scale(X, mx, sx)
X <- matrix(as.numeric(X), N, p)
colnames(X) <- colnames(X1)
y_test <- as.matrix(test[, 1:ncol(y)], ncol = ncol(y))
z_test <- as.matrix(test[, (ncol(y) + 1):(ncol(y) + 1)], ncol = 1)
x_test <- test[, -(1:(ncol(y) + 1))]
mx <- colMeans(x_test)
sx <- sqrt(apply(x_test, 2, var))
x_test <- scale(x_test, mx, sx)
x_test <- matrix(as.numeric(x_test), nrow(x_test), p)
colnames(x_test) <- colnames(X1)
y <- y[, c(1:10)]
TT <- tree_parms(y, h = 0.6)
gg1 <- matrix(0, 2, 2)
gg1[1, ] <- c(0.0002, 0.0002)
gg1[2, ] <- c(0.005, 0.005)
nlambda <- 50
e.abs <- 1E-4
e.rel <- 1E-2
alpha <- .3
tol <- 1E-3
# Benchmarking =================================================================
library(microbenchmark)
microbenchmark(
"MADMMplasso()" = suppressWarnings(
suppressMessages(
MADMMplasso(
X = X[, c(1:100)], Z, y, alpha = alpha, my_lambda = NULL,
lambda_min = 0.01, max_it = 5000, e.abs = e.abs, e.rel = e.rel,
maxgrid = 50, nlambda = nlambda, rho = 5, tree = TT, my_print = FALSE,
alph = 1, parallel = FALSE, pal = 1, gg = gg1, tol = tol, cl = 6,
legacy = TRUE
)
)
),
times = 10L
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment