tests/testthat/test-methods.R

# Tests for S3 methods

test_that("methods for models with negative shift (Lm)", {
  m0 <- Lm(dist ~ 1, data = cars)
  x <- as.matrix(cars[, "speed", drop = FALSE])
  mt <- tramnet(m0, x = x, alpha = 0, lambda = 0)
  m1 <- Lm(dist ~ speed, data = cars)
  expect_equal(coef(mt), coef(m1), tolerance = 1e-4)
  expect_equal(coef(mt, as.lm = TRUE),
    coef(lm(dist ~ speed, data = cars)),
    tolerance = 1e-3,
    ignore_attr = TRUE
  )
  expect_equal(c(logLik(mt)), c(logLik(m1)), tolerance = 1e-6)
  expect_equal(c(resid(mt)), c(resid(m1)), tolerance = 1e-3)
  expect_equal(c(predict(mt, type = "trafo", q = 50)),
    c(predict(m1, newdata = cars, type = "trafo", q = 50)),
    tolerance = 1e-4
  )
  expect_equal(unname(estfun(mt)), unname(estfun(m1)), tolerance = 1e-2)
  ## formula interface
  mf <- Lmnet(dist ~ speed, data = cars, lambda = 0, alpha = 0)
  expect_equal(coef(mf, as.lm = TRUE), coef(mt, as.lm = TRUE),
    tolerance = 1e-4
  )
  expect_equal(c(predict(mf, newdata = cars[1:3, ], type = "trafo", q = 50)),
    c(predict(m1, newdata = cars[1:3, ], type = "trafo", q = 50)),
    tolerance = 1e-4
  )
})

test_that("methods for the formula interface", {
  skip_if_not_installed("survival")
  skip_if_not_installed("TH.data")
  library("survival")
  data("GBSG2", package = "TH.data")
  GBSG2$surv <- with(GBSG2, Surv(time, cens))
  n <- nrow(GBSG2)
  m <- Coxphnet(surv ~ horTh + age + tsize + pnodes,
    data = GBSG2,
    lambda = 10, alpha = 1, log_first = TRUE
  )
  ## coef
  cf <- coef(m, tol = 0)
  cfs <- coef(m, tol = 0, scaled = TRUE)
  expect_equal(cf, cfs / m$scaling)
  expect_length(coef(m, with_baseline = TRUE, tol = 0), 7L + 4L)
  expect_lte(length(coef(m)), 4L)
  expect_warning(coef(m, foo = 1), "ignored")
  ## logLik includes the penalty
  ll <- logLik(m)
  expect_equal(c(ll), -m$result$value, tolerance = 1e-6)
  expect_true(is.na(attr(ll, "df")))
  ## penalty lambda * (0.5 * (1 - alpha) * ||beta||_2^2 + alpha * ||beta||_1)
  ## as in glmnet, for the elastic net and lambda != 1
  me <- Coxphnet(surv ~ horTh + age + tsize + pnodes,
    data = GBSG2,
    lambda = 7, alpha = 0.3, log_first = TRUE
  )
  expect_equal(c(logLik(me)), -me$result$value, tolerance = 1e-6)
  b <- coef(me, tol = 0, scaled = TRUE)
  pen <- 7 * (0.5 * 0.7 * sum(b^2) + 0.3 * sum(abs(b)))
  expect_equal(
    c(logLik(me, parm = coef(me,
      with_baseline = TRUE, tol = 0,
      scaled = TRUE
    ))),
    c(logLik(me)),
    tolerance = 1e-8
  )
  expect_equal(c(tramnet:::.elnet(me)), pen)
  ## predict, residuals, simulate
  expect_equal(
    unname(dim(predict(m,
      newdata = GBSG2[1:7, ],
      type = "survivor", q = c(500, 1000)
    ))),
    c(2L, 7L)
  )
  expect_length(resid(m), n)
  expect_length(resid(m, newdata = GBSG2[1:7, ]), 7L)
  expect_equal(NROW(simulate(m, newdata = GBSG2[1:7, ], seed = 1)), 7L)
  ## estfun only without penalty
  expect_error(estfun(m), "penalised")
  m0 <- Coxphnet(surv ~ horTh + age,
    data = GBSG2, lambda = 0, alpha = 0,
    log_first = TRUE
  )
  expect_equal(unname(colSums(estfun(m0))[8:9]), rep(0, 2), tolerance = 1e-2)
  ## model.matrix
  expect_equal(dim(model.matrix(m)), c(n, 4L))
  expect_equal(
    colnames(model.matrix(m, newdata = GBSG2[1:2, ])),
    colnames(model.matrix(m))
  )
  ## summary and print
  s <- summary(m)
  expect_s3_class(s, "summary.tramnet")
  expect_equal(s$coef, coef(m))
  expect_match(s$type, "Regularized")
  expect_output(print(m), "Coxphnet")
  ## plot
  pdf(NULL)
  on.exit(dev.off())
  expect_no_error(plot(m, newdata = GBSG2[1:3, ], type = "survivor"))
  expect_no_error(plot(m, type = "trafo", K = 20))
})

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.