From 60c41369fd875b4017988e5dba303cd5d91e5221 Mon Sep 17 00:00:00 2001 From: kaz-yos Date: Sun, 1 Jun 2014 11:25:28 -0400 Subject: [PATCH 1/9] version 0.6.2 for 1xM table issue created --- DESCRIPTION | 2 +- NEWS | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1310ec1..233988a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: tableone Type: Package Title: Create "Table 1" to describe baseline characteristics -Version: 0.6.1 +Version: 0.6.2 Date: 2014-06-01 Author: Kazuki Yoshida, Justin Bohn. Maintainer: Kazuki Yoshida diff --git a/NEWS b/NEWS index 972f909..e9d7b4e 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,16 @@ +tableone 0.6.2 (2014-06-01) +---------------------------------------------------------------- + +BUG FIXES + +* The testing of 1 x m table was problematic when a categorical + variable only have one level. chisq.test() returns Chi-squared + test for given probabilities (test for strata imbalance) in such + cases. + +* Special thanks to Atsushi Shiraishi for reporting this issues. + + tableone 0.6.1 (2014-06-01) ---------------------------------------------------------------- From 1bb9fb54201a42b71400b6de94f9ee28d2174c8d Mon Sep 17 00:00:00 2001 From: kaz-yos Date: Sun, 1 Jun 2014 12:53:49 -0400 Subject: [PATCH 2/9] unit test files added --- tests/test-all.R | 12 ++++++++++++ tests/testthat/test-modules.R | 14 ++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 tests/test-all.R create mode 100644 tests/testthat/test-modules.R diff --git a/tests/test-all.R b/tests/test-all.R new file mode 100644 index 0000000..6e6e989 --- /dev/null +++ b/tests/test-all.R @@ -0,0 +1,12 @@ +################################################################################ +### Unit tests for the modules +## Reference: http://adv-r.had.co.nz/Testing.html +## Created on: 2014-06-01 +## Author: Kazuki Yoshida +################################################################################ + + +### Prepare environment +################################################################################ +library(testthat) +test_check("tableone") diff --git a/tests/testthat/test-modules.R b/tests/testthat/test-modules.R new file mode 100644 index 0000000..eb77861 --- /dev/null +++ b/tests/testthat/test-modules.R @@ -0,0 +1,14 @@ +################################################################################ +### Unit tests for the modules +## Reference: http://adv-r.had.co.nz/Testing.html +## Created on: 2014-06-01 +## Author: Kazuki Yoshida +################################################################################ + + +### Prepare environment +################################################################################ + + +### +cat("Running unit tests for the modules\n") From 647b68c628879f4e7f039f6e9877430aa31a1c1a Mon Sep 17 00:00:00 2001 From: kaz-yos Date: Sun, 1 Jun 2014 13:01:38 -0400 Subject: [PATCH 3/9] testthat suggested --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 233988a..56ca6fb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,5 +17,6 @@ Imports: e1071, gmodels Suggests: - survival + survival, + testthat URL: https://github.com/kaz-yos/tableone From df098c9e240c7a825e9f749f15b722da3f3f01eb Mon Sep 17 00:00:00 2001 From: kaz-yos Date: Sun, 1 Jun 2014 18:22:48 -0400 Subject: [PATCH 4/9] ModuleTestSafe checks for 1xM xtabs and return NA --- R/modules.R | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/R/modules.R b/R/modules.R index 6cb3c49..0d4bce6 100644 --- a/R/modules.R +++ b/R/modules.R @@ -163,6 +163,7 @@ ModuleCreateStrataVarName <- function(obj) { paste0(names(attr(obj, "dimnames")), collapse = ":") } +### ModuleTryCatchWE ## Try catch function # Taken from demo(error.catching) ## Used to define non-failing functions, that return NA when there is an error ModuleTryCatchWE <- function(expr) { @@ -176,14 +177,34 @@ ModuleTryCatchWE <- function(expr) { warning = W) } +### ModuleTestSafe ## Function to perform non-failing tests (obj should be xtabs or formula) ModuleTestSafe <- function(obj, testFunction, testArgs = NULL) { + ## Result from a function has to have $p.value element out <- ModuleTryCatchWE(do.call(testFunction, args = c(list(obj), testArgs))$p.value) - ## If it returns a numeric value, return it. Otherwise, return NA. - ifelse(is.numeric(out$value), out$value, NA) + + ## If it contains a numeric value, return it. Otherwise, return NA. + pValue <- ifelse(is.numeric(out$value), out$value, NA) + + ## When obj is an xtabs object + if (any(class(obj) %in% "xtabs")) { + ## and has 1 x M dimension, always return NA, and end there. + if (dim(obj)[1] == 1) { + ## ends here, returning NA + return(NA) + + } else { + ## If obj is a multi-row xtabs object, return the p-value + pValue + } + } else { + ## If obj is not an xtabs (formula for continuous variables), return the p-value + pValue + } } + ## Define special skewness and kurtosis functions that do not fail (SAS definitions) ModuleSasSkewness <- function(x) { out <- ModuleTryCatchWE(e1071::skewness(x, na.rm = TRUE, type = 2)) From 73e7ef2df6d95f41f3eb44b73d72858a6b6362ca Mon Sep 17 00:00:00 2001 From: kaz-yos Date: Sun, 1 Jun 2014 18:42:29 -0400 Subject: [PATCH 5/9] testthat unit testing for ModuleTestSafe's handling of 1xM xtabs --- tests/testthat/test-modules.R | 57 +++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-modules.R b/tests/testthat/test-modules.R index eb77861..2879b62 100644 --- a/tests/testthat/test-modules.R +++ b/tests/testthat/test-modules.R @@ -5,10 +5,63 @@ ## Author: Kazuki Yoshida ################################################################################ +### Structure +## expectations within tests within context ### Prepare environment ################################################################################ +library(testthat) + +### Context (1 for each file) +context("Unit tests for the modules") + + +### Tests for ModuleTestSafe +## Tests for ModuleTestSafe, a wrapper for test functions such as oneway.test and chisq.test + +## Create a dataset for a table +dat <- read.table(header = TRUE, text = " +rowVar colVar +m s +f s +m d +f d +") + +## Increase numbers to avoid "cells are too small" warnings +dat1 <- dat[rep(seq_len(nrow(dat)), 100), ] + +## Create a null cross table +xtabs1 <- xtabs( ~ rowVar + colVar, data = dat1) + +## Create a non-null cross table by replacement +xtabs2 <- xtabs1 +xtabs2[2,2] <- 50 + +test_that("P-values are returned for appropriate 2x2 xtabs", { + ## chisq.test + expect_that(ModuleTestSafe(xtabs1, chisq.test), equals(chisq.test(xtabs1)$p.value)) + expect_that(ModuleTestSafe(xtabs2, chisq.test), equals(chisq.test(xtabs2)$p.value)) + ## fisher.test + expect_that(ModuleTestSafe(xtabs1, fisher.test), equals(fisher.test(xtabs1)$p.value)) + expect_that(ModuleTestSafe(xtabs2, fisher.test), equals(fisher.test(xtabs2)$p.value)) +}) + + +## 1xM equal size +xtabs3 <- xtabs1[2,,drop = FALSE] +class(xtabs3) <- c("xtabs", "table") +## 1xM unequal size +xtabs4 <- xtabs2[2,,drop = FALSE] +class(xtabs4) <- c("xtabs", "table") + +test_that("P-values should be NA for 1xM xtabs", { + ## chisq.test + expect_that(ModuleTestSafe(xtabs3, chisq.test), equals(NA)) + expect_that(ModuleTestSafe(xtabs4, chisq.test), equals(NA)) + ## fisher.test + expect_that(ModuleTestSafe(xtabs3, fisher.test), equals(NA)) + expect_that(ModuleTestSafe(xtabs4, fisher.test), equals(NA)) +}) -### -cat("Running unit tests for the modules\n") From 72c194ce934f7b549a899cac8500b60ae3afb4e1 Mon Sep 17 00:00:00 2001 From: kaz-yos Date: Sun, 1 Jun 2014 19:16:21 -0400 Subject: [PATCH 6/9] tests added for CreateTableOne's categorical testing --- tests/testthat/test-CreateTableOne.R | 80 ++++++++++++++++++++++++++++ tests/testthat/test-modules.R | 1 + 2 files changed, 81 insertions(+) create mode 100644 tests/testthat/test-CreateTableOne.R diff --git a/tests/testthat/test-CreateTableOne.R b/tests/testthat/test-CreateTableOne.R new file mode 100644 index 0000000..b09fd2f --- /dev/null +++ b/tests/testthat/test-CreateTableOne.R @@ -0,0 +1,80 @@ +################################################################################ +### Unit tests for the CreateTableOne function +## Reference: http://adv-r.had.co.nz/Testing.html +## Created on: 2014-06-01 +## Author: Kazuki Yoshida +################################################################################ + +### Structure +## expectations within tests within context + +### Prepare environment +################################################################################ +library(testthat) +library(tableone) + +### Context (1 for each file) +context("Unit tests for the CreateTableOne function") + + +### Tests for +## Tests for ModuleTestSafe, a wrapper for test functions such as oneway.test and chisq.test + +## Create a dataset for a table +dat <- read.table(header = TRUE, text = " +rowVar colVar +m s +f s +m d +f d +") + +## Increase numbers to avoid "cells are too small" warnings +dat1 <- dat[rep(seq_len(nrow(dat)), 100), ] +## m's only dataset but level f is retained +dat2 <- dat1[dat1$rowVar == "m", ] +## m's only dataset without level f +dat3 <- dat2 +dat3$rowVar <- factor(dat3$rowVar) +## m's only dataset without level f. Strata imbalance +dat4 <- dat3[c(1:100, seq(101,200,2)), ] + + +test_that("P-values are returned for appropriate 2x2 xtabs, even with an empty factor level.", { + + ## Create tables + tab1 <- CreateTableOne(vars = "rowVar", strata = "colVar", data = dat1) + tab2 <- CreateTableOne(vars = "rowVar", strata = "colVar", data = dat2) + + ## Create corresponding xtabs + xtabs1 <- xtabs(~ rowVar + colVar, dat1) + xtabs2 <- xtabs(~ rowVar + colVar, dat2) + + ## chisq.test + expect_that(attributes(tab1)$pValues["rowVar","pApprox"], equals(chisq.test(xtabs1)$p.value)) + expect_that(attributes(tab2)$pValues["rowVar","pApprox"], equals(chisq.test(xtabs2)$p.value)) + ## fisher.test + expect_that(attributes(tab1)$pValues["rowVar","pExact"], equals(fisher.test(xtabs1)$p.value)) + expect_that(attributes(tab2)$pValues["rowVar","pExact"], equals(fisher.test(xtabs2)$p.value)) +}) + + +test_that("P-values should be NA for 1xM xtabs", { + + ## Create a table + tab3 <- CreateTableOne(vars = "rowVar", strata = "colVar", data = dat3) + tab4 <- CreateTableOne(vars = "rowVar", strata = "colVar", data = dat4) + + ## Create corresponding xtabs + xtabs3 <- xtabs(~ rowVar + colVar, dat3) + xtabs4 <- xtabs(~ rowVar + colVar, dat4) + + ## chisq.test + expect_that(attributes(tab3)$pValues["rowVar","pApprox"], equals(NA)) + expect_that(attributes(tab4)$pValues["rowVar","pApprox"], equals(NA)) + ## fisher.test + expect_that(attributes(tab3)$pValues["rowVar","pExact"], equals(NA)) + expect_that(attributes(tab4)$pValues["rowVar","pExact"], equals(NA)) +}) + + diff --git a/tests/testthat/test-modules.R b/tests/testthat/test-modules.R index 2879b62..a6dec57 100644 --- a/tests/testthat/test-modules.R +++ b/tests/testthat/test-modules.R @@ -11,6 +11,7 @@ ### Prepare environment ################################################################################ library(testthat) +library(tableone) ### Context (1 for each file) context("Unit tests for the modules") From 4753fb55c1fc035d8643505400cd7ded53ea7533 Mon Sep 17 00:00:00 2001 From: kaz-yos Date: Sun, 1 Jun 2014 19:22:16 -0400 Subject: [PATCH 7/9] commetns in test-all.R fixed --- tests/test-all.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test-all.R b/tests/test-all.R index 6e6e989..98c442c 100644 --- a/tests/test-all.R +++ b/tests/test-all.R @@ -1,12 +1,13 @@ ################################################################################ -### Unit tests for the modules +### Unit tests for the package ## Reference: http://adv-r.had.co.nz/Testing.html +## See the testthat subdirectory for actual test code ## Created on: 2014-06-01 ## Author: Kazuki Yoshida ################################################################################ -### Prepare environment +### Run all tests ################################################################################ library(testthat) test_check("tableone") From f06a7b54ad557d887e2f1463427010d04051dbc4 Mon Sep 17 00:00:00 2001 From: kaz-yos Date: Sun, 1 Jun 2014 19:25:55 -0400 Subject: [PATCH 8/9] indentation cleaned --- tests/testthat/test-modules.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testthat/test-modules.R b/tests/testthat/test-modules.R index a6dec57..4115ae1 100644 --- a/tests/testthat/test-modules.R +++ b/tests/testthat/test-modules.R @@ -41,8 +41,8 @@ xtabs2[2,2] <- 50 test_that("P-values are returned for appropriate 2x2 xtabs", { ## chisq.test - expect_that(ModuleTestSafe(xtabs1, chisq.test), equals(chisq.test(xtabs1)$p.value)) - expect_that(ModuleTestSafe(xtabs2, chisq.test), equals(chisq.test(xtabs2)$p.value)) + expect_that(ModuleTestSafe(xtabs1, chisq.test), equals(chisq.test(xtabs1)$p.value)) + expect_that(ModuleTestSafe(xtabs2, chisq.test), equals(chisq.test(xtabs2)$p.value)) ## fisher.test expect_that(ModuleTestSafe(xtabs1, fisher.test), equals(fisher.test(xtabs1)$p.value)) expect_that(ModuleTestSafe(xtabs2, fisher.test), equals(fisher.test(xtabs2)$p.value)) @@ -58,8 +58,8 @@ class(xtabs4) <- c("xtabs", "table") test_that("P-values should be NA for 1xM xtabs", { ## chisq.test - expect_that(ModuleTestSafe(xtabs3, chisq.test), equals(NA)) - expect_that(ModuleTestSafe(xtabs4, chisq.test), equals(NA)) + expect_that(ModuleTestSafe(xtabs3, chisq.test), equals(NA)) + expect_that(ModuleTestSafe(xtabs4, chisq.test), equals(NA)) ## fisher.test expect_that(ModuleTestSafe(xtabs3, fisher.test), equals(NA)) expect_that(ModuleTestSafe(xtabs4, fisher.test), equals(NA)) From 900cc96dd952a4f7b16cc7fa1d958ca0b50083ee Mon Sep 17 00:00:00 2001 From: kaz-yos Date: Sun, 1 Jun 2014 19:56:03 -0400 Subject: [PATCH 9/9] news edited to mention what's been fixed and testthat unit testing --- NEWS | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index e9d7b4e..edfc61c 100644 --- a/NEWS +++ b/NEWS @@ -6,10 +6,15 @@ BUG FIXES * The testing of 1 x m table was problematic when a categorical variable only have one level. chisq.test() returns Chi-squared test for given probabilities (test for strata imbalance) in such - cases. + cases. In this version, testing a 1 x m table always return NA, + as test for a cross table is not defined in this context. * Special thanks to Atsushi Shiraishi for reporting this issues. +* Unit testing with the testthat package was added for some + functions. Thus, the testthat package was added as a suggested + package. + tableone 0.6.1 (2014-06-01) ----------------------------------------------------------------