tests/testthat/test-methods.R

test_that("print and summary give different, classed output", {
  d <- make_test_data()
  fit <- gcecox(Surv(time, status) ~ smoke + age + bmi, data = d$df, t = 5)
  s <- summary(fit)
  expect_s3_class(s, "summary.gcemod")
  expect_equal(dim(s$coefficients), c(3L, 5L))
  expect_identical(colnames(s$coefficients),
                   c("coef", "exp(coef)", "se(coef)", "z", "Pr(>|z|)"))
  expect_equal(dim(s$conf.int), c(3L, 3L))
  po <- capture.output(print(fit)); so <- capture.output(print(s))
  expect_false(identical(po, so))
  expect_false(any(grepl("Pr\\(>\\|z\\|\\)", po)))
  expect_true(any(grepl("Pr\\(>\\|z\\|\\)", so)))
  expect_true(any(grepl("Omnibus", so)))

  ls <- summary(lunnmcneil(Surv(time, status) ~ smoke + age, data = d$df))
  expect_s3_class(ls, "summary.gce_lunnmcneil")
})

test_that("coef, vcov and confint are mutually consistent", {
  d <- make_test_data()
  fit <- gcecox(Surv(time, status) ~ smoke + age + bmi, data = d$df, t = 5)
  cf <- coef(fit); V <- vcov(fit)
  expect_named(cf, c("smoke", "age", "bmi"))
  expect_equal(dimnames(V), list(names(cf), names(cf)))
  ci <- confint(fit)
  expect_equal(unname(ci[, 1]), unname(cf - qnorm(0.975) * sqrt(diag(V))))
  expect_equal(confint(fit, exponentiate = TRUE), exp(ci))
  expect_equal(rownames(confint(fit, "age")), "age")
  s <- summary(fit)
  expect_equal(unname(s$coefficients[, "se(coef)"]), unname(sqrt(diag(V))))
  expect_equal(unname(s$conf.int[, 2:3]), unname(exp(ci)))

  # per-SD reporting rescales coef and vcov but leaves the tests unchanged
  fs <- gcecox(Surv(time, status) ~ smoke + age + bmi, data = d$df, t = 5,
               standardize = TRUE)
  expect_equal(coef(fs), cf * fit$scale)
  expect_equal(coef(summary(fs))[, "z"], coef(s)[, "z"])
  expect_equal(fs$riskscore, fit$riskscore)
})

test_that("coefficients are stored at full precision", {
  d <- make_test_data()
  fit <- gcecox(Surv(time, status) ~ smoke + age + bmi, data = d$df, t = 5)
  expect_equal(unname(fit$result[, 1]), unname(exp(coef(fit))), tolerance = 1e-12)
})

test_that("model-specification extractors and update() work", {
  d <- make_test_data()
  df <- d$df
  fit <- gcecox(Surv(time, status) ~ smoke + age + bmi, data = df, t = 5)
  expect_equal(nobs(fit), nrow(df))
  expect_s3_class(formula(fit), "formula")
  expect_s3_class(terms(fit), "terms")
  expect_equal(nrow(model.frame(fit)), nrow(df))
  expect_equal(colnames(model.matrix(fit)), names(coef(fit)))
  u <- update(fit, . ~ . - bmi)
  expect_named(coef(u), c("smoke", "age"))
  expect_error(model.frame(update(fit, model = FALSE)), "model = TRUE")
})

test_that("logLik is available for the hazard-scale fits only", {
  d <- make_test_data()
  fit <- gcecox(Surv(time, status) ~ smoke + age + bmi, data = d$df, t = 5)
  ll <- logLik(fit)
  expect_s3_class(ll, "logLik")
  expect_equal(attr(ll, "df"), 6L)
  expect_true(is.finite(AIC(fit)))
  # stacked partial likelihood = sum of the two cause-specific partial likelihoods
  l1 <- survival::coxph(Surv(time, status == 1) ~ smoke + age + bmi, d$df)$loglik[2]
  l2 <- survival::coxph(Surv(time, status == 2) ~ smoke + age + bmi, d$df)$loglik[2]
  expect_equal(as.numeric(ll), l1 + l2, tolerance = 1e-6)
  cif <- gcecif(Surv(time, status) ~ smoke + age, data = d$df, t = 3)
  expect_error(logLik(cif), "no likelihood")
})

