tests/testthat/test-svycoxme.R

# test for inadvertent changes to the output from print, coef, and summary.

test_that('compare summary(fit1) to saved value', {
  des <- survey::svydesign(ids = ~group_id, weights = ~weight, data = samp_srcs)

  fit1 = svycoxme(survival::Surv(stat_time, stat) ~ X1 + (1 | group_id), design = des)

  expect_equal(summary(fit1), summary_fit1)
})


test_that("compare coef(fit) to saved value", {
  des <- survey::svydesign(ids = ~group_id, weights = ~weight, data = samp_srcs)

  fit1 = svycoxme(survival::Surv(stat_time, stat) ~ X1 + (1 | group_id), design = des)

  expect_equal(coef(fit1), coef_fit1)
})

# I can't see why this is failing!
# test_that("compare print(fit) to saved value", {
#   des <- survey::svydesign(ids = ~group_id, weights = ~weight, data = samp_srcs)
#
#   fit1 = svycoxme(survival::Surv(stat_time, stat) ~ X1 + (1 | group_id), design = des)
#
#   expect_equal(capture_output_lines(print(fit1)), print_fit1)
#
# })

test_that("Check that print generates output",{
  des <- survey::svydesign(ids = ~group_id, weights = ~weight, data = samp_srcs)

  fit1 = svycoxme(survival::Surv(stat_time, stat) ~ X1 + (1 | group_id), design = des)

  expect_output(print(fit1))

})

# test that svycoxme models fit with replicate-weight designs have no errors.
# some designs need a stratified design (BRR, JKn, Fay).
test_that("No errors with svyrep designs",{

  des <- survey::svydesign(ids = ~group_id, weights = ~weight, data = samp_srcs, fpc = ~fpc)

  repdes1 <- survey::as.svrepdesign(des, type = 'JK1')
  repdes3 <- survey::as.svrepdesign(des, type = 'bootstrap')
  repdes4 <- survey::as.svrepdesign(des, type = 'subbootstrap')
  repdes5 <- survey::as.svrepdesign(des, type = 'mrbbootstrap')

  expect_no_error(fit1 <- svycoxme(survival::Surv(stat_time, stat) ~ X1 + (1 | group_id), design = repdes1))
  expect_no_error(fit2 <- svycoxme(survival::Surv(stat_time, stat) ~ X1 + (1 | group_id), design = repdes3))
  expect_no_error(fit3 <- svycoxme(survival::Surv(stat_time, stat) ~ X1 + (1 | group_id), design = repdes4))
  expect_no_error(fit4 <- svycoxme(survival::Surv(stat_time, stat) ~ X1 + (1 | group_id), design = repdes5))

  expect_output(summary(fit1))
  expect_output(summary(fit2))
  expect_output(summary(fit3))
  expect_output(summary(fit4))

  expect_output(print(fit1))
  expect_output(print(fit2))
  expect_output(print(fit3))
  expect_output(print(fit4))

})


# test the use of futures
test_that("multicore gives same results as sequential",{

  des <- survey::svydesign(ids = ~group_id, weights = ~weight, data = samp_srcs, fpc = ~fpc)

  repdes1 <- survey::as.svrepdesign(des, type = 'bootstrap')

  future::plan(future::multicore,
               workers = floor(parallelly::availableCores() * 0.8))

  expect_no_error(fit_multicore <- svycoxme(survival::Surv(stat_time, stat) ~ X1 + (1 | group_id),
                                            design = repdes1, multicore = TRUE))

  future::plan(future::sequential)

  fit_sequential <- svycoxme(survival::Surv(stat_time, stat) ~ X1 + (1 | group_id),
                             design = repdes1)

  expect_equal(coef(fit_multicore), coef(fit_sequential))
  expect_equal(vcov(fit_multicore), vcov(fit_sequential))

})

Try the svycoxme package in your browser

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

svycoxme documentation built on June 8, 2025, 1:13 p.m.