tests/testthat/test-Trial.R

library(trialcostR)
context('Trial class')

test_that('Empty trial data', {

    expect_is(Trial(df=data.frame(),
                    participant=~1,
                    outcome=~1,
                    followup=numeric(0),
                    discount=0),
              'Trial')

    A <- Trial(df=data.frame(),
               participant=~1,
               outcome=~1,
               followup=numeric(0),
               discount=0)

    expect_identical(dim(participants(A)), rep(as.integer(0), 2))
    expect_identical(dim(outcomes(A)), rep(as.integer(0), 2))
    expect_identical(lifeyear(A), numeric(0))
    expect_identical(dlifeyear(A), numeric(0))

})

test_that('Construct Trial from simple data', {

    expect_is(Trial(df=data.frame(x=1:3, y=2),
                    participant=~1,
                    outcome=~x,
                    followup=y,
                    discount=0),
              'Trial')

})

test_that('Participant and outcome data retreivable from simple Trial', {

    df=data.frame(x=1:3, y=2)

    A <- Trial(df=df,
               participant=~1,
               outcome=~x,
               followup=y,
               discount=0)

    # rethink
    expect_identical({x <- participants(A)
                      attr(x, 'terms') <- NULL
                      x},
                     data.frame(row.names=attr(df, 'row.names')))
    expect_identical({x <- outcomes(A)
                      attr(x, 'terms') <- NULL
                      x},
                     data.frame(x=df$x))

})

test_that('Calculate ly from simple trial data', {


    A <- Trial(df=data.frame(x=1:3, y=2),
               participant=~1,
               outcome=~x,
               followup=y,
               discount=0)

    expect_identical(lifeyear(A), rep(2, 3))

})

test_that('Discount rate of zero results in ly equal to dly', {

    A <- Trial(df=data.frame(x=1:3, y=2),
               participant=~1,
               outcome=~x,
               followup=y,
               discount=0)

    expect_identical(lifeyear(A), dlifeyear(A))

})

test_that('Discount rate of infinity results in dly of zero', {

    A <- Trial(df=data.frame(x=1:3, y=2),
               participant=~1,
               outcome=~x,
               followup=y,
               discount=Inf)

    expect_identical(dlifeyear(A), rep(0,3))

})

test_that('Discount rate of one reproduces analytic (exact) dly result', {

    A <- Trial(df=data.frame(x=1:3, y=2),
               participant=~1,
               outcome=~x,
               followup=y,
               discount=1)

    expect_identical(dlifeyear(A), rep(1-exp(-2), 3))

})
stephematician/trialcostR documentation built on May 30, 2019, 3:18 p.m.