Nothing
# Tests for hyperparameter tuning
.gbsg2 <- function() {
skip_if_not_installed("survival")
skip_if_not_installed("TH.data")
library("survival")
data("GBSG2", package = "TH.data", envir = environment())
GBSG2$surv <- with(GBSG2, Surv(time, cens))
GBSG2
}
test_that("cross validation", {
GBSG2 <- .gbsg2()
set.seed(241068)
folds <- sample(rep(1:2, length.out = nrow(GBSG2)))
## default interface
X <- 1 * matrix(GBSG2$horTh == "yes", ncol = 1)
colnames(X) <- "horThyes"
m <- Coxph(surv ~ 1, data = GBSG2, log_first = TRUE)
mt <- suppressWarnings(tramnet(model = m, x = X, lambda = 0, alpha = 0))
cv <- suppressMessages(suppressWarnings(
cvl_tramnet(mt, lambda = c(0, 1), alpha = c(0, 1), folds = folds)
))
expect_s3_class(cv, "cvl_tramnet")
expect_equal(nrow(cv$logLik_tab), 4L)
## formula interface: scaling within folds
mf <- Coxphnet(surv ~ horTh + age,
data = GBSG2, lambda = 0, alpha = 0,
log_first = TRUE
)
cvf <- suppressMessages(suppressWarnings(
cvl_tramnet(mf,
lambda = c(0, 5), alpha = 1, folds = folds,
fit_opt = TRUE
)
))
expect_true(all(is.finite(cvf$logLik_tab$sum_logLik)))
expect_equal(
colnames(cvf$coefficients[[1]]),
c("lambda", "alpha", "horThyes", "age")
)
expect_s3_class(cvf$full_fit, "tramnet")
## out-of-sample log-likelihood by hand
trn <- GBSG2[folds != 1, ]
tst <- GBSG2[folds == 1, ]
m1 <- Coxphnet(surv ~ horTh + age,
data = trn, lambda = 5, alpha = 1,
log_first = TRUE
)
expect_equal(cvf$logLik_tab$logLik_fold_1[2], c(logLik(m1, newdata = tst)),
tolerance = 1e-6
)
})
test_that("profiling", {
GBSG2 <- .gbsg2()
mf <- Coxphnet(surv ~ horTh + age + tsize,
data = GBSG2, lambda = 1,
alpha = 1, log_first = TRUE
)
pfl <- suppressMessages(prof_lambda(mf, nprof = 3))
expect_s3_class(pfl, "prof_lambda")
expect_equal(dim(pfl$cfx), c(3L, 3L))
pfa <- suppressMessages(prof_alpha(mf, nprof = 3))
expect_s3_class(pfa, "prof_alpha")
pdf(NULL)
on.exit(dev.off())
expect_no_error(plot_path(pfl, plot_logLik = TRUE))
expect_no_error(plot_path(pfa))
## default interface
X <- scale(model.matrix(~ 0 + age + tsize, data = GBSG2))
m0 <- Coxph(surv ~ 1, data = GBSG2, log_first = TRUE)
mt <- tramnet(m0, x = X, lambda = 1, alpha = 1)
pfd <- suppressMessages(prof_lambda(mt, nprof = 2))
expect_equal(colnames(pfd$cfx), c("age", "tsize"))
## Lm
ml <- Lmnet(dist ~ speed, data = cars, lambda = 0, alpha = 0)
pfl <- suppressMessages(prof_lambda(ml, nprof = 3, as.lm = TRUE))
expect_equal(colnames(pfl$cfx), "speed")
})
test_that("model based optimization", {
skip_on_cran()
skip_if_not_installed("DiceKriging")
GBSG2 <- .gbsg2()
set.seed(241068)
mf <- Coxphnet(surv ~ horTh + age + tsize,
data = GBSG2, lambda = 0,
alpha = 0, log_first = TRUE
)
for (type in c("lasso", "ridge", "elnet")) {
tmbo <- suppressWarnings(mbo_tramnet(mf,
obj_type = type, fold = 2,
n_design = 4, n_iter = 2,
verbose = FALSE
))
expect_equal(nrow(tmbo$instance$archive$data), 6L)
expect_true(tmbo$x$lmb >= 0 & tmbo$x$lmb <= 16)
rec <- mbo_recommended(tmbo, mf)
expect_s3_class(rec, "tramnet")
expect_equal(rec$tuning_parm[["lambda"]], tmbo$x$lmb)
alp <- switch(type,
"lasso" = 1,
"ridge" = 0,
tmbo$x$alp
)
expect_equal(rec$tuning_parm[["alpha"]], alp)
}
## default interface and objective functions
X <- scale(model.matrix(~ 0 + age + tsize, data = GBSG2))
m0 <- Coxph(surv ~ 1, data = GBSG2, log_first = TRUE)
mt <- tramnet(m0, x = X, lambda = 0, alpha = 0)
tmbo <- suppressWarnings(mbo_tramnet(mt,
obj_type = "lasso", n_design = 3,
n_iter = 1, verbose = FALSE,
opt_crit = "cb"
))
expect_s3_class(mbo_recommended(tmbo, m0, X), "tramnet")
## user-specified folds, noisy objective and learner object
lrn_km <- mlr3learners::LearnerRegrKM$new()
lrn_km$predict_type <- "se"
lrn_km$param_set$set_values(
nugget.estim = TRUE,
control = list(trace = FALSE)
)
tmbo <- suppressWarnings(mbo_tramnet(mt,
obj_type = "ridge", n_design = 3,
n_iter = 1, verbose = FALSE,
noisy = TRUE, learner = lrn_km
))
expect_equal(nrow(tmbo$instance$archive$data), 4L)
tmbo <- suppressWarnings(mbo_tramnet(
mt,
obj_type = "lasso", n_design = 3, n_iter = 1, verbose = FALSE,
folds = rep(1:3, length.out = nrow(GBSG2)), noisy = FALSE
))
expect_equal(nrow(tmbo$instance$archive$data), 4L)
expect_warning(
suppressMessages(
mbo_tramnet(mf, n_design = 3, n_iter = 1, verbose = FALSE, foo = 1)
),
"ignored"
)
obj <- elnet_obj(mt, folds = rep(1:2, length.out = nrow(GBSG2)), fold = 2)
expect_s3_class(obj, "ObjectiveRFun")
expect_true(is.finite(suppressWarnings(
suppressMessages(obj$eval(list(lmb = 1, alp = 0.5)))$y
)))
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.