Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nutterb committed Aug 27, 2015
1 parent b21300c commit 36c6a80
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: HydeNet
Type: Package
Title: Hybrid Bayesian Networks Using R and JAGS
Version: 0.9.1
Date: 2015-07-06
Date: 2015-08-27
Author: Jarrod E. Dalton <daltonj@ccf.org> and Benjamin Nutter <benjamin.nutter@gmail.com>
Maintainer: Benjamin Nutter <benjamin.nutter@gmail.com>
Description: Facilities for easy implementation of hybrid Bayesian networks
Expand Down Expand Up @@ -38,7 +38,9 @@ Imports:
utils
Suggests:
knitr,
survival,
testthat
VignetteBuilder: knitr
SystemRequirements: JAGS (http://mcmc-jags.sourceforge.net)
LazyLoad: yes
LazyData: true
Expand Down
2 changes: 1 addition & 1 deletion R/rToJags.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ rToJags <- function(f){
x <- gsub(" ", "", x)
if (grepl("logit[(]", x)){
if (grepl("inverse[=]T", x)){
x <- gsub("(logit[(]|qlogis[(]", "ilogit(", x)
x <- gsub("(logit[(]|qlogis[(])", "ilogit(", x)
}
x <- gsub(",[[:print:]]+[)]", ")", x)
return(x)
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test-cpt.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
context("cpt")

test_that("cpt.list",
{
n <- 50000
data <- data.frame(
di1 = as.factor(1:6 %*% rmultinom(n,1,prob=c(.4,.3,.15,.10,.03,.02))),
di2 = as.factor(1:6 %*% rmultinom(n,1,prob=rev(c(.4,.3,.15,.10,.03,.02)))),
di3 = as.factor(1:6 %*% rmultinom(n,1,prob=c(.15,.10,.02,.3,.4,.03)))
)

expect_that(cpt(list(y = "di3", x = c("di1", "di2")), data= data),
not(throws_error()))
})

test_that("cpt with weights",
{
echodata <- cbind(expand.grid(list(echo = c("Negative", "Positive"),
cad = c("No","Yes"))),
data.frame(pr=c(0.83,0.17,0.12,0.88)))
expect_that(cpt(echo ~ cad, data=echodata, wt=echodata$pr),
not(throws_error()))
})

19 changes: 19 additions & 0 deletions tests/testthat/test-modelToNode.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
context("modelToNode")

test_that("modelToNode: coxph should return an error",
{
library(survival)
test2 <- list(start=c(1,2,5,2,1,7,3,4,8,8),
stop=c(2,3,6,7,8,9,9,9,14,17),
event=c(1,1,1,1,1,1,1,0,0,0),
x=c(1,0,0,1,0,1,1,1,0,0))
fit <- coxph(Surv(start, stop, event) ~ x, test2)
expect_error(modelToNode(fit))
})

test_that("modelToNode: multinom",
{
fit.gear <- multinom(gear ~ mpg + factor(am), data=mtcars)
expect_that(modelToNode(fit.gear), not(throws_error()))
})

13 changes: 13 additions & 0 deletions tests/testthat/test-rToJags.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
context("rToJags")

test_that("rToJags: exponents",
{
expect_equal(rToJags(y ~ x^2),
"y ~ pow(x,2)")
})

test_that("rToJags: logit (from VGAM package)",
{
expect_equal(rToJags(y ~ logit(x, inverse=TRUE)),
"y ~ ilogit(x)")
})
14 changes: 14 additions & 0 deletions tests/testthat/test-writeJagsFormula.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
context("writeJagsFormula")

test_that("writeJagsFormula: Poisson Regression",
{
fit <- glm(gear ~ mpg + am, data = mtcars, family = poisson)
expect_that(writeJagsFormula(fit, c("gear", "mpg", "am")),
not(throws_error()))
})

test_that("writeJagsFormula: Multinomial Regression",
{
fit.gear <- multinom(gear ~ mpg + factor(am), data=mtcars)
expect_that(writeJagsFormula(fit.gear), not(throws_error()))
})

0 comments on commit 36c6a80

Please sign in to comment.