testthat::context("mean survival testing") library("data.table") library("Epi") testthat::test_that("survmean() agrees with old results", { popEpi:::skip_normally() sr <- data.table(popEpi::sire)[dg_date < ex_date, ] sr$agegr <- cut(sr$dg_age, c(0,45,60,Inf), right=FALSE) x <- Lexis(entry = list(FUT = 0, AGE = dg_age, CAL = get.yrs(dg_date)), exit = list(CAL = get.yrs(ex_date)), data = sr, exit.status = factor(status, levels = 0:2, labels = c("alive", "canD", "othD")), entry.status = factor(0, levels = 0:2, labels = c("alive", "canD", "othD")), merge = TRUE) ## observed survival pm <- data.table(popEpi::popmort) names(pm) <- c("sex", "CAL", "AGE", "haz") sm <- survmean(Surv(time = FUT, event = lex.Xst != "alive") ~ agegr, pophaz = pm, data = x, breaks = list(FUT = seq(0, 10, 1/12)), e1.breaks = list(FUT = c(seq(0, 10, 1/12), 11:100))) ## values to test against computed on 2016-03-04; ## git ref: 5077677 testthat::expect_equal(sm$est, c(33.951439, 21.611419, 7.604318), tol = 0.005, scale = 1) testthat::expect_equal(sm$exp, c(45.25686, 31.22712, 13.06725), tol = 0.005, scale = 1) }) # test_that("survmean() agrees with results computed using pkg survival", { # popEpi:::skip_normally() # # # BL <- list(fot= seq(0,15,1/24)) # eBL <- list(fot = unique(c(BL$fot, seq(15, 115,0.5)))) # # sire2 <- data.table(popEpi::sire)[dg_date= "1998-01-01" & dg_date < "2003-01-01", pophaz = pm, data = x, r = 1, breaks = BL, e1.breaks = eBL) BL <- list(FUT = seq(0, 5, 1/12), CAL = c(1998,2003)) eBL <- list(FUT = c(BL$FUT, seq(max(BL$FUT), 10, 1/12), seq(10,110,1/2))) eBL$FUT <- sort(unique(eBL$FUT)) smp <- survmean(Surv(time = FUT, event = lex.Xst != "alive") ~ 1, pophaz = pm, data = x, breaks = BL, r = 1, e1.breaks = eBL) testthat::expect_equal(sm$obs, smp$obs) testthat::expect_equal(sm$exp, smp$exp) testthat::expect_equal(smp$est, 10.01542, tol = 0.0005, scale = 1) testthat::expect_equal(sm$est, 10.0216, tol = 0.0005, scale = 1) }) testthat::test_that("Dates and frac. yrs produce congruent results", { popEpi:::skip_normally() x <- data.table(popEpi::sire) x <- x[dg_date