tests/testthat/test-formula.R

# Tests for the formula interface

.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("formula interface recovers unpenalized tram models", {
  GBSG2 <- .gbsg2()
  sup <- c(1e-12, max(GBSG2$time))
  fm <- surv ~ horTh + age + tsize
  chk <- function(a, b, tol = 1e-3) {
    expect_s3_class(a, "tramnet")
    expect_equal(coef(a), coef(b), tolerance = tol)
    expect_equal(c(logLik(a)), c(logLik(b)), tolerance = 1e-5)
  }
  chk(
    Coxphnet(fm, data = GBSG2, lambda = 0, alpha = 0, support = sup),
    Coxph(fm, data = GBSG2, support = sup)
  )
  chk(
    Colrnet(fm, data = GBSG2, lambda = 0, alpha = 0, support = sup),
    Colr(fm, data = GBSG2, support = sup)
  )
  chk(
    Survregnet(fm, data = GBSG2, lambda = 0, alpha = 0),
    Survreg(fm, data = GBSG2)
  )
  chk(
    Lmnet(dist ~ speed, data = cars, lambda = 0, alpha = 0),
    Lm(dist ~ speed, data = cars)
  )
  chk(
    BoxCoxnet(dist ~ speed, data = cars, lambda = 0, alpha = 0),
    BoxCox(dist ~ speed, data = cars)
  )
  chk(
    Lehmannnet(dist ~ speed, data = cars, lambda = 0, alpha = 0),
    Lehmann(dist ~ speed, data = cars)
  )
  ## stratified baseline
  chk(
    Coxphnet(surv | horTh ~ age + tsize,
      data = GBSG2, lambda = 0,
      alpha = 0, support = sup
    ),
    Coxph(surv | horTh ~ age + tsize, data = GBSG2, support = sup)
  )
})

test_that("tramnet.formula is the analogue of tram::tram", {
  GBSG2 <- .gbsg2()
  fm <- surv ~ horTh + age + tsize
  m1 <- tramnet(fm,
    data = GBSG2, lambda = 2, alpha = 0.5,
    distribution = "MinExtrVal", transformation = "smooth",
    negative = FALSE
  )
  m2 <- Coxphnet(fm, data = GBSG2, lambda = 2, alpha = 0.5)
  expect_equal(coef(m1), coef(m2), tolerance = 1e-5)
  expect_equal(c(logLik(m1)), c(logLik(m2)), tolerance = 1e-5)
  expect_true(inherits(Lmnet(dist ~ speed,
    data = cars, lambda = 1,
    alpha = 0
  ), "tramnet_Lm"))
})

test_that("variables are found without data argument", {
  y <- cars$dist
  s <- cars$speed
  m1 <- Lmnet(y ~ s, lambda = 1, alpha = 0.5)
  m2 <- Lmnet(dist ~ speed, data = cars, lambda = 1, alpha = 0.5)
  expect_equal(unname(coef(m1)), unname(coef(m2)))
})

test_that("formula and default interface agree", {
  GBSG2 <- .gbsg2()
  X <- model.matrix(~ horTh + age + tsize, data = GBSG2)[, -1]
  sds <- apply(X, 2, sd)
  m0 <- Coxph(surv ~ 1, data = GBSG2, log_first = TRUE)
  ## centered and scaled covariates in the default interface
  md <- suppressWarnings(tramnet(m0, x = scale(X), lambda = 3, alpha = 0.5))
  mf <- Coxphnet(surv ~ horTh + age + tsize,
    data = GBSG2, lambda = 3,
    alpha = 0.5, log_first = TRUE
  )
  expect_equal(coef(mf, scaled = TRUE, tol = 0), coef(md, tol = 0),
    tolerance = 1e-3
  )
  expect_equal(coef(mf, tol = 0), coef(md, tol = 0) / sds, tolerance = 1e-3)
  expect_equal(c(logLik(mf)), c(logLik(md)), tolerance = 1e-5)
  ## scaling is stored and the design matrix is not centered
  expect_equal(mf$scaling, sds)
  expect_equal(unname(model.matrix(mf)), unname(sweep(X, 2, sds, "/")))
})

