testthat::context("utility functions") testthat::test_that("subsetting in ltable works and ltable has no side effects", { popEpi:::skip_normally() sr <- popEpi::sire[1:100, ] set.seed(1L) sr[, sex := rbinom(.N, 1, prob = 0.5)] sr[c(1, 50), sex := NA] setkeyv(sr, "bi_date") old_sr <- copy(sr) lt1 <- ltable(sr, by = "sex", subset = sex == 0, na.rm = TRUE) lt2 <- ltable(sr, by = "sex", subset = sex == 1, na.rm = TRUE) lt3 <- ltable(sr, by = "sex", na.rm = TRUE) testthat::expect_equal(lt3$obs, c(lt1[1, ]$obs, lt2[2, ]$obs)) testthat::expect_true(all.equal(sr, old_sr)) }) testthat::test_that("ltable works with NA values", { popEpi:::skip_normally() sr <- setDT(popEpi::sire[1:100, ]) set.seed(1L) sr[, sex := rbinom(.N, 1, prob = 0.5)] sr[c(1, 50), sex := NA] lt1 <- ltable(sr, by = "sex", na.rm = FALSE) lt2 <- ltable(sr, by = "sex", na.rm = TRUE) testthat::expect_equal(lt1[!is.na(sex),], lt2) }) testthat::test_that("evalPopArg produces intended results",{ set.seed(1L) dt <- data.table(a = rbinom(10, 100, 0.25), b = 1:2, c = 1:5) tf <- function(x=dt, arg) { as <- substitute(arg) byTab <- evalPopArg(x, arg = as, enclos = parent.frame(1L)) x[, list(sum = sum(a)), by = byTab] } ## symbol t1 <- tf(arg=b) ## name string t2 <- tf(arg="b") testthat::expect_equal(t1$sum, c(127, 131)) testthat::expect_equal(t1, t2) ## list of symbols / expressions t3 <- tf(arg=list(b, c)) ## name strings t4 <- tf(arg=c("b", "c")) ## object containing name strings byVars <- c("b", "c") t5 <- tf(arg=byVars) testthat::expect_equal(t4$sum, c(22,24,26,31,21, 31,32,27,26,18)) testthat::expect_equal(t4, t3) testthat::expect_equal(t4, t5) ## list of symbols / expressions t6 <- tf(arg=list(var1 = b,c, cut(c,3))) testthat::expect_equal(names(t6), c("var1", "c", "cut", "sum")) ## NULL object byVars <- NULL t7 <- tf(arg=byVars) t8 <- tf(arg=NULL) testthat::expect_equal(t7, t8) ## a list of predetermined values byList <- as.list(dt[, list(b, var1 = c)]) t9 <- tf(arg=byList) ## list without any names byList <- list(dt$b, dt$c) t10<- tf(arg=byList) ## partially named list byList <- list(var1 = dt$b, dt$c) t11<- tf(arg=byList) testthat::expect_equal(t9$sum, t10$sum) testthat::expect_equal(t10$sum, t11$sum) testthat::expect_equal(names(t11), c("var1", "BV2", "sum")) t12 <- tf(arg=list(V0=dt$b, dt$c)) byList <- list(V0 = dt$b, dt$c) t13 <- tf(arg=byList) testthat::expect_equal(t12, t13) ## pre-substituted list bl <- substitute(byList) t14 <- tf(arg = bl) testthat::expect_equal(t12, t14) ## pre-substituted vector of names nv <- c("a", "b") nvs <- substitute(nv) t15a <- tf(arg = nv) t15b <- tf(arg = nvs) testthat::expect_equal(t15a, t15b) ## nested functions tf2 <- function(a, x = dt) { tf(x = x, arg = a) } nv <- c("a", "b") nvs <- substitute(nv) t15a <- tf2(a = nv) t15b <- tf2(a = nvs) testthat::expect_equal(t15a, t15b) }) testthat::test_that("cutLowMerge merges succesfully what is intended", { popEpi:::skip_normally() all_names_present(popEpi::popmort, c("sex", "year", "agegroup", "haz")) all_names_present(popEpi::sire, c("sex", "bi_date", "dg_date", "ex_date", "status")) pm <- copy(popEpi::popmort) pm[, haz := rbinom(.N, 100, 0.5)/1e5L] sr <- popEpi::sire[1:100,] setDT(sr) sr1 <- lexpand(sr, birth = bi_date, entry = dg_date, exit = ex_date, status = status, fot = seq(0, 5, 1/12)) sr1 <- data.table(sr1) setattr(sr1, "class", c("Lexis", "data.table", "data.frame")) sr1[, year := per + 0.5*lex.dur] sr1[, agegroup := age + 0.5*lex.dur] sr2 <- cutLowMerge(sr1, pm, by.x = c("sex", "per", "age"), by.y = c("sex", "year", "agegroup"), all.x = TRUE, all.y = FALSE, old.nums = TRUE) sr3 <- copy(sr2) sr3[, haz := NULL] sr4 <- lexpand(sr, birth = bi_date, entry = dg_date, exit = ex_date, status = status, fot = seq(0, 5, 1/12), pophaz = pm, pp = FALSE) testthat::expect_equal(sr1, sr3, check.attributes = FALSE) testthat::expect_equal(sr2$haz*1e5L, sr4$pop.haz*1e5L, check.attributes = FALSE) sr1[, year := popEpi:::cutLow(year, breaks = sort(unique(pm$year)))] sr1[, agegroup := popEpi:::cutLow(agegroup, breaks = sort(unique(pm$agegroup)))] sr5 <- merge(sr1, pm, by = c("sex", "year", "agegroup")) setDT(sr5) setkey(sr5, lex.id, fot) testthat::expect_equal(sr4$haz*1e5L, sr5$pop.haz*1e5L, check.attributes = FALSE) }) testthat::test_that("detectEvents works as intended", { popEpi:::skip_normally() x <- sire[dg_date