tests/testthat/test-simulate_tte_models.R

library(testthat)

context("Test the simulate method with time-to-event models (TTE)")

source(file.path(getwd(), test_path(), "test-utils.R"))

test_that("Simulate simple TTE model", {
  if (skip_long_tests()) {
    return(TRUE)
  }
  regFilename <- "simple_tte_model"
  model <- read.campsis(file.path(getwd(), test_path(), "models/simple_tte_model/"))

  events <- Events()
  duration <- 24

  event <- Event(name = "Check patient state", times = seq(1, duration), fun = function(inits) {
    updateCount <- inits$A_SURVIVAL < inits$TRIGGER

    # Update counter and trigger
    inits$COUNT <- ifelse(updateCount, inits$COUNT + 1, inits$COUNT)
    inits$TRIGGER <- ifelse(updateCount, runif(updateCount %>% length(), 0, 1), inits$TRIGGER)

    # Reset survival compartment
    inits$A_SURVIVAL <- ifelse(updateCount, 1, inits$A_SURVIVAL)

    return(inits)
  })

  ds <- Dataset(2) %>%
    add(EventCovariate("COUNT", 0)) %>%
    add(EventCovariate("TRIGGER", UniformDistribution(0, 1))) %>%
    add(Observations(times = seq(0, duration, by = 0.1)))

  events <- events %>% add(event)

  # p1 <- spaghettiPlot(results1, "A_SURVIVAL")
  # p2 <- spaghettiPlot(results1, "COUNT")
  # p3 <- spaghettiPlot(results1, "TRIGGER")
  # gridExtra::grid.arrange(p1, p2, p3, ncol=1)
  #
  # p1 <- spaghettiPlot(results2, "A_SURVIVAL")
  # p2 <- spaghettiPlot(results2, "COUNT")
  # p3 <- spaghettiPlot(results2, "TRIGGER")
  # gridExtra::grid.arrange(p1, p2, p3, ncol=1)

  simulation <- expression(simulate(
    model = model,
    dataset = ds,
    dest = destEngine,
    events = events,
    outvars = c("COUNT", "TRIGGER"),
    seed = 5
  ))
  test <- expression(
    output_regression_test(results, output = c("A_SURVIVAL", "TRIGGER"), filename = regFilename)
  )
  campsis_test(simulation, test, env = environment())
})

Try the campsis package in your browser

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

campsis documentation built on Aug. 5, 2026, 9:07 a.m.