-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'hotfix-1xM-table-chisq.test-issue2' (returns NA when te…
…sting 1xM xtabs)
- Loading branch information
Showing
6 changed files
with
205 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[email protected]> | ||
|
@@ -17,5 +17,6 @@ Imports: | |
e1071, | ||
gmodels | ||
Suggests: | ||
survival | ||
survival, | ||
testthat | ||
URL: https://github.com/kaz-yos/tableone |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
################################################################################ | ||
### 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 | ||
################################################################################ | ||
|
||
|
||
### Run all tests | ||
################################################################################ | ||
library(testthat) | ||
test_check("tableone") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
################################################################################ | ||
### Unit tests for the modules | ||
## 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 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)) | ||
}) | ||
|
||
|