Skip to content

Commit

Permalink
streamline kendall cor functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed Jul 6, 2024
1 parent 5d08dc8 commit bdfaaba
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
43 changes: 18 additions & 25 deletions R/kendall_correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
#' @export
kendall_cor <- function(x, y) {
arr <- cbind(x, y)
storage.mode(arr) <- "double"
if (storage.mode(arr) != "double") { storage.mode(arr) <- "double" }
arr <- arr[complete.cases(arr), ]

kw <- kendall_warnings(arr)
n <- ncol(arr)

if (isFALSE(kw)) {
return(NA_real_)
}
kw <- kendall_warnings(arr, n)

if (isFALSE(kw)) { return(NA) }

kendall_cor_(arr)
}
Expand Down Expand Up @@ -70,29 +70,22 @@ kendall_cor <- function(x, y) {
#' kendall_cor_test(x, y)
#'
#' @export
kendall_cor_test <- function(x,y,
kendall_cor_test <- function(x, y,
alternative = c("two.sided", "greater", "less")) {
alternative <- match.arg(alternative)

arr <- cbind(x, y)
storage.mode(arr) <- "double"
if (storage.mode(arr) != "double") { storage.mode(arr) <- "double" }
arr <- arr[complete.cases(arr), ]

kw <- kendall_warnings(arr)
n <- nrow(arr)
m <- ncol(arr)

if (isFALSE(kw)) {
return(NA)
}
kw <- kendall_warnings(arr, m)

r <- kendall_cor_(arr)
n <- nrow(arr)
if (isFALSE(kw)) { return(NA) }

if (n < 2) {
stop("not enough finite observations")
}

# r = correlation
# n = number of observations
r <- kendall_cor_(arr)

if (n < 50) {
q <- round((r + 1) * n * (n - 1) / 4)
Expand Down Expand Up @@ -145,21 +138,21 @@ kendall_cor_test <- function(x,y,
)
}

kendall_warnings <- function(arr) {
if (ncol(arr) != 2) {
kendall_warnings <- function(arr, n) {
if (n != 2) {
stop("x and y must be uni-dimensional vectors")
}

if (nrow(arr) < 2) {
stop("x and y must have at least 2 observations")
if (n < 2) {
stop("x and y must have at least 2 non-null observations")
}

if (sd(arr[, 1]) == 0) {
if (var(arr[, 1]) == 0) {
warning("x has zero variance")
return(FALSE)
}

if (sd(arr[, 2]) == 0) {
if (var(arr[, 2]) == 0) {
warning("y has zero variance")
return(FALSE)
}
Expand Down
2 changes: 1 addition & 1 deletion man/kendall_cor_test.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions src/05_kendall_correlation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// Data)
// Filzmoser, Fritz, and Kalcher 2023 (pcaPP package)

// note: the len < 2 conditions are commented out because the R function checks
// for this condition before calling the C++ functions

#include <algorithm>
#include <cmath>
#include <cpp11.hpp>
Expand All @@ -13,9 +16,9 @@
using namespace cpp11;

uint64_t insertion_sort_(double *arr, size_t len) {
if (len < 2) {
return 0;
}
// if (len < 2) {
// return 0;
// }

size_t maxJ = len - 1, i;
uint64_t swapCount = 0;
Expand Down Expand Up @@ -70,9 +73,9 @@ static uint64_t merge_(double *from, double *to, size_t middle, size_t len) {
}

uint64_t merge_sort_(double *x, double *buf, size_t len) {
if (len < 2) {
return 0;
}
// if (len < 2) {
// return 0;
// }

if (len < 10) {
return insertion_sort_(x, len);
Expand Down
13 changes: 11 additions & 2 deletions tests/testthat/test-kendall.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that("kendall", {
expect_equal(kendall_cor(x, x), 1)

x <- rep(1, 3)
expect_equal(kendall_cor(x, x), cor(x, x, method = "kendall"))
expect_warning(kendall_cor(x, x), "zero variance")

x <- c(1, 0, 2)
y <- c(5, 3, 4)
Expand Down Expand Up @@ -34,6 +34,16 @@ test_that("kendall", {
k2 <- cor.test(x, y, method = "kendall", alternative = "two.sided")
expect_equal(k1$statistic, unname(k2$estimate))
expect_equal(k1$p_value, k2$p.value)

k1 <- kendall_cor_test(x, y, alternative = "greater")
k2 <- cor.test(x, y, method = "kendall", alternative = "greater")
expect_equal(k1$statistic, unname(k2$estimate))
expect_equal(k1$p_value, k2$p.value)

k1 <- kendall_cor_test(x, y, alternative = "less")
k2 <- cor.test(x, y, method = "kendall", alternative = "less")
expect_equal(k1$statistic, unname(k2$estimate))
expect_equal(k1$p_value, k2$p.value)

x <- rnorm(1e3)
y <- rpois(1e3, 2)
Expand All @@ -48,7 +58,6 @@ test_that("kendall", {
}
t_kendall <- median(t_kendall)


t_cor <- c()
for (i in 1:100) {
t1 <- Sys.time()
Expand Down

0 comments on commit bdfaaba

Please sign in to comment.