test_that("predict and fitted return the documented quantities", {
  d <- make_test_data()
  df <- d$df
  fit <- gcecox(Surv(time, status) ~ smoke + age + bmi, data = df, t = 5)
  expect_equal(unname(fitted(fit)), fit$riskscore)
  expect_equal(unname(predict(fit, type = "risk")), exp(fit$riskscore))
  expect_equal(unname(predict(fit, type = "ratio")), fit$omegaplus)
  expect_equal(unname(predict(fit, type = "share")), fit$omega)
  expect_equal(mean(predict(fit)), 0, tolerance = 1e-10)
  m <- predict(fit, df[1:4, ], type = "ratio", times = c(1, 3, 5))
  expect_equal(dim(m), c(4L, 3L))
  expect_equal(unname(m[, 3]), fit$omegaplus[1:4])
  # under the model, a subject's ratio to the average is constant over time
  expect_equal(unname(m[1, ] / m[2, ]), rep(exp(fit$riskscore[1] - fit$riskscore[2]), 3))

  cif <- gcecif(Surv(time, status) ~ smoke + age, data = df, t = 3)
  expect_equal(unname(predict(cif, type = "ratio")), cif$rho)
  expect_equal(unname(predict(cif, df[1:3, ], type = "share")),
               (cif$rho / (1 + cif$rho))[1:3])
  expect_error(predict(cif, type = "ratio", times = 4), "landmark")
})

test_that("predict works for gcefg at several times", {
  skip_on_cran()
  d <- make_test_data(n = 200)
  fit <- gcefg(Surv(time, status) ~ smoke + age, data = d$df, t = 5)
  expect_equal(unname(predict(fit, type = "ratio")), fit$omegaplus)
  m <- predict(fit, d$df[1:3, ], type = "share", times = c(2, 5))
  expect_equal(dim(m), c(3L, 2L))
  expect_equal(unname(m[, 2]), fit$omega[1:3])
})

test_that("display objects are classed and their plot methods return ggplots", {
  d <- make_test_data()
  fit <- gcecox(Surv(time, status) ~ smoke + age + bmi, data = d$df, t = 5)
  grDevices::pdf(NULL); on.exit(grDevices::dev.off())

  cp <- gce_cutpoints(fit, groups = 2)
  expect_s3_class(cp, "gce_cutpoints")
  expect_s3_class(summary(cp), "summary.gce_cutpoints")
  expect_output(print(cp), "cutpoint")
  expect_output(print(summary(cp)), "High vs. Low")
  expect_s3_class(plot(cp), "ggplot")

  # no message about unknown labels when there is no grouping
  expect_silent(p <- plot(gce_omegatime(fit, which = "omegaplus")))
  expect_s3_class(p, "ggplot")
  expect_output(print(gce_omegatime(fit, groups = 2)), "High")
  expect_s3_class(plot(gce_calibration(fit)), "ggplot")
  expect_output(print(gce_alligator(fit, groups = cp)), "Gray")
  al <- plot(gce_alligator(fit, groups = cp))
  expect_true(inherits(al, "ggplot") || inherits(al, "patchwork"))

  for (w in c("riskscore", "omegatime", "calibration"))
    expect_s3_class(plot(fit, which = w), "ggplot")
  expect_s3_class(plot(fit, which = "riskscore", bins = 8), "ggplot")
})

test_that("calibration can be evaluated at a time other than the fitted t", {
  d <- make_test_data()
  fit <- gcecox(Surv(time, status) ~ smoke + age + bmi, data = d$df, t = 5)
  c3 <- gce_calibration(fit, t = 3)
  expect_equal(c3$t, 3)
  expect_false(isTRUE(all.equal(c3$table$predicted,
                                gce_calibration(fit)$table$predicted)))
})