test_that("newdata is scaled with the training data", {
  GBSG2 <- .gbsg2()
  trn <- GBSG2[1:400, ]
  tst <- GBSG2[401:686, ]
  m <- Colrnet(surv ~ horTh + age + tsize + tgrade,
    data = trn, lambda = 1,
    alpha = 0.5
  )
  Xtst <- model.matrix(~ horTh + age + tsize + tgrade, data = tst)[, -1]
  expect_equal(
    unname(model.matrix(m, newdata = tst)),
    unname(sweep(Xtst, 2, m$scaling, "/"))
  )
  ## predictions on newdata coincide with predictions on the training data
  p1 <- predict(m, newdata = trn[1:5, ], type = "distribution", q = 1000)
  p2 <- predict(m, type = "distribution", q = 1000)[, 1:5, drop = FALSE]
  expect_equal(c(p1), c(p2))
  ## newdata without response
  p3 <- predict(m,
    newdata = trn[1:5, c("horTh", "age", "tsize", "tgrade")],
    type = "distribution", q = 1000
  )
  expect_equal(c(p1), c(p3))
  ## log-likelihood on newdata
  ll1 <- logLik(m, newdata = trn)
  ## penalized on the training data, unpenalized for newdata
  expect_equal(c(ll1) - c(tramnet:::.elnet(m)), c(logLik(m)), tolerance = 1e-6)
  expect_true(is.finite(logLik(m, newdata = tst)))
  ## factor levels missing in newdata
  expect_equal(
    nrow(model.matrix(m, newdata = tst[tst$tgrade == "II", ])),
    sum(tst$tgrade == "II")
  )
})

test_that("weights, offsets, subset and na.action", {
  GBSG2 <- .gbsg2()
  set.seed(29)
  sup <- c(1e-12, max(GBSG2$time))
  w <- runif(nrow(GBSG2))
  off <- rnorm(nrow(GBSG2), sd = 0.2)
  mf <- Colrnet(surv ~ horTh + age,
    data = GBSG2, weights = w, offset = off,
    lambda = 0, alpha = 0, support = sup
  )
  mt <- Colr(surv ~ horTh + age,
    data = GBSG2, weights = w, offset = off,
    support = sup
  )
  expect_equal(coef(mf), coef(mt), tolerance = 1e-3)
  expect_equal(c(logLik(mf)), c(logLik(mt)), tolerance = 1e-5)
  ## weights in the default interface
  m0 <- Colr(surv ~ 1, data = GBSG2, weights = w, support = sup)
  X <- model.matrix(~horTh, data = GBSG2)[, -1, drop = FALSE]
  md <- tramnet(m0, X, lambda = 0, alpha = 0)
  expect_equal(unname(coef(md)),
    unname(coef(Colr(surv ~ horTh,
      data = GBSG2, weights = w,
      support = sup
    ))),
    tolerance = 1e-3
  )
  ## subset
  ms <- Coxphnet(surv ~ horTh + age,
    data = GBSG2, subset = age > 50,
    lambda = 1, alpha = 1
  )
  expect_equal(nrow(model.matrix(ms)), sum(GBSG2$age > 50))
  ## na.action
  GBSG2$age[1:3] <- NA
  mn <- Coxphnet(surv ~ horTh + age, data = GBSG2, lambda = 1, alpha = 1)
  expect_equal(nrow(model.matrix(mn)), nrow(GBSG2) - 3)
  expect_equal(NROW(predict(mn,
    newdata = GBSG2[4:8, ], type = "trafo",
    q = 1000
  )[1, ]), 5L)
})

test_that("constraints refer to the original scale", {
  GBSG2 <- .gbsg2()
  m <- Coxphnet(surv ~ horTh + age,
    data = GBSG2, lambda = 0, alpha = 0,
    constraints = list(matrix(c(0, 1), nrow = 1), 0.01)
  )
  expect_gte(coef(m, tol = 0)[["age"]], 0.01 - 1e-6)
})

