Skip to content

Commit

Permalink
Fix incorrect calculation of Pearson's r
Browse files Browse the repository at this point in the history
Previously, bootES calculated Pearson's r incorrectly if the
`contrasts` argument to `bootES` was not lexicographically sorted.
The error arose from a bug in the reordering of the `lambdas` at
line 39 in `pearsonsr.R`.
  • Loading branch information
dgerlanc committed Nov 10, 2024
1 parent 1d3276f commit 9dd14c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/pearsonsr.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ calcPearsonsR <- function(vals, freq, grps, lambdas) {
## This function is meant to be passed as the 'statistic' argument
## to the 'boot' function. 'freq' should be a frequency vector of
## the type returned by 'boot'

## Get the integer indices of the different groups.
grp.idx = split(seq_along(vals), grps, drop=TRUE)
grp.nms = names(grp.idx)
Expand All @@ -36,7 +36,7 @@ calcPearsonsR <- function(vals, freq, grps, lambdas) {
}

## Put means and lambdas in the same order
lambdas = lambdas[match(names(lambdas), grp.nms)]
lambdas = lambdas[grp.nms]
C = sum(lambdas * means) # contrast
sumWts = sum(lambdas^2 / ns) # sum of the weights
SSw = sum(ss)
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test_calcPearsonsR.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,25 @@ test_that("calcPearsonsR produces known result", {
r.res = bootES:::calcPearsonsR(threeGpsVec, freq=rep(1, length(threeGpsVec)), grps=grpLabels3, lambdas=lambdas3)
expect_equal(truth, r.res, tolerance=1e-4)
})

test_that("produces same result with non-lexicographic contrast sorting", {
dat = data.frame(cond=rep(c("A", "B", "C"), each=10), score=1:30)

truth = 0.926
res_1 = bootES(
dat, data.col="score", group.col="cond", contrast=c(A=-1, B=0.5, C=0.5),
effect.type="r")$t0
expect_equal(truth, res_1, tolerance=1e-3)


res_2 = bootES(
dat, data.col="score", group.col="cond", contrast=c(B=0.5, C=0.5, A=-1),
effect.type="r")$t0
expect_equal(truth, res_2, tolerance=1e-3)


res_3 = bootES(
dat, data.col="score", group.col="cond", contrast=c(C=0.5, A=-1, B=0.5),
effect.type="r")$t0
expect_equal(truth, res_3, tolerance=1e-3)
})

0 comments on commit 9dd14c2

Please sign in to comment.