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

# Generated by roxytest: Do not edit by hand!

# File R/surv_km.r: @tests

test_that("Function define_surv_km() @ L75", {
  df <- data.frame(
       month = c(0, 1, 5, 10),
       p_surv = c(1, 0.9, 0.7, 0.5)
  )
  dist1 <- define_surv_km(df, 'month', 'p_surv')
  dist2 <- define_surv_km(df[c(4,1,3,2), ], 'month', 'p_surv')
  dist3 <- define_surv_km(mutate(df, time = month, survival = p_surv))
  expect_equal(dist1, dist2)
  expect_equal(dist1, dist3)
  
  expect_error(
   define_surv_km(data.frame()),
   'Error defining KM, the following columns were expected but not found: "time", "survival".',
    fixed = TRUE
  )
  expect_error(
   define_surv_km(
       mutate(df, p_surv = c(1, 0.9, 0.7, 0.9)), 'month', 'p_surv'
   ),
   'Error defining KM, column "p_surv" may not be increasing with respect to "month".',
    fixed = TRUE
  )
  expect_error(
   define_surv_km(
       mutate(df, month = c(0, 1, 1, 10)), 'month', 'p_surv'
   ),
   'Error defining KM, column "month" contained duplicate values.',
   fixed = TRUE
  )
  expect_error(
   define_surv_km(df[-1, ], 'month', 'p_surv'),
   fixed = TRUE,
   'Error defining KM, column "month" must start with a value 0 and "p_surv" must start with a value 1.'
  )
  expect_error(
   define_surv_km(mutate(df, p_surv = NA), 'month', 'p_surv'),
   fixed = TRUE,
   'Error defining KM, the following columns contained missing values: "p_surv".'
  )
  expect_error(
   define_surv_km(mutate(df, p_surv = "foo"), 'month', 'p_surv'),
   fixed = TRUE,
   'Error defining KM, the following columns were of invalid type: "p_surv".'
  )
  expect_error(
   define_surv_km(mutate(df, p_surv = 1.2), 'month', 'p_surv'),
   fixed = TRUE,
   'Error defining KM, values in column "p_surv" must be within the interval [0-1].'
  )
})


test_that("Function print.surv_km() @ L185", {
  df <- data.frame(
       month = c(0, 1, 5, 10),
       p_surv = c(1, 0.9, 0.7, 0.5)
  )
  dist1 <- define_surv_km(df, 'month', 'p_surv')
  expect_output(
       print(dist1),
       "A Kaplan-Meier distribution:",
       fixed = T
  )
})


test_that("Function define_surv_table() @ L201", {
  df <- data.frame(
       time = c(0, 1, 5, 10),
       survival = c(1, 0.9, 0.7, 0.5)
  )
  dist1 <- define_surv_table(df)
  dist2 <- define_surv_km(df)
  expect_equal(dist1, dist2)
})


test_that("Function surv_prob.surv_km() @ L227", {
  df <- data.frame(
       time = c(0, 1, 5, 10),
       survival = c(1, 0.9, 0.7, 0.5)
  )
  dist1 <- define_surv_table(df)
  expect_equal(
   surv_prob(dist1, c(0, 0.99, 1, 1.01, 4.99, 5, 5.01, 11)),
   c(1, 1, 0.9, 0.9, 0.9, 0.7, 0.7, NA)
  )
  df2 <- data.frame(
       time = c(0, 1, 5, 10),
       survival = c(1, 0.9, 0.7, 0)
  )
  dist2 <- define_surv_table(df2)
  expect_equal(
   surv_prob(dist2, c(0, 0.99, 1, 1.01, 4.99, 5, 5.01, 11)),
   c(1, 1, 0.9, 0.9, 0.9, 0.7, 0.7, 0)
  )
})
PolicyAnalysisInc/herosurv documentation built on May 21, 2023, 10:12 a.m.