tests/testthat/test-formula.R

test_that("formula and matrix interfaces give identical fits", {
  d <- make_test_data()
  Ind <- data.frame(d$CA, d$CM)
  f <- Surv(time, status) ~ smoke + age + bmi

  a <- gcecox(f, data = d$df, t = 5)
  b <- gcecox(d$Time, Ind, d$Cov, t = 5)
  expect_equal(coef(a), coef(b))
  expect_equal(vcov(a), vcov(b))
  expect_equal(a$riskscore, b$riskscore)
  expect_equal(a$omegaplus, b$omegaplus)

  a <- gcecif(f, data = d$df, t = 3)
  b <- gcecif(d$Time, Ind, d$Cov, t = 3)
  expect_equal(coef(a), coef(b))
  expect_equal(a$rho, b$rho)

  a <- lunnmcneil(f, data = d$df)
  b <- lunnmcneil(d$Time, d$cause, d$Cov)
  expect_equal(coef(a), coef(b))
  expect_equal(a$omnibus, b$omnibus)
})

test_that("formula and matrix interfaces agree for gcefg", {
  skip_on_cran()
  d <- make_test_data(n = 200)
  a <- gcefg(Surv(time, status) ~ smoke + age + bmi, data = d$df, t = 5)
  b <- gcefg(d$Time, data.frame(d$CA, d$CM), d$Cov, t = 5)
  expect_equal(coef(a), coef(b))
  expect_equal(a$omegaplus, b$omegaplus)
})

test_that("the matrix interface accepts a named 'Time' and warns about 'M'", {
  d <- make_test_data()
  Ind <- data.frame(d$CA, d$CM)
  a <- gcecox(Time = d$Time, Ind = Ind, Cov = d$Cov, t = 5)
  b <- gcecox(d$Time, Ind, d$Cov, t = 5)
  expect_equal(coef(a), coef(b))
  expect_warning(gcecox(d$Time, Ind, d$Cov, M = 4, t = 5), "deprecated")
  expect_equal(coef(lunnmcneil(Time = d$Time, cause = d$cause, Cov = d$Cov)),
               coef(lunnmcneil(d$Time, d$cause, d$Cov)))
})

test_that("status may be a factor, and event/competing can be chosen", {
  d <- make_test_data()
  df <- d$df
  df$fstat <- factor(df$status, 0:2, c("censored", "relapse", "death"))
  a <- gcecox(Surv(time, status) ~ age + smoke, data = df)
  b <- gcecox(Surv(time, fstat) ~ age + smoke, data = df)
  expect_equal(coef(a), coef(b))

  # swapping the roles of the two events flips the sign of the log ratios
  s1 <- gcecox(Surv(time, status) ~ age + smoke, data = df,
               event = 2, competing = 1)
  s2 <- gcecox(Surv(time, fstat) ~ age + smoke, data = df,
               event = "death", competing = "relapse")
  expect_equal(coef(s1), -coef(a))
  expect_equal(coef(s2), -coef(a))
})

test_that("subset, na.action, '.', and transformations are honoured", {
  d <- make_test_data()
  df <- d$df
  a <- gcecox(Surv(time, status) ~ age + smoke, data = df, subset = age > 55)
  b <- gcecox(Surv(time, status) ~ age + smoke, data = df[df$age > 55, ])
  expect_equal(coef(a), coef(b))
  expect_equal(nobs(a), sum(df$age > 55))

  df2 <- df; df2$age[1:7] <- NA
  expect_equal(nobs(gcecox(Surv(time, status) ~ age + smoke, data = df2)),
               nrow(df) - 7L)

  dot <- gcecox(Surv(time, status) ~ ., data = df)
  expect_setequal(names(coef(dot)), c("smoke", "age", "bmi"))

  tr <- gcecox(Surv(time, status) ~ log(bmi) + factor(smoke) * age, data = df)
  expect_true("factor(smoke)1:age" %in% names(coef(tr)))
})

test_that("an invalid response is rejected with an informative error", {
  d <- make_test_data()
  expect_error(gcecox(time ~ age, data = d$df), "Surv\\(time, status\\)")
  expect_error(gcecox(~ age, data = d$df), "two-sided")
  expect_error(gcecox(Surv(time, status == 1) ~ age, data = d$df), "logical")
  bad <- d$df; bad$status[1] <- 5L
  expect_error(gcecox(Surv(time, status) ~ age, data = bad), "codes other than")
  expect_error(gcecif(Surv(time, status) ~ age, data = d$df), "landmark")
})

test_that("predict() rebuilds the design for new data from stored terms", {
  d <- make_test_data()
  df <- d$df
  df$grp <- factor(sample(c("a", "b", "c"), nrow(df), replace = TRUE))
  fit <- gcecox(Surv(time, status) ~ splines::ns(age, 3) + grp * smoke,
                data = df, t = 5)
  # predictions for a subset of the fitting data reproduce the fitted values,
  # including data-dependent spline bases and factor coding with absent levels
  new <- df[c(3, 11, 20), ]
  new <- new[new$grp != "c" | TRUE, ]
  expect_equal(unname(predict(fit, new)), fit$riskscore[c(3, 11, 20)])
  one <- df[5, ]                                   # single row, one level only
  expect_equal(unname(predict(fit, one, type = "ratio")), fit$omegaplus[5])
  expect_error(predict(fit, df[1:3, c("age", "smoke")]), "missing covariate")
})

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.