From 77401b12ec2e441584c7eba0543e23dccacede2c Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Tue, 20 Aug 2024 16:47:00 +0200 Subject: [PATCH 1/2] Added validation for `nlambda <= maxgrid` (closes #54) --- R/MADMMplasso.R | 3 ++ tests/testthat/test-validation.R | 80 ++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tests/testthat/test-validation.R diff --git a/R/MADMMplasso.R b/R/MADMMplasso.R index 970ab59..05fe31a 100644 --- a/R/MADMMplasso.R +++ b/R/MADMMplasso.R @@ -49,6 +49,9 @@ #' @example inst/examples/MADMMplasso_example.R #' @export MADMMplasso <- function(X, Z, y, alpha, my_lambda = NULL, lambda_min = 0.001, max_it = 50000, e.abs = 1E-3, e.rel = 1E-3, maxgrid, nlambda, rho = 5, my_print = FALSE, alph = 1.8, tree, pal = cl == 1L, gg = NULL, tol = 1E-4, cl = 1L, legacy = FALSE) { + # Validation + stopifnot(nlambda <= maxgrid) + # Recalculating the number of CPUs if (pal && cl > 1L) { cl <- 1L diff --git a/tests/testthat/test-validation.R b/tests/testthat/test-validation.R new file mode 100644 index 0000000..6ff5962 --- /dev/null +++ b/tests/testthat/test-validation.R @@ -0,0 +1,80 @@ + +# Setting up objects ========================================================= +N <- 100 +p <- 50 +nz <- 4 +K <- nz +X <- matrix(rnorm(n = N * p), nrow = N, ncol = p) +mx <- colMeans(X) +sx <- sqrt(apply(X, 2, var)) +X <- scale(X, mx, sx) +X <- matrix(as.numeric(X), N, p) +Z <- matrix(rnorm(N * nz), N, nz) +mz <- colMeans(Z) +sz <- sqrt(apply(Z, 2, var)) +Z <- scale(Z, mz, sz) +beta_1 <- rep(x = 0, times = p) +beta_2 <- rep(x = 0, times = p) +beta_3 <- rep(x = 0, times = p) +beta_4 <- rep(x = 0, times = p) +beta_5 <- rep(x = 0, times = p) +beta_6 <- rep(x = 0, times = p) + +beta_1[1:5] <- c(2, 2, 2, 2, 2) +beta_2[1:5] <- c(2, 2, 2, 2, 2) +beta_3[6:10] <- c(2, 2, 2, -2, -2) +beta_4[6:10] <- c(2, 2, 2, -2, -2) +beta_5[11:15] <- c(-2, -2, -2, -2, -2) +beta_6[11:15] <- c(-2, -2, -2, -2, -2) + +Beta <- cbind(beta_1, beta_2, beta_3, beta_4, beta_5, beta_6) +colnames(Beta) <- 1:6 + +theta <- array(0, c(p, K, 6)) +theta[1, 1, 1] <- 2 +theta[3, 2, 1] <- 2 +theta[4, 3, 1] <- -2 +theta[5, 4, 1] <- -2 +theta[1, 1, 2] <- 2 +theta[3, 2, 2] <- 2 +theta[4, 3, 2] <- -2 +theta[5, 4, 2] <- -2 +theta[6, 1, 3] <- 2 +theta[8, 2, 3] <- 2 +theta[9, 3, 3] <- -2 +theta[10, 4, 3] <- -2 +theta[6, 1, 4] <- 2 +theta[8, 2, 4] <- 2 +theta[9, 3, 4] <- -2 +theta[10, 4, 4] <- -2 +theta[11, 1, 5] <- 2 +theta[13, 2, 5] <- 2 +theta[14, 3, 5] <- -2 +theta[15, 4, 5] <- -2 +theta[11, 1, 6] <- 2 +theta[13, 2, 6] <- 2 +theta[14, 3, 6] <- -2 +theta[15, 4, 6] <- -2 + +pliable <- matrix(0, N, 6) +for (e in 1:6) { + pliable[, e] <- compute_pliable(X, Z, theta[, , e]) +} + +esd <- diag(6) +e <- MASS::mvrnorm(N, mu = rep(0, 6), Sigma = esd) +y_train <- X %*% Beta + pliable + e +y <- y_train + +colnames(y) <- c(paste0("y", seq_len(ncol(y)))) +TT <- tree_parms(y) +gg1 <- matrix(0, 2, 2) +gg1[1, ] <- c(0.02, 0.02) +gg1[2, ] <- c(0.02, 0.02) + +# Running MADMMplasso ======================================================== +test_that("MADMMplasso() prevents silly arguments", { + expect_error( + MADMMplasso(X, Z, y, 0.2, maxgrid = 1, nlambda = 2, tree = TT, gg = gg1) + ) +}) From 195122cf5de2bdb7211f887468a6ded4645e1c3a Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Tue, 20 Aug 2024 16:47:05 +0200 Subject: [PATCH 2/2] Increment version number to 0.0.0.9023 --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 81c007a..c0fe5fd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: MADMMplasso Title: Multi Variate Multi Response 'ADMM' with Interaction Effects -Version: 0.0.0.9022 +Version: 0.0.0.9023 Authors@R: c( person(