Skip to content

Commit

Permalink
skip if suggested pkgs are not available
Browse files Browse the repository at this point in the history
  • Loading branch information
hfrick committed Jan 17, 2024
1 parent 9016378 commit c39ddb0
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/testthat/test-aaa_survival_prob.R
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ test_that("combine_list_of_survfit_summary() works for survbagg", {

test_that("combine_list_of_survfit_summary() works for ctree", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

lung_pred <- tidyr::drop_na(lung)

Expand Down Expand Up @@ -338,6 +339,7 @@ test_that("combine_list_of_survfit_summary() works for ctree", {

test_that("combine_list_of_survfit_summary() works for cforest", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

lung_pred <- tidyr::drop_na(lung)

Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-bag_tree-rpart.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
library(testthat)

test_that("model object", {
skip_if_not_installed("ipred")

set.seed(1234)
exp_f_fit <- ipred::bagging(Surv(time, status) ~ age + ph.ecog, data = lung)

Expand All @@ -17,6 +19,8 @@ test_that("model object", {
})

test_that("main args work without set_model_arg()", {
skip_if_not_installed("ipred")

set.seed(1234)
exp_f_fit <- ipred::bagging(
Surv(time, status) ~ age + ph.ecog,
Expand All @@ -41,6 +45,8 @@ test_that("main args work without set_model_arg()", {
# prediction: time --------------------------------------------------------

test_that("time predictions", {
skip_if_not_installed("ipred")

set.seed(1234)
exp_f_fit <- ipred::bagging(Surv(time, status) ~ age + ph.ecog, data = lung)
exp_f_pred <- predict(exp_f_fit, lung)
Expand All @@ -64,6 +70,8 @@ test_that("time predictions", {
})

test_that("time predictions without surrogate splits for NA", {
skip_if_not_installed("ipred")

mod_spec <- bag_tree(engine = "rpart") %>% set_mode("censored regression")
f_fit <- fit(mod_spec, Surv(time, status) ~ ph.ecog, data = lung)

Expand All @@ -81,6 +89,8 @@ test_that("time predictions without surrogate splits for NA", {
# prediction: survival ----------------------------------------------------

test_that("survival predictions", {
skip_if_not_installed("ipred")

set.seed(1234)
exp_f_fit <- ipred::bagging(Surv(time, status) ~ age + ph.ecog, data = lung)

Expand Down Expand Up @@ -131,6 +141,8 @@ test_that("survival predictions", {
})

test_that("survival_prob_survbagg() works", {
skip_if_not_installed("ipred")

set.seed(1234)
# use only ph.ecog to force missings by avoiding surrogate splits
mod <- ipred::bagging(Surv(time, status) ~ ph.ecog, data = lung)
Expand Down Expand Up @@ -205,6 +217,8 @@ test_that("survival_prob_survbagg() works", {
})

test_that("survival predictions without surrogate splits for NA", {
skip_if_not_installed("ipred")

mod_spec <- bag_tree(engine = "rpart") %>% set_mode("censored regression")
f_fit <- fit(mod_spec, Surv(time, status) ~ ph.ecog, data = lung)

Expand All @@ -227,6 +241,8 @@ test_that("survival predictions without surrogate splits for NA", {
})

test_that("can predict for out-of-domain timepoints", {
skip_if_not_installed("ipred")

eval_time_obs_max_and_ood <- c(1022, 2000)
obs_without_NA <- lung[2,]

Expand All @@ -243,6 +259,8 @@ test_that("can predict for out-of-domain timepoints", {
# fit via matrix interface ------------------------------------------------

test_that("`fix_xy()` works", {
skip_if_not_installed("ipred")

lung_x <- as.matrix(lung[, c("age", "ph.ecog")])
lung_y <- Surv(lung$time, lung$status)
lung_pred <- lung[1:5, ]
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-decision_tree-partykit.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ library(testthat)

test_that("model object", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

set.seed(1234)
exp_f_fit <- partykit::ctree(Surv(time, status) ~ age + ph.ecog, data = lung)

Expand Down Expand Up @@ -31,6 +33,8 @@ test_that("model object", {

test_that("time predictions", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

set.seed(1234)
exp_f_fit <- partykit::ctree(Surv(time, status) ~ age + ph.ecog, data = lung)

Expand Down Expand Up @@ -58,6 +62,8 @@ test_that("time predictions", {

test_that("survival predictions", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

set.seed(1234)
exp_f_fit <- partykit::ctree(Surv(time, status) ~ age + ph.ecog, data = lung)

Expand Down Expand Up @@ -106,6 +112,9 @@ test_that("survival predictions", {
})

test_that("can predict for out-of-domain timepoints", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

eval_time_obs_max_and_ood <- c(1022, 2000)
obs_without_NA <- lung[2,]

Expand All @@ -122,6 +131,9 @@ test_that("can predict for out-of-domain timepoints", {
# fit via matrix interface ------------------------------------------------

test_that("`fix_xy()` works", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

lung_x <- as.matrix(lung[, c("age", "ph.ecog")])
lung_y <- Surv(lung$time, lung$status)
lung_pred <- lung[1:5, ]
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-decision_tree-rpart.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
library(testthat)

test_that("model object", {
skip_if_not_installed("pec")

set.seed(1234)
exp_f_fit <- pec::pecRpart(Surv(time, status) ~ age + ph.ecog, data = lung)

Expand All @@ -21,6 +23,8 @@ test_that("model object", {
# prediction: time --------------------------------------------------------

test_that("time predictions", {
skip_if_not_installed("pec")

set.seed(1234)
exp_f_fit <- pec::pecRpart(Surv(time, status) ~ age + ph.ecog, data = lung)

Expand All @@ -47,6 +51,8 @@ test_that("time predictions", {
# prediction: survival ----------------------------------------------------

test_that("survival predictions", {
skip_if_not_installed("pec")

set.seed(1234)
exp_f_fit <- pec::pecRpart(Surv(time, status) ~ age + ph.ecog, data = lung)

Expand Down Expand Up @@ -96,6 +102,8 @@ test_that("survival predictions", {
})

test_that("can predict for out-of-domain timepoints", {
skip_if_not_installed("pec")

eval_time_obs_max_and_ood <- c(1022, 2000)
obs_without_NA <- lung[2,]

Expand All @@ -112,6 +120,7 @@ test_that("can predict for out-of-domain timepoints", {
# fit via matrix interface ------------------------------------------------

test_that("`fix_xy()` works", {
skip_if_not_installed("pec")
skip_if_not_installed("prodlim", minimum_version = "2023.3.31")

lung_x <- as.matrix(lung[, c("age", "ph.ecog")])
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-partykit.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
test_that("survival_prob_partykit() works for ctree", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

set.seed(1234)
# use only ph.ecog to rule out surrogate splits
mod <- partykit::ctree(Surv(time, status) ~ ph.ecog, data = lung)
Expand Down Expand Up @@ -69,6 +72,9 @@ test_that("survival_prob_partykit() works for ctree", {
})

test_that("survival_prob_partykit() works for cforest", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

# partykit::cforest takes care of missing values via some form of randomness
# hence set the seed before predicting on data with missings

Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-proportional_hazards-glmnet.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
library(testthat)
skip_if_not_installed("glmnet")
suppressPackageStartupMessages(library(glmnet))

test_that("model object", {
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-rand_forest-aorsf.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ test_that("survival predictions", {
})

test_that("can predict for out-of-domain timepoints", {
skip_if_not_installed("aorsf")

eval_time_obs_max_and_ood <- c(1022, 2000)
obs_without_NA <- lung[2,]
lung_orsf <- na.omit(lung)
Expand All @@ -177,6 +179,8 @@ test_that("can predict for out-of-domain timepoints", {
# fit via matrix interface ------------------------------------------------

test_that("`fix_xy()` works", {
skip_if_not_installed("aorsf")

lung_orsf <- na.omit(lung)

lung_x <- as.matrix(lung_orsf[, c("age", "ph.ecog")])
Expand Down Expand Up @@ -228,6 +232,8 @@ test_that("`fix_xy()` works", {
# case weights ------------------------------------------------------------

test_that("can handle case weights", {
skip_if_not_installed("aorsf")

dat <- make_cens_wts()

expect_error(
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-rand_forest-partykit.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library(testthat)

test_that("model object", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

set.seed(1234)
exp_f_fit <- partykit::cforest(
Expand Down Expand Up @@ -36,6 +37,7 @@ test_that("model object", {

test_that("time predictions", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

set.seed(1234)
exp_f_fit <- partykit::cforest(
Expand Down Expand Up @@ -69,6 +71,7 @@ test_that("time predictions", {

test_that("survival predictions", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

set.seed(1234)
exp_f_fit <- partykit::cforest(
Expand Down Expand Up @@ -132,6 +135,9 @@ test_that("survival predictions", {
})

test_that("can predict for out-of-domain timepoints", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

eval_time_obs_max_and_ood <- c(1022, 2000)
obs_without_NA <- lung[2,]

Expand All @@ -148,6 +154,9 @@ test_that("can predict for out-of-domain timepoints", {
# fit via matrix interface ------------------------------------------------

test_that("`fix_xy()` works", {
skip_if_not_installed("partykit")
skip_if_not_installed("coin")

lung_x <- as.matrix(lung[, c("age", "ph.ecog")])
lung_y <- Surv(lung$time, lung$status)
lung_pred <- lung[1:5, ]
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-survival_reg-flexsurv.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
library(testthat)

test_that("model object", {
skip_if_not_installed("flexsurv")

set.seed(1234)
exp_f_fit <- flexsurv::flexsurvreg(
Surv(time, status) ~ age + ph.ecog,
Expand Down Expand Up @@ -29,6 +31,8 @@ test_that("model object", {
# prediction: time --------------------------------------------------------

test_that("flexsurv time prediction", {
skip_if_not_installed("flexsurv")

exp_fit <- flexsurv::flexsurvreg(
Surv(time, status) ~ age,
data = lung,
Expand All @@ -52,6 +56,8 @@ test_that("flexsurv time prediction", {
# prediction: survival ----------------------------------------------------

test_that("survival probability prediction", {
skip_if_not_installed("flexsurv")

rms_surv <- readRDS(test_path("data", "rms_surv.rds"))
f_fit <- survival_reg(dist = "weibull") %>%
set_engine("flexsurv") %>%
Expand Down Expand Up @@ -121,6 +127,8 @@ test_that("survival probability prediction", {
})

test_that("survival probabilities for single eval time point", {
skip_if_not_installed("flexsurv")

f_fit <- survival_reg(engine = "flexsurv") %>%
fit(Surv(time, status) ~ age + sex, data = lung)

Expand All @@ -140,6 +148,8 @@ test_that("survival probabilities for single eval time point", {
})

test_that("can predict for out-of-domain timepoints", {
skip_if_not_installed("flexsurv")

eval_time_obs_max_and_ood <- c(1022, 2000)
obs_without_NA <- lung[2,]

Expand All @@ -160,6 +170,8 @@ test_that("can predict for out-of-domain timepoints", {
# prediction: linear_pred -------------------------------------------------

test_that("linear predictor", {
skip_if_not_installed("flexsurv")

f_fit <- survival_reg() %>%
set_engine("flexsurv") %>%
fit(Surv(time, status) ~ age + sex, data = lung)
Expand Down Expand Up @@ -201,6 +213,8 @@ test_that("linear predictor", {
# prediction: quantile ----------------------------------------------------

test_that("quantile predictions", {
skip_if_not_installed("flexsurv")

set.seed(1)
fit_s <- survival_reg() %>%
set_engine("flexsurv") %>%
Expand Down Expand Up @@ -266,6 +280,8 @@ test_that("quantile predictions", {
# prediction: hazard ------------------------------------------------------

test_that("hazard prediction", {
skip_if_not_installed("flexsurv")

rms_haz <- readRDS(test_path("data", "rms_haz.rds"))
f_fit <- survival_reg(dist = "weibull") %>%
set_engine("flexsurv") %>%
Expand Down Expand Up @@ -314,6 +330,8 @@ test_that("hazard prediction", {
})

test_that("hazard for single eval time point", {
skip_if_not_installed("flexsurv")

f_fit <- survival_reg(engine = "flexsurv") %>%
fit(Surv(time, status) ~ age + sex, data = lung)

Expand All @@ -335,6 +353,8 @@ test_that("hazard for single eval time point", {
# fit via matrix interface ------------------------------------------------

test_that("`fix_xy()` works", {
skip_if_not_installed("flexsurv")

lung_x <- as.matrix(lung[, c("age", "ph.ecog")])
lung_y <- Surv(lung$time, lung$status)
lung_pred <- lung[1:5, ]
Expand Down
Loading

0 comments on commit c39ddb0

Please sign in to comment.