Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests #22

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: SUMMER
Type: Package
Title: Spatio-Temporal Under-Five Mortality Methods for Estimation
Version: 1.0.0
Date: 2019-07-02
Date: 2020-07-02
Authors@R: c(
person(given = "Bryan D", family = "Martin", email= "[email protected]", role = "aut"),
person(given = "Zehang R", family = "Li", email= "[email protected]", role = c("cre","aut")),
Expand All @@ -25,6 +25,6 @@ Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1
Additional_repositories: https://inla.r-inla-download.org/R/stable/
Suggests: INLA, knitr, rmarkdown, readstata13, patchwork, rdhs, R.rsp
Suggests: INLA, knitr, rmarkdown, readstata13, patchwork, rdhs, R.rsp, testthat (>= 2.1.0)
VignetteBuilder: R.rsp, knitr
NeedsCompilation: no
4 changes: 4 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(SUMMER)

test_check("SUMMER")
Binary file added tests/testthat/smoothDirect_regress.RData
Binary file not shown.
46 changes: 46 additions & 0 deletions tests/testthat/smoothDirect_run.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
rm(list=ls())

###########################################################
##
## Test for smoothDirect
##
###########################################################

library(SUMMER)
data(DemoData)
years <- levels(DemoData[[1]]$time)
years.all <- c(years, "15-19")
data_multi <- getDirectList(births = DemoData, years = years,
regionVar = "region", timeVar = "time", clusterVar = "~clustid+id",
ageVar = "age", weightsVar = "weights", geo.recode = NULL)
data <- aggregateSurvey(data_multi)

###########################################################

##
## national model, rw2
##
fit <- smoothDirect(data = data, Amat = NULL, time.model = "rw2",
year_label = years.all, year_range = c(1985, 2019),
is.yearly=TRUE, m = 5)
out1 <- getSmoothed(fit)


##
## subnational model, rw2
##
fit <- smoothDirect(data = data, Amat = NULL, time.model = "rw2",
year_label = years.all, year_range = c(1985, 2019),
is.yearly=TRUE, m = 5)
out2 <- getSmoothed(fit)


##
## subnational model, period, rw2 and ar1 interaction
##
fit <- smoothDirect(data = data, Amat = DemoMap$Amat, time.model = "rw2", st.time.model = "ar1",
year_label = years.all, year_range = c(1985, 2019),
is.yearly=FALSE, type.st = 4)
out3 <- getSmoothed(fit, Amat = DemoMap$Amat)

save(data, years.all, out1, out2, out3, file="smoothDirect_regress.RData")
15 changes: 15 additions & 0 deletions tests/testthat/test-expit.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
context("test-expit")

# The following functions are tested in this file:
# expit()
# logit()

test_that("Expit works", {
expect_equal(expit(2), exp(2)/(1 + exp(2)))
expect_equal(expit(-2), exp(-2)/(1 + exp(-2)))
})

test_that("Logit works", {
expect_equal(logit(0.2), log(0.2/(1-0.2)))
expect_equal(logit(0.8), log(0.8/(1-0.8)))
})
71 changes: 71 additions & 0 deletions tests/testthat/test-smoothDirect.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
context("test-smoothDirect")

expect_5pd_prob <- function(x, y) {
raw_difference <- abs(x-y)
return( expect_lte(max(raw_difference), 0.05) )
}

load("smoothDirect_regress.RData")


## run tests
test_that("Smooth Direct comparison: RW2 National", {


## national model, rw2
##
fit <- smoothDirect(data = data, Amat = NULL, time.model = "rw2",
year_label = years.all, year_range = c(1985, 2019),
is.yearly=TRUE, m = 5)
newout1 <- getSmoothed(fit)



# regression test to see if the results match expectation
expect_equal(newout1$region, out1$region)
expect_equal(newout1$years, out1$years)
expect_5pd_prob(newout1$upper, out1$upper)
expect_5pd_prob(newout1$lower, out1$lower)
expect_5pd_prob(newout1$median, out1$median)

})



test_that("Smooth Direct comparison: RW2 Subnational", {

##
## subnational model, rw2
##
fit <- smoothDirect(data = data, Amat = NULL, time.model = "rw2",
year_label = years.all, year_range = c(1985, 2019),
is.yearly=TRUE, m = 5)
newout2 <- getSmoothed(fit)

expect_equal(newout2$region, out2$region)
expect_equal(newout2$years, out2$years)
expect_5pd_prob(newout2$upper, out2$upper)
expect_5pd_prob(newout2$lower, out2$lower)
expect_5pd_prob(newout2$median, out2$median)

})


test_that("Smooth Direct comparison: RW2 And AR1 Interaction", {

##
## subnational model, period, rw2 and ar1 interaction
##
fit <- smoothDirect(data = data, Amat = DemoMap$Amat, time.model = "rw2", st.time.model = "ar1",
year_label = years.all, year_range = c(1985, 2019),
is.yearly=FALSE, type.st = 4)
newout3 <- getSmoothed(fit, Amat = DemoMap$Amat)


expect_equal(newout3$region, out3$region)
expect_equal(newout3$years, out3$years)
expect_5pd_prob(newout3$upper, out3$upper)
expect_5pd_prob(newout3$lower, out3$lower)
expect_5pd_prob(newout3$median, out3$median)

})
Loading