Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parameters:::.extract_htest_correlation should always return CI info if the htest object provides it #1003

Open
DarwinAwardWinner opened this issue Aug 7, 2024 · 0 comments
Labels
Feature idea 🔥 New feature or request

Comments

@DarwinAwardWinner
Copy link

DarwinAwardWinner commented Aug 7, 2024

Currently, parameters:::.extract_htest_correlation only returns confidence intervals for method = "pearson", which makes sense since the default cor.test only returns CIs for this method. However, I implemented an alternative Spearman correlation test that does return CIs, but parameters::parameters does not return the CI in the table (see below reprex). It would be nice if instead of checking the method used, parameters:::.extract_htest_correlation would always check for a conf.int in the htest object and use it if it exists.

library(DescTools)
library(parameters)
library(assertthat)

spearman.test.with.ci <- function(x, y, ..., conf.level = 0.95) {
  res <- cor.test(x, y, method = "spearman", ...)
  scor <- DescTools::SpearmanRho(x,y, conf.level = conf.level)
  if (all(c("lwr.ci", "upr.ci") %in% names(scor))) {
    ci_vec <- unname(scor[c("lwr.ci", "upr.ci")])
    attr(ci_vec, "conf.level") <- conf.level
    res$conf.int <- ci_vec
  }
  res
}

set.seed(1986)
x <- rnorm(500)
y <- rnorm(500)

ht_pearson <- cor.test(x, y, method = "pearson")
ht_spearman <- cor.test(x, y, method = "spearman")
## Same as above, but computes a CI
ht_spearman_ci <- spearman.test.with.ci(x, y)
print(ht_spearman_ci)
#> 
#>  Spearman's rank correlation rho
#> 
#> data:  x and y
#> S = 20578916, p-value = 0.7853
#> alternative hypothesis: true rho is not equal to 0
#> 95 percent confidence interval:
#>  -0.07556340  0.09979184
#> sample estimates:
#>        rho 
#> 0.01220808
## Object indeed contains CI
assert_that("conf.int" %in% names(ht_spearman_ci))
#> [1] TRUE

ci_colnames <- c("CI", "CI_low", "CI_high")

## Pearson has CI columns
print(parameters(ht_pearson))
#> Pearson's product-moment correlation
#> 
#> Parameter1 | Parameter2 |        r |        95% CI | t(498) |     p
#> -------------------------------------------------------------------
#> x          |          y | 5.20e-03 | [-0.08, 0.09] |   0.12 | 0.908
#> 
#> Alternative hypothesis: true correlation is not equal to 0
assert_that(all(ci_colnames %in% colnames(parameters(ht_pearson))))
#> [1] TRUE

## Regular Spearman test does not have CI columns
print(parameters(ht_spearman))
#> Spearman's rank correlation rho
#> 
#> Parameter1 | Parameter2 |  rho |        S |     p
#> -------------------------------------------------
#> x          |          y | 0.01 | 2.06e+07 | 0.785
#> 
#> Alternative hypothesis: true rho is not equal to 0
assert_that(!any(ci_colnames %in% colnames(parameters(ht_spearman))))
#> [1] TRUE

## Modified Spearman test should have CI columns but doesn't
print(parameters(ht_spearman_ci))
#> Spearman's rank correlation rho
#> 
#> Parameter1 | Parameter2 |  rho |        S |     p
#> -------------------------------------------------
#> x          |          y | 0.01 | 2.06e+07 | 0.785
#> 
#> Alternative hypothesis: true rho is not equal to 0
assert_that(all(ci_colnames %in% colnames(parameters(ht_spearman_ci))))
#> Error: Elements 1, 2, 3 of ci_colnames %in% colnames(parameters(ht_spearman_ci)) are not true

Created on 2024-08-07 with reprex v2.1.0

@strengejacke strengejacke added the Feature idea 🔥 New feature or request label Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature idea 🔥 New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants