tests/testthat/test-roxytest-tests-set_covariates.r

# Generated by roxytest: Do not edit by hand!

# File R/set_covariates.r: @tests

test_that("Function set_covariates() @ L53", {
  fs1 <- flexsurvreg(
    Surv(rectime, censrec)~group,
    data=flexsurv::bc,
    dist = "llogis"
  )
  
  expect_error(
   set_covariates("foo", data.frame(group = 'Good')),
   'Error setting covariates, only survfit and flexsurv models are supported.',
   fixed = TRUE
  )
  expect_error(
   set_covariates(fs1, "foo"),
   'Error setting covariates, "data" must be provided as a data frame.',
   fixed = TRUE
  )
  expect_error(
   set_covariates(fs1),
   'Error setting covariates, must provide either "data" or named arguments for covariate values.',
   fixed = TRUE
  )
})


test_that("Function surv_prob.surv_model() @ L119", {
  fs <- flexsurvreg(Surv(rectime, censrec)~group, data = flexsurv::bc, dist = 'weibull')
  model1 <- set_covariates(fs, group = 'Good')
  expect_equal(
   surv_prob(model1, seq_len(100)),
   summary(fs, type = 'survival', t = seq_len(100), tidy = TRUE, newdata = data.frame(group = 'Good'))$est
  )
  fs <- flexsurvreg(Surv(rectime, censrec)~group, data = flexsurv::bc, dist = 'weibull')
  model2 <- set_covariates(fs, data.frame(group = c('Good', 'Medium', 'Poor')))
  expect_equal(
   surv_prob(model2, seq_len(100)),
   summary(fs, type = 'survival', t = seq_len(100), tidy = TRUE) %>%
       group_by(time) %>%
       summarize(est = mean(est)) %>% .$est
  )
})


test_that("Function print.surv_model() @ L137", {
  fs <- flexsurvreg(Surv(rectime, censrec)~group, data = flexsurv::bc, dist = 'weibull')
  model <- set_covariates(fs, group = 'Good')
  expect_output(
   print(model),
   'Predicted survival from fitted model for specified cohort'
  )
  expect_output(
   print(model),
   'flexsurvreg'
  )
})
PolicyAnalysisInc/herosurv documentation built on May 21, 2023, 10:12 a.m.