Skip to content

Commit

Permalink
even more tests with glmnet
Browse files Browse the repository at this point in the history
  • Loading branch information
lgessl committed Jan 9, 2024
1 parent 62dfd77 commit 50f4e35
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Suggests:
testthat (>= 3.0.0),
withr (>= 2.5.2),
tibble (>= 3.0.0),
zeroSum (>= 2.0.6)
zeroSum (>= 2.0.6),
glmnet(>= 4.1.8)
Imports:
dplyr (>= 1.1.3),
readr (>= 2.1.4),
Expand Down
7 changes: 4 additions & 3 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ generate_mock_data <- function(
){
# expression matrix
expr_mat <- matrix(
sample(1:100, n_samples*n_genes, replace = TRUE),
sample(1:10, n_samples*n_genes, replace = TRUE),
nrow = n_samples
) |> log()
rownames(expr_mat) <- stringr::str_c("sample_", 1:n_samples)
Expand All @@ -60,9 +60,10 @@ generate_mock_data <- function(
pheno_tbl <- tibble::tibble(.rows = n_samples)
pheno_tbl[["patient_id"]] <- rownames(expr_mat)
pheno_tbl[["progression"]] <- sample(
c(0, 1),
0:1,
size = n_samples,
replace = TRUE
replace = TRUE,
prob = c(.3, .7)
)
pheno_tbl[["pfs_years"]] <- runif(n_samples, 0, 4)
pheno_tbl[["discrete_var"]] <- sample(1:3, size = n_samples, replace = TRUE)
Expand Down
18 changes: 10 additions & 8 deletions tests/testthat/test-assess_model.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
test_that("assess_model() works", {

set.seed(134)
n <- 10
set.seed(132)
n <- 50

dir <- withr::local_tempdir()
n_fold <- 1
n_fold <- 3
lambda <- 1
data <- generate_mock_data(
n_samples = n,
n_genes = 5,
n_na_in_pheno = 3
n_na_in_pheno = 1
)
expr_mat <- data[["expr_mat"]]
pheno_tbl <- data[["pheno_tbl"]]
Expand All @@ -20,14 +20,16 @@ test_that("assess_model() works", {
model_spec_1 <- ModelSpec(
name = "cox-zerosum",
fitter = zeroSum::zeroSum,
optional_fitter_args = list(family = "cox", alpha = 1, nFold = n_fold, lambda = lambda),
optional_fitter_args = list(family = "cox", alpha = 1, nFold = n_fold,
lambda = lambda, zeroSum = FALSE),
response_type = "survival_censored",
base_dir = dir
)
model_spec_2 <- ModelSpec(
name = "cox-lasso",
fitter = zeroSum::zeroSum,
optional_fitter_args = list(family = "binomial", alpha = 1, nFold = n_fold, lambda = lambda),
name = "logistic-lasso",
fitter = glmnet::cv.glmnet,
optional_fitter_args = list(family = "binomial", alpha = 1,
nfolds = n_fold, lambda = c(lambda, 2)),
response_type = "binary",
base_dir = dir
)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-assess_multiple_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test_that("assess_multiple_models() works", {
fname = file.path(res_dir, "perf_plot.pdf"),
x_metric = "rpp",
y_metric = "prec",
show_plots = TRUE
show_plots = FALSE
)

expect_no_error(
Expand Down

0 comments on commit 50f4e35

Please sign in to comment.