test_that("coef path returns every stored beta solution", { beta_matrix <- matrix( c(0, 0, 1, 0, 2, 3), nrow = 3, byrow = TRUE, dimnames = list(NULL, c("x1", "x2")) ) path_details <- structure( list( beta_matrix = beta_matrix, # Ensure the implementation does not partially match beta_mat. beta_mat = matrix(99, nrow = 1, ncol = 2) ), class = "autotune_lasso_path" ) fit <- structure( list( beta = beta_matrix[3, ], a0 = 4, nvars = 2, CD.path.details = path_details ), class = "autotune_lasso" ) result <- coef(fit, path = TRUE, intercept = FALSE) expect_s4_class(result, "dgCMatrix") expect_equal(dim(result), c(2L, 3L)) expect_equal(unname(as.matrix(result)), unname(t(beta_matrix))) expect_identical(colnames(result), c("lambda_1", "lambda_2", "final_lambda")) expect_identical(rownames(result), c("x1", "x2")) }) test_that("path coefficients include an intercept for every solution", { beta_matrix <- matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE) path_details <- structure( list(beta_matrix = beta_matrix), class = "autotune_lasso_path" ) fit <- structure( list( beta = beta_matrix[2, ], a0 = 2, nvars = 2, CD.path.details = path_details ), class = "autotune_lasso" ) result <- coef(fit, path = TRUE) expect_equal(dim(result), c(3L, 2L)) expect_equal(as.numeric(result[1, ]), c(2, 2)) expect_equal(unname(as.matrix(result[-1, ])), unname(t(beta_matrix))) }) test_that("the registered path method dispatches through coef generic", { beta_matrix <- matrix(seq_len(6), nrow = 3, byrow = TRUE) path_details <- structure( list(beta_matrix = beta_matrix), class = "autotune_lasso_path" ) result <- coef(path_details, intercept = FALSE) expect_equal(unname(as.matrix(result)), unname(t(beta_matrix))) }) test_that("path FALSE continues to return only the final solution", { beta_matrix <- matrix(c(1, 0, 2, 3), nrow = 2, byrow = TRUE) fit <- structure( list( beta = beta_matrix[2, ], a0 = 5, nvars = 2, CD.path.details = structure( list(beta_matrix = beta_matrix), class = "autotune_lasso_path" ) ), class = "autotune_lasso" ) result <- coef(fit, path = FALSE, intercept = FALSE, sparse = FALSE) expect_equal(unname(result), beta_matrix[2, ]) })