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

# Generated by roxytest: Do not edit by hand!

# File R/surv_lifetable.r: @tests

test_that("Function define_surv_lifetable() @ L68", {
  x <- data.frame(
       age = c(0, 1, 2, 3),
       male = c(0.011, 0.005, 0.003, 0.002),
       female = c(0.010, 0.005, 0.004, 0.002)
   )
   expect_equal(
       define_surv_lifetable(x, 1, 0.45)[-4],
       define_surv_lifetable(x[c(4,3,2,1), ], 1, 0.45)[-4]
   )
   expect_equal(
       define_surv_lifetable(x, 1, 0.45)[-4],
       define_surv_lifetable(x, 1, percent_female = 0.55)[-4]
   )
   expect_error(
       define_surv_lifetable(x, 1, 0.45, age_col = "foo", male_col = "bar"),
       'Error defining life-table, the following columns were expected but not found: "foo", "bar".'
   )
   expect_error(
       define_surv_lifetable(x, 1, 0.45, percent_female = 0.1),
       'Error defining life-table, must provide either "percent_male", or "percent_female", but not both.'
   )
   expect_error(
       define_surv_lifetable(x[c(1,1,2,3,4), ], 1, 0.45),
       'Error defining life-table, column "age" contained duplicate values.'
   )
   expect_error(
       define_surv_lifetable(x[c(1,3,4), ], 1, 0.45),
       'Error defining life-table, life-table must use constant age bands.'
   )
})


test_that("Function print.surv_lifetable() @ L172", {
  dist1 <- define_surv_lifetable(
   data.frame(age=c(1,2,3),male=c(0.01,0.01,0.01),female=c(0.009,0.009,0.009)),
   percent_male = 0.49,
   start_age=1
  )
  expect_output(
   print(dist1),
   'A life-table survival distribution (49.0% male, 51.0% female):',
   fixed = T
  )
})


test_that("Function surv_prob.surv_lifetable() @ L199", {
  surv_lifetable_df <- data.frame(
   age = c(0, 1, 2, 3),
   male = c(0.011, 0.004, 0.003, 0.002),
   female = c(0.010, 0.005, 0.004, 0.002)
  )
  reg <- define_surv_lifetable(surv_lifetable_df, c(1,1,1), 0.5)     
  expect_equal(
   surv_prob(reg, time = c(0, 0.5, 1, 1.5, 2, 3, 10)),
   c(1, 0.9977474, 0.9955000, 0.9937564, 0.9920160, 0.9900320, 0.9762544),
   tolerance = 1e-7
  )
})
PolicyAnalysisInc/herosurv documentation built on May 21, 2023, 10:12 a.m.