tests/testthat/test-pqrdata.R

context('pdata(), qdata(), and rdata()')


test_that("formula interface works.", {
	expect_equivalent( length(rdata( ~age, 10, data = mosaicData::HELPrct)), 10 )
	expect_equivalent( pdata( ~age, 30, data = mosaicData::HELPrct), prop( ~ mosaicData::HELPrct$age <= 30) )
	expect_equivalent( qdata( ~age, .75, mosaicData::HELPrct)['quantile'], quantile(mosaicData::HELPrct$age, .75) )
	expect_equivalent( qdata(~ age, c(.25,.75), mosaicData::HELPrct)$quantile, quantile(mosaicData::HELPrct$age, c(.25,.75)) )
})

test_that("vector/data interface works.", {
	expect_equivalent( length(rdata( age, 10, mosaicData::HELPrct)), 10 )
	expect_equivalent( pdata(age, 30, data = mosaicData::HELPrct), prop(~ mosaicData::HELPrct$age <= 30) )
	expect_equivalent( qdata(age, .75, data = mosaicData::HELPrct)['quantile'], quantile(mosaicData::HELPrct$age, .75) )
	expect_equivalent( qdata(~age, c(.25,.75), data = mosaicData::HELPrct)$quantile, quantile(mosaicData::HELPrct$age, c(.25,.75)) )
})


test_that("vector (no data) interface works.", {
	expect_equivalent( length(rdata( n = 10, mosaicData::HELPrct$age)), 10 )
	expect_equivalent( pdata(mosaicData::HELPrct$age, 30), prop( ~ mosaicData::HELPrct$age <= 30) )
	expect_equivalent( qdata(mosaicData::HELPrct$age, .75)['quantile'], quantile(mosaicData::HELPrct$age, .75) )
	expect_equivalent( qdata( p = c(.25,.75), mosaicData::HELPrct$age)$quantile, 
	                   quantile(mosaicData::HELPrct$age, c(.25,.75)) )
})

#test_that("error messages generated", {
#	expect_error( pdata( 30, ~age | sex, mosaicData::HELPrct) )
#	expect_error( qdata( .75, ~age | sex, mosaicData::HELPrct) )
#	expect_error( rdata( 30, ~age | sex, mosaicData::HELPrct) )
#	expect_error( qdata( 30, ~age, mosaicData::HELPrct) )
#})
ProjectMOSAIC/mosaic documentation built on Aug. 19, 2018, 9:14 a.m.