tests/testthat/test-Colr-pen.R

# Tests for COLR models

test_that("unpenalized Colr is recovered", {
  skip_if_not_installed("survival")
  skip_if_not_installed("TH.data")
  library("survival")

  ## Exact and Right censored
  data("GBSG2", package = "TH.data")
  GBSG2$surv <- with(GBSG2, Surv(time, cens))
  x <- matrix(1 * (GBSG2$horTh == "yes"), ncol = 1)
  colnames(x) <- "horTh"

  yCOLR <- Colr(surv ~ 1, data = GBSG2, log_first = TRUE, order = 10)
  modCOLR <- tramnet(yCOLR, x, lambda = 0, alpha = 0)
  yCOLRb <- Colr(surv ~ horTh, data = GBSG2, log_first = TRUE, order = 10)
  expect_s3_class(modCOLR, "tramnet")
  expect_equal(unname(coef(modCOLR, with_baseline = FALSE)),
    unname(coef(yCOLRb, with_baseline = FALSE)),
    tolerance = 1e-3
  )
  expect_equal(c(logLik(modCOLR)), c(logLik(yCOLRb)), tolerance = 1e-5)
  expect_equal(-modCOLR$result$value, c(logLik(modCOLR)), tolerance = 1e-6)
  expect_length(
    c(logLik(modCOLR,
      newdata = tramnet:::.get_tramnet_data(modCOLR)[1:10, ]
    )),
    1L
  )

  ## methods
  n <- nrow(GBSG2)
  expect_length(coef(modCOLR, tol = 0, with_baseline = TRUE), 12L)
  expect_length(resid(modCOLR), n)
  expect_equal(c(resid(modCOLR)), c(resid(yCOLRb)), tolerance = 1e-3)
  expect_equal(
    unname(dim(predict(modCOLR, type = "distribution", q = 1))),
    c(1L, n)
  )
  expect_equal(
    unname(dim(predict(modCOLR, type = "quantile", prob = 0.5))),
    c(1L, n)
  )
  expect_equal(NROW(simulate(modCOLR)), n)
  ef <- estfun(modCOLR)
  expect_equal(dim(ef), c(n, 12L))
  ## score of the (unconstrained) shift parameter vanishes
  expect_equal(sum(ef[, 12]), 0, tolerance = 1e-2)
  pdf(NULL)
  on.exit(dev.off())
  expect_no_error(plot(modCOLR, type = "survivor"))
  expect_no_error(plot(modCOLR, type = "density", K = 120))
  expect_output(print(modCOLR), "Continuous Outcome Logistic Regression")
})

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.