Skip to content
Merged
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
55 changes: 41 additions & 14 deletions R/ttestones.b.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,51 @@ ttestOneSClass <- R6::R6Class(

if (self$options$wilcoxon || self$options$mann) {

if (is.factor(column))
if (is.factor(column)) {
res <- createError(.('Variable is not numeric'))
else if (length(column) == 0)
} else if (length(column) == 0) {
res <- createError(.('Variable does not contain enough observations'))
else
res <- try(suppressWarnings(wilcox.test(column, mu=testValue,
alternative=Ha,
paired=FALSE,
conf.int=TRUE,
conf.level=cl)), silent=TRUE)
} else {
# Determine method based on sample size to balance precision and performance.
# R 4.6+ supports exact conditional inference (Pratt's method) for data with
# ties/zeros. For N >= 50, asymptotic approximation is used for efficiency.
useExact <- (n < 50)

res <- try(
suppressWarnings(
wilcox.test(
column,
mu=testValue,
alternative=Ha,
paired=FALSE,
conf.int=TRUE,
conf.level=cl,
exact=useExact
)
),
silent=TRUE
)
}

if ( ! isError(res)) {

nTies <- sum(column == testValue)
totalRankSum <- ((n-nTies) * ((n-nTies) + 1)) / 2
biSerial <- (2 * (res$statistic / totalRankSum)) - 1
if ( ! isError(res)) {
# The Rank Biserial Correlation (effect size) denominator must align with the
# ranking method used by wilcox.test to ensure the value remains within [-1, 1].
# Pratt's method (used when exact = TRUE) retains zero-differences in the rank pool.
# The asymptotic method (used when exact = FALSE) traditionally excludes zeros.
if (useExact) {
denom_n <- n
} else {
nTies <- sum(column == testValue)
denom_n <- n - nTies
}

totalRankSum <- (denom_n * (denom_n + 1)) / 2

if (totalRankSum > 0)
biSerial <- (2 * (res$statistic / totalRankSum)) - 1
else
biSerial <- NaN

ttest$setRow(rowNo=i, list(
"stat[wilc]"=res$statistic,
Expand All @@ -133,9 +162,7 @@ ttestOneSClass <- R6::R6Class(
"es[wilc]"=biSerial,
"ciles[wilc]"='',
"ciues[wilc]"=''))

} else {

ttest$setRow(rowNo=i, list(
"stat[wilc]"=NaN,
"p[wilc]"='',
Expand Down
34 changes: 31 additions & 3 deletions R/ttestps.b.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,22 @@ ttestPSClass <- R6::R6Class(
}
else {
stud <- try(t.test(column1, column2, paired=TRUE, conf.level=confInt, alternative=Ha), silent=TRUE)
wilc <- try(suppressWarnings(wilcox.test(column1, column2, alternative=Ha, paired=TRUE, conf.int=TRUE, conf.level=confInt)), silent=TRUE)

# Determine method based on sample size to balance precision and performance.
# R 4.6+ supports exact conditional inference (Pratt's method) for data with
# ties/zeros. For N >= 50, asymptotic approximation is used for efficiency.
useExact <- (n < 50)
wilc <- try(suppressWarnings(
wilcox.test(
column1,
column2,
alternative=Ha,
paired=TRUE,
conf.int=TRUE,
conf.level=confInt,
exact=useExact
)
), silent=TRUE)
}

if ( ! isError(stud)) {
Expand Down Expand Up @@ -115,8 +130,21 @@ ttestPSClass <- R6::R6Class(

if ( ! isError(wilc)) {

totalRankSum <- ((n-nTies) * ((n-nTies) + 1)) / 2
biSerial <- (2 * (wilc$statistic / totalRankSum)) - 1
# The Rank Biserial Correlation (effect size) denominator must align with the
# ranking method used by wilcox.test to ensure the value remains within [-1, 1].
# Pratt's method (used when exact = TRUE) retains zero-differences in the rank pool.
# The asymptotic method (used when exact = FALSE) traditionally excludes zeros.
if (useExact) {
denom_n <- n
} else {
denom_n <- n - nTies
}

totalRankSum <- (denom_n * (denom_n + 1)) / 2
if (totalRankSum > 0)
biSerial <- (2 * (wilc$statistic / totalRankSum)) - 1
else
biSerial <- NaN

ttestTable$setRow(rowKey=pair, list(
'stat[wilc]'=wilc$statistic,
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/testttestones.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ testthat::test_that('Matched rank biserial correlation is correct', {
# Test rank biserial correlation
ttestTable <- r$ttest$asDF
testthat::expect_equal('dif', ttestTable[['var[wilc]']])
testthat::expect_equal(27, ttestTable[['stat[wilc]']])
testthat::expect_equal(0.234, ttestTable[['p[wilc]']], tolerance = 1e-3)
testthat::expect_equal(0.5, ttestTable[['es[wilc]']])
testthat::expect_equal(32, ttestTable[['stat[wilc]']])
testthat::expect_equal(0.273, ttestTable[['p[wilc]']], tolerance = 1e-3)
testthat::expect_equal(0.422, ttestTable[['es[wilc]']], tolerance = 1e-3)
})

testthat::test_that('Matched rank biserial correlation works with non zero test value', {
Expand All @@ -88,5 +88,5 @@ testthat::test_that('Matched rank biserial correlation works with non zero test

# Test rank biserial correlation
ttestTable <- r$ttest$asDF
testthat::expect_equal(-0.0303, ttestTable[['es[wilc]']], tolerance = 1e-4)
testthat::expect_equal(-0.0476, ttestTable[['es[wilc]']], tolerance = 1e-3)
})
6 changes: 3 additions & 3 deletions tests/testthat/testttestps.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ testthat::test_that('Matched rank biserial correlation is correct', {
ttestTable <- r$ttest$asDF
testthat::expect_equal('before', ttestTable[['var1[wilc]']], tolerance = 1e-3)
testthat::expect_equal('after', ttestTable[['var2[wilc]']], tolerance = 1e-3)
testthat::expect_equal(9, ttestTable[['stat[wilc]']])
testthat::expect_equal(0.234, ttestTable[['p[wilc]']], tolerance = 1e-3)
testthat::expect_equal(-0.5, ttestTable[['es[wilc]']])
testthat::expect_equal(12, ttestTable[['stat[wilc]']])
testthat::expect_equal(0.273, ttestTable[['p[wilc]']], tolerance = 1e-3)
testthat::expect_equal(-0.467, ttestTable[['es[wilc]']], tolerance = 1e-3)
})
Loading