test_that("specific classes have their own methods and reuse the parent's", {
  d <- make_test_data()
  cox <- gcecox(Surv(time, status) ~ smoke + age + bmi, data = d$df, t = 5)
  cif <- gcecif(Surv(time, status) ~ smoke + age, data = d$df, t = 3)
  m <- function(cl) as.character(utils::methods(class = cl))
  expect_true(all(c("predict.gcecox", "summary.gcecox") %in% m("gcecox")))
  expect_true(all(c("predict.gcefg", "summary.gcefg", "plot.gcefg") %in% m("gcefg")))
  expect_true(all(c("predict.gcecif", "summary.gcecif", "logLik.gcecif",
                    "plot.gcecif") %in% m("gcecif")))

  # common part from the parent method, model-specific part from the subclass
  s <- summary(cox)
  expect_s3_class(s, "summary.gcemod")
  expect_named(s$baseline, c("omega+", "omega"))
  expect_equal(summary(cox, conf.level = 0.9)$conf.level, 0.9)
  sc <- summary(cif)
  expect_named(sc$baseline, c("F1", "F2", "rho"))
  expect_identical(sc$label, "rho ratio")
  expect_match(sc$se_method, "sandwich")

  # lp and risk are computed by the parent method for every class
  expect_equal(predict(cox), predict(structure(cox, class = "gcemod")))
  expect_equal(predict(cif, type = "risk"),
               predict(structure(cif, class = "gcemod"), type = "risk"))
  expect_error(predict(structure(cox, class = "gcemod"), type = "ratio"),
               "not available")
})

test_that("coef() of a summary returns the coefficient table", {
  d <- make_test_data()
  fit <- gcecox(Surv(time, status) ~ smoke + age + bmi, data = d$df, t = 5)
  s <- summary(fit)
  expect_identical(coef(s), s$coefficients)
  expect_equal(coef(s)[, "coef"], coef(fit))
  ls <- summary(lunnmcneil(Surv(time, status) ~ smoke + age, data = d$df))
  expect_identical(coef(ls), ls$coefficients)
})

test_that("eps controls how small p-values are printed", {
  expect_identical(gcemod:::.fmt_p(1e-6), "<1e-04")
  expect_identical(gcemod:::.fmt_p(1e-6, eps = 1e-3), "<0.001")
  expect_false(grepl("<", gcemod:::.fmt_p(1e-6, eps = 1e-8)))

  d <- make_test_data()
  fit <- gcecox(Surv(time, status) ~ smoke + age + bmi, data = d$df, t = 5)
  s <- summary(fit)
  s$omnibus["p.value"] <- 1e-6
  omni <- function(...) grep("Omnibus", capture.output(print(s, ...)), value = TRUE)
  expect_match(omni(), "p = <1e-04", fixed = TRUE)
  expect_match(omni(eps = 1e-3), "p = <0.001", fixed = TRUE)
  expect_match(omni(eps = 1e-8), "p = 1e-06", fixed = TRUE)

  cp <- gce_cutpoints(fit, groups = 2)
  cp$test$p.value <- 1e-6
  expect_output(print(cp), "p = <1e-04", fixed = TRUE)
  expect_output(print(cp, eps = 1e-8), "p = 1e-06", fixed = TRUE)
  expect_output(print(summary(cp), eps = 1e-3), "p = <0.001", fixed = TRUE)
})

test_that("the risk-score plot takes its axis label from the model class", {
  skip_on_cran()
  d <- make_test_data(n = 200)
  grDevices::pdf(NULL); on.exit(grDevices::dev.off())
  cif <- gcecif(Surv(time, status) ~ smoke + age, data = d$df, t = 3)
  fg  <- gcefg(Surv(time, status) ~ smoke + age, data = d$df, t = 5)
  lab <- function(p) deparse(p$labels$y)
  expect_match(lab(plot(cif)), "rho")
  expect_match(lab(plot(fg)), "tilde")
  expect_identical(plot(fg, ylab = "my label")$labels$y, "my label")
  expect_s3_class(plot(fg, which = "omegatime"), "ggplot")
})

Try the gcemod package in your browser

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

gcemod documentation built on Dec. 1, 2026, 1:06 a.m.