test_that("slim handles vector y with missing values", { set.seed(1) x <- matrix(rnorm(80), nrow = 20, ncol = 4) y <- rnorm(20) y[c(3, 7)] <- NA_real_ fit <- suppressWarnings( slim(x, y, nlambda = 2, method = "lasso", verbose = FALSE, max.ite = 2000) ) expect_s3_class(fit, "slim") expect_equal(nrow(fit$X), 18L) expect_equal(nrow(fit$Y), 18L) expect_equal(ncol(fit$beta), fit$nlambda) expect_false(anyNA(fit$X)) expect_false(anyNA(fit$Y)) # Verify warning is emitted expect_warning( slim(x, y, nlambda = 2, method = "lasso", verbose = FALSE, max.ite = 2000), "Removed 2 rows" ) }) test_that("predict.slim validates input shape and returns prediction matrix", { set.seed(2) x <- matrix(rnorm(120), nrow = 30, ncol = 4) y <- rnorm(30) fit <- slim(x, y, nlambda = 3, method = "lasso", verbose = FALSE, max.ite = 2000) bad_newdata <- matrix(rnorm(30), nrow = 10, ncol = 3) expect_error(predict.slim(fit, bad_newdata, lambda.idx = 1, Y.pred.idx = 1), "incompatible") good_pred <- predict.slim(fit, x, lambda.idx = 1:2, Y.pred.idx = 1:2) expect_type(good_pred, "list") expect_equal(dim(good_pred$Y.pred), c(nrow(x), 2L)) }) test_that("slim rejects non-positive or non-finite lambda values", { set.seed(3) x <- matrix(rnorm(60), nrow = 15, ncol = 4) y <- rnorm(15) expect_error( slim(x, y, lambda = c(0.2, 0, 0.1), method = "lasso", verbose = FALSE), "positive finite" ) }) test_that("coef.slim returns a matrix", { set.seed(4) x <- matrix(rnorm(120), nrow = 30, ncol = 4) y <- rnorm(30) fit <- slim(x, y, nlambda = 3, method = "lasso", verbose = FALSE, max.ite = 2000) cm <- coef.slim(fit, lambda.idx = 1:3, beta.idx = 1:4) expect_true(is.matrix(cm)) expect_equal(nrow(cm), 5L) # 1 intercept + 4 betas expect_equal(ncol(cm), 3L) # 3 lambdas }) test_that("slim.cv selects a lambda and returns expected structure", { set.seed(5) x <- matrix(rnorm(200), nrow = 50, ncol = 4) y <- x[, 1] * 2 + rnorm(50) * 0.5 cv <- slim.cv(x, y, nlambda = 5, method = "lasso", fold = 3, verbose = FALSE) expect_s3_class(cv, "slim.cv") expect_length(cv$loss.mean, 5L) expect_length(cv$loss.sd, 5L) expect_true(cv$opt.idx >= 1 && cv$opt.idx <= 5) expect_equal(cv$lambda.opt, cv$lambda[cv$opt.idx]) expect_s3_class(cv$fit, "slim") })