tests/testthat/test-CENSUR.R

# Author: ilya
###############################################################################
context("test-censur")

# data --------------------------------------------------------------------

# Based on Feeney's spreadsheets CENSUR~1.XLS, CENSUR~2.XLS, and CENSUR~3.XLS
# this is census data from Japan

# create a bunch of data to show the three variants.
pop1960 <- c(3831870, 4502304, 5397061, 4630775, 4193184, 4114704, 
             3770907, 3274822, 2744786, 2559755, 2160716, 1839025, 
             1494043, 1133409, 870238, 577972, 313781, 131547)
pop1965 <- c(3983902, 3854281, 4513237, 5373547, 4572392, 4206801, 
             4110076, 3751030, 3231736, 2697217, 2485095, 2071540, 
             1719370, 1343444, 955567, 644043, 341170, 176068)
pop1970 <- c(4292503, 3988292, 3852101, 4492096, 5347327, 4571868, 
             4190340, 4085338, 3674127, 3198934, 2648360, 2382691, 
             1970485, 1584699, 1172155, 736258, 408191, 53116)
Age          <- seq(0, 85, by = 5)
date1960     <- "1960-10-01"
date1965     <- "1965-10-01"
date1970     <- "1970-10-01"
date1960fake <- "1960-10-05"
ex1  <- c(NA, 69.45, 64.62, 59.72, 54.87, 50.11, 45.37, 40.65, 35.99,
          31.40, 26.94, 22.64, 18.52, 14.67, 11.20, 8.25, 5.95, 6.00)
ex2  <- c(NA, 70.19, 65.33, 60.41, 55.54, 50.74, 45.96, 41.21,
          36.52, 31.89, 27.39, 23.05, 18.89, 14.99, 11.45, 8.43, 6.06)


# tests -------------------------------------------------------------------

test_that("survRatioError works", {
    # reproduces CENSUR~1.XLS
    expect_equal(
        survRatioError(
            pop1 = pop1960,
            pop2 = pop1970,
            Age = Age,
            date1 = date1960,
            date2 = date1970,
            exprior = ex1),
        0.444659710063221,
        tolerance = 1e-12
    )


    expect_equal(
        survRatioError(
            pop1 = pop1965,
            pop2 = pop1970,
            Age = Age,
            date1 = date1965,
            date2 = date1970,
            exprior = ex2),
        0.396434724201574,
        tolerance = 1e-12
    )

    expect_equal(
        survRatioError(
            pop1 = pop1960,
            pop2 = pop1970,
            Age = Age,
            date1 = date1960fake,
            date2 = date1970,
            exprior = ex1),
      1.06781792562135,
      tolerance = 0.001
    )
})
timriffe/DemoTools documentation built on Jan. 28, 2024, 5:13 a.m.