tests/testthat/test-loglik.R

test_that("loglik_frailty evaluates finite log-likelihood across all 10 combinations", {
  time <- c(1.2, 2.5, 0.8, 3.1, 4.0)
  status <- c(1, 0, 1, 2, 3)
  time2 <- c(NA, NA, NA, NA, 5.0)
  x <- matrix(c(0.2, -0.5, 0.1, 0.8, -0.3), ncol = 1)

  combos <- list(
    list(f = "none", p = c(log(2), log(1.5), 0.1)),
    list(f = "gamma", p = c(log(2), log(1.5), log(0.8), 0.1)),
    list(f = "ig", p = c(log(2), log(1.5), log(0.7), 0.1)),
    list(f = "gl1", p = c(log(2), log(1.5), log(1.2), log(0.5), 0.1)),
    list(f = "gl2", p = c(log(2), log(1.5), log(1.5), stats::qlogis(0.5), 0.1)),
    list(f = "none", b = "gw", p = c(log(0.5), log(1.2), log(1.1), 0.1)),
    list(f = "gamma", b = "gw", p = c(log(0.5), log(1.2), log(1.1), log(0.8), 0.1)),
    list(f = "ig", b = "gw", p = c(log(0.5), log(1.2), log(1.1), log(0.7), 0.1)),
    list(f = "gl1", b = "gw", p = c(log(0.5), log(1.2), log(1.1), log(1.2), log(0.5), 0.1)),
    list(f = "gl2", b = "gw", p = c(log(0.5), log(1.2), log(1.1), log(1.5), stats::qlogis(0.5), 0.1))
  )

  for (item in combos) {
    b_type <- if (is.null(item$b)) "weibull" else item$b
    ll <- loglik_frailty(item$p, time = time, status = status, x = x,
                         baseline = b_type, frailty = item$f, time2 = time2)

    expect_true(is.finite(ll), label = paste("Finite LL for", item$f, b_type))
    expect_true(ll > -1e10, label = paste("Non-sentinel LL for", item$f, b_type))
  }
})

test_that("loglik_frailty returns -1e12 sentinel on invalid parameter vectors", {
  time <- c(1, 2, 3)
  status <- c(1, 0, 1)

  # Invalid parameter count
  ll_bad <- loglik_frailty(c(1, 2), time, status, baseline = "weibull", frailty = "gamma")
  expect_equal(ll_bad, -1e12)
})

Try the MultiFrailty package in your browser

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

MultiFrailty documentation built on Aug. 8, 2026, 1:07 a.m.