test_that("informative errors", {
  GBSG2 <- .gbsg2()
  expect_error(
    Coxphnet(surv ~ 1, data = GBSG2, lambda = 1, alpha = 1),
    "at least one covariate"
  )
  expect_error(Coxphnet(surv ~ age | horTh,
    data = GBSG2, lambda = 1,
    alpha = 1
  ), "Scale terms")
  expect_error(
    Coxphnet(surv ~ age, data = GBSG2, lambda = -1, alpha = 1),
    "lambda >= 0"
  )
  expect_error(
    Coxphnet(~age, data = GBSG2, lambda = 1, alpha = 1),
    "response"
  )
  m0 <- Coxph(surv ~ 1, data = GBSG2)
  m <- tramnet(m0,
    x = scale(model.matrix(~ 0 + age, data = GBSG2)),
    lambda = 1, alpha = 1
  )
  expect_error(model.matrix(m, newdata = GBSG2), "formula interface")
})

test_that("backward compatibility of the default interface", {
  GBSG2 <- .gbsg2()
  m0 <- Coxph(surv ~ 1, data = GBSG2, log_first = TRUE)
  X <- scale(model.matrix(~ 0 + age + tsize, data = GBSG2))
  m1 <- tramnet(model = m0, x = X, lambda = 1, alpha = 1)
  m2 <- tramnet(m0, X, 1, 1)
  expect_equal(coef(m1), coef(m2))
  expect_equal(coef(m1, scaled = TRUE), coef(m1))
  expect_null(m1$scaling)
  expect_warning(tramnet(m0,
    x = model.matrix(~ 0 + age, data = GBSG2),
    lambda = 1, alpha = 1
  ), "Unscaled")
  expect_error(tramnet(lm(dist ~ speed, data = cars),
    x = X, lambda = 1,
    alpha = 1
  ))
  expect_identical(m1$call[[1L]], as.name("tramnet"))
})

test_that("newdata with weights, non-syntactic terms and local functions", {
  GBSG2 <- .gbsg2()
  ## weights do not end up in newdata
  w <- rep(1:2, 25)
  l <- Lmnet(dist ~ speed, data = cars, weights = w, lambda = 0, alpha = 0)
  expect_equal(c(logLik(l, newdata = cars)),
    c(logLik(Lm(dist ~ speed, data = cars, weights = w),
      newdata = cars
    )),
    tolerance = 1e-5
  )
  expect_true(all(is.finite(suppressMessages(cvl_tramnet(
    l,
    folds = rep(1:2, 25), lambda = 1, alpha = 1
  ))$logLik_tab$sum_logLik)))
  ## interactions and transformations of covariates
  GBSG2$ltsize <- log(GBSG2$tsize)
  m <- Coxphnet(surv ~ horTh * age + log(tsize),
    data = GBSG2, lambda = 0,
    alpha = 0, log_first = TRUE
  )
  mc <- Coxph(surv ~ horTh * age + ltsize, data = GBSG2, log_first = TRUE)
  expect_equal(
    names(coef(m)),
    c("horThyes", "age", "log(tsize)", "horThyes:age")
  )
  expect_equal(
    c(predict(m,
      newdata = GBSG2[1:3, ], type = "distribution",
      q = 1000
    )),
    c(predict(mc,
      newdata = GBSG2[1:3, ], type = "distribution",
      q = 1000
    )),
    tolerance = 1e-3
  )
  expect_equal(NROW(simulate(m, newdata = GBSG2[1:3, ], seed = 1)), 3L)
  ## non-syntactic column names in the default interface
  X <- scale(model.matrix(~ 0 + log(age) + horTh:age, data = GBSG2))
  m0 <- Coxph(surv ~ 1, data = GBSG2, log_first = TRUE)
  md <- tramnet(m0, X, lambda = 0, alpha = 0)
  nd <- cbind(GBSG2[1:3, "surv", drop = FALSE], X[1:3, ])
  expect_equal(unname(dim(predict(md,
    newdata = nd, type = "distribution",
    q = 1000
  ))), c(1L, 3L))
  ## functions defined locally are found
  g <- function(dd) {
    tf <- function(z) z / 10
    Lmnet(tf(dist) ~ speed, data = dd, lambda = 0, alpha = 0)
  }
  d <- cars
  d$d10 <- d$dist / 10
  expect_equal(coef(g(cars)), coef(Lm(d10 ~ speed, data = d)),
    tolerance = 1e-4
  )
})

Try the tramnet package in your browser

Any scripts or data that you put into this service are public.

tramnet documentation built on Oct. 16, 2026, 5:07 p.m.